Running Containers in Pods

KubernetesKubernetesBeginner
Practice Now

Introduction

In this lab, you will learn how to run containers in Kubernetes Pods. Pods are the smallest and simplest unit in Kubernetes, and they can contain one or more containers. Running containers in Pods provides many benefits, including better resource utilization, easier scaling, and more efficient deployment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/create -.-> lab-14998{{"`Running Containers in Pods`"}} kubernetes/apply -.-> lab-14998{{"`Running Containers in Pods`"}} end

Create a Pod with a Single Container

The first step is to create a Pod with a single container. To do this, you will create a YAML file that defines the Pod and its container.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-1
spec:
  containers:
    - name: my-container
      image: nginx

Save the above code in a file named /home/labex/project/pod-single-container.yaml and execute the following command:

kubectl apply -f /home/labex/project/pod-single-container.yaml

This command will create a Pod named my-pod-1 with a single container named my-container that runs the Nginx image.

Create a Pod with Multiple Containers

The second step is to create a Pod with multiple containers. To do this, you will modify the previous YAML file to add another container.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-2
spec:
  containers:
    - name: my-container
      image: nginx
    - name: my-sidecar
      image: busybox
      command: ["sh", "-c", "echo Hello from the sidecar! && sleep 3600"]

Save the above code in a file named /home/labex/project/pod-multiple-containers.yaml and execute the following command:

kubectl apply -f /home/labex/project/pod-multiple-containers.yaml

This command will create a Pod named my-pod-2 with two containers. The first container runs the Nginx image, and the second container runs the BusyBox image and prints a message to the console.

Create a Pod with Environment Variables

The third step is to create a Pod with environment variables. To do this, you will modify the YAML file to add environment variables to the Nginx container.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-3
spec:
  containers:
    - name: my-container
      image: nginx
      env:
        - name: MY_ENV_VAR
          value: "Hello World!"

Save the above code in a file named /home/labex/project/pod-env-vars.yaml and execute the following command:

kubectl apply -f /home/labex/project/pod-env-vars.yaml

This command will create a Pod named my-pod-3 with a single container named my-container that runs the Nginx image and has an environment variable named MY_ENV_VAR with the value Hello World!.

Create a Pod with Configmaps

The fourth step is to create a Pod with ConfigMaps. ConfigMaps are a way to store configuration data separately from the application code.

To do this, you will first create a ConfigMap with some data.

kubectl create configmap my-config --from-literal=MY_ENV_VAR=labex

This command will create a ConfigMap named my-config with a key MY_ENV_VAR and a value labex.

Next, you will modify the YAML file to add the ConfigMap to the Nginx container.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-4
spec:
  containers:
    - name: my-container
      image: nginx
      envFrom:
        - configMapRef:
            name: my-config

Save the above code in a file named /home/labex/project/pod-configmap.yaml and execute the following command:

kubectl apply -f /home/labex/project/pod-configmap.yaml

This command will create a Pod named my-pod-4 with a single container named my-container that runs the Nginx image and has a ConfigMap named my-config injected as environment variables.

Create a Pod with Persistent Volumes

The fifth step is to create a Pod with a Persistent Volume (PV) and a Persistent Volume Claim (PVC). PVs and PVCs are used to store and access data persistently across Pod restarts.

To do this, you will first create a PV.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

Save the above code in a file named /home/labex/project/pv.yaml and execute the following command:

kubectl apply -f /home/labex/project/pv.yaml

This command will create a PV named my-pv with a capacity of 1Gi and a host path of /mnt/data.

Next, you will create a PVC that requests 1Gi of storage from the PV.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Save the above code in a file named /home/labex/project/pvc.yaml and execute the following command:

kubectl apply -f /home/labex/project/pvc.yaml

This command will create a PVC named my-pvc that requests 1Gi of storage.

Finally, you will modify the YAML file to add a volume and a volume mount to the Nginx container.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-5
spec:
  containers:
    - name: my-container
      image: nginx
      volumeMounts:
        - name: my-volume
          mountPath: /mnt/data
  volumes:
    - name: my-volume
      persistentVolumeClaim:
        claimName: my-pvc

Save the above code in a file named /home/labex/project/pod-pv.yaml and execute the following command:

kubectl apply -f /home/labex/project/pod-pv.yaml

This command will create a Pod named my-pod-5 with a single container named my-container that runs the Nginx image and has a volume mount at /mnt/data that is backed by the PVC named my-pvc.

Summary

Congratulations! You have completed this lab and learned how to run containers in Kubernetes Pods. You have also learned how to create Pods with multiple containers, environment variables, ConfigMaps, and Persistent Volumes. These are powerful concepts that will help you build more resilient and scalable applications.

Other Kubernetes Tutorials you may like