Kubernetes Pod in practice

kubernetes pod in practice
kubernetes pod in practice

On the last article ( The lifecycle of a Kubernetes Pod )  we discussed what a pod is in theory, now let’s see what it actually looks like in practice. We will first go over what a simple pod manifest looks like, then we will deploy an example app showing how to work with it.

This post assumes you understand the purpose of Kubernetes and you have a running cluster and kubectl installed.

Kubernetes Pod in practice

First, what does the manifest (YAML) look like?

apiVersion: "api version"              (1)
kind: "object to create"                  (2)
Metadata:                   (3)
  Name: "Pod name"
    App: "label value"
Spec:                       (4)
  - name: "container name"
    image: "image to use for container"

Let’s break the manifest down to 4 parts. The ApiVersion, Kind, Metadata and Spec.

  • ApiVersion: The version of the Kubernetes API you’re using to create this object.
  • Kind: The kind of object you want to create.
  • Metadata: The information that uniquely identify the object we are creating. Name or namespace.
  • Spec: Here we specify the config of our pod e.g image name, container name, volumes etc.

The ApiVersion, Kind and Metadata are required fields and applies to all Kubernetes objects, not just pods. The layout of spec (also required) on the other hand looks different from one object to another. The example manifest shown above shows what a single container pod spec looks like.

Great! now that we understand what the manifest looks like, we are going to show both model types of creating a pod.

1- Single Container Pod

Copy past the themanifest for our single container pod below into a file named sc-pod-1.yaml. This config runs an nginx pod for us:

apiVersion: v1
kind: Pod
  name: sc-pod
    app: myapp
  - name: single-container
    image: nginx

Then, we have to deploy this manifest into our local Kubernetes cluster by running The following command:

kubectl create -f sc-pod-1.yaml

Then we run the following command to confirm that our pod is running as expected:

kubectl get pods
kubectl get pods
NAME                                          READY     STATUS    RESTARTS   AGE
sc-pod                                      1/1       Running   0          45s

As you can see our pod is now running now.

How can we confirm nginx is actually running?

Just run the following command:

kubectl exec sc-pod service nginx status

. What this is doing is running a command inside our pod by passing in “service nginx status”. Note that this is very similar to running “docker exec” if you are already familiar with running docker.

kubectl exec sc-pod service nginx status
nginx is running.

Cool. Now let’s clean up by running “kubectl delete pod sc-pod

kubectl delete pod sc-pod
pod "sc-pod" deleted

2- Multi container Pod

Here we will deploy something more useful. We will create a pod with multiple containers that work together as one entity. One container writes the current date to a file every 10 sec, the other container serves the logs for us.
Go ahead and create a file named mc-pod-2.yaml  with the following config:

apiVersion: v1
kind: Pod
  name: multi-container-pod # Name of our pod
  - name: shared-date-logs  # Creating a shared volume for my containers
    emptyDir: {}
  - name: container-writing-dates # Name of first container
    image: alpine # Image to use for first container
    command: ["/bin/sh"]
    args: ["-c", "while true; do date >> /var/log/output.txt; sleep 10;done"] # writing date every 10secs
    - name: shared-date-logs
      mountPath: /var/log # Mounting log dir so app can write to it.
  - name: container-serving-dates # Name of second container
    image: nginx:1.7.9 # Image for second container
      - containerPort: 80 # Defining what port to use.
    - name: shared-date-logs
      mountPath: /usr/share/nginx/html # Where nginx will serve the written file

deploy it with the following command:

kubectl create -f mc-pod-2.yaml

Actually, It is worth stopping here and briefly touching on “volumes” in pods. Volumes in our example provides a way for the containers to communicate during the life of the pod. If the pod is deleted and recreated, any data stored in the shared volume is lost(Persistent Volumes object solves this issue so that your data can persist pod loss). We are using this multi-container example to not only demonstrate how to create two container pod but to also show the way both containers share resources.

kubectl create -f mc-pod-2.yaml
pod "multi-container-pod" created

Then we check to see if it’s really deployed.

kubectl get pods 
NAME                                          READY     STATUS    RESTARTS   AGE
multi-container-pod                           2/2       Running   0          1m

Great! It is running. Now let’s make sure things are working as we expect. We need to make sure that our second container is serving the dates.

We check to make sure two containers are in our pod by running the following command:

kubectl describe pod multi-container-pod

This command is good to see what the created object looks like.

    Container ID:  docker://b109959930d5ed336797269a7abf200ae67bc8bc007619d1c2aea8c674c17433
    Image:         alpine
    Image ID:      docker-pullable://alpine@sha256:b3dbf31b77fd99d9c08f780ce6f5282aba076d70a513a8be859d8d3a4d0c92b8
    Port:          <none>
    Host Port:     <none>
      while true; do date >> /var/log/output.txt; sleep 10;done
    State:          Running
      Started:      Fri, 08 Feb 2019 08:33:03 +0100
    Ready:          True
    Restart Count:  0
    Environment:    <none>
      /var/log from shared-date-logs (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-z2lhf (ro)
    Container ID:   docker://50c987a086924c5abc2d65c892faf9f32b30e2da19f3c6cd62df49d5ba182e77
    Image:          nginx:1.7.9
    Image ID:       docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 08 Feb 2019 08:33:53 +0100
    Ready:          True
    Restart Count:  0
    Environment:    <none>
      /usr/share/nginx/html from shared-date-logs (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-z2lhf (ro)

Now we see that both containers are in fact running.

SO to make sure that both containers are doing their jobs, we need to connect to the container by running:

kubectl exec -ti multi-container-pod -c container-serving-dates bash

Now we are inside the container.

Next, we run the following command inside the container:

curl 'http://localhost:80/output.txt'

and it should serve our file:

curl 'http://localhost:80/app.txt'
Fri Nov 16 18:31:44 UTC 2018
Fri Nov 16 18:31:54 UTC 2018
Fri Nov 16 18:32:04 UTC 2018
Fri Nov 16 18:32:14 UTC 2018
Fri Nov 16 18:32:24 UTC 2018
Fri Nov 16 18:32:34 UTC 2018
Fri Nov 16 18:32:44 UTC 2018
Fri Nov 16 18:32:54 UTC 2018

If you don’t have curl installed in the container, run this command:

apt-get update && apt-get install curl

then run “ curl ‘http://localhost:80/output.txt’ ” again.

There are other things you can do with pods. For example you can have an init container that initializes the second container, once the second container come up and serving, the first container stops since it’s job is done.

That’s it for this article, we are done with Kubernetes pods, on the next articles we will talk about Deployment and replicaset.


Please enter your comment!
Please enter your name here