使用 Service 把前端连接到后端

    • 使用部署对象(Deployment object)创建并运行一个微服务
    • 从后端将流量路由到前端
    • 使用服务对象把前端应用连接到后端应用

    你必须拥有一个 Kubernetes 的集群,同时你的 Kubernetes 集群必须带有 kubectl 命令行工具。 如果你还没有集群,你可以通过 构建一 个你自己的集群,或者你可以使用下面任意一个 Kubernetes 工具构建:

    要获知版本信息,请输入 .

    本任务使用 外部负载均衡服务, 所以需要对应的可支持此功能的环境。如果你的环境不能支持,你可以使用 类型的服务代替。

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

    service/access/hello.yaml

    创建后端 Deployment:

    1. kubectl apply -f https://k8s.io/examples/service/access/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. Annotations: deployment.kubernetes.io/revision=1
    8. Selector: app=hello,tier=backend,track=stable
    9. Replicas: 7 desired | 7 updated | 7 total | 7 available | 0 unavailable
    10. StrategyType: RollingUpdate
    11. MinReadySeconds: 0
    12. RollingUpdateStrategy: 1 max unavailable, 1 max surge
    13. Pod Template:
    14. Labels: app=hello
    15. tier=backend
    16. track=stable
    17. Containers:
    18. hello:
    19. Image: "gcr.io/google-samples/hello-go-gke:1.0"
    20. Port: 80/TCP
    21. Environment: <none>
    22. Mounts: <none>
    23. Volumes: <none>
    24. Conditions:
    25. ---- ------ ------
    26. Available True MinimumReplicasAvailable
    27. Progressing True NewReplicaSetAvailable
    28. OldReplicaSets: <none>
    29. NewReplicaSet: hello-3621623197 (7/7 replicas created)
    30. Events:
    31. ...

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

    service/access/hello-service.yaml 使用 Service 把前端连接到后端 - 图1

    1. apiVersion: v1
    2. kind: Service
    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: hellotier: backend 标签的 Pod。

    创建 hello Service:

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

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

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

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

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

    service/access/frontend.yaml 使用 Service 把前端连接到后端 - 图2

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

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

    1. deployment.apps/frontend created
    2. service/frontend created

    一旦你创建了 LoadBalancer 类型的 Service,你可以使用这条命令查看外部 IP:

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

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

    当外部 IP 地址被分配可用时,配置会更新,在 EXTERNAL-IP 头部下显示新的 IP:

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

    这一新的 IP 地址就可以用来从集群外与 frontend 服务交互了。

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

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

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

    要删除服务,输入下面的命令:

    1. kubectl delete deployment frontend hello