Kubernetes basic Objects - Blogs
X
14Apr

Kubernetes basic Objects

In this article, we will learn about the basic objects of Kubernetes in detail with their respective YAML files.

  • POD
  • Replication Controller
  • Replica Set
  • Daemon Set
  • Deployment
  • Service
  • Volume
  • Job

POD

A pod is a basic unit of K8S. Pod always run on node. In K8S cluster Pod represents running process. Inside the pod, you can have one or, more containers. Those containers all share a unique Network IP, storage, network and any other specification applies to the pod. Pods abstract network and storage away from the underlying container. This lets you move containers around the cluster more easily. Each pod has its own unique ip address within the cluster.

Any data saved inside the pod will disappear without a persistent storage.

Note : For better understanding, let’s consider an NGINX Image for all the YAML files as an example.

YAML for POD:

 

apiVersion: v1
kind: Pod
metadata:
    name: 1stPOD                                                    
    labels:
    app: nginx                                                            
    spec:
      containers:
      - name: 1stContainer
        image: nginx
        ports:
        - containerPort: 80

 

1

PODs are ephemeral (lasts for a very short time) and won’t be rescheduled to a new node once it dies.

Make sure that a pod is not used directly for deployment, because Kubernetes has controllers such as Replica Sets, Deployment, Daemon Sets which keep the pods alive.

 

Replication Controller

Replication controller is one of the key features of Kubernetes which manages the lifecycle of a Pod. It is responsible for creating multiple pods and ensure that the specified number of pod replicas are always present and running at any point of time.

YAML for Replication Controller

 

apiVersion: v1
kind: ReplicationControlller
metadata:
  name: 1stRC-Pod
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: 1st RC-Container
        image: nginx
        imagePullPolicy: Always
        ports:
        - containerPort: 80

2

Replica Set

Replica Set is the next-generation Replication Controller. The only difference between a Replica Set and Replication Controller is the selector support.

Replica Set supports the new set-based selector approach whereas Replication Controller uses equality-based selector requirements.

YAML for Replica Set

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-rs-pod
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-rs-pod
    matchExpressions:
-	Key: env
Operator: In
values:
-dev
  template:
    metadata:
      labels:
        env: dev
        app: nginx-rs-pod 
    spec:
      containers:
      - name: 1stRS-container
        image: nginx
        ports:
            containerPorts: 80

3

Daemon Set

A daemon Set ensures that all or some of the Kubernetes nodes run a copy of the pod.

When a new node is added to the cluster, a Pod is added to it match the rest of the nodes and when a node is removed from the cluster, the pod is garbage collected.

YAML for Daemon Set:

 

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: my-Ds-pod
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-ds-pod
  template:
    metadata:
      labels:
        app: nginx-ds-pod 
    spec:
      nodeSelector:
         name: node1    #labled the node; kubectl label nodes  =
      containers:       
      - name: 1stDS-container
        image: nginx
        ports:
            containerPorts: 80

4

Deployment

In Kubernetes, using a deployment is the recommended way to deploy a pod or ReplicaSet because of the advanced features it comes with.

Below are the some of the key benefits of using Deployment:

  • Update pods
  • Rollback to previous versions
  • Scale deployment up or down
  • Pause and resume the deployment
  • Use the status of deployment to determine state of replicas
  • Clean up older RS that you don’t need anymore.

 

Deployment has two strategies

Recreate strategy: In this type of very simple deployment, all the old pods are killed at once and they are replaced all at once with the new pods.

YAML for Recreate type:

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-Deployment-pod
spec:
  replicas: 3
  selector:
  strategy:
  type: Recreate
    matchLabels:
      app: nginx-deployment-pod
  template:
    metadata:
      labels:
        app: nginx-deployment-pod 
    spec:
       containers:       
       - name: 1stdeployment-container
          image: nginx:1.7.9
            ports:
               containerPorts: 80

5

Rolling Update strategy: The rolling update is the standard default deployment for Kubernetes. It works slowly, replacing pods in the previous version of your application with the new version without any cluster downtime

YAML for Rolling Update:

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: 1st-deployment-Ru-pod
  labels:
    app: nginx
spec:
strategy:
  type: Rollingupdate
 rollingupdate:
    maxSurge: 1
    maxUnavailable: 1
minReadySeconds: 30
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: 1stdeployment-RU-Conatiner
        image: nginx:1.7.9
        ports:
        - containerPort: 80

6

Related

Volume Management in Kubernetes

Since pods created in k8s are ephemeral, we are able to get the data as long as pods are alive, but ...

Read More >

Quick Understanding on Stateful Sets in Kubernetes

Stateful sets are similar to deployments, they can scale up and scale down, they can perform rolling...

Read More >

How to do Installation of K8S using kubeadm?

Till now I have given blogs on k8s objects, services, namespaces, ingress etc. but where to execute ...

Read More >

HOW TO DO BACKUP AND RESTORE ENTIRE K8S CLUSTER?

If you have deployed different applications on k8s cluster using various objects like deployments, p...

Read More >

Taints and Tolerations

Taints and tolerations are used to restrict the pods to schedule them onto respective nodes. There i...

Read More >

Node Selector

Assume you have 3 nodes cluster of which two of them are having lower hardware resources and one of ...

Read More >

NODE AFFINITY

The k8s node affinity feature is to ensure pods are hosted on a particular node. As mentioned in pre...

Read More >

What is ingress and what are the components of it?

Ingress is a resource, which exposes the http and https from external sources to the services within...

Read More >

Kubernetes RBAC (Role Based Access Control)

The Kubernetes RBAC (role bases access control) system helps us in defining set of rules in controll...

Read More >

Namespaces in Kubernetes

A default namespace is created automatically when the cluster is being setup. To isolate or prevent ...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Contact Us
  • *
  • *