DEV Community

nithinalias
nithinalias

Posted on • Updated on

Introduction to Kubernetes

Introduction to MINIKUBE

MINIKUBE

- Open source tool
- To run K8s locally on your system
- Runs a single node K8s cluster inside a VM on your system.
 ___________________                    _____________________
|                   |                  |                     |
|    Minikube       |                  |      MASTER         |
|       /|\         |                  |      ______         |
|        |          |                  |      ______         |
|        |          |______________|\  |                     |
|       VM          |              | \ |      WORKER         |
|       /|\         |______________| / |      ______         |
|        |          |              |/  |      ______         |
|        |          |                  |                     |
|        |          |                  |DOCKER PRE-INSTALLED |
|     HOST OS       |                  |                     |
|___________________|                  |_____________________|
  HOST MACHINE                                 NODE

Enter fullscreen mode Exit fullscreen mode

Install minikube

apt-get update
apt-get install curl
apt-get install apt-transport-https
apt install virtualbox virtualbox-ext-pack
Enter fullscreen mode Exit fullscreen mode

Install kubectl
https://kubernetes.io/docs/tasks/tools/
Install Docker
https://docs.docker.com/engine/install/ubuntu/
Install minikube
https://minikube.sigs.k8s.io/docs/start/

minikube start
kubectl get po -A
minikube dashboard
Enter fullscreen mode Exit fullscreen mode

K8s Commands

kubectl get nodes
kubectl get pods -o wide
kubectl cluster-info
kubectl config view
kubectl config get-clusters
kubectl config delete-clusters
kubectl config get-contexts
kubectl config current-context
kubectl config set-context context1 --cluster=cluster1 --user=user1 --namespace=namespace1
kubectl config use-context context1
kubectl config current-context
kubectl config get-contexts
kubectl config delete-context context1
kubectl get namespace
kubectl config set-context --current --namespace=namespace1
kubectl create namespace namespace2
kubectl get namespace
kubectl delete namespace namespace2
kubectl get pods --namespace=default
kubectl api-versions
Enter fullscreen mode Exit fullscreen mode

Introduction to KUBECTL

KUBECTL

- Kubernetes command-line tool
- Allows you to run commands against K8s clusters.
- use kubectl to deploy applications, inspect and manage cluster 
  resources, and view logs
- KUBECTL is the most powerful of three clients(UI,API,KUBECTL).
- create pods,create services,destroy pods....

                                                       UI
                                 ______________________/_    
                                |       ____________  /  |
                                |      |            |/   |
   MASTER PROCESSES--------------------| API SERVER |----- API
                                |      |____________|\   |           
                                |                     \  |
                                |                    CLI (KUBECTL)
                                |                        |
                                | /  POD       SERVICE   | 
   WORKER PROCESSES------------------POD       SECRET    |
                                | \  POD       CONFIGMAP |
                                |                        | 
                                |________________________|
                                           NODE
Install Kubectl
https://kubernetes.io/docs/tasks/tools/
Enter fullscreen mode Exit fullscreen mode

To Create a Kubernetes Cluster Using Kubeadm

You can use the two links shown below for Kubernetes Cluster creation


https://github.com/kodekloudhub/certified-kubernetes-administrator-course

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

https://www.cloudsigma.com/how-to-install-and-use-kubernetes-on-ubuntu-20-04/

https://alibaba-cloud.medium.com/how-to-install-and-deploy-kubernetes-on-ubuntu-16-04-6769fd1646db

https://www.digitalocean.com/community/tutorials/how-to-create-a-kubernetes-cluster-using-kubeadm-on-ubuntu-20-04
Enter fullscreen mode Exit fullscreen mode
 ___________________                    _____________________
|                   |                  |                     |
|Container Platform |                  |  Container Platform |
|___________________|                  |_____________________|
| Kubeadm,Kubeclet, |                  |  Kubeadm,Kubeclet,  |
|     Kubectl       |                  |      Kubectl        | 
|___________________|                  |_____________________|
|Designate to become|                  |                     |
|      Master       |         _________|  Join the Cluster   |
|___________________|         |        |_____________________|
|  CN Installation  |         |                 SLAVE
|___________________|         |                                 
|                   |         |
|   Cluster Ready   |         | 
|                   |/|_______| 
|___________________|\|                  
      MASTER                                   
Enter fullscreen mode Exit fullscreen mode

Kubeadm Commands

kubeadm init
kubeadm join
kubeadm reset
Enter fullscreen mode Exit fullscreen mode

Deploy a webapplication using kubernetes
In Master create directory and make deployment file

