Lab: Pods,InitContainers

In case you haven’t cloned the repo

 git clone https://github.com/amitopenwriteup/k8s.git

Lab1: Create a simple pod

  • apiVersion: Specifies the API version being used (in this case, v1).
  • kind: Specifies the kind of resource, which is a Pod in this case.
  • metadata: Contains metadata about the Pod, such as its name and labels.
  • spec: Defines the specification for the Pod.
    • containers: A list of containers within the Pod.
      • name: Name of the container.
      • image: The Docker image to use for the container.
      • ports: Specifies the ports that the container exposes.
cd k8s
kubectl create -f expod.yaml
kubectl get pod
kubectl describe pod example-pod
kubectl delete pod example-pod

Lab2: Create pod in dev namespace

  • namespace: Specifies the namespace where the Pod will be created (in this case, “dev”).
  • spec: Defines the specification for the Pod
    • command: Specifies the command to run in the container. In this example, it’s set to [“sleep”, “3600”] to sleep for 3600 seconds
cd k8s
cat devpod.yaml
kubectl create ns dev
kubectl create -f devpod.yaml
kubectl get pods --namespace=dev
kubectl describe pod busybox-pod --namespace=dev
kubectl delete pod busybox-pod --namespace=dev

Lab3: Multicontainer pods

Pod named “multi-container-pod” contains two containers: “main-container” running the Nginx image and “sidecar-container” running the BusyBox image. The main container exposes port 80, and the sidecar container runs a simple loop to continuously print a message.

cd k8s
kubectl create -f multicont.yaml
kubectl get pods
kubectl describe pod multi-container-pod
kubectl exec multi-container-pod -c main-container -- date
kubectl exec multi-container-pod -c sidecar-container -- ls
kubectl delete pod multi-container-pod

Lab 4: Init Containers

A list of init containers that run before the main containers.

cd k8s
kubectl create -f initpod.yaml
kubectl describe pod pod-with-init
kubectl delete pod pod-with-init

Try following Lab5:

Change the init container image name  to busbox:lates, in above yaml. 

Create pod, and check whether main container starts or not