上一篇文章中介紹了基於Nginx實現Ingress Controller的實現,介紹了Nginx Ingress Controller安裝、相關功能,TLS,高級特性等介紹,本章開始介紹基於騰訊雲TKE實現ingress服務暴露。node
TKE是Tencent Kubernetes Engine即騰訊雲基於kubernetes提供的公有云上容器雲服務,TKE提供了兩種暴露服務的方式:service和ingress。nginx
要使用TKE的ingress功能,須要瞭解一下相關的組件內容:後端
因爲nginx ingress controller是直接以Pod的形勢部署在kubernetes集羣中,藉助於service的服務發現可直接實現和pod通信,而TKE中ingress controller未直接部署在k8s集羣中,網絡的接入需藉助於service的NodePort實現接入,其數據流以下圖:centos
環境說明: 建立兩個Deployment並以NodePort方式暴露服務,www1.happylau.cn對應tke-app-1服務,同理www2.happylau.cn對應tke-app-2服務,以下演示操做過程:api
一、建立兩個Deployments瀏覽器
[root@VM_10_2_centos ingress]# kubectl create deployment tke-app-1 --image=nginx:1.7.9 [root@VM_10_2_centos ingress]# kubectl create deployment tke-app-2 --image=nginx:1.7.9
二、 將兩個Deployment以NodePort的方式暴露服務網絡
[root@VM_10_2_centos ~]# kubectl expose deployment tke-app-1 --port=80 --type=NodePort [root@VM_10_2_centos ~]# kubectl expose deployment tke-app-2 --port=80 --type=NodePort 查看服務列表 [root@VM_10_2_centos ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 172.16.255.1 <none> 443/TCP 83d tke-app-1 NodePort 172.16.255.91 <none> 80:30597/TCP 2s tke-app-2 NodePort 172.16.255.236 <none> 80:31674/TCP 73s
三、定義ingress規則,定義兩個host將不一樣主機轉發至backend不一樣的servicesession
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: tke-ingress-demo annotations: kubernetes.io/ingress.class: qcloud spec: rules: - host: www1.happylau.cn http: paths: - path: / backend: serviceName: tke-app-1 servicePort: 80 - host: www2.happylau.cn http: paths: - path: / backend: serviceName: tke-app-2 servicePort: 80
四、 應用ingress規則,並查看ingress詳情,能夠看到ingress建立了一個公網CLB實例架構
#應用ingress規則 [root@VM_10_2_centos ingress]# kubectl apply -f tke-ingress-demo.yaml ingress.extensions/tke-ingress-demo created #查看ingress列表 [root@VM_10_2_centos ingress]# kubectl get ingresses NAME HOSTS ADDRESS PORTS AGE tke-ingress-demo www1.happylau.cn,www2.happylau.cn 140.143.84.xxx 80 67s #查看 ingress詳情 [root@VM_10_2_centos ingress]# kubectl describe ingresses tke-ingress-demo Name: tke-ingress-demo Namespace: default Address: 140.143.84.xxx Default backend: default-http-backend:80 (<none>) Rules: Host Path Backends ---- ---- -------- www1.happylau.cn / tke-app-1:80 (172.16.1.15:80) www2.happylau.cn / tke-app-2:80 (172.16.2.17:80) Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"qcloud"},"name":"tke-ingress-demo","namespace":"default"},"spec":{"rules":[{"host":"www1.happylau.cn","http":{"paths":[{"backend":{"serviceName":"tke-app-1","servicePort":80},"path":"/"}]}},{"host":"www2.happylau.cn","http":{"paths":[{"backend":{"serviceName":"tke-app-2","servicePort":80},"path":"/"}]}}]}} kubernetes.io/ingress.class: qcloud kubernetes.io/ingress.qcloud-loadbalance-id: lb-a0xwhcx3 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal EnsuringIngress 69s (x3 over 89s) loadbalancer-controller Ensuring ingress Normal CREATE 69s (x2 over 70s) loadbalancer-controller create loadbalancer succ Normal EnsuredIngress 68s (x3 over 70s) loadbalancer-controller Ensured ingress
五、測試驗證,將IP和域名寫入到hosts文件中,訪問域名測試驗證,以下經過curl解析的方式測試驗證app
六、ingress會建立一個CLB,並在CLB中建立監聽器、設置轉發規則、綁定後端RS,下圖是CLB上自動生成的規則
經過上面演示可知:
TKE支持將在CLB中加載證書實現https加密傳輸,證書是通過第三方認證的CA簽名過的證書,須要先購買好證書,經過Secrets對象在kubernetes集羣中定義,以下演示https的實現。
一、 經過Secrets建立證書,先獲取到證書的id,若是沒有則先建立證書,證書管理,本文以證書id TKPmsWb3 爲例,經過stringData能實現base64自動加密
apiVersion: v1 kind: Secret metadata: name: ingress-ssl-key stringData: qcloud_cert_id: TKPmsWb3 type: Opaque #生成Secrets對象 [root@VM_10_2_centos ingress]# kubectl apply -f ingress-secret.yaml secret/ingress-ssl-key created [root@VM_10_2_centos ingress]# kubectl get secrets ingress-ssl-key NAME TYPE DATA AGE ingress-ssl-key Opaque 1 7s #查看secrets詳情,可得知VEtQbXNXYjM= 已自動經過base64加密 [root@VM_10_2_centos ingress]# kubectl get secrets ingress-ssl-key -o yaml apiVersion: v1 data: qcloud_cert_id: VEtQbXNXYjM= kind: Secret metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Secret","metadata":{"annotations":{},"name":"ingress-ssl-key","namespace":"default"},"stringData":{"qcloud_cert_id":"TKPmsWb3"},"type":"Opaque"} creationTimestamp: "2020-01-03T11:53:33Z" name: ingress-ssl-key namespace: default resourceVersion: "7083702418" selfLink: /api/v1/namespaces/default/secrets/ingress-ssl-key uid: aaea4a86-2e1f-11ea-a618-ae9224ffad1a type: Opaque #能夠經過base64查看解密後的內容,和配置文件中定義的id一致 [root@VM_10_2_centos ingress]# echo VEtQbXNXYjM= | base64 -d TKPmsWb3
二、準備環境,建立一個nginx的Deployment
[root@VM_10_2_centos ~]# kubectl create deployment tke-ingress-ssl-demo --image=nginx:1.7.9 deployment.apps/tke-ingress-ssl-demo created [root@VM_10_2_centos ~]# kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE tke-ingress-ssl-demo 1/1 1 1 6s
三、將Deployment暴露以NodePort類型暴露service
[root@VM_10_2_centos ~]# kubectl expose deployment tke-ingress-ssl-demo --port=80 --type=NodePort service/tke-ingress-ssl-demo exposed [root@VM_10_2_centos ~]# kubectl get service tke-ingress-ssl-demo -o yaml apiVersion: v1 kind: Service metadata: creationTimestamp: "2020-01-03T12:00:05Z" labels: app: tke-ingress-ssl-demo name: tke-ingress-ssl-demo namespace: default resourceVersion: "7083890283" selfLink: /api/v1/namespaces/default/services/tke-ingress-ssl-demo uid: 94659f42-2e20-11ea-a618-ae9224ffad1a spec: clusterIP: 172.16.255.64 externalTrafficPolicy: Cluster ports: - nodePort: 30324 port: 80 protocol: TCP targetPort: 80 selector: app: tke-ingress-ssl-demo sessionAffinity: None type: NodePort #類型爲NodePort status: loadBalancer: {}
四、定義ingress規則,加載證書實現https轉發
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: tke-ingress-ssl annotations: kubernetes.io/ingress.class: qcloud qcloud_cert_id: TKPmsWb3 spec: rules: - host: www.happylauliu.cn http: paths: - path: / backend: serviceName: tke-ingress-ssl-demo servicePort: 80 tls: - hosts: - www.happylauliu.cn secretName: ingress-ssl-key
五、應用ingress規則,並查看詳情,此時已正常建立CLB並配置規則
[root@VM_10_2_centos ingress]# kubectl apply -f ingress-demo.yaml ingress.extensions/tke-ingress-ssl created #查看ingress詳情 [root@VM_10_2_centos ingress]# kubectl describe ingresses tke-ingress-ssl Name: tke-ingress-ssl Namespace: default Address: 140.143.83.xxx #CLB的外網IP Default backend: default-http-backend:80 (<none>) TLS: ingress-ssl-key terminates www.happylauliu.cn Rules: Host Path Backends ---- ---- -------- www.happylauliu.cn / tke-ingress-ssl-demo:80 (172.16.0.25:80) Annotations: qcloud_cert_id: TKPmsWb3 kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"qcloud","qcloud_cert_id":"TKPmsWb3"},"name":"tke-ingress-ssl","namespace":"default"},"spec":{"rules":[{"host":"www.happylauliu.cn","http":{"paths":[{"backend":{"serviceName":"tke-ingress-ssl-demo","servicePort":80},"path":"/"}]}}],"tls":[{"hosts":["www.happylauliu.cn"],"secretName":"ingress-ssl-key"}]}} kubernetes.io/ingress.class: qcloud kubernetes.io/ingress.qcloud-loadbalance-id: lb-2kcrtwbn #CLB的實例id Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal EnsuringIngress 51s (x3 over 73s) loadbalancer-controller Ensuring ingress Normal CREATE 51s (x2 over 52s) loadbalancer-controller create loadbalancer succ Normal EnsuredIngress 49s (x3 over 52s) loadbalancer-controller Ensured ingress
六、測試驗證,hosts文件中解析www.happylauliu.cn到CLB的VIP,或者DNS解析,打開瀏覽器訪問站點,因爲是通過CA認證簽名的證書,所以沒有提示告警信息,查看證書的詳情信息
七、查看CLB的配置可得知,CLB上配置了443的監聽端口,並關聯了證書,採用單向認證方式
經過CLB的配置規則可知,CLB配置了監聽443的監聽器,80端口並未設置規則,所以此時沒法訪問http,如何實如今TKE使用ingress實現http和https共存呢,能夠經過定義kubernetes.io/ingress.http-rules和
kubernetes.io/ingress.https-rules實現
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: tke-ingress-ssl annotations: kubernetes.io/ingress.class: qcloud kubernetes.io/ingress.rule-mix: "true" #開啓混合規則配置,kubernetes.io/ingress.http-rules配置規則 kubernetes.io/ingress.http-rules: '[{"host":"www.happylauliu.cn","path":"/","backend":{"serviceName":"tke-ingress-ssl-demo","servicePort":"80"}}]' qcloud_cert_id: TKPmsWb3 spec: rules: - host: www.happylauliu.cn http: paths: - path: / backend: serviceName: tke-ingress-ssl-demo servicePort: 80 tls: - hosts: - www.happylauliu.cn secretName: ingress-ssl-key
設置ingress.http-rules和ingress.https-rules註解以後,會在監聽器中建立http和https的轉發規則,並綁定RS,此時訪問http和https均能實現站點訪問,CLB對應的規則內容以下圖:
經過測試訪問http://www.happylauliu.cn/和https://www.happylauliu.cn/均能正常訪問,若是要實現訪問http自動跳轉到https,則能夠在控制檯開啓自動跳轉的功能,以下圖:
開啓重定向功能後再次訪問http站點後此時會自動跳轉到https,以下圖所示location已經跳轉至https://www.happylauliu.cn/
經過上述的演示在騰訊雲公有云環境下ingress controller的實現,騰訊雲TKE經過使用CLB實現和kubernetes ingress集成,藉助於service的NodePort實現轉發,經過公有云專用的CLB可以最大程度保障ingress接入性能。同時,ingress可以使用騰訊雲上的證書實現https加密功能。
Ingress配置:https://kubernetes.io/docs/concepts/services-networking/ingress/
Ingress控制器:https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/
ingress基本配置:https://cloud.tencent.com/document/product/457/31711
ingress證書:https://cloud.tencent.com/document/product/457/40538
CLB配置http自動跳轉:https://cloud.tencent.com/document/product/214/8839
當你的才華撐不起你的野心時,你就應該靜下心來學習