Create and Deploy POD Definitions in Kubernetes

In this guide, we will be working on How you can Create and Deploy Pod Definitions in a Kubernetes cluster and run containers on it. I don’t know why Kubernetes introduce this concept but you can create a collection of a container-based application under one pod. So what is Pod? The pod is also referred to as logical host for the container applications like there is multiple container application running under one pod.

But there is one question that comes to your mind here, that why the need for pod when you are running Kubernetes cluster already on VM’s well you can comment better on that, I haven’t found one good application for this concept. You can find out more detail in Kubernetes documentation.

So here we go we will create our first pod, and then we will try to expose the application using the port forwarding concept.

First, we will create a YAML file for Nginx container, on master-minion. YAML is a language that is mostly used in automation processes, so what this file will do, it will create Nginx pod while pulling it from the docker hub.

# mkdir build
# cd build
# vi nginx.yaml

apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80

We will shut down the two nodes minion-1 and minion-2 so the pod will be created on minion-1 only. (In our case I have three minions and one master, so if the minion-1 and minion-2 will be shut down remaining will be minion-3)

Now to run the yaml file on minion master node use the below command.

# kubectl create -f ./nginx.yaml

To check the status of the pod

# kubectl get pods
pod "nginx" created

It means that we have created a pod that running a single Nginx container from within our environment Then we check on all minions by using the below commands (In our case its minion-3)

# docker ps

For finding out in which minion the container is created, use the below command.

# tail -f /var/log/messages
Sep 25 03:46:37 minoin-master dbus[629]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Sep 25 03:46:37 minoin-master dbus-daemon: dbus[629]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Sep 25 03:46:37 minoin-master systemd: Starting Network Manager Script Dispatcher Service...
Sep 25 03:46:37 minoin-master dhclient[2251]: bound to 192.168.18.141 -- renewal in 773 seconds.
Sep 25 03:46:37 minoin-master dbus[629]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Sep 25 03:46:37 minoin-master dbus-daemon: dbus[629]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Sep 25 03:46:37 minoin-master nm-dispatcher: req:1 'dhcp4-change' [ens33]: new request (2 scripts)
Sep 25 03:46:37 minoin-master systemd: Started Network Manager Script Dispatcher Service.
Sep 25 03:46:37 minoin-master nm-dispatcher: req:1 'dhcp4-change' [ens33]: start running ordered scripts...
Sep 25 03:47:39 minoin-master kube-scheduler: I0925 03:47:39.986537     636 event.go:217] Event(api.ObjectReference{Kind:"Pod", Namespace:"default", Name:"nginx", UID:"ce4349c1-a1c5-11e7-a826-000c29a393ed", APIVersion:"v1", ResourceVersion:"1419242", FieldPath:""}): type: 'Normal' reason: 'Scheduled' Successfully assigned nginx to minion-3

Now in our case, the container creation has been assigned to minion-3 Successful logs of minion-3 are given below

#Sep 25 03:50:32 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:32 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:45 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-5): Mounting V5 Filesystem
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-5): Ending clean mount
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-5): Unmounting Filesystem
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:46 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-5): Mounting V5 Filesystem
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-5): Ending clean mount
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-5): Unmounting Filesystem
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:47 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:48 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:48 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:48 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:48 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:48 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:48 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-5): Mounting V5 Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-5): Ending clean mount
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-5): Unmounting Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-5): Mounting V5 Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-5): Ending clean mount
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-5): Unmounting Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-5): Mounting V5 Filesystem
Sep 25 03:50:49 minoin-3 kernel: XFS (dm-5): Ending clean mount
Sep 25 03:50:50 minoin-3 kernel: XFS (dm-5): Unmounting Filesystem
Sep 25 03:50:50 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:50 minoin-3 dockerd-current: time="2017-09-25T03:50:50.701168892-04:00" level=info msg="{Action=create, LoginUID=4294967295, PID=2256}"
Sep 25 03:50:50 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:50 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:50 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:50 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:50 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:51 minoin-3 kernel: XFS (dm-4): Unmounting Filesystem
Sep 25 03:50:51 minoin-3 dockerd-current: time="2017-09-25T03:50:51.697408375-04:00" level=info msg="{Action=start, LoginUID=4294967295, PID=2256}"
Sep 25 03:50:51 minoin-3 kernel: XFS (dm-4): Mounting V5 Filesystem
Sep 25 03:50:51 minoin-3 kernel: XFS (dm-4): Ending clean mount
Sep 25 03:50:51 minoin-3 systemd: Started docker container 620bf91191171a2cfa75790961044710751d7e776b089617dc89e4eb9cdaab38.
Sep 25 03:50:51 minoin-3 systemd: Starting docker container 620bf91191171a2cfa75790961044710751d7e776b089617dc89e4eb9cdaab38.
Sep 25 03:50:51 minoin-3 kernel: SELinux: mount invalid.  Same superblock, different security settings for (dev mqueue, type mqueue)
Sep 25 03:50:51 minoin-3 oci-register-machine[43291]: 2017/09/25 03:50:51 Register machine: prestart 620bf91191171a2cfa75790961044710751d7e776b089617dc89e4eb9cdaab38 43284 /var/lib/docker/devicemapper/mnt/67c6c3d26eae9d1128327ea0a3ea0713a06484141acc1f5062c31ce3b67b6429/rootfs
Sep 25 03:50:51 minoin-3 systemd-machined: New machine 620bf91191171a2cfa75790961044710.
Sep 25 03:50:51 minoin-3 oci-systemd-hook: systemdhook : gidMappings not found in config
Sep 25 03:50:51 minoin-3 oci-systemd-hook: systemdhook : uidMappings not found in config
Sep 25 03:50:51 minoin-3 kubelet: E0925 03:50:51.881036    2256 docker_manager.go:1741] Failed to create symbolic link to the log file of pod "nginx_default(ce4349c1-a1c5-11e7-a826-000c29a393ed)" container "nginx": symlink  /var/log/containers/nginx_default_nginx-620bf91191171a2cfa75790961044710751d7e776b089617dc89e4eb9cdaab38.log: no such file or directory

