Difficulty: beginner
Estimated Time: 20-30 minutes

Kubernetes - Pods

Pods are groups of 1 or more containers:

  • that share:
    • storage / network configurations
    • resource contraints
    • a single IP address
  • are co-located on the same host
  • whose containers communicate over localhost
  • that are emphemeral

This labs will let you inspsect pod configurations, running containers and networking.

Please email feedback to: [email protected]

Kubernetes Pods

Step 1 of 4

Kubernetes: Simple Pods

Explore the Kubernetes cluster

Check the Kubernetes version:

kubectl version --short

Lets see what our cluster looks like.

kubectl get nodes

We should have 2 nodes running: master and node1.

List Kubernetes Pods

To see if any pods are currently running:

kubectl get pods

The No resources found. message means that there are no pods running.

Launch a Kubernetes Pod

Examine the single-container.yaml file in the resource browser.

This file specifies a very simple NGiNX container.

Generally you will want to launch pods into your K8S cluster from a file, since infrastructure as code promotes transparency and reproducibily.

Deploy a new NGiNX container using the create command:

kubectl create -f ./single-container.yaml

NOTE: If you receive this message: error: the path "./single-container.yaml" does not exist, click on the file single-container.yaml in the explorer window above the terminal window to the right of this pane, then retry the command.

Verify that the new NGiNX pod is running:

kubectl get pods

It may take a few seconds for the pod STATUS to change from ContainerCreating to Running.

You should see output similar to this:

NAME      READY     STATUS    RESTARTS   AGE
nginx     1/1       Running   0          27s

Explore a Kubernetes Pod

kubectl describe pod nginx

To get the IP address of the NGiNX pod, use this command:

kubectl describe pod nginx | grep IP | awk '{print $2}'

View pod logs

View the internal container logs of the NGiNX pod:

kubectl logs nginx

Notice that logfile is empty because NGiNX has served no requests. To create a log entry, lets request something from NGiNX using the curl command:

curl $(kubectl describe pod nginx | grep IP | awk '{print $2}')

Now lets look at the logs again:

kubectl logs nginx

Notice the new log entry.

Log into the pod

kubectl exec -ti nginx bash

You are now logged into the running NGiNX container. Poke around and examine the container:

ls -al

When you are done, exit the container by typing exit.

Delete a Kubernetes Pod

Now we can remove the running NGiNX pod using the YAML file:

kubectl delete -f ./single-container.yaml

or directly with this command:

kubectl delete pod nginx

We can now verify that the NGiNX pod has been deleted:

kubectl get pods

You should see a message confirming that there are no running pods:

No resources found.

Summary

We learned about Kubernetes Pods: how to create them, interact with them, and remove them.

In the next lesson, we will explore multi-container pods.