上一篇文章我们构建了微服务的 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
$ helm repo add hashicorp https://helm.releases.hashicorp.com
$ helm install consul hashicorp/consul
编写 user deployment
创建 k8s-user-deployment.yaml 文件,代码如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: user-service
labels:
svc: user
app: service
spec:
replicas: 2
selector:
matchLabels:
svc: user
app: service
template:
metadata:
labels:
svc: user
app: service
spec:
containers:
- name: user-service
image: harbor.justin.com/micro-kit/user-service:latest
imagePullPolicy: Always
ports:
- containerPort: 10086
resources:
requests:
memory: 64Mi
cpu: 250m
limits:
memory: 64Mi
cpu: 250m
?
编写 book deployment
创建 k8s-book-deployment.yaml 文件,代码如下:
?
apiVersion: apps/v1
kind: Deployment
metadata:
name: book-service
labels:
svc: book
app: service
spec:
replicas: 2
selector:
matchLabels:
svc: book
app: service
template:
metadata:
labels:
svc: book
app: service
spec:
containers:
- name: book-service
image: harbor.justin.com/micro-kit/book-service:latest
imagePullPolicy: Always
ports:
- containerPort: 10087
resources:
requests:
memory: 64Mi
cpu: 250m
limits:
memory: 64Mi
cpu: 250m
编写 book rpc deployment
创建 k8s-book-rpc-deployment.yaml 文件,代码如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: book-rpc-service
labels:
svc: book-rpc
app: service
spec:
replicas: 2
selector:
matchLabels:
svc: book-rpc
app: service
template:
metadata:
labels:
svc: book-rpc
app: service
spec:
containers:
- name: book-rpc-service
image: harbor.justin.com/micro-kit/book-rpc-service:latest
imagePullPolicy: Always
ports:
- containerPort: 10088
resources:
requests:
memory: 64Mi
cpu: 250m
limits:
memory: 64Mi
cpu: 250m
编写 apigateway service
创建 k8s-apigateway-service.yaml 文件,代码如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: library-apigateway
labels:
svc: library
app: apigateway
spec:
replicas: 2
selector:
matchLabels:
svc: library
app: apigateway
template:
metadata:
labels:
svc: library
app: apigateway
spec:
containers:
- name: library-apigateway
image: harbor.justin.com/micro-kit/apigateway:latest
imagePullPolicy: Always
ports:
- containerPort: 80
resources:
requests:
memory: 64Mi
cpu: 250m
limits:
memory: 64Mi
cpu: 250m
---
apiVersion: v1
kind: Service
metadata:
name: library-apigateway
spec:
ports:
- port: 80
selector:
svc: library
app: apigateway
编写 zipkin service
创建 k8s-zipkin-service.yaml 文件,代码如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: zipkin-server
spec:
replicas: 1
selector:
matchLabels:
app: zipkin-server
template:
metadata:
labels:
app: zipkin-server
spec:
containers:
- name: zipkin-server
image: openzipkin/zipkin
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9411
---
apiVersion: v1
kind: Service
metadata:
name: zipkin-server
spec:
ports:
- port: 9411
selector:
app: zipkin-server
编写 hystrix dashboard service
创建 k8s-hystrixdashboard-service.yaml 文件,代码如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hystrix-dashboard
spec:
replicas: 1
selector:
matchLabels:
app: hystrix-dashboard
template:
metadata:
labels:
app: hystrix-dashboard
spec:
containers:
- name: hystrix-dashboard
image: mlabouardy/hystrix-dashboard
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9002
---
apiVersion: v1
kind: Service
metadata:
name: hystrix-dashboard
spec:
ports:
- port: 9002
selector:
app: hystrix-dashboard
编写 ingress
创建 k8s-ingress.yaml 文件,代码如下:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: apigateway-ingress
spec:
rules:
- host: api.library.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: library-apigateway
port:
number: 80
- host: consul.library.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: consul-ui
port:
number: 80
- host: zipkin.library.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: zipkin-server
port:
number: 9411
- host: hystrix.library.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: hystrix-dashboard
port:
number: 9002
执行命令
进入项目目录下,执行以下命令部署到 K8S:
kubectl apply -f deploy/k8s/k8s-zipkin-service.yaml
kubectl apply -f deploy/k8s/k8s-hystrixdashboard-service.yaml
kubectl apply -f deploy/k8s/k8s-book-rpc-deployment.yaml
kubectl apply -f deploy/k8s/k8s-book-deployment.yaml
kubectl apply -f deploy/k8s/k8s-user-deployment.yaml
kubectl apply -f deploy/k8s/k8s-apigateway-service.yaml
kubectl apply -f deploy/k8s/k8s-ingress.yaml
查看 Pod
所有的 pod 都部署成功且正常运行:
查看 Service
所有的 service 都部署成功且正常运行:
?
查看 Ingress
验证
注册到 consul 成功:
接口调用成功:?
查看 zipkin 调用链路:
?
查看 Hystrix dashboard:
完整代码:
https://github.com/Justin02180218/micro-kit
更多【分布式专辑】【架构实战专辑】系列文章,请关注公众号
|