SpringCloud 微服务迁移到 Kubernetes 容器化完整流程

k8s容器部署流程

具体步骤:

熟悉Spring Cloud微服务项目

微服务架构图

源代码编译构建

拉取仓库代码

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

编译代码

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

这儿构建时间久是因为第一次构建,需要下载maven依赖,之后构建就会很快了。

构建项目镜像并推送到镜像仓库

制作镜像

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

编写gateway服务的DockerFile,制作镜像

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

编写完成的DockerFile放置的文件位置

通过DockerFile构建镜像

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

查看构建好的镜像

docker images

将镜像推送到harbor仓库

之前本地部署的镜像仓库Harbor: http://192.168.0.127:8084/,如果没有可拿docker-hub注册一个账号。

登录仓库

docker login 192.168.0.127:8084

推送镜像到镜像仓库需要满足镜像仓库的镜像名称,因此需要给构建好的镜像打个tag。

给构建的镜像打tag

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

推送镜像仓库

docker push 192.168.0.127:8084/onlee/gateway:v1

其他模块构建和推送参考gateway模块

K8s服务编排

制作gateway的k8s yaml文件(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

其他模块编写k8s yaml文件参考gateway模块

部署基础环境

这一步暂时省略,后续补充…

部署微服务程序

准备namespace

kubectl create namespace wms-dev

部署服务

kubectl apply -f gateway.yaml

其他模块部署服务参考gateway模块

部署微服务前端

编写DockerFile文件

FROM nginx

COPY dist /usr/share/nginx/html/

EXPOSE 80

构建镜像

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

镜像打tag

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

推送到镜像仓库

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

服务编排(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

部署服务

kubectl apply -f web.yaml

微服务对外发布

通过整个微服务架构可知,只有gateway和前端需要暴露服务。

NorePort方式暴露

gateway对外暴露

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

前端对外暴露

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方式暴露

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

gateway对外暴露

---
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

前端对外暴露

---
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

至此,所有微服务已经迁移到Kubernetes容器上了。

把我们上面手动做的这些,通过Jenkins等组件搭建成一个自动化部署的过程,就涉及到DevOps相关的知识了。接下来就会编写这一块的内容。

以上内容还有一些不够完善的地方,后续也会不断完善的。

来源:blog.csdn.net/qq_40722827/article/details/127958192

展开阅读全文

页面更新:2024-03-26

标签:容器   源代码   仓库   中间件   模块   熟悉   流程   完整   代码   文件   基础   项目

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top