WHCSRL 技术网

十三,部署微服务到K8S

上一篇文章我们构建了微服务的 Docker 镜像,接下来我们将镜像部署到K8S中。

完整代码:

https://github.com/Justin02180218/micro-kit

部署架构

图片

每个微服务启动2个 pod 注册到 consul 上,网关也启动2个 pod 从 consul 上获取微服务的一个 pod 实例来访问接口。

在 ingress 上配置了4个域名转发:

  • api.library.com: 访问 api 网关

  • consul.library.com: 访问 consul ui 页面

  • zipkin.library.com: 访问  zipkin 页面

  • hystrix.library.com: 访问 hystrix dashboard 页面

编写 K8S apply 文件

创建 deploy/k8s 目录,在此目录下编写 k8s apply 文件,如图:

图片

 

 

镜像仓库

在编写 k8s apply 文件前,先把我们构建好的 Docker 镜像上传到镜像仓库上。镜像仓库采用Harbor,Harbor是由VMware公司开源的企业级的Docker Registry管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能。

图片

安装 consul

  1. $ helm repo add hashicorp https://helm.releases.hashicorp.com
  2. $ helm install consul hashicorp/consul

编写 user deployment

创建 k8s-user-deployment.yaml 文件,代码如下:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: user-service
  5. labels:
  6. svc: user
  7. app: service
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. svc: user
  13. app: service
  14. template:
  15. metadata:
  16. labels:
  17. svc: user
  18. app: service
  19. spec:
  20. containers:
  21. - name: user-service
  22. image: harbor.justin.com/micro-kit/user-service:latest
  23. imagePullPolicy: Always
  24. ports:
  25. - containerPort: 10086
  26. resources:
  27. requests:
  28. memory: 64Mi
  29. cpu: 250m
  30. limits:
  31. memory: 64Mi
  32. cpu: 250m

 

编写 book deployment

创建 k8s-book-deployment.yaml 文件,代码如下:

 

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: book-service
  5. labels:
  6. svc: book
  7. app: service
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. svc: book
  13. app: service
  14. template:
  15. metadata:
  16. labels:
  17. svc: book
  18. app: service
  19. spec:
  20. containers:
  21. - name: book-service
  22. image: harbor.justin.com/micro-kit/book-service:latest
  23. imagePullPolicy: Always
  24. ports:
  25. - containerPort: 10087
  26. resources:
  27. requests:
  28. memory: 64Mi
  29. cpu: 250m
  30. limits:
  31. memory: 64Mi
  32. cpu: 250m

编写 book rpc deployment

创建 k8s-book-rpc-deployment.yaml 文件,代码如下:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: book-rpc-service
  5. labels:
  6. svc: book-rpc
  7. app: service
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. svc: book-rpc
  13. app: service
  14. template:
  15. metadata:
  16. labels:
  17. svc: book-rpc
  18. app: service
  19. spec:
  20. containers:
  21. - name: book-rpc-service
  22. image: harbor.justin.com/micro-kit/book-rpc-service:latest
  23. imagePullPolicy: Always
  24. ports:
  25. - containerPort: 10088
  26. resources:
  27. requests:
  28. memory: 64Mi
  29. cpu: 250m
  30. limits:
  31. memory: 64Mi
  32. cpu: 250m

编写 apigateway service

创建 k8s-apigateway-service.yaml 文件,代码如下:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: library-apigateway
  5. labels:
  6. svc: library
  7. app: apigateway
  8. spec:
  9. replicas: 2
  10. selector:
  11. matchLabels:
  12. svc: library
  13. app: apigateway
  14. template:
  15. metadata:
  16. labels:
  17. svc: library
  18. app: apigateway
  19. spec:
  20. containers:
  21. - name: library-apigateway
  22. image: harbor.justin.com/micro-kit/apigateway:latest
  23. imagePullPolicy: Always
  24. ports:
  25. - containerPort: 80
  26. resources:
  27. requests:
  28. memory: 64Mi
  29. cpu: 250m
  30. limits:
  31. memory: 64Mi
  32. cpu: 250m
  33. ---
  34. apiVersion: v1
  35. kind: Service
  36. metadata:
  37. name: library-apigateway
  38. spec:
  39. ports:
  40. - port: 80
  41. selector:
  42. svc: library
  43. app: apigateway

编写 zipkin service

创建 k8s-zipkin-service.yaml 文件,代码如下:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: zipkin-server
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: zipkin-server
  10. template:
  11. metadata:
  12. labels:
  13. app: zipkin-server
  14. spec:
  15. containers:
  16. - name: zipkin-server
  17. image: openzipkin/zipkin
  18. imagePullPolicy: IfNotPresent
  19. ports:
  20. - containerPort: 9411
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: zipkin-server
  26. spec:
  27. ports:
  28. - port: 9411
  29. selector:
  30. app: zipkin-server

编写 hystrix dashboard service

创建 k8s-hystrixdashboard-service.yaml 文件,代码如下:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: hystrix-dashboard
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: hystrix-dashboard
  10. template:
  11. metadata:
  12. labels:
  13. app: hystrix-dashboard
  14. spec:
  15. containers:
  16. - name: hystrix-dashboard
  17. image: mlabouardy/hystrix-dashboard
  18. imagePullPolicy: IfNotPresent
  19. ports:
  20. - containerPort: 9002
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: hystrix-dashboard
  26. spec:
  27. ports:
  28. - port: 9002
  29. selector:
  30. app: hystrix-dashboard

编写 ingress

创建 k8s-ingress.yaml 文件,代码如下:

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: apigateway-ingress
  5. spec:
  6. rules:
  7. - host: api.library.com
  8. http:
  9. paths:
  10. - path: /
  11. pathType: Prefix
  12. backend:
  13. service:
  14. name: library-apigateway
  15. port:
  16. number: 80
  17. - host: consul.library.com
  18. http:
  19. paths:
  20. - path: /
  21. pathType: Prefix
  22. backend:
  23. service:
  24. name: consul-ui
  25. port:
  26. number: 80
  27. - host: zipkin.library.com
  28. http:
  29. paths:
  30. - path: /
  31. pathType: Prefix
  32. backend:
  33. service:
  34. name: zipkin-server
  35. port:
  36. number: 9411
  37. - host: hystrix.library.com
  38. http:
  39. paths:
  40. - path: /
  41. pathType: Prefix
  42. backend:
  43. service:
  44. name: hystrix-dashboard
  45. port:
  46. number: 9002

执行命令

进入项目目录下,执行以下命令部署到 K8S:

  1. kubectl apply -f deploy/k8s/k8s-zipkin-service.yaml
  2. kubectl apply -f deploy/k8s/k8s-hystrixdashboard-service.yaml
  3. kubectl apply -f deploy/k8s/k8s-book-rpc-deployment.yaml
  4. kubectl apply -f deploy/k8s/k8s-book-deployment.yaml
  5. kubectl apply -f deploy/k8s/k8s-user-deployment.yaml
  6. kubectl apply -f deploy/k8s/k8s-apigateway-service.yaml
  7. kubectl apply -f deploy/k8s/k8s-ingress.yaml

查看 Pod

所有的 pod 都部署成功且正常运行:

图片

查看 Service

所有的 service 都部署成功且正常运行:

图片

 

查看 Ingress

图片

验证

注册到 consul 成功:

图片

接口调用成功: 

图片

查看 zipkin 调用链路:

图片

 

查看 Hystrix dashboard:

图片

完整代码:

https://github.com/Justin02180218/micro-kit


更多【分布式专辑】【架构实战专辑】系列文章,请关注公众号

推荐阅读