您好,登錄后才能下訂單哦!
本篇內容介紹了“k8s部署ingress-nginx的方法是什么”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1、獲取配置文件
#文件已下載到本地 https://github.com/kubernetes/ingress-nginx/tree/nginx-0.20.0/deploy
2、準備鏡像
unzip ingress-nginx-nginx-0.20.0.zip cd ingress-nginx-nginx-0.20.0/deploy/ vim mandatory.yaml #其他文件的集合 #編輯mandatory.yaml文件,將defaultbackend鏡像地址改成阿里云的鏡像地址(如下圖) image: registry.cn-hangzhou.aliyuncs.com/allinpay/defaultbackend-amd64:v1.5
3、安裝
kubectl apply -f mandatory.yaml
#稍等片刻,使用下列命令查詢 kubectl get namespace kubectl get pods -n ingress-nginx
4、創建后端pod和service (pod-B、service-B)
vim deploy-demo.yaml apiVersion: v1 kind: Service metadata: name: myapp namespace: default spec: selector: app: myapp release: canary ports: - name: http targetPort: 80 port: 80 --- apiVersion: apps/v1 kind: Deployment metadata: name: myapp-deploy namespace: default spec: replicas: 3 selector: matchLabels: app: myapp release: canary template: metadata: labels: app: myapp release: canary spec: containers: - name: myapp image: ikubernetes/myapp:v2 ports: - name: http containerPort: 80 #應用配置 kubectl apply -f deploy-demo.yaml #查看 kubectl get pods
5、創建service-A
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 nodePort: 30080 - name: https port: 443 targetPort: 443 protocol: TCP nodePort: 30443 selector: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx #應用配置 kubectl apply -f service-nodeport.yaml #查看 kubectl get svc -n ingress-nginx
在外部瀏覽器中中訪問:ip:30080
此時報錯的原因是因為沒有生成ingress-controller關聯到service-B的ingress規則;
6、定義ingress規則
定義ingress規則,此間規則會自動注入到ingress-controller(pod)的nginx.conf中;
ingress-controller是直接關聯到service-B的,但是中間由ingress來生成各種規則;
vim ingress-myapp.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-myapp namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: myapp.magedu.com http: paths: - path: backend: serviceName: myapp servicePort: 80
此時ingress-controller的nginx.conf中會自動生成一個server段的配置,也就是一個nginx的虛擬主機的字段,還包括upstream配置等;
upstream自動代理到后端pod(pod-B),這些都是自動生成,所以就實現了自動生成配置,自動更改配置等;
只需要改此yaml文件即可;
#應用配置 kubectl apply -f ingress-myapp.yaml
在外部主機上配置好host文件
然后在瀏覽器中再訪問,發現已經可以訪問到后端pod了
1、創建k8s證書(要注意和后端的pod使用相同的namespace)
kubectl -n default create secret tls ingress-test --key /home/centos/cert/cash532.key --cert /home/centos/cert/cash532.crt
2、創建ingress規則
注意namespace的相同性,要不然證書無法生效
vim ingress-myapp-cash532.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-myapp namespace: default annotations: kubernetes.io/ingress.class: "nginx" kubernetes.io/secure-backends: "true" kubernetes.io/ssl-passthrough: "true" spec: tls: - hosts: - myapp.cash532.xyz secretName: ingress-test rules: - host: myapp.cash532.xyz http: paths: - path: backend: serviceName: myapp servicePort: 80 #應用配置 kubectl apply -f ingress-myapp-cash532.yaml
瀏覽器訪問
“k8s部署ingress-nginx的方法是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。