k8s_basic_deployments

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
k8s_basic_deployments [2020/05/02 17:02] – [Rolling-Updates] andonovjk8s_basic_deployments [2020/05/03 12:16] (current) andonovj
Line 153: Line 153:
  
 What we do then ? What we do then ?
- 
 Well, let's edit our application, let's add "v2" into our awesome app: Well, let's edit our application, let's add "v2" into our awesome app:
  
 +====Edit Software====
 <Code: C#|Edit Source> <Code: C#|Edit Source>
 namespace HttpServerDemo namespace HttpServerDemo
Line 166: Line 166:
     using System.IO;     using System.IO;
     using System.Threading;     using System.Threading;
- 
     class Program     class Program
     {     {
Line 173: Line 172:
             TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);             TcpListener tcpListener = new TcpListener(IPAddress.Any, 1234);
             tcpListener.Start();             tcpListener.Start();
- 
             while (true)             while (true)
             {             {
                 TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync();                 TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync();
                 await ProcessClientAsync(tcpClient);                 await ProcessClientAsync(tcpClient);
- 
             }             }
         }         }
Line 190: Line 187:
                 int bytesRead = await networkStream.ReadAsync(requestBytes, 0, requestBytes.Length);                 int bytesRead = await networkStream.ReadAsync(requestBytes, 0, requestBytes.Length);
                 string request = Encoding.UTF8.GetString(requestBytes, 0, bytesRead);                 string request = Encoding.UTF8.GetString(requestBytes, 0, bytesRead);
-                string responseText = @"<h1>Working... v2</h1>" ++                string responseText = @"<h1>Working... v2</h1>"                     <- Updated this line with "v2" :)
                                           $"<form> <h1> Time is: {System.DateTime.Now} </h1> </form>";                                           $"<form> <h1> Time is: {System.DateTime.Now} </h1> </form>";
                 string response = "HTTP/1.1 200 OK" + NewLine                 string response = "HTTP/1.1 200 OK" + NewLine
Line 211: Line 208:
 The Rebuild part you can see in the Docker section, but I will put the output here as well: The Rebuild part you can see in the Docker section, but I will put the output here as well:
  
-<Code: bash|Update Software, Dockerize, Tag & Push>+====Dockerize, Tag & Push=== 
 +<Code: bash|Dockerize, Tag & Push>
 [root@postgresqlmaster httpserverdemo]# ls -lart [root@postgresqlmaster httpserverdemo]# ls -lart
 total 16 total 16
Line 219: Line 217:
 dr-xr-x---. 7 root root 4096 May  2 12:39 .. dr-xr-x---. 7 root root 4096 May  2 12:39 ..
 drwxr-xr-x. 2 root root   71 May  2 12:45 . drwxr-xr-x. 2 root root   71 May  2 12:45 .
-[root@postgresqlmaster httpserverdemo]# docker build -t httpserverdemo . +root@k8s-master:/home/vagrant/HttpServerDemo# docker build -t httpserverdemo . 
-Sending build context to Docker daemon  5.632kB +Sending build context to Docker daemon  6.144kB 
-Step 1/10 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env+Step 1/11 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
  ---> 4aa6a74611ff  ---> 4aa6a74611ff
-Step 2/10 : WORKDIR /app+Step 2/11 : WORKDIR /app
  ---> Using cache  ---> Using cache
- ---> b7f1f954c7e2 + ---> e6fb8470c359 
-Step 3/10 : COPY *.csproj ./+Step 3/11 : COPY *.csproj ./
  ---> Using cache  ---> Using cache
- ---> eb10b9d797fa + ---> c6e7e3257ccd 
-Step 4/10 : RUN dotnet restore+Step 4/11 : RUN dotnet restore
  ---> Using cache  ---> Using cache
- ---> 7db569180b05 + ---> 073b0e6dcfac 
-Step 5/10 : COPY . ./ +Step 5/11 : COPY . ./ 
- ---> c1d5d3f3ebb4 + ---> dbb416239305 
-Removing intermediate container 56e2e79130b2 +Step 6/11 : RUN dotnet publish -c Release -o out 
-Step 6/10 : RUN dotnet publish -c Release -o out + ---> Running in cdccacf739ec
- ---> Running in 8d0859d775dd+
 Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
 Copyright (C) Microsoft Corporation. All rights reserved. Copyright (C) Microsoft Corporation. All rights reserved.
  
-  Restore completed in 49.81 ms for /app/HttpServerDemo.csproj.+  Restore completed in 38.37 ms for /app/HttpServerDemo.csproj.
   HttpServerDemo -> /app/bin/Release/netcoreapp3.1/HttpServerDemo.dll   HttpServerDemo -> /app/bin/Release/netcoreapp3.1/HttpServerDemo.dll
   HttpServerDemo -> /app/out/   HttpServerDemo -> /app/out/
