DEV Community

Cover image for Bite-Sized Kubernetes Part 2 - Pods
Hrittik Bhattacharjee for espelar.dev

Posted on • Updated on

Bite-Sized Kubernetes Part 2 - Pods

Arr!☸️

In Part 1 we were introduced to the basics of kubernetes and took a brief look at the k8s "objects".
Onward now with our expedition with Pods, the smallest object in k8s.

OBJECT DESCRIPTION
👉 Pods The lowest level, single instance of an application
ReplicaSets The next level, manages a set of pods
Deployments The next level, manages a set of ReplicaSets
Services The next level, manages a set of deployments
Ingress The next level, manages a set of services
Volumes The next level, manages a set of ingress
Namespaces The next level, manages a set of volumes
Cluster The highest level, manages a set of namespaces

Pods:

  • K8s does not deploy the containers directly on the worker nodes.
  • The containers are encapsulated into a kubernetes object, known as 'pod'.
  • A pod is a single instance of an application.
  • Usually a pod contains a single container, but it can contain multiple containers, and these containers are not the same containers.
  • For example, a pod can contain a container running the application, and another container running a sidecar process or helper application.
  • Containers inside the pod share the same network namespace, and can communicate with each other using localhost, and can share the same storage space.
  • Commands:

      # deploy a container by creating a pod
      $ kubectl run <pod name> --image=<image name>
    
      # list all pods
      $ kubectl get pods
    
      # list detailed info about all pods
      $ kubectl get pods -o wide
    
      # describe a pod
      $ kubectl describe pod <pod name>  
    
      # delete a pod
      $ kubectl delete pod <pod name>
    
  • Creating pods:

    • The .yml file must contain 4 top-level fields/properties i.e. apiVersion, kind, metadata and spec.
    • apiVersion: the version of the k8s API that is being used e.g. v1, apps/v1
    • kind: the type of object that is being created e.g. Pod, Service, Deployment, ReplicaSet
    • metadata: data that helps to uniquely identify the object e.g. name, labels
      • name can only have a string value
      • labels can have any kind of key-value pairs
    • spec: the specification of the object
      • containers: the list of containers that will be launched in the pod
      • name: the name of the container
      • image: the image that will be used to launch the container
    • Example pod-definition.yml:

      apiVersion: v1
      kind: Pod
      metadata:
        name: my-pod
        labels:
          app: my-app
          type: my-type
      spec:
        containers:
        - name: my-container
          image: my-image-name:my-image-tag
      
    • Once the .yml file is created, it can be used to create a pod using the kubectl create command:

      $ kubectl create -f <file name>.yml
      
    • View detailed info about the pod:

      $ kubectl describe pod <pod name>
      
    • Delete the pod:

      $ kubectl delete pod <pod name>
      
    • Update the pod with an updated .yml file e.g. with a new image, etc.:

      $ kubectl apply -f <file name>.yml
      

Great job sailor!
You now know what pods are, hopefully you found this post an interesting and useful read.
Onwards now to "Controllers and ReplicaSets" in Part 3 which will be out soon!

Haul Wind!!!

Top comments (0)