Creation of container takes time in my case it takes 2-5 minutes below is the status of minion-master when container successfully started.

# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
nginx     1/1       Running   0          11m

On minion-3 for confirmation use the below command.

# docker ps
CONTAINER ID        IMAGE                                      COMMAND                  CREATED             STATUS              PORTS               NAMES
620bf9119117        nginx:1.7.9                                "nginx -g 'daemon off"   38 seconds ago      Up 37 seconds                           k8s_nginx.b0df00ef_nginx_default_ce4349c1-a1c5-11e7-a826-000c29a393ed_3c1672be
dd47ef2ecb04        gcr.io/google_containers/pause-amd64:3.0   "/pause"                 3 minutes ago       Up 3 minutes                            k8s_POD.b2390301_nginx_default_ce4349c1-a1c5-11e7-a826-000c29a393ed_7d8dae57

On master-minion again, we can obtain the information about PODs using the below command.

# kubectl describe pods
Name:           nginx
Namespace:      default
Node:           minion-3/192.168.18.144
Start Time:     Mon, 25 Sep 2017 03:47:39 -0400
Labels:
Status:         Running
IP:             172.17.0.2
Controllers:
Containers:
nginx:
Container ID:               docker://620bf91191171a2cfa75790961044710751d7e776b089617dc89e4eb9cdaab38
Image:                      nginx:1.7.9
Image ID:                   docker-pullable://docker.io/nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
Port:                       80/TCP
State:                      Running
Started:                  Mon, 25 Sep 2017 03:50:51 -0400
Ready:                      True
Restart Count:              0
Volume Mounts:
Environment Variables:
Conditions:
Type          Status
Initialized   True
Ready         True
PodScheduled  True
No volumes.
QoS Class:      BestEffort
Tolerations:
Events:
FirstSeen     LastSeen        Count   From                    SubObjectPath           Type            Reason                  Message
---------     --------        -----   ----                    -------------           --------        ------                  -------
31m           31m             1       {default-scheduler }                            Normal         Scheduled               Successfully assigned nginx to minion-3
31m           31m             1       {kubelet minion-3}      spec.containers{nginx}  Normal          Pulling                 pulling image "nginx:1.7.9"
31m           28m             2       {kubelet minion-3}                              Warning         MissingClusterDNS       kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
28m           28m             1       {kubelet minion-3}      spec.containers{nginx}  Normal          Pulled                  Successfully pulled image "nginx:1.7.9"
28m           28m             1       {kubelet minion-3}      spec.containers{nginx}  Normal          Created                 Created container with docker id 620bf9119117; Security:[seccomp=unconfined]
28m           28m             1       {kubelet minion-3}      spec.containers{nginx}  Normal          Started                 Started container with docker id 620bf9119117

Now to get specific information about the pod use below mentioned command.

# docker describe pod nginx (for specific pod)

Note down the IP of the container and try to ping it from master-minion

# ping 172.17.0.2

You would not be able to ping because it is contained and there is no route for it, but what we can do is we will create a new container within the same pod (which will be on the same host), as in our case it will be on the same host they will be reachable to another. We will now create a container busybox that run in pod, it is a small container and has a minimal Linux operating system

# kubectl run busybox --image=busybox --restart=Never --tty -i --generator=run-pod/v1
Waiting for pod default/busybox to be running, status is Pending, pod ready: false
Waiting for pod default/busybox to be running, status is Pending, pod ready: false
/ #

Now you are entered into the busybox container and now we will try to ping the nginx container.

# / # ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.185 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.083 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.073 ms
64 bytes from 172.17.0.2: seq=3 ttl=64 time=0.085 ms