Kubernetes - Pod

Intro to smallest deployable unit in Kubernetes

ta-ching chen

3 minute read

 Table of Contents

Pod 

A Pod is a group of one or more containers and it is the smallest deployable unit in Kubernetes. Pod and VM (virtual machine) are similar in many ways:

  • Containers in pod share the same IP address and port space
  • Shared storage (volumes)
  • Relatively tightly coupled applications in one pod

Shared IP address and port space 

Kubernetes applies IP at Pod scope. Containers within a pod share an IP address and port space, and can find each other via localhost. Its just like what we do in VM: one prcoess find others with localhost:port.

Also pod can be accessed by other pod in Kubernetes cluster, it helps developer no need to worry about how to access other containers cross hosts. However, normally we don’t access other Pod directly but using service instead and we will discuss it in later article.

Shared storage (volumes) 

For example, emptyDir is one of volume types in Kubernetes, it share the lifetime with Pod which means that it exists as long as the Pod is running on that node. Containers in same pod share the emptyDir, its quite helpful when you want to access the same files across Pod. Imagine that there is a service running in Pod and you need a log parser to parse the service log in order to monitor service state, we can access the log files easily in different containers through mounting shared emptyDir volume.

Relatively tightly coupled applications 

Container often runs single application. But sometimes we also need a sidecar container to help the main container, for example we need a monitor container to monitor service container and sending alerts if something goes wrong. In a pre-container world, these tightly coupled applications would have run on the VM, and now they runs on different containers in the same Pod.

Service container and sidecar containers

Hands-On 

Lets create a web server pod with two containers backend and frontend in it.

  • backend: prints current date to index.html(/tmp/index.html) located at the share volume every second.
  • frontend: runs a simple http server that display the index.html, which the directory is mount to /var/www, to user.
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
  labels:
    service: example-pod
spec:
  containers:
    - name: backend
      image: ubuntu
      command:
        - "bash"
        - "-c"
      args:
        - "while true; do date > /tmp/index.html ; sleep 1; done"
      volumeMounts:
        - mountPath: /tmp # the mount point for volume
          name: content-volume # should match the volume name defined below
    - name: frontend
      image: trinitronx/python-simplehttpserver
      command:
        - "sh"
        - "-c"
      args:
        - "python -m SimpleHTTPServer 8080"
      ports:
        - containerPort: 8080
      volumeMounts:
      - mountPath: /var/www # we can define different mount point for different container
        name: content-volume
  volumes:
    - name: content-volume
      emptyDir: {}

Find the internal IP Address of pod in Kubernetes cluster

$ kubectl get pod -o wide
NAME                    READY     STATUS    RESTARTS   AGE       IP             NODE
example-pod             2/2       Running   0          4m        172.16.31.4    10.101.0.199

Try fetch web page from the http server.

$ curl http://172.16.31.4:8080
Sun Nov 27 15:25:04 UTC 2016

Further Readings 

comments powered by Disqus