使用 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 的資訊