Kubernetes PODs
Tags: kubernetes, docker, devops, opensource
Published: 2021-10-12 12:04:12

Here we will see the practical aspects of Orchestration Containers.
We will see how same task is perform by the Docker set of Commands comparing to the Kubernetes commands.

### Architecture

![architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hcchala6xwqu1kvwufm.png)
 
### Run Our First Container

#### Docker Command 

```shell
docker run --name mywebserver nginx
```

#### Kubectl command

```shell
kubectl run mywebserver --image=nginx
```

![run kubectl pod](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4s1bume15zwh2o4e3o9d.png)

---
### Exec into Container

#### Docker Command

```shell
docker exec -it mywebserver bash
```

#### K8s Command

```shell
kubectl exec -it mywebserver -- bash
OR
kuberctl exec -it mywebserver -- ls -l /
```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn9f6dub0jxyqwyfttwi.png)

---
### Removing Container

#### Docker commands
Before removing the particular container we have to stop the container and then we can remove it

```shell
docker container stop mywebserver
docker container rm mywebserver
```

#### kubectl command 
We don't need to stop the pod before removing, k8s take care of it.

```shell
kubectl delete pod mywebserver
```

![remove pod](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xytly11r792oh5cefxjz.png)
 
---
### Benefits of Pods

Many applications might have more than one container which is tightly coupled in one-to-one relationship.

Here you need to know the relation between the containers, so to keep track on all the containers having dependencies. As if one goes down, complete application goes down.

Linking in Docker commands,

```shell
docker run -dt --name myweb01 function01
docker run -dt --name myapp01 function02
```

Whereas, in K8s Containers within a pod share an IP address and port space, and can find each other via localhost.
Not need to create individual container, just need to refer as a pod. No worries of one-to-one connection.
If pod1 is not working it can create another pod2 and be available whenever needed.

#### * Pod always runs on a Node
#### * A node is a worker machine in k8s
#### * Each node is managed by master
#### * A node have multiple pods


![pod oweview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xf3mgiszlyqqwrtwov1n.png)
 
#### Launch multi containers Pod

We need to use yaml file to mention the different containers and Objects/Volumes.

```yml
apiVersion: v1
kind: Pod
metadata:
  name: sidecar-pod-1
spec:
  volumes:
  - name: log
    emptyDir: {}

  containers:
  - image: busybox
    name: main-container
    args:
     - /bin/sh
     - -c
     - >
      while true; do
        echo "$(date) INFO hello from main-container" >> /var/log/myapp.log ;
        sleep 1;
      done
    volumeMounts:
    - name: log
      mountPath: /var/log

  - name: sidecar-container
    image: busybox
    args:
     - /bin/sh
     - -c
     - tail -fn+1 /var/log/myapp.log
    volumeMounts:
    - name: log
      mountPath: /var/log
```

run the above yaml file, which I have named as ***pod.yaml***

```shell
kubectl apply -f pod.yaml
```

![multicontainer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0oc7e0k83kbczsnv1x4.png)
 
Let us understand this YAML file:

* For the sake of understanding I have named my containers as main-container and sidecar-container.
* The main container will be our application which will continuously write something to /var/log/myapp.log
* The /var/log/ path is mounted on the containers using separate volume. This path is mounted using volumeMounts in both the containers so that the path is shared across both the containers.
* The sidecar container will read the log file content using tail -fn+1 /var/log/myapp.log

---
#### Benefits of Configuration File

* Integrates well will change review processes
* Provides the source of record on what is live within the Kubernetes cluster.
* Easier to troubleshoot changes with version control

---

### Understanding POD Configurations in YAML

In the above ***pod.yaml*** you would have seen many Important fields like apiVersion, kind, metadata, container etc.

|key|Description|
|:---|----:|
|apiVersion|Version of API|
|kind | kind of object you want to create|
|metadata name| name of the object that name uniquely identifies it|
|spec | Describe state of the object |

To check apiVersion we need to access API Primitives at path ***localhost:8080/api*** through 

```shell
kubectl proxy --port 8080
```
For more fields references you can check
[API Documentation](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#-strong-api-overview-strong-)
[Github Documentation](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#metadata)

you can also take reference from CLI using below commands, but these could be not updated details.

```shell
kubectl api-resources
#specific to pod details
kubectl explain pod
```

![important configuration details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psmc7pu5gt8r0j116vzd.png)
 
### Exposing Ports for Pods

Here we have the ***yaml*** file that, to demonstrate how we expose port. It work similar way as in Docker.


[Reference](https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/)

![Expose port](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cc6c3h52rd3qk9p7o4i.png)

Lets create a pod with container at port ***8080*** and we will see the pod details.

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx
    image: democontainer
    ports:
      - containerPort: 8080
```
![Lunching Pod](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vni63ax6wp45pmhsq911.png)

and checking details

![pod port details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85i14qjt7nffeppk8ffs.png)

to check for more details about the port field in document. you can run following command.

```shell
kubectl explain pod.spec.containers.ports
```
![CLI ports documents](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vn54x97ci17uf82933fb.png)
 
### Generating Pod Manifests using CLI

```shell
# Create a pod from nginx Image
kubectl run nginx --image=nginx

#Create a pod and expose to port 8080
kubectl run nginx --image=nginx --port=8080

# Output the manifest file
kubectl run nginx --image=nginx --port=80 --dry-run=client -o yaml
```

### detele pods

```shell
#delete specific pod
kubectl delete pod command

#delete all pods
kubectl delete pod --all
```

![delete pods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2oojkzj44gnzdebata4.png)
 


References:
[Official Documentation](https://kubernetes.io/docs/home/)
[Udemy Course](https://www.udemy.com/course/certified-kubernetes-administrator/)
[Getting Started with Kubernetes](https://dev.to/dporwal/getting-started-with-kubernetes-4mdc)
[Docker Commands](https://dev.to/dporwal/image-creation-management-and-registry-part-1-pk9)

Credit:
[Zeal Vora](https://in.linkedin.com/in/zealvora)