Table of Contents
Core Concepts
Namespaces
Exercise 1
List all namespaces.
Show Solution
kubectl get namespaces
Exercise 2
Create a new namespace mynamespace
.
Show Solution
kubectl create namespace mynamespace
Exercise 3
List all pods in a namespace.
Show Solution
kubectl get pods --namespace mynamespace
Exercise 4
List all pods in all namespaces.
Show Solution
kubectl get pods --all-namespaces
Exercise 5
List all deployments and services in all namespaces.
Show Solution
kubectl get deployments --all-namespaces
kubectl get services --all-namespaces
Pod Basics
Exercise 6
Create a new pod nginx-webapp
in a namespace of your choice other than default
. Confirm that the pod has started successfully.
Show Solution
kubectl run nginx-webapp --image=nginx --restart=Never --namespace mynamespace
kubectl get pod nginx-webapp --namespace mynamespace
## Make sure the pod is in STATUS = Running
## Example:
## NAME READY STATUS RESTARTS AGE
## nginx-web 1/1 Running 0 2m6s
NOTE: Note that we added --restart=Never
to make sure that only a pod is created. If we would omit this, a deployment would be created that would spin up our pod. See here for more information.
Exercise 7
Create a pod nginx
with image nginx:1.18-alpine
using a YAML file.
Show Solution
kubectl run nginx --image=nginx:1.18-alpine --restart=Never -o yaml --dry-run=client > pod.yaml
kubectl create -f pod.yaml
Exercise 8
Get the pod specification for an existing pod in YAML format.
Show Solution
kubectl get pod nginx -o yaml
Exercise 9
Get the pod specification for an existing pod in JSON format.
Show Solution
kubectl get pod nginx -o json
Exercise 10
Get the details for an existing pod (such as pod status, recent events, related resources, etc.).
Show Solution
kubectl describe pod nginx
Exercise 11
Delete a pod.
Show Solution
kubectl delete pod nginx
Exercise 12
Create a pod busybox
that executes the shell command sleep 3600
and then stops.
Show Solution
kubectl run busybox --image=busybox --restart=Never -- sh -c "sleep 3600"
Exercise 13
Create a pod with image busybox
that only prints ‘Hi!’ and then stops.
Open the pod logs and check if there is any output.
Show Solution
kubectl run my-busybox-pod --image=busybox --restart=Never -- sh -c "echo Hi!"
kubectl logs my-busybox-pod
Exercise 14
Connect to a pod and print all environment variables.
Show Solution
kubectl exec nginx -- sh -c env
## or connect to it in interactive mode
kubectl exec nginx -it sh
## print all environment variables using the 'env' command
Exercise 15
Create a pod nginx
with image nginx:1.18-alpine
. When the pod was created, change its image to nginx:1.19-alpine
. Convince yourself that the pod has been successfully updated.
Show Solution
kubectl run nginx --image=nginx:1.18-alpine --restart=Never
kubectl set image pod/nginx nginx=nginx:1.19-alpine
kubectl describe pod nginx
## 1: Inspect the output and make sure that the Image attribute has been updated to
## nginx:1.19-alpine (see attribute "Containers" > "nginx" > "Image")
## 2: The event log should contain information about a successful image pull and pod restart.
Exercise 16
Show the logs of a previously crashed pod.
Show Solution
kubectl logs my-busybox-pod -p
TIP: This command does not show logs for already terminated pods!
Exercise 17
Show a pods logs from the last hour.
Show Solution
kubectl logs --since=1h nginx
Exercise 18
- Create a pod
nginx-server
with imagenginx
and expose it on port 80. - Create another pod
nginx-checker
with imagebusybox
that fetches the index HTML page fromnginx-server
and prints it to standard output. - Check the logs of
nginx-checker
to see the HTML content.
Show Solution
kubectl run nginx-server --image=nginx --restart=Never --port 80
kubectl describe pod nginx-server
## Read the IP of the nginx-server pod from the `describe` command output above.
kubectl run nginx-checker --image=busybox --restart=Never -- sh -c "wget -O- <IP>"
kubectl logs nginx-checker
Multi-Container Pods
Multi-Container Pod Basics
Exercise 19
Create a multi-container pod named multibox
with two busybox containers that execute the command sleep 3600
. Check that both containers are running.
Show Solution
# Create a single-container pod as usual and pipe the YAML output into a file
kubectl run multibox --image=busybox --restart=Never --dry-run=client -o yaml -- sh -c "sleep 3600" > multi-container.yaml
Add a second container to the pod specification file we just created.
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: multibox
name: multibox
spec:
containers:
- image: busybox
name: busybox1
args:
- sh
- -c
- sleep 3600
- image: busybox
name: busybox2
args:
- sh
- -c
- sleep 3600
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
Create the pod:
kubectl create -f multi-container.yaml
kubectl get pod multibox
# The READY column in the command output should show 2/2 containers in STATUS = Running.
Exercise 20
Check the logs of one of the containers from a multi-container pod.
Show Solution
# Show the logs of container "multibox" of the multi-container pod named "multibox".
kubectl logs multibox -c multibox1
Exercise 21
Show the metrics for all pods.
Show Solution
kubectl top pods
Exercise 22
Show the metrics for all nodes.
Show Solution
kubectl top nodes
Exercise 23
- Create a pod that shares a volume with two containers
nginx
andbusybox
. - Mount a volume to path
/usr/share/nginx/html/
in both containers. - Let the
busybox
container write the current time into file/usr/share/nginx/html/index.html
every 5 seconds. - Start the pod, connect to the
nginx
container and verify that the time is being updated every 5 seconds.
TIP: Use
kubectl exec
to connecto to a pod container.
TIP: To write the current time into the file, use the shell command
sh -c "while true; do $(date) > /usr/share/nginx/html/index.html; sleep 5; done
Show Solution
kubectl run multi-container --image=nginx --restart=Never --dry-run=client -o yaml > multi-container.yaml
Add the second container to the multi-container.yaml
file we just created:
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: multi-container
name: multi-container
spec:
volumes:
- name: html-volume
emptyDir: {}
containers:
- image: nginx
name: nginx
resources: {}
volumeMounts:
- mountPath: /usr/share/nginx/html/
name: html-volume
- image: busybox
name: busybox
resources: {}
volumeMounts:
- mountPath: /usr/share/nginx/html/
name: html-volume
args:
- sh
- -c
- 'while true; do echo "$(date)" > /usr/share/nginx/html/index.html; sleep 5; done'
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
# Verify that the pod started successfully.
kubectl describe pod multi-container
# Exec into the "nginx" container of the "multi-container" pod .
kubectl exec multi-container -c nginx -it -- sh
Once you’re connected to the container, execute the following command to show the file content:
cat /usr/share/nginx/html/index.html