下載地址:https://github.com/kubernetes/ingress-nginx/archive/nginx-0.22.0.tar.gzhtml
# with-rbac.yaml apiVersion: apps/v1 # kind: Deployment kind: DeamonSet metadata: name: nginx-ingress-controller namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx spec: # replicas: 1 selector: matchLabels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx template: metadata: labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx annotations: prometheus.io/port: "10254" prometheus.io/scrape: "true" spec: serviceAccountName: nginx-ingress-serviceaccount hostNetwork: true containers: - name: nginx-ingress-controller image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.22.0 args: - /nginx-ingress-controller - --configmap=$(POD_NAMESPACE)/nginx-configuration - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services - --udp-services-configmap=$(POD_NAMESPACE)/udp-services - --publish-service=$(POD_NAMESPACE)/ingress-nginx - --annotations-prefix=nginx.ingress.kubernetes.io securityContext: allowPrivilegeEscalation: true capabilities: drop: - ALL add: - NET_BIND_SERVICE # www-data -> 33 runAsUser: 33 env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace ports: - name: http containerPort: 80 - name: https containerPort: 443 livenessProbe: failureThreshold: 3 httpGet: path: /healthz port: 10254 scheme: HTTP initialDelaySeconds: 10 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 1 readinessProbe: failureThreshold: 3 httpGet: path: /healthz port: 10254 scheme: HTTP periodSeconds: 10 successThreshold: 1 timeoutSeconds: 1 nodeSelector: custom/ingress-controller-ready: "true" ---
kubectl label nodes 192.168.2.101 custom/ingress-controller-ready=true kubectl label nodes 192.168.2.102 custom/ingress-controller-ready=true kubectl label nodes 192.168.2.103 custom/ingress-controller-ready=true
kubectl create -f mandatory.yaml
kubectl create -f configmap.yaml kubectl create -f namespace.yaml kubectl create -f rbac.yaml kubectl create -f with-rbac.yaml
# cat > my-apache.yaml << EOF apiVersion: extensions/v1beta1 kind: Deployment metadata: name: my-apache spec: replicas: 2 template: metadata: labels: run: my-apache spec: containers: - name: my-apache image: httpd ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: my-apache spec: metadata: labels: run: my-apache spec: type: NodePort ports: - port: 80 targetPort: 80 nodePort: 30002 selector: run: my-apache EOF
# cat > my-nginx.yaml << EOF apiVersion: extensions/v1beta1 kind: Deployment metadata: name: my-nginx spec: replicas: 2 template: metadata: labels: run: my-nginx spec: containers: - name: my-nginx image: nginx ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: my-nginx spec: template: matadata: lables: run: my-nginx spec: type: NodePort ports: - port: 80 targetPort: 80 nodePort: 30001 selector: run: my-nginx EOF
# cat > test-ingress.yaml << EOF apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test-ingress namespace: default spec: rules: - host: test.apache.ingress http: paths: - path: / backend: serviceName: my-apache servicePort: 80 - host: test.nginx.ingress http: paths: - path: / backend: serviceName: my-nginx servicePort: 80 EOF
# kubectl get ingress NAME HOSTS ADDRESS PORTS AGE test-ingress test.apache.ingress,test.nginx.ingress 80 23s
# kubectl get deploy,pod,svc NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deployment.extensions/my-apache 2 2 2 1 12s deployment.extensions/my-nginx 2 2 2 2 12s NAME READY STATUS RESTARTS AGE pod/my-apache-57874fd49c-dc4vx 1/1 Running 0 12s pod/my-apache-57874fd49c-lfhld 0/1 ContainerCreating 0 12s pod/my-nginx-756f645cd7-fvq9d 1/1 Running 0 11s pod/my-nginx-756f645cd7-ngj99 1/1 Running 0 12s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 10d service/my-apache NodePort 10.254.95.131 <none> 80:30002/TCP 12s service/my-nginx NodePort 10.254.92.19 <none> 80:30001/TCP 11s
# curl -v http://192.168.2.100 -H 'host: test.apache.ingress' * About to connect() to 192.168.2.100 port 80 (#0) * Trying 192.168.2.100... * Connected to 192.168.2.100 (192.168.2.100) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Accept: */* > host: test.apache.ingress > < HTTP/1.1 200 OK < Server: nginx/1.15.8 < Date: Fri, 25 Jan 2019 08:24:37 GMT < Content-Type: text/html < Content-Length: 45 < Connection: keep-alive < Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT < ETag: "2d-432a5e4a73a80" < Accept-Ranges: bytes < <html><body><h1>It works!</h1></body></html> * Connection #0 to host 192.168.2.100 left intact
# curl -v http://192.168.2.100 -H 'host: test.nginx.ingress' * About to connect() to 192.168.2.100 port 80 (#0) * Trying 192.168.2.100... * Connected to 192.168.2.100 (192.168.2.100) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Accept: */* > host: test.nginx.ingress > < HTTP/1.1 200 OK < Server: nginx/1.15.8 < Date: Fri, 25 Jan 2019 08:24:53 GMT < Content-Type: text/html < Content-Length: 612 < Connection: keep-alive < Vary: Accept-Encoding < Last-Modified: Tue, 25 Dec 2018 09:56:47 GMT < ETag: "5c21fedf-264" < Accept-Ranges: bytes < <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> * Connection #0 to host 192.168.2.100 left intact
# cat > monitoring/prometheus-grafana-ingress.yaml << EOF apiVersion: extensions/v1beta1 kind: Ingress metadata: name: prometheus-grafana-ingress namespace: ingress-nginx spec: rules: - host: grafana.k8s.ing http: paths: - path: / backend: serviceName: grafana servicePort: 3000 - host: prometheus.k8s.ing http: paths: - path: / backend: serviceName: prometheus-server servicePort: 9090 EOF
# ls monitoring/ configuration.yaml grafana.yaml prometheus-grafana-ingress.yaml prometheus.yaml
# kubectl apply -f monitoring/
# kubectl get pod,svc,ingress -n ingress-nginx NAME READY STATUS RESTARTS AGE pod/grafana-5ccff7668d-7lk6q 1/1 Running 0 2d14h pod/prometheus-server-7f87788f6-7zcfx 1/1 Running 0 2d14h NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/glusterfs-dynamic-pvc-grafana ClusterIP 10.254.146.102 <none> 1/TCP 2d14h service/glusterfs-dynamic-pvc-prometheus ClusterIP 10.254.160.58 <none> 1/TCP 2d14h service/grafana NodePort 10.254.244.77 <none> 3000:30303/TCP 2d14h service/prometheus-server NodePort 10.254.168.143 <none> 9090:32090/TCP 2d14h NAME HOSTS ADDRESS PORTS AGE ingress.extensions/prometheus-grafana-ingress grafana.k8s.ing,prometheus.k8s.ing 80 2d14h