--- title: 在 Minikube 环境中使用 NGINX Ingress 控制器配置 Ingress content_type: task weight: 110 min-kubernetes-server-version: 1.19 --- [Ingress](/zh-cn/docs/concepts/services-networking/ingress/)是一种 API 对象, 其中定义了一些规则使得集群中的服务可以从集群外访问。 [Ingress 控制器](/zh-cn/docs/concepts/services-networking/ingress-controllers/)负责满足 Ingress 中所设置的规则。 本节为你展示如何配置一个简单的 Ingress,根据 HTTP URI 将服务请求路由到服务 `web` 或 `web2`。 ## {{% heading "prerequisites" %}} 本教程假设你正在使用 `minikube` 运行一个本地 Kubernetes 集群。 参阅[安装工具](/zh-cn/docs/tasks/tools/#minikube)了解如何安装 `minikube`。 {{< note >}} 本教程使用需要 AMD64 架构的容器。 如果你在其他 CPU 架构的计算机上使用 minikube,可以尝试使用带有可以模拟 AMD64 的驱动程序的 minikube。 例如,Docker Desktop 驱动程序可以执行此操作。 {{< /note >}} {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} 如果你使用的是较早的 Kubernetes 版本,请切换到该版本的文档。 ### 创建一个 Minikube 集群 {#create-minikube-cluster} 如果你还未在本地搭建集群,运行 `minikube start` 创建集群。 ## 启用 Ingress 控制器 {#enable-ingress-controller} 1. 为了启用 NGINIX Ingress 控制器,可以运行下面的命令: ```shell minikube addons enable ingress ``` 2. 检查验证 NGINX Ingress 控制器处于运行状态: ```shell kubectl get pods -n ingress-nginx ``` {{< note >}} 最多可能需要等待一分钟才能看到这些 Pod 运行正常。 {{< /note >}} 输出类似于: ```none 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 应用 {#deploy-hello-world} 1. 使用下面的命令创建一个 Deployment: ```shell kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0 ``` 输出: ```none deployment.apps/web created ``` 验证 Deployment 是否处于 Ready 状态: ```shell kubectl get deployment web ``` 输出应类似于: ```none NAME READY UP-TO-DATE AVAILABLE AGE web 1/1 1 1 53s ``` 2. 将 Deployment 暴露出来: ```shell kubectl expose deployment web --type=NodePort --port=8080 ``` 输出类似于: ```none service/web exposed ``` 3. 验证 Service 已经创建,并且可以从节点端口访问: ```shell kubectl get service web ``` 输出类似于: ```none NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE web NodePort 10.104.133.249 8080:31637/TCP 12m ``` 4. 使用节点端口信息访问服务,使用 [`minikube service`](https://minikube.sigs.k8s.io/docs/handbook/accessing/#using-minikube-service-with-tunnel) 命令。 请按照适合你平台的说明进行操作: {{< tabs name="minikube_service" >}} {{% tab name="Linux" %}} ```shell minikube service web --url ``` 输出类似于: ```none http://172.17.0.15:31637 ``` 调用上一步输出中获取的 URL: ```shell curl http://172.17.0.15:31637 ``` {{% /tab %}} {{% tab name="MacOS" %}} ```shell # 该命令必须在单独的终端中运行。 minikube service web --url ``` 输出类似于: ```none 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: ```shell curl http://127.0.0.1:62445 ``` {{% /tab %}} {{< /tabs >}}
输出类似于: ```none Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564 ``` 你现在应该可以通过 Minikube 的 IP 地址和节点端口来访问示例应用了。 下一步是让自己能够通过 Ingress 资源来访问应用。 ## 创建一个 Ingress {#create-ingress} 下面是一个定义 Ingress 的配置文件,负责通过 `hello-world.example` 将请求转发到你的服务。 1. 根据下面的 YAML 创建文件 `example-ingress.yaml`: {{% code_sample file="service/networking/example-ingress.yaml" %}} 2. 通过运行下面的命令创建 Ingress 对象: ```shell kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml ``` 输出类似于: ```none ingress.networking.k8s.io/example-ingress created ``` 3. 验证 IP 地址已被设置: ```shell kubectl get ingress ``` {{< note >}} 此操作可能需要几分钟时间。 {{< /note >}} 接下来你将会在 `ADDRESS` 列中看到 IPv4 地址,例如: ```none NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress nginx hello-world.example 172.17.0.15 80 38s ``` 4. 验证 Ingress 控制器能够转发请求流量,按照适用于所属平台的说明进行操作: {{< note >}} 如果在 MacOS(Darwin)上使用 Docker 驱动程序,网络会受到限制,并且无法直接访问节点 IP。 要让 Ingress 正常工作,你需要打开一个新终端并运行 `minikube tunnel`。 此操作需要 `sudo` 权限,因此请在出现提示时提供密码。 {{< /note >}} {{< tabs name="ingress" >}} {{% tab name="Linux" %}} ```shell curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example ``` {{% /tab %}} {{% tab name="MacOS" %}} ```shell minikube tunnel ``` 输出类似于: ```none 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. ``` 从新终端中调用以下命令: ```shell curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example ``` {{% /tab %}} {{< /tabs >}}
你应该看到类似输出: ```none Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564 ``` 你也可以从浏览器访问 `hello-world.info`。 * **可选** 查看 Minikube 报告的外部 IP 地址: ```shell minikube ip ``` 将类似以下这一行添加到你计算机上的 `/etc/hosts` 文件的末尾(需要管理员访问权限): ```none 172.17.0.15 hello-world.info ``` {{< note >}} 更改 IP 地址以匹配 `minikube ip` 的输出。 {{< /note >}} 更改完成后,在浏览器中访问 URL `hello-world.info`,请求将被发送到 Minikube。 ## 创建第二个 Deployment {#create-second-deployment} 1. 使用下面的命令创建第二个 Deployment: ```shell kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0 ``` 输出类似于: ```none deployment.apps/web2 created ``` 验证 Deployment 是否处于 Ready 状态: ```shell kubectl get deployment web2 ``` 输出应类似于: ```none NAME READY UP-TO-DATE AVAILABLE AGE web2 1/1 1 1 16s ``` 2. 将第二个 Deployment 暴露出来: ```shell kubectl expose deployment web2 --port=8080 --type=NodePort ``` 输出类似于: ```none service/web2 exposed ``` ## 编辑现有的 Ingress {#edit-ingress} 1. 编辑现有的 `example-ingress.yaml`,在文件最后添加以下行: ```yaml - path: /v2 pathType: Prefix backend: service: name: web2 port: number: 8080 ``` 2. 应用变更: ```shell kubectl apply -f example-ingress.yaml ``` 输出类似于: ```none ingress.networking/example-ingress configured ``` ## 测试你的 Ingress {#test-ingress} 1. 访问 Hello World 应用的第一个版本: {{< tabs name="ingress2-v1" >}} {{% tab name="Linux" %}} ```shell curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example ``` {{% /tab %}} {{% tab name="MacOS" %}} ```shell minikube tunnel ``` 输出类似于: ```none 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. ``` 从新终端中调用以下命令: ```shell curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example ``` {{% /tab %}} {{< /tabs >}}
输出类似于: ```none Hello, world! Version: 1.0.0 Hostname: web-55b8c6998d-8k564 ``` 2. 访问 Hello World 应用的第二个版本: {{< tabs name="ingress2-v2" >}} {{% tab name="Linux" %}} ```shell curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example/v2 ``` {{% /tab %}} {{% tab name="MacOS" %}} ```shell minikube tunnel ``` 输出类似于: ```none 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. ``` 从新终端中调用以下命令: ```shell curl --resolve "hello-world.example:80:127.0.0.1" -i http://hello-world.example/v2 ``` {{% /tab %}} {{< /tabs >}} 输出类似于: ```none Hello, world! Version: 2.0.0 Hostname: web2-75cd47646f-t8cjk ``` {{< note >}} 如果你执行了更新 `/etc/hosts` 的可选步骤,你也可以从你的浏览器中访问 `hello-world.example` 和 `hello-world.example/v2`。 {{< /note >}} ## {{% heading "whatsnext" %}} * 进一步了解 [Ingress](/zh-cn/docs/concepts/services-networking/ingress/) * 进一步了解 [Ingress 控制器](/zh-cn/docs/concepts/services-networking/ingress-controllers/) * 进一步了解 [Service](/zh-cn/docs/concepts/services-networking/service/)