1. 首页 >  移动应用开发 >  Nginx Ingress

Nginx Ingress

Installation

Use the compatible version with your Kubernetes cluster, otherwise, you may get some unexpected exception or error.

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.3/deploy/static/provider/baremetal/deploy.yaml

Make sure pod ingress-nginx-controller-xxxx is running, otherwise using ‘kubectl -n ingress-nginx get ingressclasses’ and check the ‘Event’ part.

This YAML will create a new namespace ‘ingress-nginx’ for Nginx, and an Ingress Class object ‘nginx’ will be created too by default, you could check from Kubernetes dashboard or the CLI below

kubectl get ingressclasses -n ingress-nginx

If this is only instance of the Ingresss-NGINX controller, you should add the annotation ingressclass.kubernetes.io/is-default-class in your ingress class:

kubectl -n ingress-nginx annotate ingressclasses nginx ingressclass.kubernetes.io/is-default-class="true"

Verification

kubectl get services -n ingress-nginx

Get the of ingress-nginx-controller

Get node with the following command

kubectl get nodes -o wide

Access http://: in the browser, and you will get “404 Not Found” from nginx, no worry, that is because no backend service configured yet. If you are using docker desktop kubenetes, the will be localhost.

Now we deploy a nginx service with the following YAML to test the ingress controller,

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.18.0
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example.com
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: nginx.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: nginx
                port: 
                  number: 80

Use the following command to check the availability.

curl --location 'http://<external IP>:<ingress-nginx-node-port>' --header 'Host: nginx.example.com'

If you aren’t familiar with curl, you can use Postmen, it; ’s more straightforward. If you want to verify in a browser, for example, chrome, then you need to use some plugin like ModHeader to provide the header parameter.

/yi-dong-ying-yong-kai-fa/nginx-ingress-13088.html