Complete process of migrating SpringCloud microservices to Kubernetes containerization

  • Familiar with Spring Cloud microservice projects

  • Source code compilation and build

  • Build the project image and push it to the image warehouse

    • Make a mirror

    • Push the image to the harbor warehouse

  • K8s service orchestration

  • Deploy basic environment

    • Deploy Nacos cluster in K8s (registration and configuration center)

    • Deploy Seata distributed transactions in k8s

    • Deploy mysql, redis, rabbitmq, minio, xxl-job on linux

  • Deploy microservice program

  • Deploy microservice front-end

  • Microservices are released to the outside world

    • NorePort mode exposed

    • Ingress mode exposed

k8s container deployment process

cced18d10f8851155ed28db5ca7435b2.png

Specific steps:

  • Step 1: Familiarize yourself with Spring Cloud microservice projects

  • Step 2: Source code compilation and construction

  • Step 3: Build the project image and push it to the image warehouse

  • Step 4: K8s service orchestration

  • Step 5: Deploy the basic environment required for the service

  • Step 6: Deploy microservice program

  • Step 7: Deploy microservice front-end

  • Step 8: Release the microservice to the outside world

Familiar with Spring Cloud microservice projects

Microservice architecture diagram

47a7a65b2212972bf081f34e9101f834.png

Source code compilation and construction

Pull warehouse code

git clone http://192.168.0.126/saas-wms/linkinsense-wms-public.git

f03b521261521a4a2d6d7a752360a4cc.png

Compile code

mvn clean package -Dmaven.test.skip=true -Pdev

619acdb8f9d242cbfdbc71f73e7b3806.png

The long build time here is because the first build requires downloading maven dependencies, and subsequent builds will be very fast.

Build the project image and push it to the image warehouse

  • Basic image: centos, ubuntu

  • Middleware image: jdk, nginx

  • Project image: base image + middleware image + project code

Make a mirror

https://blog.csdn.net/qq_40722827/article/details/126337904

Write the DockerFile of the gateway service and create an image

vi Dockerfile
FROM openjdk:8-jre
  
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone
  
WORKDIR/wms-center/wms-gateway
  
ADD ./target/wms-gateway-1.0.0.jar ./
  
EXPOSE 8901
  
CMD java -jar wms-gateway-1.0.0.jar

The file location where the completed DockerFile is placed

a32c0b0db884b020794f2b25b33f53fb.png

Build image through DockerFile

docker build -t wms-gateway:v1 -f wms-gateway/Dockerfile ./wms-gateway/

bb6ee0f17a804f63e222dba6965027c6.png

View the built image

docker images

14d79bf6e37b88a0dec45ef20eac017a.png

Push the image to harbor warehouse

The previously deployed mirror warehouse Harbor: http://192.168.0.127:8084/, if not, you can register an account with docker-hub.

Log in to the warehouse

docker login 192.168.0.127:8084

1aae0e86a3abe48d6a254246b7d385eb.png

Pushing the image to the image warehouse needs to meet the image name of the image warehouse, so the built image needs to be tagged.

Tag the built image

docker tag wms-gateway:v1 192.168.0.127:8084/onlee/gateway:v1

6bed9264de444ab924d3b78eff296cc5.png

Push mirror warehouse

docker push 192.168.0.127:8084/onlee/gateway:v1

dc3029117df9d0e3b3cd57e7bf3611d3.png
f7f31cd0230d69e9fccfc55de0a3f050.png

Other modules build and push reference gateway module

K8s service orchestration

Make the k8s yaml file of gateway (gateway.yaml)

---
apiVersion: apps/v1
Kind: Deployment
metadata:
  name: gateway
  namespace: wms-dev
spec:
  replicas: 1
  selector:
    matchLabels:
      project: wms-dev
      app: gateway
  template:
    metadata:
      labels:
        project: wms-dev
        app: gateway
    spec:
      imagePullSecrets:
      - name: registry-harbor
      containers:
      - name: gateway
        image: 192.168.0.127:8084/onlee/gateway:v1
        imagePullPolicy: Always
        ports:
          - protocol: TCP
            containerPort: 8901
        env:
          - name: JAVA_OPTS
            value: "-Xmx1g"
        resources:
          requests:
            CPU: 0.5
            memory: 256Mi
          limits:
            cpu: 1
            memory: 1Gi
        readinessProbe:
          tcpSocket:
            port: 8901
          initialDelaySeconds: 60
          periodSeconds: 10
        livenessProbe:
          tcpSocket:
            port: 8901
          initialDelaySeconds: 60
          periodSeconds: 10

For other modules to write k8s yaml files, please refer to the gateway module.