- ---> b70ac7b2e6d8 +Removing intermediate container cdccacf739ec 
-Removing intermediate container 8d0859d775dd + ---> 29d5a30972d4 
-Step 7/10 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.+Step 7/11 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 
-2.2: Pulling from dotnet/core/aspnet + ---> 4aa6a74611ff 
-804555ee0376: Already exists +Step 8/11 : WORKDIR /app 
-970251047358: Pull complete + ---> Using cache 
-f3d4c41a4fd1: Pull complete + ---> e6fb8470c359 
-bd391c46585f: Pull complete +Step 9/11 : COPY --from=build-env /app/out . 
-Digest: sha256:08277d629af9d5324b63420a650cd96f86e73c4cfdcef6ea3c45912e7578956d + ---> ff034e158a2e 
-Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/aspnet:2.2 +Step 10/11 : RUN find -type d -name bin -prune -exec rm -rf {} \; && find -type d -name obj -prune -exec rm -rf {} \; 
- ---> e7e3b238011c + ---> Running in e217c60056a6 
-Step 8/10 : WORKDIR /app +Removing intermediate container e217c60056a6 
- ---> ceb374fc780a + ---> f3755f21e57e 
-Removing intermediate container d77bfe0f6d8f +Step 11/11 : ENTRYPOINT ["dotnet", "HttpServerDemo.dll"] 
-Step 9/10 : COPY --from=build-env /app/out . + ---> Running in e49283dde769 
- ---> ee079c0d9469 +Removing intermediate container e49283dde769 
-Removing intermediate container 9bb39a98ec96 + ---> 9f2f48860257 
-Step 10/10 : ENTRYPOINT dotnet HttpServerDemo.dll +Successfully built 9f2f48860257
- ---> Running in 99cf13dd6c77 +
- ---> 452b7f6a095c +
-Removing intermediate container 99cf13dd6c77 +
-Successfully built 452b7f6a095c +
-Successfully tagged httpserverdemo:latest +
-[root@postgresqlmaster httpserverdemo]# +
-[root@postgresqlmaster httpserverdemo]# docker image ls +
-REPOSITORY                             TAG                 IMAGE ID            CREATED              SIZE +
-httpserverdemo                         latest              452b7f6a095c        About a minute ago   261MB +
-[root@postgresqlmaster httpserverdemo]#  docker tag 452b7f6a095c andonovj/httpserverdemo:v2 +
-[root@postgresqlmaster httpserverdemo]# docker push andonovj/httpserverdemo:v2 +
-The push refers to a repository [docker.io/andonovj/httpserverdemo] +
-9411dc505491: Pushed +
-e595e200408f: Pushed +
-579a8f1d6a12: Pushed +
-15e45d99c926: Pushed +
-0cf75cb98eb2: Pushed +
-814c70fdae62: Pushed +
-v2: digest: sha256:f83ed7e653ec409ba00e3710391608d124aac397b1abf6dab5c0482447137cdf size: 1579 +
-[root@postgresqlmaster httpserverdemo]#+
 </Code> </Code>
  
