在集群中暴露外部 IP 地址以访问应用
本页面介绍如何创建一个 Kubernetes Service 对象来暴露一个外部 IP 地址。
开始之前
- 安装 kubectl。
- 使用 Google Kubernetes Engine 或 Amazon Web Services 等云提供商创建 Kubernetes 集群。本教程会创建一个外部负载均衡器,这需要一个云提供商。
- 配置
kubectl
以便与你的 Kubernetes API 服务器通信。有关说明,请参阅你的云提供商文档。
目标
- 运行五个 Hello World 应用实例。
- 创建一个暴露外部 IP 地址的 Service 对象。
- 使用 Service 对象访问正在运行的应用。
为运行在五个 Pod 中的应用创建 Service
在集群中运行 Hello World 应用
apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: load-balancer-example name: hello-world spec: replicas: 5 selector: matchLabels: app.kubernetes.io/name: load-balancer-example template: metadata: labels: app.kubernetes.io/name: load-balancer-example spec: containers: - image: gcr.io/google-samples/hello-app:2.0 name: hello-world ports: - containerPort: 8080
kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml
上述命令创建了一个 Deployment 和一个关联的 ReplicaSet。该 ReplicaSet 有五个 Pod,每个 Pod 都运行 Hello World 应用。
显示 Deployment 的信息
kubectl get deployments hello-world kubectl describe deployments hello-world
显示你的 ReplicaSet 对象的信息
kubectl get replicasets kubectl describe replicasets
创建暴露此 Deployment 的 Service 对象
kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
显示 Service 的信息
kubectl get services my-service
输出类似于
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-service LoadBalancer 10.3.245.137 104.198.205.71 8080/TCP 54s
说明
如果外部 IP 地址显示为 <pending>,请等待一分钟后再次输入相同的命令。显示有关 Service 的详细信息
kubectl describe services my-service
输出类似于
Name: my-service Namespace: default Labels: app.kubernetes.io/name=load-balancer-example Annotations: <none> Selector: app.kubernetes.io/name=load-balancer-example Type: LoadBalancer IP: 10.3.245.137 LoadBalancer Ingress: 104.198.205.71 Port: <unset> 8080/TCP NodePort: <unset> 32377/TCP Endpoints: 10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more... Session Affinity: None Events: <none>
记下你的 Service 暴露的外部 IP 地址 (
LoadBalancer Ingress
)。在此示例中,外部 IP 地址为 104.198.205.71。同时注意Port
和NodePort
的值。在此示例中,Port
是 8080,NodePort
是 32377。在上述输出中,你可以看到该 Service 有多个端点:10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 另外 2 个。这些是运行 Hello World 应用的 Pod 的内部地址。要验证这些是 Pod 地址,请输入此命令
kubectl get pods --output=wide
输出类似于
NAME ... IP NODE hello-world-2895499144-1jaz9 ... 10.0.1.6 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-2e5uh ... 10.0.1.8 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-9m4h1 ... 10.0.0.6 gke-cluster-1-default-pool-e0b8d269-5v7a hello-world-2895499144-o4z13 ... 10.0.1.7 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-segjf ... 10.0.2.5 gke-cluster-1-default-pool-e0b8d269-cpuc
使用外部 IP 地址 (
LoadBalancer Ingress
) 访问 Hello World 应用curl http://<external-ip>:<port>
其中
<external-ip>
是你的 Service 的外部 IP 地址 (LoadBalancer Ingress
),<port>
是你的 Service 描述中Port
的值。如果你正在使用 minikube,输入minikube service my-service
将自动在浏览器中打开 Hello World 应用。成功请求的响应是 hello 消息
Hello, world! Version: 2.0.0 Hostname: 0bd46b45f32f
清理
要删除 Service,请输入此命令
kubectl delete services my-service
要删除运行 Hello World 应用的 Deployment、ReplicaSet 和 Pod,请输入此命令
kubectl delete deployment hello-world
接下来
了解更多关于使用 Service 连接应用的信息。