Everything a DevOps Engineer must know about kubernetes.
Tags: kubernetes, devops
Published: 2023-02-13 05:13:45

**KUBERNETES **

- Kubernetes is an open-source container orchestration tool which helps in deployment of applications. 

- Kubernetes helps in scaling the applications among the users. It is a CD tool that helps in deployments 

- It helps manage containerized applications in different deployment environments 
- There are two types of services in Kubernetes. Deployment and Stateful services 

- Stateful services: It is a Kubernetes controller that manages multiple pods that have unique identities and are not interchangeable. In a stateful set each pod has a persistent, unique ID. 

- Deployment set: the Deployment Controller changes the actual state to the desired state at a controlled rate. We can define Deployments to create new Replica Sets, or to remove existing Deployments and adopt all their resources with new Deployments. 

- Stateful sets are generally used for databases and stateful applications because databases are generally hosted outside the Kubernetes cluster as a database can’t be replicated.  

- Kubernetes consists of the following components- pods, replica sets, services etc. 

- Pods and replica sets are used to scale the application. It creates multiple instances of applications. 

- It load balances using load balancer and sends requests from users to the pods according to the load on different pods. Hence adjusts the traffic preventing the server or application from crashing 

- We have to create ‘yaml’ files to define the type of deployments, pods and replica sets 

-  We have a create pods in template section to define the blue print of pods 

-  Minikube is a tool which lets us run Kubernetes locally. It runs on the node-kubernetes cluster 

**Kubectl- It is a command line tool for k8s cluster.** 

- Whenever the pods gets destroyed, deployment will restore the pods with a different IP address 

- Generally, pods contain containers where the application is resided in the form of an image inside the container 

- A pod can have one or more containers running inside it. Each container will be having different port numbers. Based on that the requests will be sent to respective containers from the service 

- We can define the number of instances of pods to be created in the section ‘replica sets’  
- Deployments are also used to roll back to previous versions of our applications. 

 

**KUBERNETES SERVICES 
**
There are 4 types of services in Kubernetes 


- Headless services
 

- Node port
  

- Load balancer 

- Cluster IP 

**Need for services: **

- When a pod dies, a new IP address is created for the pod. It becomes difficult to access the IP address for newly created pods.
For each pod we set a service, service has a stable IP address pointing to the pod.
 Svc receives requests and forwards it to one of the pod 

- **CLUSTER IP**: It is a default type of service. When we create a svc without specifying the type it automatically takes cluster IP 
- When a svc is created end points are created. K8s will use the endpoints to keep track which pods are members of svc. 
- Selector in svc and label in pod should be same so that svc points to the pod. 
- Target port should be mentioned in svc. Target port and container port should be same. 
 

**HEADLESS: **

- If client wants to communicate with 1 specific pod directly, headless svc is used  
- Pods are not randomly selected by the svc 
- Stateful apps like SQL, databases use headless services 
- If client wants to communicate with a selected pod then the IP address of the pod should be known  
- To achieve it, DNS lookup is used where it returns svc IP address to get pod IP address. If we set cluster IP address to none, pod IP address is returned. 

 

- **Cluster IP:** only accessible with in the cluster 

- **Node port:** External traffic has access to fixed port on each worker node. The range of the port is 30000-32767 

- **Load Balancer:** becomes accessible externally through cloud provider. 

  

**KUBECTL COMMANDS **

 

- Kubectl appy -f <.yamlFile>: To create the resource from the file.

- Kubectl create -f <fileName> --edit -o json: edit the data in the file then creates the resource using the edited data. 

- Kubectl create deployment <deployName> --image=<imageName>: creates a deployment with the specified image name. 

- Kubectl create deployment <deployName> --image=<imageName>--replicas=3: creates a deployment with the specified image name and 3 replicas.

- Kubectl create deployment <deployName> --image=<imageName> --port: <portNo>: creates a deployment that runs on an image on the specified port number. 

- Kubectl delete deployment <deployName>: to delete a deployment
 
- Kubectl get deployments: displays list of created deployments .

- Kubectl get svc: displays list of created services .

- Kubectl get pods: displays list of pods .

- Kubectl describe deployment <deployName>: describes about the deployment in detail .

- Kubectl describe svc <svcName>: describes about the svc in detail .

- Kubectl describe pod <podName>: describes about the pod in detail .

- Kubectl get pod <podName> -o wide: describes the pod with cluster IP address .

- Kubectl rollout status deployment <deployname>: gives the status of deployment .

- Kubectl rollout history deployment <deployname> : gives the revisions of deployments .

- Kubectl rollout undo deployment <deployname> --to-revison=<revNumber>: goes back to the revised deployment.