• 使用Deployment 对象创建并运行一个微服务
  • 从后端将流量路由到前端
  • 使用Service对象把前端应用连接到后端应用
  • You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube.
  • 本任务使用 外部负载均衡服务, 所以需要对应的可支持此功能的环境。如果你的环境不能支持,你可以使用 NodePort 类型的服务代替。

后端是一个简单的 hello 欢迎微服务应用。这是后端应用的 Deployment 配置文件:

创建后端 Deployment:

  1. kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/hello.yaml

查看后端的 Deployment 信息:

  1. kubectl describe deployment hello

输出类似于:

  1. Name: hello
  2. Namespace: default
  3. CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700
  4. Labels: app=hello
  5. tier=backend
  6. track=stable
  7. Selector: app=hello,tier=backend,track=stable
  8. Replicas: 7 updated | 7 total | 7 available | 0 unavailable
  9. StrategyType: RollingUpdate
  10. MinReadySeconds: 0
  11. RollingUpdateStrategy: 1 max unavailable, 1 max surge
  12. OldReplicaSets: <none>
  13. NewReplicaSet: hello-3621623197 (7/7 replicas created)
  14. Events:
  15. ...

前端连接到后端的关键是 Service。Service 创建一个固定 IP 和 DNS 解析名入口, 使得后端微服务可达。Service 使用 selector 标签来寻找目标 Pod。

首先,浏览 Service 的配置文件:

  1. kind: Service
  2. apiVersion: v1
  3. name: hello
  4. spec:
  5. selector:
  6. app: hello
  7. tier: backend
  8. ports:
  9. - protocol: TCP
  10. port: 80
  11. targetPort: http

配置文件中,你可以看到 Service 将流量路由到包含 app: hello 和 tier: backend 标签的 Pod。

此时,你已经有了一个在运行的后端 Deployment,你也有了一个 Service 用于路由网络流量。

既然你已经有了后端应用,你可以创建一个前端应用连接到后端。前端应用通过 DNS 名连接到后端的工作 Pods。 DNS 名是 “hello”,也就是 Service 配置文件中 name 字段的值。

前端 Deployment 中的 Pods 运行一个 nginx 镜像,这个已经配置好镜像去寻找后端的 hello Service。 只是 nginx 的配置文件:

  1. upstream hello {
  2. server hello;
  3. }
  4.  
  5. server {
  6. listen 80;
  7.  
  8. location / {
  9. proxy_pass http://hello;
  10. }
  11. }

与后端类似,前端用包含一个 Deployment 和一个 Service。Service 的配置文件包含了 type: LoadBalancer, 也就是说,Service 会使用你的云服务商的默认负载均衡设备。

  1. kind: Service
  2. apiVersion: v1
  3. metadata:
  4. name: frontend
  5. spec:
  6. selector:
  7. app: hello
  8. tier: frontend
  9. ports:
  10. - protocol: "TCP"
  11. port: 80
  12. type: LoadBalancer
  13. ---
  14. apiVersion: apps/v1beta1
  15. kind: Deployment
  16. metadata:
  17. name: frontend
  18. spec:
  19. replicas: 1
  20. template:
  21. metadata:
  22. labels:
  23. app: hello
  24. tier: frontend
  25. track: stable
  26. spec:
  27. containers:
  28. - name: nginx
  29. image: "gcr.io/google-samples/hello-frontend:1.0"
  30. lifecycle:
  31. preStop:
  32. exec:
  33. command: ["/usr/sbin/nginx","-s","quit"]

创建前端 Deployment 和 Service:

  1. kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/frontend.yaml

通过输出确认两个资源都已经被创建:

  1. deployment "frontend" created
  2. service "frontend" created

注意:这个 nginx 配置文件是被打包在 里的。 更好的方法是使用 ConfigMap,这样的话你可以更轻易地更改配置。

外部 IP 字段的生成可能需要一些时间。如果是这种情况,外部 IP 会显示为 <pending>。

  1. NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  2. frontend 10.51.252.116 <pending> 80/TCP 10s

使用相同的命令直到它显示外部 IP 地址:

  1. NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  2. frontend 10.51.252.116 XXX.XXX.XXX.XXX 80/TCP 1m

前端和后端已经完成连接了。你可以使用 curl 命令通过你的前端 Service 的外部 IP 访问服务端点。

  1. curl http://<EXTERNAL-IP>

后端生成的消息输出如下:

  1. {"message":"Hello"}

译者:zhangqx2010