问题描述
我有一个简单的应用程序,下面是基本教程中的内容:
https://medium.com/@bhargavshah2011/hello-world-on-kubernetes-cluster-6bec6f4b1bfd
- 我创建一个集群 hello-world2-cluster
-
我使用以下方式“连接”到集群:
gcloud容器集群获取凭据hello-world2-cluster --zone us-central1-c --project strange-vortex-286312
-
我执行“ hellow world”项目的git克隆,这基本上是某种蛇游戏:
$ git clone https://github.com/skynet86/hello-world-k8s.git
$ cd hello-world-k8s /
接下来,我在YAML上运行“创建”:
kubectl create -f hello-world.yaml
按预期方式创建商品和服务:
好!很好!
但是....现在怎么办??
我现在该怎么办?
我想从外界访问此应用程序。我可以在哪里访问应用程序的URL?
如何让我的朋友Bob在廷巴克图(Timbuktu)调用某种URL(无需DNS)来访问我的应用程序?
我知道答案与LoadBalancer节点和Ingress有关,但是似乎没有适当的文档说明如何针对简单的hello world应用程序执行此操作。
解决方法
带有Ingress
官方示例的GKE hello-world :https://cloud.google.com/kubernetes-engine/docs/how-to/load-balance-ingress
Ingress控制器是GKE内置的,您只需要一个指向kind: Ingress
的{{1}}对象即可。解决您的问题:
Service
一旦部署,请查询apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: hello-ingress
annotations:
kubernetes.io/ingress.class: "gce"
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: hello-world
servicePort: 80
,以在kubectl get ingress hello-ingress -oyaml
字段中找到生成的负载均衡器的IP。
如果您不想设置DNS和/或入口,则可以使用节点外部ip 来简单地对已有的设置进行设置。
似乎您已经部署完毕,并且NodePort
服务已映射到端口30081
。
因此,下面是使它与现有功能兼容的剩余步骤:
- 获取您的节点IP:
kubectl get nodes -o wide
- 现在您已拥有节点外部IP,需要允许tcp流量流向该端口
gcloud compute firewall-rules create allow-tcp-30081 --allow tcp:30081
- 就是这样!您只需告诉您朋友Bob,即可通过 node-ip:30081 访问该服务
您可以查看this page来查看其他可能性,具体取决于您的服务类型。
此外,当您使用NodePort
时,端口将映射到群集的所有节点,因此您可以使用任何IP。无需查找部署副本的节点。
如果您不想使用Ingress或其他任何东西。您可以将部署公开为LoadBalancer,然后使用外部IP,您的朋友bob可以访问应用程序。所以要使用
kubectl expose deployment hello-world-deployment --name=hello-world-deployment --type=LoadBalancer --port 80 --target-port 80
这大约需要1分钟才能为该服务分配一个外部IP地址。
获得外部IP使用
kubectl get services
获取服务{@ {1}}的内容,然后打开浏览器并输入External IP address
。
完成!这是最简单,最简单的上手方法!
,您可以使用Ingress
在Internet上公开Kubernetes服务。
GKE允许您使用不同种类的Ingress
(基于nginx
,依此类推),但是默认情况下,它将通过Cloud Load Balancing服务公开您的服务。
如@MaxLobur所示,您可以应用类似于以下内容的配置文件来配置此默认Ingress
:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: hello-world-ingress
spec:
backend:
serviceName: hello-world
servicePort: 80
或者等效地,如果您想为各种后端服务准备Ingress
并根据规则进行配置,则:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: hello-world-ingress
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: hello-world
servicePort: 80
此Ingress
将被分配一个外部IP地址。
您可以使用以下命令找到哪个:
kubectl get ingress hello-world-ingress
此命令将输出类似于以下内容的内容:
NAME HOSTS ADDRESS PORTS AGE
hello-world-ingress * 203.0.113.12 80 2m
但是,如果您将此IP地址提供给Timbuktu中的朋友Bob,他很快就会给您回电,告诉您他无法访问该应用程序...
现在非常重要:GCP分配的此外部IP地址是临时的,暂时可以更改。
如果您需要为应用程序提供确定的固定IP地址,则需要为Ingress
配置静态IP。
如果有必要,这还将允许您为应用程序配置DNS记录和SSL证书。
在GCP中,此过程的第一步是reserving a static IP address。您可以在GCP控制台中进行配置,也可以通过发出以下gcloud
命令:
gcloud compute addresses create hello-world-static-ip --global
此IP应该稍后通过修改其配置文件分配给您的Ingress
:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: hello-world-ingress
annotations:
kubernetes.io/ingress.global-static-ip-name: "hello-world-static-ip"
spec:
backend:
serviceName: hello-world
servicePort: 80
这种静态IP方法只会增加价值,因为只要它与Google Cloud网络资源相关联,您就无需为此付费。
所有此过程都记录在此GCP documentation page中。