-aaaaand....we are done :) We have edited our awesome software and uploaded it to docker. Let's now update the containers in the pods :)+aaaaand....we are done :) We have edited our awesome software and uploaded it to docker hub
  
 +Let's now update the containers in the pods :)
 +
 +====Perform the Update====
 +For that purpose, we have to edit the YAML file with couple more values like:
 +
 +  * Strategy: RollingUpdate(default)
 +  * maxUnavailable: 1 - Never have more than 1 unavailable
 +  * maxSurge: 1 - Never have more than 1 additional. In our case, never have more than 11 pods
 +
 +So, our YAML file should look as follows:
 +
 +<Code: bash| Updated YAML file>
 +apiVersion: apps/v1
 +kind: Deployment
 +metadata:
 +  name: hello-deploy
 +spec:
 +  replicas: 10
 +  minReadySeconds: 10
 +  strategy:
 +    type: RollingUpdate
 +    rollingUpdate:
 +      maxUnavailable: 1
 +      maxSurge: 1
 +  selector:
 +    matchLabels:
 +      app: hello-date
 +  template:
 +    metadata:
 +      labels:
 +        app: hello-date
 +    spec:
 +      containers:
 +      - name: hello-pod
 +        image: andonovj/httpserverdemo:v2
 +        ports:
 +        - containerPort: 1234
 +</Code>
 +
 +Once we have the file, you can start the process as so:
 +
 +<Code: bash|Rolling Update Status>
 +ubuntu@k8s-master:~$ kubectl apply -f deploy.yml
 +Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
 +deployment.apps/hello-deploy configured
 +</Code>
 +
 +
 +You can see the progress of the Rolling Update as follows:
 +<Code: bash|Rolling Update Status>
 +ubuntu@k8s-master:~$ kubectl rollout status deploy hello-deploy
 +Waiting for deployment "hello-deploy" rollout to finish: 2 out of 10 new replicas have been updated...
 +^Cubuntu@k8s-master:~$ kubectl get pods
 +NAME                            READY   STATUS              RESTARTS   AGE
 +hello-deploy-6cd458494-6mvt7    1/1     Running                      80s
 +hello-deploy-6cd458494-9bl7c    1/1     Running                      80s
 +hello-deploy-6cd458494-fvwvz    1/1     Running                      80s
 +hello-deploy-6cd458494-grp2w    1/1     Running                      80s
 +hello-deploy-6cd458494-ldsxq    1/1     Running                      80s
 +hello-deploy-6cd458494-lpgdj    1/1     Running                      80s
 +hello-deploy-6cd458494-mjsmh    1/1     Running                      80s
 +hello-deploy-6cd458494-rd58j    1/1     Running                      80s
 +hello-deploy-6cd458494-rrkhq    1/1     Running                      80s
 +hello-deploy-7f44bd8b96-k92sr   0/    ContainerCreating            23s
 +hello-deploy-7f44bd8b96-lqmdx   0/    ContainerCreating            23s
 +ubuntu@k8s-master:~$ kubectl rollout status deploy hello-deploy
 +Waiting for deployment "hello-deploy" rollout to finish: 4 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 4 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 1 old replicas are pending termination...
 +Waiting for deployment "hello-deploy" rollout to finish: 1 old replicas are pending termination...
 +Waiting for deployment "hello-deploy" rollout to finish: 1 old replicas are pending termination...
 +Waiting for deployment "hello-deploy" rollout to finish: 1 old replicas are pending termination...
 +</Code>
 +
 +We can also check the version which is currently being used:
 +
 +<Code: bash|Rolling Update Status>
 +ubuntu@k8s-master:~$ kubectl get deploy hello-deploy
 +NAME           READY   UP-TO-DATE   AVAILABLE   AGE
 +hello-deploy   10/10   10           10          5m7s
 +ubuntu@k8s-master:~$ kubectl describe deploy hello-deploy
 +Name:                   hello-deploy
 +Namespace:              default
 +CreationTimestamp:      Sun, 03 May 2020 11:28:33 +0000
 +Labels:                 <none>
 +Annotations:            deployment.kubernetes.io/revision: 2
 +Selector:               app=hello-date
 +Replicas:               10 desired | 10 updated | 10 total | 10 available | 0 unavailable
 +StrategyType:           RollingUpdate
 +MinReadySeconds:        10
 +RollingUpdateStrategy:  1 max unavailable, 1 max surge
 +Pod Template:
 +  Labels:  app=hello-date
 +  Containers:
 +   hello-pod:
 +    Image:        andonovj/httpserverdemo:edge                        <- We are using the "edge" version, which we have just compiled.
 +    Port:         1234/TCP
 +    Host Port:    0/TCP
 +    Environment:  <none>
 +    Mounts:       <none>
 +  Volumes:        <none>
 +Conditions:
 +  Type           Status  Reason
 +  ----           ------  ------
 +  Available      True    MinimumReplicasAvailable
 +  Progressing    True    NewReplicaSetAvailable
 +OldReplicaSets:  <none>
 +NewReplicaSet:   hello-deploy-7f44bd8b96 (10/10 replicas created)
 +Events:
 +  Type    Reason             Age                 From                   Message
 +  ----    ------             ----                ----                   -------
 +  Normal  ScalingReplicaSet  5m11s               deployment-controller  Scaled up replica set hello-deploy-6cd458494 to 10
 +  Normal  ScalingReplicaSet  4m14s               deployment-controller  Scaled up replica set hello-deploy-7f44bd8b96 to 1
 +  Normal  ScalingReplicaSet  4m14s               deployment-controller  Scaled down replica set hello-deploy-6cd458494 to 9
 +  Normal  ScalingReplicaSet  4m14s               deployment-controller  Scaled up replica set hello-deploy-7f44bd8b96 to 2
 +  Normal  ScalingReplicaSet  2m18s               deployment-controller  Scaled down replica set hello-deploy-6cd458494 to 7
 +  Normal  ScalingReplicaSet  2m18s               deployment-controller  Scaled up replica set hello-deploy-7f44bd8b96 to 4
 +  Normal  ScalingReplicaSet  2m3s                deployment-controller  Scaled down replica set hello-deploy-6cd458494 to 5
 +  Normal  ScalingReplicaSet  2m3s                deployment-controller  Scaled up replica set hello-deploy-7f44bd8b96 to 6
 +  Normal  ScalingReplicaSet  108s                deployment-controller  Scaled down replica set hello-deploy-6cd458494 to 3
 +  Normal  ScalingReplicaSet  82s (x4 over 108s)  deployment-controller  (combined from similar events): Scaled down replica set hello-deploy-6cd458494 to 0
 +
 +</Code>
 +
 +=====Rolling Back=====
 +Now, before we continue, let's speak a little bit about Change History. We can check the history of all changes as follows:
 +
 +<Code: bash| Check Change History>
 +ubuntu@k8s-master:~$ kubectl rollout history deploy hello-deploy
 +deployment.apps/hello-deploy
 +REVISION  CHANGE-CAUSE
 +1         <none>
 +2         <none>
 +</Code>
 +
 +The reason why you don't see anything in the "change-cause" is because we didn't use "-record" when we applied the change. But let's reverse our changes and apply them again. We can reverse our changes using the following command:
 +
 +<Code: bash| Rollback>
 +ubuntu@k8s-master:~$ kubectl rollout undo deployment hello-deploy --to-revision=1
 +deployment.apps/hello-deploy rolled back
 +ubuntu@k8s-master:~$
 +</Code>
 +
 +Of course we can monitor it with the same command as before:
 +
 +<Code: bash| Rollback>
 +ubuntu@k8s-master:~$ kubectl rollout status deploy hello-deploy
 +Waiting for deployment "hello-deploy" rollout to finish: 4 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 4 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 6 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 8 out of 10 new replicas have been updated...
 +Waiting for deployment "hello-deploy" rollout to finish: 1 old replicas are pending termination...
 +Waiting for deployment "hello-deploy" rollout to finish: 1 old replicas are pending termination...
 +Waiting for deployment "hello-deploy" rollout to finish: 1 old replicas are pending termination...
 +Waiting for deployment "hello-deploy" rollout to finish: 1 old replicas are pending termination...
 +deployment "hello-deploy" successfully rolled out
 +ubuntu@k8s-master:~$ kubectl describe deploy
 +Name:                   hello-deploy
 +Namespace:              default
 +CreationTimestamp:      Sun, 03 May 2020 11:28:33 +0000
 +Labels:                 <none>
 +Annotations:            deployment.kubernetes.io/revision: 3
 +Selector:               app=hello-date
 +Replicas:               10 desired | 10 updated | 10 total | 10 available | 0 unavailable
 +StrategyType:           RollingUpdate
 +MinReadySeconds:        10
 +RollingUpdateStrategy:  1 max unavailable, 1 max surge
 +Pod Template:
 +  Labels:  app=hello-date
 +  Containers:
 +   hello-pod:
 +    Image:        andonovj/httpserverdemo:latest             <- We are back to the old deployment.
 +    Port:         1234/TCP
 +    Host Port:    0/TCP
 +    Environment:  <none>
 +    Mounts:       <none>
 +  Volumes:        <none>
 +Conditions:
 +  Type           Status  Reason
 +  ----           ------  ------
 +  Available      True    MinimumReplicasAvailable
 +  Progressing    True    NewReplicaSetAvailable
 +OldReplicaSets:  <none>
 +NewReplicaSet:   hello-deploy-6cd458494 (10/10 replicas created)
 +Events:
 +  Type    Reason             Age                From                   Message
 +  ----    ------             ----               ----                   -------
 +  Normal  ScalingReplicaSet  45m                deployment-controller  Scaled up replica set hello-deploy-6cd458494 to 10
 +  Normal  ScalingReplicaSet  44m                deployment-controller  Scaled up replica set hello-deploy-7f44bd8b96 to 1
 +  Normal  ScalingReplicaSet  44m                deployment-controller  Scaled down replica set hello-deploy-6cd458494 to 9
 +</Code>
 +
 +====Rollupdate with Record====
 +Let's again apply the RollUpdate WITH the record this time:
 +
 +<Code: bash|Rollupdate with Record>
 +ubuntu@k8s-master:~$ kubectl apply -f deploy.yml --record
 +deployment.apps/hello-deploy configured
 +ubuntu@k8s-master:~$
 +ubuntu@k8s-master:~$ kubectl rollout history deploy hello-deploy
 +deployment.apps/hello-deploy
 +REVISION  CHANGE-CAUSE
 +3         <none>
 +4         kubectl apply --filename=deploy.yml --record=true
 +</Code>
  
 +Now, we can see that, the record is there and we can see the history of changes.
  • k8s_basic_deployments.1588438944.txt.gz
  • Last modified: 2020/05/02 17:02
  • by andonovj