1.将Petclinic重构为微服务架构

在这里插入图片描述
项目源码:https://github.com/chengyinwu-hash/spring-petclinic-msa
微服务架构需要引入网关或者反向代理,实现反向路由和转发
Web App实现API聚合+单页SPA展示

2.本地K8s部署架构

K8S内部服务暴露8080端口,后端服务采用嵌入式数据库,每个模块均为Service与Pod构成
在这里插入图片描述

3.准备资源配置清单

资源配置清单地址:https://github.com/chengyinwu-hash/spring-petclinic-msa/tree/master/k8s/local

3.1 customer服务
[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat customer-dp.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: customers
spec:
  selector:
    matchLabels:
      app: customers
  replicas: 1
  template:
    metadata:
      labels:
        app: customers
    spec:
      containers:
        - name: customers
          image: harbor.od.com/bobo/spring-petclinic-customers-service:1.0.0.RELEASE
          resources:
            requests:
              memory: "128Mi"
            limits:
              memory: "512Mi"
          env:
            - name: JAVA_OPTS
              value: "-XX:MaxRAMPercentage=80.0"
            - name: SERVER_PORT
              value: "8080"

[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat customer-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: customers
  labels:
    svc: customers
spec:
  selector:
    app: customers
  ports:
    - name: http
      port: 8080
      targetPort: 8080
  type: ClusterIP

3.2 visits服务
[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat visits-dp.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: visits
spec:
  selector:
    matchLabels:
      app: visits
  replicas: 1
  template:
    metadata:
      labels:
        app: visits
    spec:
      containers:
        - name: visits
          image: harbor.od.com/bobo/spring-petclinic-visits-service:v1.0.0
          readinessProbe:
            httpGet:
              path: /actuator/health
              port: 8080
          env:
            - name: SERVER_PORT
              value: "8080"

[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat visits-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: visits
  labels:
    svc: visits
spec:
  selector:
    app: visits
  ports:
    - name: http
      port: 8080
      targetPort: 8080
  type: ClusterIP
3.3 vets服务
[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat vets-dp.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: vets
spec:
  selector:
    matchLabels:
      app: vets
  replicas: 1
  template:
    metadata:
      labels:
        app: vets
    spec:
      containers:
        - name: vets
          image: harbor.od.com/bobo/spring-petclinic-vets-service:v1.0.0
          env:
            - name: SERVER_PORT
              value: "8080"

[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat vets-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: vets
  labels:
    svc: vets
spec:
  selector:
    app: vets
  ports:
    - name: http
      port: 8080
      targetPort: 8080
  type: ClusterIP

3.4 web-app应用
[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat web-app-dp.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 1
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: harbor.od.com/bobo/spring-petclinic-web-app:v1.0.0
          env:
            - name: SERVER_PORT
              value: "8080"
            - name: VISITS_SERVICE_ENDPOINT
              value: http://visits:8080
            - name: CUSTOMERS_SERVICE_ENDPOINT
              value: http://customers:8080

[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat web-app-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    svc: web
spec:
  selector:
    app: web
  ports:
    - name: http
      port: 8080
      targetPort: 8080
  type: ClusterIP

3.5 gateway应用
[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat gateway-dp.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway
spec:
  selector:
    matchLabels:
      app: gateway
  replicas: 1
  template:
    metadata:
      labels:
        app: gateway
    spec:
      containers:
        - name: gateway-service
          image: harbor.od.com/bobo/spring-petclinic-cloud-gateway:1.0.0.RELEASE
          env:
            - name: SERVER_PORT
              value: "8080"
            - name: WEB_APP_ENDPOINT
              value: http://web:8080
            - name: VETS_SERVICE_ENDPOINT
              value: http://vets:8080
            - name: VISITS_SERVICE_ENDPOINT
              value: http://visits:8080
            - name: CUSTOMERS_SERVICE_ENDPOINT
              value: http://customers:8080

[root@k8s7-200.host.com /data/k8s-yaml/k8s-msa-in-action/ch06]# cat gateway-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: gateway
  labels:
    svc: gateway
spec:
  selector:
    app: gateway
  ports:
    - name: http
      port: 8080
      targetPort: 8080
      nodePort: 3665
  type: NodePort

4.应用资源配置清单

在这里插入图片描述
在这里插入图片描述

5. 浏览器校验

在这里插入图片描述
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