ingress controller是獨立與controller-manager的
Ingress的主要作用是可以利用nginx,haproxy,envoy,traefik等負載均衡器來暴露集群內部服務。
利用Ingress可以解決內部資源訪問外部資源的方式,和四層調度替換為七層調度的問題。
解決因為四層iptables/ipvs規則實現調度或加密tsl的方式過於復雜且效率低下的問題,因為四層調度的iptables/ipvs模塊無法卸載,所以使用七層負載均衡來替代。
用戶訪問過程:
用戶訪問->nodeIP:Nodeport->podIP:containerPort
ingress原理:
用戶訪問-->LB-->ingress-nginx-service-->ingressController-ingress-nginx-pod-->ingress字段中調用的后端pod
后端pod的service只提供pod歸類,歸類后ingress會將此service中的后端pod信息提取出來,然后動態注入到ingress-nginx-pod中的ingress字段中
隨后,后端pod就被調用到了
例:myapp
1.下載資源。github上搜索ingress-nginx,找到deploy項目,下載里面的mandatory.yaml,或在https://kubernetes.github.io/ingress-nginx/deploy/查看教程下載mandatory.yaml和service-nodeport.yaml
下載yaml后,kubectl apply -f mandatory.yaml。會創建1個pod:nginx-ingress-controller,1個svc:ingress-nginx,1個namespace
kubectl apply -f service-nodeport.yaml會創建一個service
這兩條命令創建出ingress框架:ns/svc/deploy/ingress/,以后創建后端和后端匹配的ingress資源就可以使用生成的svc來訪問了
2.創建myapp后端資源:
1 apiVersion: v1 2 kind: Service 3 metadata: 4 name: myapp 5 namespace: default
6 spec: 7 selector: 8 app: myapp 9 release: canary 10 ports: 11 - name: http 12 targetPort: 80
13 port: 80
14 ---
15 apiVersion: apps/v1 16 kind: Deployment 17 metadata: 18 name: myapp-deploy 19 namespace: default
20 spec: 21 replicas: 3
22 selector: 23 matchLabels: 24 app: myapp 25 release: canary 26 template: 27 metadata: 28 labels: 29 app: myapp 30 release: canary 31 spec: 32 containers: 33 - name: myapp 34 image: ikubernetes/myapp:v2 35 ports: 36 - name: http 37 containerPort: 80
3.創建ingress
1 apiVersion: extensions/v1beta1 2 kind: Ingress 3 metadata: 4 name: ingress-myapp 5 namespace: default
6 annotations: 7 kubernetes.io/ingess.class: "nginx"
8 spec: 9 rules: 10 - host: smbands.com 11 http: 12 paths: 13 - path: 14 backend: 15 serviceName: myapp ##注此處必須要和后端pod的service的名稱一致,否則會報503錯誤 16 servicePort: 80 ##注此處必須要和后端pod的service的端口一致,否則會報503錯誤
#然后kubectl apply -f myapp-demo.yaml、kubectl apply -f ingress-demo.yaml。
4.訪問。
確保hosts文件可以正確解析即可訪問,使用域名+ingress-nginx的service暴露的端口
自簽證書https tls
例:
1.生成所需文件
生成私鑰文件:openssl genrsa -out tls.key 2048
生成證書文件:openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Beijing/L=Beijing/O=DepOps/CN=tomcat.smbands.com
會有tls.crt tls.key兩個文件
2.創建后端pod資源
1 apiVersion: v1 2 kind: Service 3 metadata: 4 name: tomcat ##下面的Ingess的資源清單里面的serviceName必須與此名稱一致,否則報503錯誤。 5 namespace: default
6 spec: 7 selector: 8 app: tomcat 9 release: canary 10 ports: 11 - name: http 12 targetPort: 8080
13 port: 8080
14 - name: ajp 15 targetPort: 8009
16 port: 8009
17 ---
18 apiVersion: apps/v1 19 kind: Deployment 20 metadata: 21 name: tomcat-deploy 22 namespace: default
23 spec: 24 replicas: 3
25 selector: 26 matchLabels: 27 app: tomcat 28 release: canary 29 template: 30 metadata: 31 labels: 32 app: tomcat 33 release: canary 34 spec: 35 containers: 36 - name: tomcat 37 image: tomcat 38 ports: 39 - name: http 40 containerPort: 8080
41 - name: ajp 42 containerPort: 8009
3.創建secert資源,使其將證書密鑰注入到tomcat配置文件中:
1 kubectl create secret tls tomcat-ingress-secret --cert=tls.crt --key=tls.key
4.創建ingress資源:
1 apiVersion: extensions/v1beta1 2 kind: Ingress 3 metadata: 4 name: ingress-tomcat-tls 5 namespace: default
6 annotations: 7 kubernetes.io/ingess.class: "nginx"
8 spec: 9 tls: 10 - hosts: 11 - tomcat-smbands.com 12 secretName: tomcat-ingress-secret 13 rules: 14 - host: tomcat.smbands.com 15 http: 16 paths: 17 - path: 18 backend: 19 serviceName: tomcat ##注:這里的serviceName必須與pod的service名稱一致,否則會報503錯誤。 20 servicePort: 8080 ##注此處必須要和后端pod的service的端口一致,否則會報503錯誤
5.應用
kubectl apply -f tomcat-ingress-tls.yaml
6.訪問。
確保hosts文件可以正確解析即可訪問,使用域名https://tomcat.smbands.com:ingress的service向外暴露的端口。