mkdir demo
cd demo
vim webappdeploymentfile.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp1
  labels:
    app: webapp-sql
    tier: frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp-sql
      tier: frontend
  template:
    metadata:
      labels:
        app: webapp-sql
        tier: frontend
    spec:
      containers:
      - name: webapp1
        image: hshar/webapp
        ports:
        - containerPort: 8081

vim mysqldeploymentfile.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sqldb
  labels:
    app: webapp-sql
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp-sql
      tier: backend
  template:
    metadata:
      labels:
        app: webapp-sql
        tier: backend
    spec:
      containers:
      - name: mysql
        image: hshar/mysql:5.5
        ports:
        - containerPort: 3306

Enter fullscreen mode Exit fullscreen mode

Run the deployment commands

kubectl apply -f webappdeploymentfile.yml
kubectl apply -f mysqldeploymentfile.yml
kubectl get deployment
Enter fullscreen mode Exit fullscreen mode

Make service file

vim webappservice.yml

apiVersion: v1
kind: Service
metadata:
  name: webapp-sql
spec:
  selector:
    app: webapp-sql
    tier: frontend
  ports:
  - port: 80
  type: NodePort

vim mysqlservice.yml

apiVersion: v1
kind: Service
metadata:
  name: webapp-sql1
spec:
  selector:
    app: webapp-sql
    tier: backend
  ports:
  - port: 3306
  clusterIP: None

Enter fullscreen mode Exit fullscreen mode

Run the service commands

kubectl apply -f webappservice.yml
kubectl apply -f mysqlservice.yml
kubectl get services
Enter fullscreen mode Exit fullscreen mode

Enter into webapp container and modify it

kubectl get pods -o wide
kubectl exec -it <webapppodname> bash
kubectl exec -it <mysqlpodname> bash
kubectl get services
Enter fullscreen mode Exit fullscreen mode

Now open the webapplication in the browser http://ip-adress:port

How to create single pod

use the below commands in master node.

kubectl get nodes
kubectl run pod1 --image nginx
kubectl get pods -o wide
kubectl describe pod pod1
kubectl describe pod pod1 | less
Enter fullscreen mode Exit fullscreen mode

How to create multiple pods and update them using yaml file
Use the deployment yaml file given below

vim deploymentfile.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mynginx
spec:
  replicas: 3
  selector:
    matchLabels:
      type: webserver
  template:
    metadata:
      name: mypod
      labels:
        type: webserver
    spec:
      containers:
        - name: c1
          image: nginx:1.7.9

kubectl get all
kubectl describe deployment mynginx
kubectl set image deploy mynginx c1=nginx:1.9.1
kubectl get all
Enter fullscreen mode Exit fullscreen mode

When we start updating nginx:1.7.9 to nginx:1.9.1,replicaset of nginx:1.7.9 scaledown and replicaset of nginx:1.9.1 scaleup.

Ingress

vim Ingress.yml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: hello-world.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 8080


kubectl apply -f Ingress.yml
kubectl get ingress

sudo vim /etc/hosts

10.0.2.15  hello-world.info
#10.0.2.15 is the ADDRESS that we got from 'kubectl get ingress' command
Enter fullscreen mode Exit fullscreen mode

Now we can check the traffic using the command 'curl hello-world.info'.

Statefulset

vim statefulset.yml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
     matchLabels:
       app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

kubectl apply -f statefulset.yml
kubectl get all
kubectl get pods -w -l app=nginx
kubectl delete pod -l app=nginx
kubectl get pod -l app=nginx
Enter fullscreen mode Exit fullscreen mode

Now we can see pods are terminating and if you use 'kubectl get pod -l app=nginx' command after sometime, it began container creating.Deleting or Scaling a statefulset down will not delete the volume associated with the statefulset.

Monitoring Kubernetes cluster
Installing and configuring helm

curl https://baltocdn.com/helm/signing.asc | sudo apt-key add -
sudo apt-get install apt-transport-https --yes
echo "deb https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm

kubectl get ns
kubectl create namespace prometheus
kubectl get ns
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack --namespace prometheus
kubectl get pods -n prometheus
kubectl port-forward -n prometheus prometheus-kube-prometheus-stack-prometheus-0 9090
Enter fullscreen mode Exit fullscreen mode

Now you browse prometheus by http://ip-addresss:9090.Here you can check memory,pod,namespace,alert,graph,status....

kubectl port-forward -n prometheus kube-prometheus-stack-grafana-6c74f5565b-vfbxq 3000
Enter fullscreen mode Exit fullscreen mode

Now you browse grafana by http://ip-addresss:3000.For getting username and password use the command below.

kubectl get secret --namespace prometheus prometheus-grafana -o yaml

echo "admin-password" | base64 --decode
echo "admin-user" | base64 --decode
Enter fullscreen mode Exit fullscreen mode

Now you can login grafana and monitor kubernetes clusters.

Top comments (0)