购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

6.2 部署服务

本节主要介绍服务部署时使用的yaml文件,以及如何在Kubernetes集群中部署查看服务。

1.服务部署yaml文件说明

每个服务部署yaml文件的大同小异,这里仅以service-go示例说明:


1 kind: Service
 2 apiVersion: v1
 3 metadata:
 4   name: service-go
 5   labels:
 6     app: service-go
 7 spec:
 8   selector:
 9     app: service-go
10   ports:
11     - name: http
12       port: 80
13 ---
14 apiVersion: apps/v1
15 kind: Deployment
16 metadata:
17   name: service-go-v1
18 spec:
19   selector:
20     matchLabels:
21       app: service-go
22       version: v1
23   replicas: 1
24   template:
25     metadata:
26       labels:
27         app: service-go
28         version: v1
29     spec:
30       restartPolicy: Always
31       containers:
32       - name: service-go
33         image: registry.cn-shanghai.aliyuncs.com/istio-lab/service-go:v1
34         ports:
35         - containerPort: 80
36         resources:
37           requests:
38             cpu: 50m
39             memory: 50Mi
40           limits:
41             cpu: 100m
42             memory: 100Mi
43 ---
44 apiVersion: apps/v1
45 kind: Deployment
46 metadata:
47   name: service-go-v2
48 spec:
49   selector:
50     matchLabels:
51       app: service-go
52       version: v2
53   replicas: 1
54   template:
55     metadata:
56       labels:
57         app: service-go
58         version: v2
59     spec:
60       restartPolicy: Always
61      containers:
62       - name: service-go
63         image: registry.cn-shanghai.aliyuncs.com/istio-lab/service-go:v2
64         ports:
65         - containerPort: 80
66         resources:
67           requests:
68             cpu: 50m
69             memory: 50Mi
70           limits:
71             cpu: 100m
72             memory: 100Mi

第1~12行是Kubernetes中的Service定义,第4行指定了服务名为service-go,第8、9行指定选择包含标签app=service-go的Pod,第11、12行指定服务开放在80端口,由于没有指定targetPort,则targetPort默认与port参数一致,即后端Pod开放的端口也应该是80端口。

第14~42行是Kubernetes的Deployment定义,第17行定义了Deployment的名称为service-go-v1。第19到22行定义了Deployment管理的是包含app=service-go,且version=v1标签的Pod。第26到28行的定义表示为启动的Pod添加app=service-go和version=v1的标签。第30行定义了重启策略为Always,此策略表示不论何种情况下,只要Pod状态不正常就持续重启。第32~42行定义了Pod启动一个名称为service-go的容器。第33行定义了service-go容器使用的Docker镜像及版本,第35行定义了容器开放了80端口。第36~42行定义了容器运行时需要的资源情况,requests定义了容器启动时需要的CPU和内存资源,limits定义了容器最多能使用的CPU和内存资源。

第44~72行定义了service-go-v2的Deployment配置,与service-go-v1类似,在此不再赘述。

从上面的Service定义可以知道,Service选择了所有包含了标签名为app、值为service-go的Pod,而这包含service-go-v1和service-go-v2管理的Pod,因此请求流量会轮询地发送给服务的v1版本和v2版本。

如果你是一个Kubernetes的用户,你可能会发现,此次实验编写的Deployment中并没有使用Kubernetes的健康查检机制liveness和探测机制readiness。这是由于Envoy代理会对服务实例进行健康查检,而且如果你使用了Istio的自动TLS来加密服务间的通信时,由于Kubernetes进行健康检查时没用TLS,这可能会导致Kubernetes健康检查失败,进而导致Pod被反复重启,影响服务的稳定性。当然也可以通过其他方式来实现,绕过Istio的TLS来完成Kubernetes的健康检查,在后面的章节中,也会介绍此类方法。

其他服务的yaml部署文件类似,只是部署名称和app标签值有改动,如service-node、service-lua等值会改动。

2.在Kubernetes集群中部署服务

1)部署服务

使用上节介绍的服务部署yaml文件进行部署:


$ kubectl apply -f service/go/service-go.yaml
service/service-go created
deployment.apps/service-go-v1 created
deployment.apps/service-go-v2 created
$ kubectl apply -f service/node/service-node.yaml
service/service-node created
deployment.apps/service-node-v1 created
deployment.apps/service-node-v2 created
$ kubectl apply -f service/lua/service-lua.yaml
service/service-lua created
deployment.apps/service-lua-v1 created
deployment.apps/service-lua-v2 created
$ kubectl apply -f service/python/service-python.yaml
service/service-python created
deployment.apps/service-python-v1 created
deployment.apps/service-python-v2 created
$ kubectl apply -f service/js/service-js.yaml
service/service-js created
deployment.apps/service-js-v1 created
deployment.apps/service-js-v2 created

2)查看服务部署状态: tJl84Sp9C/3eih4lzFCW/jhA/Dmpvj5NoB+wQEoVWtjV3ccA03he+qE8nBV/fPVf


$ kubectl get deploy
NAME                 DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
service-go-v1        1         1         1            1           3h28m
service-go-v2        1         1         1            1           3h28m
service-js-v1        1         1         1            1           3h28m
service-js-v2        1         1         1            1           3h28m
service-lua-v1       1         1         1            1           3h28m
service-lua-v2       1         1         1            1           3h28m
service-node-v1      1         1         1            1           3h28m
service-node-v2      1         1         1            1           3h28m
service-python-v1    1         1         1            1           3h28m
service-python-v2    1         1         1            1           3h28m
$ kubectl get pod
NAME                                 READY   STATUS    RESTARTS   AGE
service-go-v1-7cc5c6f574-2dn6n       1/1     Running   0          3h29m
service-go-v2-7656dcc478-2dgc6       1/1     Running   0          3h29m
service-js-v1-55756d577-49cl5        1/1     Running   0          3h29m
service-js-v2-86bdfc86d9-pbq2d       1/1     Running   0          3h29m
service-lua-v1-5c9bcb7778-hsbr6      1/1     Running   0          3h29m
service-lua-v2-75cb5cdf8-75tm4       1/1     Running   0          3h29m
service-node-v1-d44b9bf7b-6hrzd      1/1     Running   0          3h29m
service-node-v2-86545d9796-4ttsf     1/1     Running   0          3h29m
service-python-v1-79fc5849fd-vtzsf   1/1     Running   0          3h29m
service-python-v2-7b6864b96b-9qk8c   1/1     Running   0          3h29m
$ kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP   4h47m
service-go       ClusterIP   10.108.207.28    <none>        80/TCP    3h29m
service-js       ClusterIP   10.97.251.7      <none>        80/TCP    3h29m
service-lua      ClusterIP   10.103.165.139   <none>        80/TCP    3h29m
service-node     ClusterIP   10.102.21.34     <none>        80/TCP    3h29m
service-python   ClusterIP   10.103.119.8     <none>        80/TCP    3h29m

点击中间区域
呼出菜单
上一章
目录
下一章
×