主機 | IP地址 | 服務 |
---|---|---|
master | 192.168.1.21 | k8s |
node01 | 192.168.1.22 | k8s |
node02 | 192.168.1.23 | k8s |
基於[ http://www.javashuo.com/article/p-wzdhboob-dy.html]() 的實驗繼續進行
node
[root@master ingress]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.29.0/deploy/static/mandatory.yaml
[root@master ingress]# vim mandatory.yaml hostNetwork: true #213
[root@master ingress]# kubectl apply -f mandatory.yaml
[root@master ingress]# kubectl get pod -n ingress-nginx
[root@master yaml]# vim service-nodeport.yaml apiVersion: v1 kind: Service metadata: name: ingress-nginx namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx spec: type: NodePort ports: - name: http port: 80 targetPort: 80 protocol: TCP - name: https port: 443 targetPort: 443 protocol: TCP selector: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx ---
[root@master ingress]# kubectl apply -f service-nodeport.yaml
[root@master ingress]# kubectl get svc -n ingress-nginx
[root@master yaml]# vim deploy1.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: deploy1 spec: replicas: 2 template: metadata: labels: app: nginx1 spec: containers: - name: nginx1 image: nginx --- apiVersion: v1 kind: Service metadata: name: svc-1 spec: selector: app: nginx1 ports: - port: 80 targetPort: 80
[root@master yaml]# kubectl apply -f deploy1.yaml
[root@master yaml]# kubectl get pod
[root@master yaml]# kubectl get svc
[root@master yaml]# vim deploy2.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: deploy2 spec: replicas: 2 template: metadata: labels: app: nginx2 spec: containers: - name: nginx2 image: nginx --- apiVersion: v1 kind: Service metadata: name: svc-2 spec: selector: app: nginx2 ports: - port: 80 targetPort: 80
[root@master yaml]# kubectl apply -f deploy2.yaml
[root@master yaml]# kubectl get deployments.
[root@master yaml]# vim ingress.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-1 spec: rules: - host: www1.bdqn.com http: paths: - path: / backend: serviceName: svc-1 servicePort: 80 --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-2 spec: rules: - host: www2.bdqn.com http: paths: - path: / backend: serviceName: svc-2 servicePort: 80
[root@master yaml]# kubectl apply -f ingress.yaml
[root@master yaml]# kubectl get ingresses.
[root@master yaml]# kubectl describe ingresses. ingress-1
[root@master yaml]# kubectl describe ingresses. ingress-2
[root@master yaml]# kubectl get svc -n ingress-nginx //查看映射的端口
http://www1.bdqn.com:30817/nginx
http://www2.bdqn.com:30817/git
後端pod===》service====》ingress規則====》寫入Ingress-nginx-controller配置文件並自動重載使更改生效===》對本機進行域名解析====》實現client經過域名的IP+端口均可以訪問到後端podgithub
在上面的操做中,實現了使用ingress-nginx爲後端全部pod提供一個統一的入口,那麼,有一個很是嚴肅的問題須要考慮,就是如何爲咱們的pod配置CA證書來實現HTTPS訪問?在pod中直接配置CA麼?那須要進行多少重複性的操做?並且,pod是隨時可能被kubelet殺死再建立的。固然這些問題有不少解決方法,好比直接將CA配置到鏡像中,可是這樣又須要不少個CA證書。web
這裏有更簡便的一種方法,就拿上面的狀況來講,後端有多個pod,pod與service進行關聯,service又被ingress規則發現並動態寫入到ingress-nginx-controller容器中,而後又爲ingress-nginx-controller建立了一個Service映射到羣集節點上的端口,來供client來訪問。vim
在上面的一系列流程中,關鍵的點就在於ingress規則,咱們只須要在ingress的yaml文件中,爲域名配置CA證書便可,只要能夠經過HTTPS訪問到域名,至於這個域名是怎麼關聯到後端提供服務的pod,這就是屬於k8s羣集內部的通訊了,即使是使用http來通訊,也無傷大雅。後端
[root@master yaml]# mkdir https //建立一個放置證書的目錄 [root@master yaml]# cd https/ [root@master https]# openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=testsvc /O=testsvc" //生成證書
[root@master https]# kubectl create secret tls tls-secret --key=tls.key --cert tls.crt
[root@master yaml]# vim deploy3.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: deploy3 spec: replicas: 2 template: metadata: labels: app: nginx3 spec: containers: - name: nginx3 image: nginx --- apiVersion: v1 kind: Service metadata: name: svc-3 spec: selector: app: nginx3 ports: - port: 80 targetPort: 80
[root@master https]# kubectl apply -f deploy3.yaml
[root@master https]# kubectl get pod
[root@master https]# kubectl get svc
[root@master https]# vim ingress.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-3 spec: tls: - hosts: - www3.bdqn.com #域名 secretName: tls-secret #保存的證書 rules: - host: www3.bdqn.com http: paths: - path: / backend: serviceName: svc-3 servicePort: 80
[root@master https]# kubectl apply -f ingress.yaml
[root@master https]# kubectl get ingresses.
[root@master https]# kubectl get svc -n ingress-nginx
https://www3.bdqn.com:31372/api
k8s集羣利用了「一切皆爲資源」的原理,把生成的ca證書當成一個公共的資源來使用,使用時只需綁定保存的ca證書便可,不像以前同樣,須要一個一個的建立ca證書,而後在關聯起來,方便好用又快捷。瀏覽器