使用服务将前端连接到后端

此任务展示了如何创建前端后端微服务。后端微服务是一个简单的问候程序。前端使用 nginx 和 Kubernetes 服务 对象来公开后端。

目标

  • 使用 部署 对象创建并运行一个示例 hello 后端微服务。
  • 使用服务对象将流量发送到后端微服务的多个副本。
  • 创建并运行一个 nginx 前端微服务,也使用部署对象。
  • 配置前端微服务以将流量发送到后端微服务。
  • 使用 type=LoadBalancer 的服务对象将前端微服务公开到集群外部。

开始之前

您需要有一个 Kubernetes 集群,并且 kubectl 命令行工具必须配置为与您的集群通信。建议在至少有两个节点的集群上运行本教程,这些节点不充当控制平面主机。如果您还没有集群,可以使用 minikube 创建一个,或者可以使用以下 Kubernetes 游乐场之一

要检查版本,请输入 kubectl version

此任务使用 带有外部负载均衡器的服务,这需要支持的环境。如果您的环境不支持此功能,则可以使用类型为 NodePort 的服务。

使用部署创建后端

后端是一个简单的问候程序微服务。以下是后端部署的配置文件

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
spec:
  selector:
    matchLabels:
      app: hello
      tier: backend
      track: stable
  replicas: 3
  template:
    metadata:
      labels:
        app: hello
        tier: backend
        track: stable
    spec:
      containers:
        - name: hello
          image: "gcr.io/google-samples/hello-go-gke:1.0"
          ports:
            - name: http
              containerPort: 80
...

创建后端部署

kubectl apply -f https://k8s.io/examples/service/access/backend-deployment.yaml

查看有关后端部署的信息

kubectl describe deployment backend

输出类似于以下内容

Name:                           backend
Namespace:                      default
CreationTimestamp:              Mon, 24 Oct 2016 14:21:02 -0700
Labels:                         app=hello
                                tier=backend
                                track=stable
Annotations:                    deployment.kubernetes.io/revision=1
Selector:                       app=hello,tier=backend,track=stable
Replicas:                       3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:                   RollingUpdate
MinReadySeconds:                0
RollingUpdateStrategy:          1 max unavailable, 1 max surge
Pod Template:
  Labels:       app=hello
                tier=backend
                track=stable
  Containers:
   hello:
    Image:              "gcr.io/google-samples/hello-go-gke:1.0"
    Port:               80/TCP
    Environment:        <none>
    Mounts:             <none>
  Volumes:              <none>
Conditions:
  Type          Status  Reason
  ----          ------  ------
  Available     True    MinimumReplicasAvailable
  Progressing   True    NewReplicaSetAvailable
OldReplicaSets:                 <none>
NewReplicaSet:                  hello-3621623197 (3/3 replicas created)
Events:
...

创建 hello 服务对象

从前端向后端发送请求的关键是后端服务。服务创建持久 IP 地址和 DNS 名称条目,以便始终可以访问后端微服务。服务使用 选择器 查找它将流量路由到的 Pod。

首先,探索服务配置文件

---
apiVersion: v1
kind: Service
metadata:
  name: hello
spec:
  selector:
    app: hello
    tier: backend
  ports:
  - protocol: TCP
    port: 80
    targetPort: http
...

在配置文件中,您可以看到名为 hello 的服务将流量路由到具有标签 app: hellotier: backend 的 Pod。

创建后端服务

kubectl apply -f https://k8s.io/examples/service/access/backend-service.yaml

此时,您有一个 backend 部署运行着三个 hello 应用程序的副本,并且您有一个服务可以将流量路由到它们。但是,此服务在集群外部既不可用也不可解析。

创建前端

现在您已经运行了后端,您可以创建一个在集群外部可访问的前端,并通过将请求代理到后端来连接到后端。

前端通过使用分配给后端服务的 DNS 名称向后端工作程序 Pod 发送请求。DNS 名称是 hello,它是 examples/service/access/backend-service.yaml 配置文件中 name 字段的值。

前端部署中的 Pod 运行一个 nginx 镜像,该镜像配置为将请求代理到 hello 后端服务。以下是 nginx 配置文件

# The identifier Backend is internal to nginx, and used to name this specific upstream
upstream Backend {
    # hello is the internal DNS name used by the backend Service inside Kubernetes
    server hello;
}

server { listen 80;

location / {
    # The following statement will proxy traffic to the upstream named Backend
    proxy_pass http://Backend;
}

}

与后端类似,前端也有部署和服务。需要注意的是,后端和前端服务之间的一个重要区别是,前端服务的配置具有 type: LoadBalancer,这意味着服务使用云提供商提供的负载均衡器,并且可以在集群外部访问。

---
apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  selector:
    app: hello
    tier: frontend
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 80
  type: LoadBalancer
...
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  selector:
    matchLabels:
      app: hello
      tier: frontend
      track: stable
  replicas: 1
  template:
    metadata:
      labels:
        app: hello
        tier: frontend
        track: stable
    spec:
      containers:
        - name: nginx
          image: "gcr.io/google-samples/hello-frontend:1.0"
          lifecycle:
            preStop:
              exec:
                command: ["/usr/sbin/nginx","-s","quit"]
...

创建前端部署和服务

kubectl apply -f https://k8s.io/examples/service/access/frontend-deployment.yaml
kubectl apply -f https://k8s.io/examples/service/access/frontend-service.yaml

输出验证了两个资源都已创建

deployment.apps/frontend created
service/frontend created

与前端服务交互

创建类型为 LoadBalancer 的服务后,可以使用此命令查找外部 IP

kubectl get service frontend --watch

这将显示 frontend 服务的配置,并监视更改。最初,外部 IP 列为 <pending>

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

但是,一旦分配了外部 IP,配置就会更新以在 EXTERNAL-IP 标题下包含新的 IP

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

现在可以使用该 IP 从集群外部与 frontend 服务交互。

通过前端发送流量

现在前端和后端已连接。您可以使用前端服务外部 IP 上的 curl 命令来访问端点。

curl http://${EXTERNAL_IP} # replace this with the EXTERNAL-IP you saw earlier

输出显示了后端生成的的消息

{"message":"Hello"}

清理

要删除服务,请输入以下命令

kubectl delete services frontend backend

要删除运行后端和前端应用程序的部署、副本集和 Pod,请输入以下命令

kubectl delete deployment frontend backend

下一步

上次修改时间:2023 年 8 月 24 日下午 6:38 PST:使用 code_sample shortcode 代替 code shortcode (e8b136c3b3)