Skip to content

Deployments

  • A Kubernetes Deployment tells Kubernetes how to create or modify instances of the pods that hold a containerized application.

  • Deployments can help to efficiently scale the number of replica pods, enable the rollout of updated code in a controlled manner, or roll back to an earlier deployment version if necessary.

  • Create deployment

$ kubectl create deployment nginx-deployment --image=nginx
deployment.apps/nginx-deployment created
  • Get deployments
$ kubectl get deployments
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           33s
$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6d6565499c-frsn7   1/1     Running   0          42s
  • Describe a specific deployment
$ kubectl describe deployments nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 23 Jan 2024 11:44:41 +0530
Labels:                 app=nginx-deployment
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx-deployment
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx-deployment
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-6d6565499c (1/1 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  4m10s  deployment-controller  Scaled up replica set nginx-deployment-6d6565499c to 1
  • Describe the pod
$ kubectl describe pods nginx-deployment-6d6565499c-frsn7
Name:             nginx-deployment-6d6565499c-frsn7
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Tue, 23 Jan 2024 11:44:41 +0530
Labels:           app=nginx-deployment
                  pod-template-hash=6d6565499c
Annotations:      <none>
Status:           Running
IP:               10.244.0.5
IPs:
  IP:           10.244.0.5
Controlled By:  ReplicaSet/nginx-deployment-6d6565499c
Containers:
  nginx:
    Container ID:   docker://f4bd6e1a044172d24aa1975ee0741c10241dcec77c715f14187c124e404e28da
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:4c0fdaa8b6341bfdeca5f18f7837462c80cff90527ee35ef185571e1c327beac
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 23 Jan 2024 11:44:46 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-27pxk (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-27pxk:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  20m   default-scheduler  Successfully assigned default/nginx-deployment-6d6565499c-frsn7 to minikube
  Normal  Pulling    20m   kubelet            Pulling image "nginx"
  Normal  Pulled     20m   kubelet            Successfully pulled image "nginx" in 3.852s (3.852s including waiting)
  Normal  Created    20m   kubelet            Created container nginx
  Normal  Started    20m   kubelet            Started container nginx
  • To scale the deployments
$ kubectl scale deployment nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled
$ kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
nginx-deployment-6d6565499c-2nh8h   1/1     Running   0          10m   10.244.0.7   minikube   <none>           <none>
nginx-deployment-6d6565499c-666bg   1/1     Running   0          10m   10.244.0.8   minikube   <none>           <none>
nginx-deployment-6d6565499c-cghkq   1/1     Running   0          10m   10.244.0.6   minikube   <none>           <none>
nginx-deployment-6d6565499c-cvbcv   1/1     Running   0          10m   10.244.0.9   minikube   <none>           <none>
nginx-deployment-6d6565499c-frsn7   1/1     Running   0          35m   10.244.0.5   minikube   <none>           <none>
  • If we try to connect to this IP address from our local system, we won't be able to do.
$ curl 10.244.0.6
^C
  • But if we connect to the node and then connect to this IP address, we will be able to do because now we are inside of kubernetes cluster.
$ minikube ssh
docker@minikube:~$ curl 10.244.0.6
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
docker@minikube:~$ exit
logout

Service

  • To create a service for deployment i.e To expose the deployment to other services within the cluster:
$ kubectl expose deployment nginx-deployment --port=8080 --target-port=80
service/nginx-deployment exposed
  • Get the services list
$ kubectl get services
NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes         ClusterIP   10.96.0.1        <none>        443/TCP    17h
nginx-deployment   ClusterIP   10.110.135.226   <none>        8080/TCP   89s

Note: Cluster IP address are not accesible/available outside of kubernetes cluster. They are acessible from any node.

  • Describe the nginx-deployment service.
$ kubectl describe service nginx-deployment
Name:              nginx-deployment
Namespace:         default
Labels:            app=nginx-deployment
Annotations:       <none>
Selector:          app=nginx-deployment
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.110.135.226
IPs:               10.110.135.226
Port:              <unset>  8080/TCP
TargetPort:        80/TCP
Endpoints:         10.244.0.5:80,10.244.0.6:80,10.244.0.9:80
Session Affinity:  None
Events:            <none>
  • Delete the deployment and service
$ kubectl delete deployment nginx-deployment
deployment.apps "nginx-deployment" deleted
$ kubectl delete service nginx-deployment
service "nginx-deployment" deleted

Creating Service with type NodePort

  • Expose the deployment with type NodePort
kubectl expose deployment k8s-web-hello --type=NodePort --port=3000
  • Get the url to view in the browser
minikube service k8s-web-hello --url

Creating Service with type LoadBalancer

  • Expose the deployment with type LoadBalancer
$ kubectl expose deployment k8s-web-hello --type=LoadBalancer --port=3000
service/k8s-web-hello exposed
$ kubectl get services
NAME            TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
k8s-web-hello   LoadBalancer   10.110.203.226   <pending>     3000:30600/TCP   19s
kubernetes      ClusterIP      10.96.0.1        <none>        443/TCP          20h

Rolling out deployment

  • Publish a new image in the dockerhub with a proper tag.
$ kubectl set image deployment k8s-web-hello k8s-web-hello=prabinkumarbaniya/k8s-web-hello:2.0.0
deployment.apps/k8s-web-hello image updated

where, in the command after the name deployment,
k8s-web-hello is the name of deployment,
k8s-web-hello indicates the Container, the update will take place
prabinkumarbaniya/k8s-web-hello:2.0.0 indicates the new image and the tag.

  • Check the status of the deployment
$ kubectl rollout status deploy k8s-web-hello
deployment "k8s-web-hello" successfully rolled out

To access dashboard

minikube dashboard

To delete every resources from default namespace

kubectl delete all --all