文章轉自微信公衆號:https://mp.weixin.qq.com/s/6S7SErPpotFsF1TyWzexpA
環境信息
軟件 | 版本 | 備註 |
---|---|---|
OS | CentOS Linux release 7.7.1908 (Core) | |
Kubernetes | v1.17.3 | |
Nginx Ingress Controller | v1.6.3 | Nginx 維護的Ingress Controller |
Network Plugin | weave 2.6.1 | |
kubeadmin | v1.17.3 |
部署
部署Nginx Ingress Controller我們參考 nginx官網。就是因爲網絡上很多的文章都沒有標註版本,導致參考的時候會遇到一些問題,對新手來說還很難排查,所以纔有了這篇文章。請部署時參照官網,並且注意自己部署的版本和文檔的一致性。
-
下載yaml文件
$ git clone https://github.com/nginxinc/kubernetes-ingress/
$ cd kubernetes-ingress/deployments
$ git checkout v1.6.3
NOTE:這裏注意 獲取對應版本的nginx ingress controller 部署文件的操作。
-
配置相應的RBAC
$ kubectl apply -f common/ns-and-sa.yaml
$ kubectl apply -f rbac/rbac.yaml
-
創建Secret、ConfigMap及CDR(Custom Resource Definitions)
$ kubectl apply -f common/default-server-secret.yaml
$ kubectl apply -f common/nginx-config.yaml
$ kubectl apply -f common/custom-resource-definitions.yaml
-
選擇daemonset的方式來部署nginx ingress controller pod。(也可以選擇deployment方式)
$ kubectl apply -f daemon-set/nginx-ingress.yaml
由於我們選擇的daemon-set的方式來部署,所以我們的Ingress Controller監聽的80和443端口會在每個Node節點上都存在。不需要再創建service。
-
檢查Pod狀態
$ kubectl get pods --namespace=nginx-ingress
如果Pod正常執行了,則我們的Ingress Controller部署完成。具體的配置項可以自行查看YAML文件。這裏注意,不同的版本
*NOTE:遇到問題,請自行查閱官方的troubleshooting文檔
部署測試應用
在部署應用的時候注意自己的Nginx Ingress Controller的版本,不同的版本之間在使用的過程中會有差別,或者不一致的表現。官方示例
我們本次的部署也是以官方示例complete-example爲例(在我們部署的時候clone的repo裏面的example目錄中)
-
部署Pod
$ kubectl create -f cafe.yaml
-
創建ingress 和secret
$ kubectl create -f cafe-secret.yaml
$ kubectl create -f cafe-ingress.yaml
-
將 yaml文件中涉及的域名“cafe.example.com”添加到您的/etc/hosts中,或者修改爲您自己的域名。
echo "cafe.example.com 192.168.1.100" >> /etc/hosts
-
CURL訪問鏈接測試
$ curl https://cafe.example.com/coffee -k
Server address: 10.12.0.20:80
Server name: coffee-7586895968-r16zn
$ curl https://cafe.example.com/tea -k
Server address: 10.12.0.22:80
Server name: tea-7cd44fcb4d-xfw99
後續
經過上面的操作基本上了解了如何在自己的K8S集羣中部署一個Nginx Ingress Controller,並部署一個簡單的hello-world 例子來體驗下Ingress的使用。但是在使用的過程中還是會有很多的細節需要考慮和處理。例如,在上面的例子中, https://cafe.example.com/teaxxxxxx , https://cafe.example.com/tea/xxxxx 和 https://cafe.example.com/tea 一樣會得被正常轉發到後段服務。
會在接下來的文章中陸續整理。現在先列個大綱:
-
Nginx Ingress Controller的工作原理。
-
troubleshooting
-
基於Nginx Ingress Controller的Ingress對象的設置以應對不同的場景(多個)。