使用 NGINX Ingress Controller 在 Minikube 上设置 Ingress
Ingress 是一个 API 对象,它定义了允许外部访问集群中服务的规则。 Ingress 控制器实现 Ingress 中设置的规则。
此页面展示了如何设置一个简单的 Ingress,该 Ingress 根据 HTTP URI 将请求路由到 Service 'web' 或 'web2'。
准备工作
本教程假设你正在使用 minikube 运行本地 Kubernetes 集群。 请访问安装工具,了解如何安装 minikube。
注意
本教程使用需要 AMD64 架构的容器。如果你在不同 CPU 架构的计算机上使用 minikube,可以尝试使用支持模拟 AMD64 的驱动程序。例如,Docker Desktop 驱动程序可以做到这一点。你需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具已配置为与你的集群通信。 建议在至少有两个不充当控制平面主机的节点上运行本教程。 如果你还没有集群,可以使用 minikube 创建一个,或者你可以使用以下 Kubernetes 演练场之一。
你的 Kubernetes 服务器版本必须是 1.19 或更高。要检查版本,请输入 kubectl version。
创建 minikube 集群
如果你尚未在本地设置集群,请运行 minikube start 来创建集群。
启用 Ingress 控制器
要启用 NGINX Ingress 控制器,请运行以下命令
minikube addons enable ingress验证 NGINX Ingress 控制器是否正在运行
kubectl get pods -n ingress-nginx注意
你可能需要等待一分钟才能看到这些 Pod 正常运行。输出类似于:
NAME READY STATUS RESTARTS AGE ingress-nginx-admission-create-g9g49 0/1 Completed 0 11m ingress-nginx-admission-patch-rqp78 0/1 Completed 1 11m ingress-nginx-controller-59b45fb494-26npt 1/1 Running 0 11m
部署一个 Hello, World 应用
使用以下命令创建一个 Deployment
kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0输出应该是
deployment.apps/web created验证 Deployment 处于 Ready 状态
kubectl get deployment web输出应该类似于
NAME READY UP-TO-DATE AVAILABLE AGE web 1/1 1 1 53s暴露 Deployment
kubectl expose deployment web --type=NodePort --port=8080输出应该是
service/web exposed验证 Service 已创建并可在节点端口上使用
kubectl get service web输出类似于:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE web NodePort 10.104.133.249 <none> 8080:31637/TCP 12m使用
minikube service命令通过 NodePort 访问 Service。请按照你平台的说明操作。minikube service web --url输出类似于:
http://172.17.0.15:31637调用上一步输出中获得的 URL
curl http://172.17.0.15:31637# The command must be run in a separate terminal. minikube service web --url输出类似于:
http://127.0.0.1:62445 ! Because you are using a Docker driver on darwin, the terminal needs to be open to run it.从另一个终端,调用上一步输出中获得的 URL
curl http://127.0.0.1:62445
输出类似于:Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564现在你可以通过 Minikube IP 地址和 NodePort 访问示例应用程序。下一步将让你使用 Ingress 资源访问应用程序。
创建 Ingress
以下清单定义了一个 Ingress,它通过 hello-world.example 将流量发送到你的 Service。
从以下文件创建
example-ingress.yamlapiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress spec: ingressClassName: nginx rules: - host: hello-world.example http: paths: - path: / pathType: Prefix backend: service: name: web port: number: 8080通过运行以下命令创建 Ingress 对象
kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml输出应该是
ingress.networking.k8s.io/example-ingress created验证 IP 地址已设置
kubectl get ingress注意
这可能需要几分钟。你应该在
ADDRESS列中看到一个 IPv4 地址;例如NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress nginx hello-world.example 172.17.0.15 80 38s按照你平台的说明,验证 Ingress 控制器正在引导流量
注意
如果在 MacOS(Darwin)上使用 Docker 驱动程序,网络受限,Node IP 无法直接访问。要使 Ingress 工作,你需要打开一个新的终端并运行minikube tunnel。
需要sudo权限,因此在提示时提供密码。curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.exampleminikube tunnel输出类似于:
Tunnel successfully started NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ... The service/ingress example-ingress requires privileged ports to be exposed: [80 443] sudo permission will be asked for it. Starting tunnel for service example-ingress.在新终端中,调用以下命令
curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example
你应该看到Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564(可选)你也可以从浏览器访问
hello-world.example。在你的计算机的
/etc/hosts文件末尾添加一行(你需要管理员权限)查找 minikube 报告的外部 IP 地址
minikube ip172.17.0.15 hello-world.example注意
更改 IP 地址以匹配minikube ip的输出。127.0.0.1 hello-world.example进行此更改后,你的 Web 浏览器会将对
hello-world.exampleURL 的请求发送到 Minikube。
创建第二个 Deployment
使用以下命令创建另一个 Deployment
kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0输出应该是
deployment.apps/web2 created验证 Deployment 处于 Ready 状态
kubectl get deployment web2输出应该类似于
NAME READY UP-TO-DATE AVAILABLE AGE web2 1/1 1 1 16s暴露第二个 Deployment
kubectl expose deployment web2 --port=8080 --type=NodePort输出应该是
service/web2 exposed
编辑现有 Ingress
编辑现有的
example-ingress.yaml清单,并在末尾添加以下行- path: /v2 pathType: Prefix backend: service: name: web2 port: number: 8080应用更改
kubectl apply -f example-ingress.yaml你应该看到
ingress.networking/example-ingress configured
测试你的 Ingress
访问 Hello World 应用程序的第一个版本。
curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.exampleminikube tunnel输出类似于:
Tunnel successfully started NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ... The service/ingress example-ingress requires privileged ports to be exposed: [80 443] sudo permission will be asked for it. Starting tunnel for service example-ingress.在新终端中,调用以下命令
curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example输出类似于:
Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564访问 Hello World 应用程序的第二个版本。
curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example/v2minikube tunnel输出类似于:
Tunnel successfully started NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ... The service/ingress example-ingress requires privileged ports to be exposed: [80 443] sudo permission will be asked for it. Starting tunnel for service example-ingress.在新终端中,调用以下命令
curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example/v2输出类似于:
Hello, world! Version: 2.0.0 Hostname: web2-75cd47646f-t8cjk注意
如果你执行了可选的更新/etc/hosts的步骤,你也可以从浏览器访问hello-world.example和hello-world.example/v2。
下一步
- 阅读更多关于 Ingress 的信息
- 阅读更多关于 Ingress 控制器 的信息
- 阅读更多关于 Service 的信息