In the Getting Started with Kubernetes blog, I explained in detail the installation procedure of tools required to setup local Kubernetes environment and creation of single node Kubernetes cluster. I also briefly touched upon Deployment, Pod and sub-commands of kubectl. I will illustrate Kubernetes objects in this blog. So, lets keep it rolling.

What Are They?

Kubernetes objects are persistent entities facilitated by Kubernetes system. The objects can be used to define, deploy, maintain and scale a containerized application in a Kubernetes cluster.  The kubectl command line interface can be used to create, update, get, describe and delete a object. The basic Kubernetes objects are following:

  • Pod
  • Service
  • Volume
  • Namespace

Kubernetes also provides higher level objects to manage the fundamental objects efficiently. These entities are built upon the basic objects. These includes following.

  • Deployment
  • StatefulSet
  • ReplicaSet
  • DaemonSet
  • Job

In this article, I will demonstrate the creation, deletion and management of Pod, ReplicaSet and Deployment and touch upon Service. There are multiple ways to create objects, I will utilize YAML manifests. Because, a YAML file illustrates all the available options and configurations of a Kubernetes object. Therefore, it gives better understanding of an entity.

Pod

A Pod is the most fundamental unit of Kubernetes cluster. A Pod represents an instance of a containerized application running on Kubernetes cluster. There is one or more running containers inside a Pod and they all share same resources, network and lifecycle. A Pod acts as a wrapper on running containers inside it.  I will create a pod with echoserver image that I used in the blog also.

Definition

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
  labels:
    app: hello-world
spec:
  containers:
  - name: echoserver
    image: k8s.gcr.io/echoserver:1.10
  restartPolicy: Always

In this example the definition of Pod is consist of 4 main fields:

  • apiVersion refers to schema version. Different Kubernetes objects and resources were introduced in different versions.
  • kind refers to the type of object
  • metadata is consist of information such as name, labels, annotations
  • spec defines the behavior of the object. In this example, the Pod will run a container name echoserver which run the image k8s.gcr.io/echoserver:1.10 and restartPolicy determines what happens when a container fails in a Pod.

You can find more information about these fields and options here.

Creation

kubectl apply -f hello-world-pod.yaml

Describe

kubectl describe pod hello-world

Get

kubectl get pod hello-world
kubectl get pod hello-world -o yaml
kubectl get pod hello-world -o json

Logs

kubectl logs hello-world
kubectl logs hello-world --tail=3
kubectl logs hello-world -f

Update

Make changes in hello-world-pod.yaml and run following command

kubectl apply -f hello-world-pod.yaml

Delete

kubectl delete -f hello-world-pod.yaml
kubectl delete pod hello-world

As you can see, it is quite easy to create, manage and delete a Pod. A Pod is scheduled on a node when it is created and remains on it throughout the lifespan. There is a problem when we create a Pod directly. A Pod is a ephemeral entity in a Kubernetes cluster, which means it gets deleted or evicted when the node fails, process gets terminated or for lack of resources. That is why, it is always recommended to utilize high level abstraction/controllers. I provided the names of available controllers in Kubernetes in the last section. It is not possible to cover all of them in this article. However, I will explain ReplicaSet and Deployment controllers.

ReplicaSet

A ReplicaSet is utilized to run multiple copies of a Pod. In the definition of a ReplicaSet, we can specify Pod definition in spec and the number of Pods in replicas option. Based on that a ReplicaSet schedule identical copies of a Pod.

Definition

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: hello-world-rs
  labels:
    app: hello-world
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: echoserver
        image: k8s.gcr.io/echoserver:1.10
      restartPolicy: Always

Creation

kubectl apply -f hello-world-replicaset.yaml

It will create the ReplicaSet name hello-world-rs and the ReplicaSet will ensure that they are 3 pods with image echoserver running in the cluster. You can get running Pods with following command.

kubectl get pod

Describe

kubectl describe rs hello-world-rs

Get

 
kubectl get rs
kubectl get rs hello-world-rs -o yaml
kubectl get rs hello-world-rs -o json

Modify

Make changes in hello-world-replicaset.yaml and run following command

kubectl apply -f hello-world-replicaset.yaml

Delete

kubectl delete -f hello-world-replicaset.yaml
kubectl delete rs hello-world-rs

Deployment

Deployment can be used to create, update and manage pods effectively in Kubernetes cluster. Deployment comes higher than ReplicaSet in Kubernetes resource hierarchy.

Definition

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-deployment
  labels:
    app: hello-world
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: echoserver
        image: k8s.gcr.io/echoserver:1.10
      restartPolicy: Always

Creation

kubectl apply -f hello-world-deployment.yaml

It will create the Deployment name hello-world-deployment. As a result, it will create a ReplicaSet which will schedule number of pods mentioned in replicas option. You can get ReplicaSet and Pods with the commands mentioned earlier.

You might wonder, there is no difference between ReplicaSet and Deployment. It also run multiple instances of a Pod defined in spec. But, a Deployment provides very important features. It is not possible to roll out updates to the Pods managed by ReplicaSet. That is where magic of Deployment comes in.  For an instance, if you want to upgrade the image of Pod from version 1.10 to 1.4, all you need to do, is change the image option in Yaml file to image: k8s.gcr.io/echoserver:1.4 and run the kubectl apply sub-command again. You can check the status of rollout using following command.

kubectl rollout status deployment hello-world-deployment

Describe

kubectl describe deployment hello-world-deployment

Get

 
kubectl get deployment
kubectl get deployment hello-world-deployment -o yaml
kubectl get deployment hello-world-deployment -o json

Modify

Make changes in hello-world-deployment.yaml and run following command

kubectl apply -f hello-world-deployment.yaml

Delete

kubectl delete -f hello-world-deployment.yaml
kubectl delete deployment hello-world-deployment