kubernetes Ingess 是有2部分組成,Ingress Controller 和Ingress服務組成,經常使用的Ingress Controller 是ingress-nginx,工做的原理是:nginx
Ingress Controller 會動態感知集羣中的Ingress的規則變化,而後讀取,動態生成Nginx的配置文件,最後注入到運行nginx的pod的中,而後會自動reload,配置生效。vim
用kubernetes Ingress 是因爲它是7層調度,能夠直接卸載https會話,代理的後端的pod能夠直接使用明文的http協議。後端
而Service NodePort得類型,是4層得調度,作不到這點,然而如今https是一種趨勢,因此在kubernetes 對外暴露服務得時候咱們仍是要選擇Ingress。api
下面咱們來看下Ingress得部署:
原理圖
app
首先建立一個文件夾專門放置Igress得yaml得文件,mkdir ingress
vim myapp.yamlide
apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 3 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp-ding image: ikubernetes/myapp:v2 --- apiVersion: v1 kind: Service metadata: name: myapp #等會ingress就靠這個來匹配 spec: selector: app: myapp type: ClusterIP ports: - port: 80 targetPort: 80
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-ding namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: www.yang.com #虛擬機主機域名 http: paths: - path: backend: serviceName: myapp #代理後端的service 的name servicePort: 80 #後端service的端口