- Ingress是什么?
- 之前我們了解到NodePort、LoadBalancer兩種方法可以讓pod暴露給外部訪問,並且通過NodePort我們需要提前規划端口,避免應用越來越來越多的時候變得端口難以分配管理;並且每一個service都會創建一個負載均衡服務,導致成本比較高。那么有沒有一種可以提供全局負載均衡器呢?那么kubernetes采用ingress來實現。
- ingress公開了從集群外部訪問到集群內部serivced的http和https路由。路由流量由ingress資源上定義規則控制。
internet | [ Ingress ] --|-----|-- [ Services ]
- Ingress不會公布任意端口或協議,將http和https以外的服務公開到internet時,通常使用service.Type=NodePort或者service.Type=LoadBalancer
- ingress通過URL、域名將請求轉發到不同的service,支持tcp/UDP 4層、7層負責均衡。ingress是訪問規則的集合,具體是由ingress Controller(ingress Controller是在node上運行) 實現Pod的負責均衡。
- 環境准備
- ingress 控制器 部署文檔 https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md
- kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.28.0/deploy/static/mandatory.yaml
- 鏡像是國外的,需要更換到國內鏡像
- hostNetwork:true #使用宿主機的網絡,確保80和443暴露到node上
- 除了官網的,使用我們定義好的yaml
- kubectl get pods -n ingress-nginx -o wide#查看部署情況
- Ingress HTTP
- 准備simple-fanout-example.yaml
-
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: simple-fanout-example annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - host: foo.bar.com #配置域名(域名記得解析到服務器) http: paths: - path: /foo backend: serviceName: service1 #service的名稱 servicePort: 4200 #clauster IP端口(應用的端口,例如nginx默認是8080) - path: /bar backend: serviceName: service2 servicePort: 8080
kubectl apply -f simple-fanout-example.yaml #執行創建
kubectl get ingress #查看創建的ingress對象
然后我們使用http://
foo.bar.com/foo:4200 就可以訪問服務了
- Ingress HTTPS
- 首先我們要准備好使用的ssl證書;自簽或購買的都是可以的。我們使用自簽方式進行演示
- 自簽證書教程 https://www.cnblogs.com/TSir/p/12213175.html 里面的域名替換成我們想要頒發的即可
- 創建 tls.yaml
apiVersion: v1 kind: Secret metadata: name: testsecret-tls namespace: default data: tls.crt: foo.bar.com.pem
tls.key: foo.bar.com-key .pem
type: kubernetes.io/tls - kubectl get secert #查看我們創建的數字信息
- 為simple-fanout-example.yaml設置證書
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: tls-example-ingress spec: tls: - hosts: - foo.bar.com
secretName: testsecret-tls rules: - host: sslexample.foo.com http: paths: - path: / backend: serviceName: service1 servicePort: 80 通過htts://
foo.bar.com即可訪問
- Ingress 主要功能
- 支持4層、7層負載均衡
- 支持獨定義service訪問策略
- 只支持基於域名的網站訪問
- 支持tls