Kubernetes Pods

A Pod is the basic building block of an application running in Kubernetes. A Pod encapsulates one or more containers and a set of resources shared by those containers.  All containers that run in a Kubernetes cluster run inside a Pod.

Creating a Pod

Below is a sample Pod definition. To apply this to the cluster run kubectl apply -f demo-pod.yml

apiVersion: v1
kind: Pod
metadata:
 name: demo-pod
 labels:
  app: demo-app
spec:
 containers:
 - name: demo-app-container
   image: busybox
   command: ['sh', '-c', 'echo hello KCAD & sleep 3600']
  • apiVersion: v1 – version of the Kubernetes API this yaml is compatible with
  • kind: Pod – type of Kubernetes Object being defined
  • metadata.name: demo-pod – name of the Pod. (used to reference object via kubectl)
  • metadata.labels.app: demo-app – identifies the name of the app this Pod is running
  • spec.containers: – list of containers this Pod will run.
  • spec.containers.name: demo-app-container – name given to container when its run
  • spec.containers.image: busybox – name of the image used to run the container. busybox is a small lightweight image, ideal for testing.
  • spec.containers.command: ['sh', '-c', 'echo hello KCAD & sleep 3600'] – command to run on container startup.

To see a list of running Pods run kubectl get pods. You should be able to see the Pod we just created. To see if the Pod printed hello KCAD on startup, run kubectl logs pod/demo-pod. You should see output like the following.

Note: A Pod is not typically created directly using a Pod definition. In reality a Pod is created indirectly as part of a Deployment definition.

Editing a Pod

To edit a Pod you can use the kubectl edit command. For example to edit demo-pod we created earlier, run kubectl edit pod demo-pod. Update labels.app: demo-app to labels.app.demo-app-updated and save the changes.

Run kubectl describe demo-pod to see the updated labels.app value as follows.

Note that not all attributes displayed when you run kubectl edit are editable.

As well as the kubectl edit command you can update a Pod by changing its yaml definition and reapplying it to the cluster with kubectl apply -f demo-pod.yml . In practice this is a better approach than using the kubectl edit command. Its preferable that the yaml definition and the Pod cluster definition remain in sync. Yaml updates have an audit trail and can be version controlled, whereas adhoc updates applied with kubectl edit cannot.

Deleting a Pod

To delete a Pod use the kubectl delete command. To delete  demo-pod run kubectl delte pod demo-pod.

The sample code for this post is available here.

Configuring Containers

Custom Commands

In spec.containers.command you can specify a command used to run the container. This overrides any default command specified in the image. Arguments can be passed to the command using spec.containers.args.

apiVersion: v1 
kind: Pod 
metadata:
 name: command-demo-pod
 labels:
  app: demo-app
spec:
 containers: 
 - name: demo-app-container 
   image: busybox 
   command: ['sh', '-c']
   args: ['echo hello KCAD! & sleep 3600']
 restartPolicy: Never

Container Port

To expose a port the container is listening on, use spec.containers.ports.containerPort. Other components in the cluster such as a Service can then access the container via the containerPort.

apiVersion: v1 
kind: Pod 
metadata:
 name: container-port-demo-pod
 labels:
  app: demo-app
spec:
 containers: 
 - name: demo-app-container 
   image: nginx
   ports:
     - containerPort: 80