website/content/zh-cn/docs/tasks/access-application-cluster/ingress-minikube.md

16 KiB
Raw Permalink Blame History

title content_type weight min-kubernetes-server-version
在 Minikube 环境中使用 NGINX Ingress 控制器配置 Ingress task 110 1.19

Ingress是一种 API 对象, 其中定义了一些规则使得集群中的服务可以从集群外访问。 Ingress 控制器负责满足 Ingress 中所设置的规则。

本节为你展示如何配置一个简单的 Ingress根据 HTTP URI 将服务请求路由到服务 webweb2

{{% heading "prerequisites" %}}

本教程假设你正在使用 minikube 运行一个本地 Kubernetes 集群。 参阅安装工具了解如何安装 minikube

{{< note >}}

本教程使用需要 AMD64 架构的容器。 如果你在其他 CPU 架构的计算机上使用 minikube可以尝试使用带有可以模拟 AMD64 的驱动程序的 minikube。 例如Docker Desktop 驱动程序可以执行此操作。 {{< /note >}}

{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}

如果你使用的是较早的 Kubernetes 版本,请切换到该版本的文档。

创建一个 Minikube 集群

如果你还未在本地搭建集群,运行 minikube start 创建集群。

启用 Ingress 控制器

  1. 为了启用 NGINIX Ingress 控制器,可以运行下面的命令:

    minikube addons enable ingress
    
  1. 检查验证 NGINX Ingress 控制器处于运行状态:

    kubectl get pods -n ingress-nginx
    

    {{< note >}}

    最多可能需要等待一分钟才能看到这些 Pod 运行正常。 {{< /note >}}

    输出类似于:

    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 应用

  1. 使用下面的命令创建一个 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
    
  1. 将 Deployment 暴露出来:

    kubectl expose deployment web --type=NodePort --port=8080
    

    输出类似于:

    service/web exposed
    
  1. 验证 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
    
  1. 使用节点端口信息访问服务,使用 minikube service 命令。 请按照适合你平台的说明进行操作:

    {{< tabs name="minikube_service" >}} {{% tab name="Linux" %}}

    minikube service web --url
    

    输出类似于:

    http://172.17.0.15:31637
    

    调用上一步输出中获取的 URL

    curl http://172.17.0.15:31637
    

    {{% /tab %}} {{% tab name="MacOS" %}}

    # 该命令必须在单独的终端中运行。
    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 
    

    {{% /tab %}} {{< /tabs >}}

    输出类似于:

    Hello, world!
    Version: 1.0.0
    Hostname: web-55b8c6998d-8k564
    

    你现在应该可以通过 Minikube 的 IP 地址和节点端口来访问示例应用了。 下一步是让自己能够通过 Ingress 资源来访问应用。

创建一个 Ingress

下面是一个定义 Ingress 的配置文件,负责通过 hello-world.example 将请求转发到你的服务。

  1. 根据下面的 YAML 创建文件 example-ingress.yaml

    {{% code_sample file="service/networking/example-ingress.yaml" %}}

  1. 通过运行下面的命令创建 Ingress 对象:

    kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml
    

    输出类似于:

    ingress.networking.k8s.io/example-ingress created
    
  1. 验证 IP 地址已被设置:

    kubectl get ingress
    

    {{< note >}}

    此操作可能需要几分钟时间。 {{< /note >}}

    接下来你将会在 ADDRESS 列中看到 IPv4 地址,例如:

    NAME              CLASS   HOSTS                 ADDRESS        PORTS   AGE
    example-ingress   nginx   hello-world.example   172.17.0.15    80      38s
    
  1. 验证 Ingress 控制器能够转发请求流量,按照适用于所属平台的说明进行操作:

    {{< note >}}

    如果在 MacOSDarwin上使用 Docker 驱动程序,网络会受到限制,并且无法直接访问节点 IP。 要让 Ingress 正常工作,你需要打开一个新终端并运行 minikube tunnel。 此操作需要 sudo 权限,因此请在出现提示时提供密码。 {{< /note >}}

    {{< tabs name="ingress" >}} {{% tab name="Linux" %}}

     curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example
    

    {{% /tab %}} {{% tab name="MacOS" %}}

    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
    

    {{% /tab %}} {{< /tabs >}}

    你应该看到类似输出:

    Hello, world!
    Version: 1.0.0
    Hostname: web-55b8c6998d-8k564
    

    你也可以从浏览器访问 hello-world.info

    • 可选

      查看 Minikube 报告的外部 IP 地址:

      minikube ip
      

      将类似以下这一行添加到你计算机上的 /etc/hosts 文件的末尾(需要管理员访问权限):

      172.17.0.15 hello-world.info
      

      {{< note >}}

更改 IP 地址以匹配 minikube ip 的输出。 {{< /note >}}

 <!--
 After you make this change, your web browser sends requests for
 `hello-world.info` URLs to Minikube.
 -->

 更改完成后,在浏览器中访问 URL `hello-world.info`,请求将被发送到 Minikube。

创建第二个 Deployment

  1. 使用下面的命令创建第二个 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
    
  1. 将第二个 Deployment 暴露出来:

    kubectl expose deployment web2 --port=8080 --type=NodePort
    

    输出类似于:

    service/web2 exposed
    

编辑现有的 Ingress

  1. 编辑现有的 example-ingress.yaml,在文件最后添加以下行:

    - path: /v2
      pathType: Prefix
      backend:
        service:
          name: web2
          port:
            number: 8080
    
  1. 应用变更:

    kubectl apply -f example-ingress.yaml
    

    输出类似于:

    ingress.networking/example-ingress configured
    

测试你的 Ingress

  1. 访问 Hello World 应用的第一个版本:

    {{< tabs name="ingress2-v1" >}} {{% tab name="Linux" %}}

     curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example
    

    {{% /tab %}} {{% tab name="MacOS" %}}

    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
    

    {{% /tab %}} {{< /tabs >}}

    输出类似于:

    Hello, world!
    Version: 1.0.0
    Hostname: web-55b8c6998d-8k564
    
  1. 访问 Hello World 应用的第二个版本:

    {{< tabs name="ingress2-v2" >}} {{% tab name="Linux" %}}

    curl --resolve "hello-world.example:80:$( minikube ip )" -i http://hello-world.example/v2
    

    {{% /tab %}} {{% tab name="MacOS" %}}

    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
    

    {{% /tab %}} {{< /tabs >}}

    输出类似于:

    Hello, world!
    Version: 2.0.0
    Hostname: web2-75cd47646f-t8cjk
    

    {{< note >}}

    如果你执行了更新 /etc/hosts 的可选步骤,你也可以从你的浏览器中访问 hello-world.examplehello-world.example/v2。 {{< /note >}}

{{% heading "whatsnext" %}}