注意:node
本文只用的ingress-nginx版本爲0.24.1,可在官方網站自行下載nginx
https://github.com/kubernetes/ingress-nginxgit
1、Ingress-Nginx工做原理github
1.ingress controller經過和kubernetes api交互,動態的去感知集羣中ingress規則變化, 2.而後讀取它,按照自定義的規則,規則就是寫明瞭哪一個域名對應哪一個service,生成一段nginx配置, 3.再寫到nginx-ingress-control的pod裏,這個Ingress controller的pod裏運行着一個Nginx服務,控制器會把生成的nginx配置寫入/etc/nginx.conf文件中, 4.而後reload一下使配置生效。以此達到域名分配置和動態更新的問題。
2、搭建vim
kubnet ~]$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.24.1/deploy/mandatory.yaml後端
kubnet ~]$ kubectl get pods -n ingress-nginxapi
kubnet ~]$ mkdir ingress瀏覽器
kubnet ingress]$ vim deploy-demo.yml 服務器
apiVersion: v1 kind: Service metadata: name: myapp namespace: default spec: selector: app: myapp release: stable ports: - name: myapp port: 80 targetPort: 80 --- apiVersion: apps/v1 kind: Deployment metadata: name: myapp namespace: default spec: selector: matchLabels: app: myapp release: stable replicas: 3 template: metadata: labels: app: myapp release: stable spec: containers: - name: myapp image: fangwenkai/kubernetes:v1.15 imagePullPolicy: IfNotPresent ports: - name: myapp containerPort: 80
kubnet ingress]$ kubectl apply -f deploy-demo.ymlapp
kubnet ingress]$ kubectl get svc
kubnet ingress]$ kubectl get deploy
kubnet ingress]$ kubectl get pods
kubnet ingress]$ wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.24.1/deploy/provider/baremetal/service-nodeport.yaml
kubnet ingress]$ 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 nodePort: 30001 // 手動指定服務器端口,不指定就是默認 protocol: TCP - name: https port: 443 targetPort: 443 nodePort: 30443 // 手動指定服務器端口,不指定就是默認 protocol: TCP selector: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx ---
kubnet ingress]$ kubectl get svc -n ingress-nginx
kubnet ingress]$ curl 192.168.23.101:30001 // 訪問本機地址,此時結果應該是404
kubnet ingress]$ vim ingress-myapp.yml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-myapp namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: myapp.kaikai.com http: paths: - path: backend: serviceName: myapp servicePort: 80
kubnet ingress]$ kubectl get ingress
kubnet ingress]$ curl myapp.kaikai.com:30001 // 能看到後端Pod結果
或者瀏覽器直接訪問地址