Thursday, May 17, 2018

kubernetes - Deployment

We can always create a Replication controller along with a pods, so that it will take care of how many pods it should keep running. What if we need to do a rolling update to the container running? . what if i have new changes and want to push them to container ?

Deployment is the one which provide these. A Deployment is a supervisor for pods and replica
sets giving you a fine grained control over how and when a new pod version is rolled out as well
as rolled back to the previous state.

The main purpose is to keep a set of identical pods running and update them in a controlled
way, performing a rolling update by default.

Create a basic deployment as,
[root@manja17-I13330 kubenetes-config]# cat basic-deployment.yml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
 name: test-service-deploy
spec:
 replicas: 2
 template:
   metadata:
     labels:
       app: test-service
   spec:
     containers:
     - name: test-ser
       image: docker.io/jagadesh1982/testing-service
       ports:
       - containerPort: 9876
       env:
       - name: VERSION
         value: "0.5"

We are actually creating a basic deployment for the testing-service. We also passed and
environment value called “Version” with a value “0.5”.

Check if the deployments are available,
[root@manja17-I13330 kube-testing]# kubectl get deployments
NAME                   DESIRED CURRENT UP-TO-DATE   AVAILABLE AGE
test-service-deploy   2 2 2            0 6s

Now if we see that we have 2 pods created automatically.
[root@manja17-I13330 kube-testing]# kubectl get pods
NAME                                                 READY STATUS RESTARTS AGE
test-service-deploy-85c8787b6f-6msvv   1/1 Running 0 13s
test-service-deploy-85c8787b6f-nc9kz    1/1 Running 0 13s

Now lets take a POD ,get the IP and see if we can access the application
[root@manja17-I13330 kube-testing]# kubectl describe pod test-service-deploy-85c8787b6f-6msvv | grep IP
IP:             10.47.0.1

[root@manja17-I13330 kube-testing]# curl 10.47.0.1:9876/info
{"host": "10.47.0.1:9876", "version": "0.5", "from": "10.32.0.1"}

If we see that we have accessed the application and we can see that the version is 0.5 in here.
Now if we want to make any modifications ,we can edit the deploy file using,
[root@manja17-I13330 kube-testing]# kubectl edit deploy/test-service-deploy
deployment.extensions "test-service-deploy" edited

I made the replicas from 2 to 3 and saved the changes. I also changed the env value from “0.5”
to “2”. This way iam actually pushing new changes to my container code.
[root@manja17-I13330 kube-testing]# kubectl get deploy
NAME                      DESIRED CURRENT UP-TO-DATE   AVAILABLE AGE
test-service-deploy   2 3          1 2      5m

Now if we see the pods we can see,
[root@manja17-I13330 kube-testing]# kubectl get pods
NAME                                                READY STATUS RESTARTS AGE
test-service-deploy-5fdf56d98c-hvsjn   1/1 Terminating 0 2m
test-service-deploy-5fdf56d98c-sglr8   1/1 Terminating 0 2m
test-service-deploy-98bf447c8-c7ct6    1/1 Running 0 14s
test-service-deploy-98bf447c8-lh7s6    1/1 Running 0 8s


Get the IP address for one of the POD and access the application as,
[root@manja17-I13330 kube-testing]# kubectl describe pod test-service-deploy-98bf447c8-c7ct6 | grep IP
IP:             10.44.0.1

[root@manja17-I13330 kube-testing]# curl http://10.44.0.1:9876/info
{"host": "10.44.0.1:9876", "version": "2", "from": "10.32.0.1"}

We can see that the application is now pointing to the version 2 as we passed in our
environment variables. Beside providing a single point for your whole deployment , it also
provides some more features like
[root@manja17-I13330 kube-testing]# kubectl rollout history deploy/test-service-deploy
deployments "test-service-deploy"
REVISION  CHANGE-CAUSE
1          <none>
2          <none>
3          <none>

We can see the rollout version of the deployment. We can also check the progress of the deployment using
[root@manja17-I13330 kube-testing]# kubectl rollout status deploy/test-service-deploy
deployment "test-service-deploy" successfully rolled out

We can also rollback to the old version using,
[root@manja17-I13330 kube-testing]# kubectl rollout undo deploy/test-service-deploy --to-revision=1
deployment.apps "test-service-deploy"

And once the rollback is done, we can access the application and see the application is pointing
to the old version as below.
[root@manja17-I13330 kube-testing]# curl 10.47.0.3:9876/info
{"host": "10.47.0.3:9876", "version": "0.5", "from": "10.32.0.1"}

More to Come , Happy learning :-)

No comments :

Post a Comment