“This is the 14th day of my participation in the First Challenge 2022.

K8s Learn 25-2, Deployment Upgrade application 1

Last time, we shared how to upgrade a new version of POD. I believe that in theory, we all know how to do it. So let’s practice it and see what problems we will encounter and whether it is convenient to operate

Rolling update could have been used

Using rolling update is actually outdated for K8S, but we still want to understand and try rolling Update. Here we will first explain why it was eliminated

Because using Rolling update would actually modify the object we created directly, this would cause the pod and RS tags to be updated directly, which is still not a good idea and is now not supported by the latest K8S

For deleting the old one first and then creating the new one, this method is relatively simple, that is to use the RS expansion and contraction pull implementation, which has been involved in the previous event case of sharing, we can review again

We can use both of these approaches

  • The way RS expands and shrinks
  • deployment

The way RS expands and shrinks

Create the required base environment

Write an application that identifies versions

app.js

const http = require('http'); const os = require('os'); console.log("xmt kubia server starting..." ); var handler = function(request, response){ console.log("received request from " + request.connection.remoteAddress); response.writeHead(200); response.end("you've hit xmt web " + os.hostname() + "version is v1" "\n"); }; var www = http.createServer(handler); www.listen(8080);Copy the code

You can continue to reuse a small application, a simple HTTP request, that accesses port 8080 of the application and gives the client the pod name and version number V1

Make a mirror image

Dockerfile

FROM node:7
ADD app.js /app.js
ENTRYPOINT ["node", "app.js"]
Copy the code

Let’s add the above applet to the Dockerfile and run it directly

docker build -t xiaomotong888/newkubia:v1

docker push xiaomotong888/newkubia:v1
Copy the code

Write yamL, create RS, Service, Pod

mynewkubia.yaml

apiVersion: v1
kind: Service
metadata:
  name: newkubia-service
spec:
  type: NodePort
  selector:
    app: newkubia
  ports:
  - port: 80
    targetPort: 8080
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: newkubia-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: newkubia
  template:
    metadata:
      labels:
        app: newkubia
    spec:
      containers:
      - name: newkubia
        image: xiaomotong888/newkubia:v1
        ports:
        - containerPort: 8080
Copy the code

Create an SVC and RS that can be deployed together in the same YAML file, separated by —

Use kubectl create -f mynewkubia.yaml to create RS, SVC, and POD

The SVC here is supposed to emulate LoadBalancer, but I’m using minikube and there’s no way to use LoadBalancer, but I can use NodePort

Curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl

You can see that the message printed by the service at this time is v1 version, no problem

Start creating a new RS2 to switch traffic to the new Pod

At this point, we create another RS2 and switch the Service traffic to the new POD by modifying the label

The specific YAML content is the same as the above RS YAML content, we just need to change the corresponding place to newKubiA-RS-2, do not conflict with the RS created previously, the label should also be modified

After kubectl Create corresponds to the YAML file, we enter the corresponding SVC modification tag

At this point, let’s see if the traffic really switches to pod V2

Curl curl curl curl curl curl curl curl curl curl curl curl curl

By viewing the effect of the response to the v2 version, we can see that the Service traffic to the new POD, corresponding to the request path looks like this:

This diagram is also shown when we share services.

Today is here, learning, if there is a deviation, please correct

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~