DEV Community

Madhavam Saxena
Madhavam Saxena

Posted on • Updated on

Deployment in Kubernetes

What is Deployment in Kubernetes?
Deployment basically is a higher level Kubernetes object which is used to manage POD's and their version.

Creating a deployment
There are basically two methods to create a deployment:

  1. Imperative
  2. Declarative We are going to learn about Declarative method. Attached below is the snippet for creating a deployment in k8s.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: <Name Of Deployment that you want to assign>
  labels: <Labels that you want to attach with deployment>
spec:
  replicas: <Defines the total number of pods expected to run>
  selector: <Selects the pods to control on the basis of 
             selector.Matchlabel is the mostly used type>
    matchLabels:
      <labelkey>: <labelvalue>
  template: <Template is basically a defined structure for 
             containers running inside the pod>
    metadata:
      labels: <if needed>
      name: <if needed>
    spec:
      containers: <Values assigned is in the form of list>
        - name: <Name of container that you want to give>
          image: <Image of container>
          ports: <port num. on which its expected to listen
                  This value is also given in form of list>
            - containerPort:    
Enter fullscreen mode Exit fullscreen mode

So what is happening above?
We have created an kubernetes object whose kind is deployment. Name of this deployment can be defined at .metadata.name and labels can be attached to this deployment at .metadata.labels .
Once the deployment is created, it generates a replicaset (rs) which is responsible to create the declared number of pods. Now, after defining replicas, we have declared selectors. So, what's the use of selector?

A selector is used to basically select the pods that needs to be controlled via deployment which we are creating. Now the selection of pods is done on the basis of labels i.e. labels mentioned in matchlabels and that .metadata.label should be same, else it might can be cause of misbehavior or Error.
.spec.template is supposed to be a defined structure for the pod following which the pod('s) will spin up.
.spec.template.spec defines the container/'s . Value if container is given in the form of list.

How to run this file?

kubectl apply -f deployment.yml      
Enter fullscreen mode Exit fullscreen mode

How to see deployments?

kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

How to delete a specific deployment?

kubectl delete deployment <name_of_deployment>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)