Giter Club home page Giter Club logo

devops-danish's Introduction

Kubernetes Tutorial: The Basics

This guide is aimed to fast-track your Kubernetes learning by focusing on a practical hands-on overview guide.

When learning Kubernetes, you usually have an idea of some existing system you own and manage, or a website that you are building.

The challenge is understanding which Kubernetes building blocks you need in order to run your workloads on Kubernetes


The problem: "I want to adopt Kubernetes"
The problem: "I have some common existing infrastructure"

Our focus: Solving the problem by learning each building block in order to port our infrastructure to Kubernetes.

Understanding Containers

Before even looking at Kubernetes, you need to have a general understanding of containers like docker. Your workloads need to fit in containers in order to be shipped on Kubernetes.
Containers also have a bunch of assumptions that you need to meet.

  • Defining the container - Dockerfile
  • Serving traffic - Exposing ports
  • Configuration - mount config files & secrets or env variables
  • Data persistence - When a container is terminated, everything inside the container is gone
  • Container entrypoint - The main process that runs in the container. Your app

Docker installation

Create Network

docker network create wordpress

Build & Test container images

Wordpress example

cd kubernetes\tutorials\basics\

docker build -f dockerfiles/wordpress.dockerfile . -t aimvector/wordpress-example

  • Run our Wordpress container
docker run -it --rm -p 80:80 --net wordpress aimvector/wordpress-example

The wordpress container will be visible on port 80 on http://localhost/

MySQL example

docker build -f dockerfiles/mysql.dockerfile . -t aimvector/mysql-example
  • How do we run our MySQL ?

We need to understand that databases require storage and state Just like installing software on a server, it will store its files in some directory. Mysql stores its files under /var/lib/mysql

  • We need a volume mount

Let's see how to run this in docker

mkdir data

docker run --rm -d `
--name mysql `
--net wordpress `
-e MYSQL_DATABASE=exampledb `
-e MYSQL_USER=exampleuser `
-e MYSQL_PASSWORD=examplepassword `
-e MYSQL_RANDOM_ROOT_PASSWORD=1 `
-v ${PWD}/data:/var/lib/mysql `
aimvector/mysql-example

# we can see the container with
docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED         STATUS         PORTS                 NAMES
92cde663a3f5   aimvector/mysql-example   "docker-entrypoint.sā€¦"   5 seconds ago   Up 3 seconds   3306/tcp, 33060/tcp   mysql

  • Run Wordpress and connect it to MySQL
docker run -d `
--rm `
-p 80:80 `
--name wordpress `
--net wordpress `
-e WORDPRESS_DB_HOST=mysql `
-e WORDPRESS_DB_USER=exampleuser `
-e WORDPRESS_DB_PASSWORD=examplepassword `
-e WORDPRESS_DB_NAME=exampledb `
aimvector/wordpress-example

Clean up

docker rm -f wordpress
docker rm -f mysql
docker network rm wordpress
rm data

Kubernetes Tools: kubectl

To manage and work with Kubernetes, you need kubectl
Let's grab that from here

Run Kubernetes Locally

  • Install kubectl to work with kubernetes

We'll head over to the kubernetes site to download kubectl

  • Install the kind binary

You will want to head over to the kind site

  • Create a cluster
kind create cluster --image kindest/node:v1.23.5

Namespaces

kubectl create namespace cms

Configmaps

Environment Variables for pods

How to use configmaps

kubectl -n cms create configmap mysql `
--from-literal MYSQL_RANDOM_ROOT_PASSWORD=1

kubectl -n cms get configmaps

Secrets

How to use secrets in pods

kubectl -n cms create secret generic wordpress `
--from-literal WORDPRESS_DB_HOST=mysql `
--from-literal WORDPRESS_DB_USER=exampleuser `
--from-literal WORDPRESS_DB_PASSWORD=examplepassword `
--from-literal WORDPRESS_DB_NAME=exampledb

kubectl -n cms create secret generic mysql `
--from-literal MYSQL_USER=exampleuser `
--from-literal MYSQL_PASSWORD=examplepassword `
--from-literal MYSQL_DATABASE=exampledb


kubectl -n cms get secret

Deployments

cd kubernetes\tutorials\basics

kubectl -n cms apply -f ./deploy.yaml
kubectl -n cms get pods

Services

Services documentation

kubectl -n cms apply -f .\service.yaml
kubectl -n cms get svc

Storage Class

StorageClass documentation

kubectl get storageclass

Statefulset

Statefulset documentation

Let's deploy our mysql using what we learnt above:

kubectl -n cms apply -f yaml/statefulset.yaml

kubectl -n cms get pods

Persistent Volumes

Documentation

Port Forwarding

We can access private service endpoints or pods using port-forward :

kubectl -n cms get pods
kubectl -n cms port-forward <pod-name> 80

Public Traffic

In order to make our site public, its common practise to expose web servers via
a proxy or API gateway.
In Kubernetes, an Ingress is used.

Ingress

To use an ingress, we need an ingress controller

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.3/deploy/static/provider/cloud/deploy.yaml

kubectl -n ingress-nginx get pods

kubectl -n ingress-nginx --address 0.0.0.0 port-forward svc/ingress-nginx-controller 80

Create an Ingress

kubectl -n cms apply -f yaml/ingress.yaml

devops-danish's People

Contributors

danishaamirs avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.