Deployment

A Deployment provides declarative updates for pods and Replicasets.

You describe the desired state in a Deployment, and the Deployment controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets or to remove existing Deployments and adopt all their resources with new Deployments.

There are two types of deployment strategies

  • Recreate strategy
  • Rolling Update

Recreate Strategy

in this strategy, deployment will convert the first rs to 0 and second rs to the desired number of replicas (here in this approach we will get a downtime)

Rolling Update:

In this strategy one pod will be deleted from an old version(rs) and a new one created for v2, similarly, second pod removed from v1 and create the second pod in v2, the same way one by one pod will be deleted and created.

Note: This is a default deployment strategy in k8

Creating a deployment

Create a file named deployment.yaml

vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: firstdeploy
  labels:
    name: firstdeploy
spec:
     replicas: 3
     selector:
       matchlabels:
          app: myapp
     template:
       metadata
          name: dpod
          labels:
            app: myapp
          spec:
            containers:
             - name: container
               image: coolgourav147/nginx-custom:v1
kubectl apply -f deployment.yaml

this will create the resources for the deployment

We can scale up and scale down the deployment for that we can use below YAML file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: firstdeploy
  labels:
    name: firstdeploy
spec:
     replicas: 4
     selector:
       matchlabels:
          app: myapp
     template:
       metadata
          name: dpod
          labels:
            app: myapp
          spec:
            containers:
             - name: container
               image: coolgourav147/nginx-custom:v1
kubectl apply -f deployment.yaml

now our pods will have a have 4 replicas instead of 3.