Deploy basic environment

This step is omitted for now and will be added later…

  • Deploy Nacos cluster in K8s (registration and configuration center)

  • Deploy Seata distributed transactions in k8s

  • Deploy mysql, redis, rabbitmq, minio, xxl-job on linux

Deploy microservice program

Prepare namespace

kubectl create namespace wms-dev

21a656d96631c3c8a6075cd29b40fc3b.png

Deployment service

kubectl apply -f gateway.yaml

28029caeaa34361034a93f178f94494b.png

For other module deployment services, please refer to the gateway module.

Deploy microservice front-end

Write DockerFile file

FROM nginx
  
COPY dist /usr/share/nginx/html/
  
EXPOSE 80

Build image

docker build -t wms-web:v1 -f Dockerfile .

Mirror tag

docker tag wms-web:v1 192.168.0.127:8084/onlee/wms-web:v1

Push to mirror warehouse

docker push 192.168.0.127:8084/onlee/wms-web:v1

Service orchestration (web.yaml)

apiVersion: apps/v1
Kind: Deployment
metadata:
  labels:
    app: wms-web
  name: wms-web
  namespace: wms-dev
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: wms-web
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: wms-web
    spec:
      imagePullSecrets:
        - name: registry-harbor
      containers:
        - image: 192.168.0.127:8084/onlee/wms-web:v1
          imagePullPolicy: Always
          name: app
          ports:
            - containerPort: 80
              protocol:TCP
          resources:
            limits:
              cpu: 300m
              memory: 600Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy:ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 30

Deployment service

kubectl apply -f web.yaml

Microservices are released to the outside world

Through the entire microservice architecture, we can see that only the gateway and front-end need to expose services.

abd7c58f58a56d45911b15f3b74d7a07.png

NorePort mode exposed

gateway exposed to the outside world

gateway-nortport.yaml

---
apiVersion: v1
Kind: Service
metadata:
  name: gateway
  namespace: wms-dev
spec:
  ports:
  - port: 8901
    name: gateway
    protocol: TCP
    targetPort: 8901
    nodePort: 32074
  selector:
    project: wms
    app: gateway
  type: NodePort

Front end exposed to the outside world

web-noreport.yaml

---
apiVersion: v1
Kind: Service
metadata:
  labels:
    app: wms-web
  name: wms-web
  namespace: wms-dev
spec:
  ports:
    - name: http
      port: 80
      protocol:TCP
      targetPort: 80
      nodePort: 32248
  selector:
    app: wms-web
  sessionAffinity: None
  type: NodePort
Ingress mode exposed

https://blog.csdn.net/qq_40722827/article/details/127929141

0651d0d24634fca98cde460878a24484.png

gateway exposed to the outside world

---
apiVersion: networking.k8s.io/v1
Kind: Ingress
metadata:
  name: gateway
  namespace: wms-dev
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/backend-protocol: "HTTP"
spec:
  rules:
    - host: gateway.wms.com
      http:
        paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: gateway
              port:
                number: 8901
---
apiVersion: v1
Kind: Service
metadata:
  name: gateway
  namespace: wms-dev
spec:
  ports:
  - port: 8901
    name: gateway
  selector:
    project: wms-dev
    app: gateway

Front end exposed to the outside world

---
apiVersion: networking.k8s.io/v1
Kind: Ingress
metadata:
  name: wms-web
  namespace: wms-dev
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/backend-protocol: "HTTP"
spec:
  rules:
    -host: dev.wms.com
      http:
        paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: wms-web
              port:
                number: 80
---
apiVersion: v1
Kind: Service
metadata:
  labels:
    app: wms-web
  name: wms-web
  namespace: wms-dev
spec:
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  type:ClusterIP
  selector:
    app: wms-web
  sessionAffinity: None

At this point, all microservices have been migrated to Kubernetes containers.

22b708d7950390c4bbbb4403ad3bf062.png

Putting what we did manually above into an automated deployment process through components such as Jenkins involves DevOps-related knowledge. This section will be written next.

There are still some imperfections in the above content, which will be continuously improved in the future.

Source: blog.csdn.net/qq_40722827/

article/details/127958192

Back-end exclusive technology group

To build a high-quality technical exchange community, HR personnel engaged in programming development and technical recruitment are welcome to join the group. Everyone is also welcome to share their own company’s internal information, help each other, and make progress together!

Speak in a civilized manner, focusing on communication technology, recommendation for positions, and industry discussion

Advertisers are not allowed to enter, and do not trust private messages to prevent being deceived.

fe2105f1cbdb354653c176d024a9ba2d.png

Add me as a friend and bring you into the group
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138,158 people are learning the system