8.6 KiB
		
	
	
	
	
	
			
		
		
	
	| title | content_type | weight | 
|---|---|---|
| 公开外部 IP 地址以访问集群中应用程序 | tutorial | 10 | 
此页面显示如何创建公开外部 IP 地址的 Kubernetes 服务对象。
{{% heading "prerequisites" %}}
- 安装 kubectl。
- 使用 Google Kubernetes Engine 或 Amazon Web Services 等云供应商创建 Kubernetes 集群。 本教程创建了一个外部负载均衡器, 需要云供应商。
- 配置 kubectl与 Kubernetes API 服务器通信。有关说明,请参阅云供应商文档。
{{% heading "objectives" %}}
- 运行 Hello World 应用程序的五个实例。
- 创建一个公开外部 IP 地址的 Service 对象。
- 使用 Service 对象访问正在运行的应用程序。
为一个在五个 pod 中运行的应用程序创建服务
- 
在集群中运行 Hello World 应用程序: {{< codenew file="service/load-balancer-example.yaml" >}} kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml前面的命令创建一个 {{< glossary_tooltip text="Deployment" term_id="deployment" >}} 对象和一个关联的 {{< glossary_tooltip term_id="replica-set" text="ReplicaSet" >}} 对象。 ReplicaSet 有五个 {{< glossary_tooltip text="Pods" term_id="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提示: type=LoadBalancer服务由外部云服务提供商提供支持,本例中不包含此部分, 详细信息请参考此页提示:如果外部 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>记下服务公开的外部 IP 地址( LoadBalancer Ingress)。 在本例中,外部 IP 地址是 104.198.205.71。还要注意Port和NodePort的值。 在本例中,Port是 8080,NodePort是 32377。
- 
在前面的输出中,你可以看到服务有几个端点: 10.0.0.6:8080、10.0.1.6:8080、10.0.1.7:8080 和另外两个, 这些都是正在运行 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>是你的服务的外部 IP 地址(LoadBalancer Ingress),<port>是你的服务描述中的port的值。 如果你正在使用 minikube,输入minikube service my-service将在浏览器中自动打开 Hello World 应用程序。成功请求的响应是一条问候消息: Hello Kubernetes!
{{% heading "cleanup" %}}
要删除 Service,请输入以下命令:
kubectl delete services my-service
要删除正在运行 Hello World 应用程序的 Deployment,ReplicaSet 和 Pod,请输入以下命令:
kubectl delete deployment hello-world
{{% heading "whatsnext" %}}
进一步了解将应用程序与服务连接。