在 Minikube 上使用 NGINX Ingress Controller 配置 Ingress
一个 Ingress 是一个 API 对象,它定义了允许外部访问集群中服务的规则。一个 Ingress 控制器 负责实现 Ingress 中设置的规则。
本页面将向你展示如何设置一个简单的 Ingress,该 Ingress 根据 HTTP URI 将请求路由到名为 'web' 或 'web2' 的 Service。
开始之前
本教程假设你正在使用 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
以下 manifest 定义了一个 Ingress,它通过 hello-world.example
将流量发送到你的 Service。
从以下文件创建
example-ingress.yaml
apiVersion: 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.example
minikube 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 ip
172.17.0.15 hello-world.example
注意
将 IP 地址更改为与minikube ip
的输出匹配。127.0.0.1 hello-world.example
进行此更改后,你的 Web 浏览器将向 Minikube 发送针对
hello-world.example
URL 的请求。
创建第二个 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
manifest,并在末尾添加以下行- 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 应用的第 1 个版本。
curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example
minikube 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 应用的第 2 个版本。
curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example/v2
minikube 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 控制器 的信息
- 阅读更多关于 Services 的信息