ingree controller - nginx

Posted by JamsBlocKK Blog on Tuesday, March 24, 2020

外部使用 k8s 建置好的服務

安裝

k8s-m1> git clone https://github.com/kubernetes/ingress-nginx.git
k8s-m1> cd ingress-nginx/delpoy/static

修改 mandatory.yaml, 設定 hostNetwork: true

k8s-m1 static > vim mandatory.yaml

annotations:
    prometheus.io/port: "10254"
    prometheus.io/scrape: "true"
spec:
  hostNetwork: true # <-- 加入這行
  # wait up to five minutes for the drain of connections
  terminationGracePeriodSeconds: 300
  serviceAccountName: nginx-ingress-serviceaccount

執行

k8s-m1 static > kubectl apply -f mandatory.yaml

到 k8s-node 查看 80, 443 端口狀況

k8s-n1 > netstat -tunlp
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      28150/nginx.conf
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28150/nginx.conf

回到 k8s-m1 設定 service

service-hello.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: hello-app
spec:
selector:
  app: hello-app
ports:
  - protocol: TCP
    port: 80
    targetPort: 80

k8s-m1 > kubectl apply -f service-hello.yaml

設定 ingress

ingress-hello.yaml
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
annotations:
  kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: nginx.testdomain.com
    http:
    paths:
    - path: /hello 
      backend:
        serviceName: hello-app
        servicePort: 80
      

k8s-m1 > kubectl apply -f ingress-hello.yaml

到 要瀏覽的主機 設定 client 的 dns 解析

echo "your-node-ip  nginx.testdomain.com" >> /etc/hosts

執行

curl nginx.testdomain.com/hello

完成!!!!!