1 introduction

Visit pumpkin Talk www.pkslow.com for more exciting articles!

Ingress is a very important Controller of Kubernetes. It is like a routing and forwarding component, which allows external access to Kubernetes internal services. In addition to the Ingress, there are NodePort, LoadBalance, etc. The Ingress is exposed to the outside world in many ways.

2 install the Ingress

It will be more convenient for us to install by HELM. First, update helm’s warehouse.

$ helm repo update
Copy the code

Helm: Kubernetes application deployment with Helm, support multi-environment deployment and version rollback

Container technology related articles

After the update, find out what packages the warehouse has for Ingress:

$$Helm Search Repo Ingress NAME CHART VERSION APP VERSION DESCRIPTION Azure/GCE-Ingress 1.2.0 1.4.0 A GCE Ingress The Controller azure/ingressmonitorcontroller 1.0.48 1.0.47 ingressmonitorcontroller chart that runs on kub... Azure /nginx-ingress 1.41.2v0.34.1 An Nginx ingress controller that uses ConfigMap... Stable /nginx-ingress 0.9.5 0.10.2 An nginx ingress controller that uses ConfigMap... Azure/Contour 0.2.0 v0.15.0forConfigure External DNS Servers (AWS Route53, Go... Azure/Kong 0.36.7 1.4 DEPRECATED The Cloud-native Ingress and API-man... Azure/LAMP 1.1.3 7 Modular and Transparent Lamp Stack chart Suppor... Azure/nginx - lego 0.3.1 ChartforNginx ingress - controller and kube lego azure/traefik 1.87.2 1.7.24 A traefik -based Kubernetes ingress controller w... Azure/Voyager 3.2.4 6.0.0 DEPRECATED Voyager by AppsCode - Secure Ingress... Stable/external-DNS 0.4.9 0.4.8 Configure external DNS Servers (AWS Route53, Go... Stable/LAMP 0.1.4 Modular and Transparent lamp stack chart suppor... Stable/nginx - lego 0.3.1 ChartforNginx-ingress-controller based Kubernetes Ingress Controller W nginx-Ingress-Controller based Kubernetes Ingress CONTROLLER W Stable/Voyager 3.1.0 6.0.0-rC. 0 Voyager by AppsCode - Secure Ingress Controller...Copy the code

Choose Azure /nginx-ingress to install. Note that versions are available. The installation is as follows:

$ helm install pkslow-ingress azure/nginx-ingress
Copy the code

After successful installation, the console will output relevant instructions. One caveat, though, is to check the Kubernetes Dashboard to see if the installation was successful. I’ve had installation failures, all due to failed image downloads. The solution is to open the global proxy and manually download the image. Such as:

Us. GCR. IO/k8s - artifacts - prod/ingress - nginx/controller: v0.34.1 k8s. GCR. IO/defaultbackend - amd64:1.5 Jettech/kube - webhook - certgen: v1.0.0 quay. IO/kubernetes - ingress - controller/nginx - ingress - controller: 0.32.0Copy the code

The installation was successful, and the Pods were running. The related Deployment is:

3 use the Ingress

3.1 Accessing a Service

Here’s the simplest example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: Nginx: 1.19.0
          ports:
            - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx-service
spec:
  ports:
    - port: 80
      name: nginx-service
      protocol: TCP
      targetPort: 80
  selector:
    app: nginx
  type: ClusterIP

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: nginx-service
              servicePort: 80
      host: localhost
Copy the code

After this configuration, when we visit http://localhost/, our request will be forwarded to port 80 of the nginx-service. As follows:

3.2 Accessing Multiple Services

When accessing multiple services, things get complicated. To access multiple services, you can configure them by matching URL paths and forwarding them through subdomain names.

3.2.1 Subdomain mode

Configure subdomain name forwarding as follows:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: nginx-service
              servicePort: 80
      host: nginx.localhost
    - http:
        paths:
          - path: /
            backend:
              serviceName: springboot-service
              servicePort: 8080
      host: springboot.localhost
Copy the code

To save space, only the Ingress configuration is shown here.

Visit http://nginx.localhost/ below:

Visit http://springboot.localhost/swagger-ui.html is as follows, please notice the URL with the subpaths swagger – UI. HTML:

3.2.2 URL Path Matching Mode

Is that the case with URL path matching?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - http:
        paths:
          - path: /nginx
            backend:
              serviceName: nginx-service
              servicePort: 80
      host: localhost
    - http:
        paths:
          - path: /springboot
            backend:
              serviceName: springboot-service
              servicePort: 8080
      host: localhost
Copy the code

When configured this way, 404 is reported directly, but not for Ingress, but for Nginx or Springboot. The request has been successfully forwarded to the corresponding service, but the path is faulty. The reason is that when configured this way, Ingress forwards path to the service as well. So the actual effect is as follows:

localhost/nginx      --> nginx-service/nginx
localhost/springboot --> springboot-service/springboot
Copy the code

So the Web Context path of the service must match the configured path. For example, the base path of nginx-service should be changed to /nginx instead of /.

If you want to keep the service Web Context path is /, then need to configure the rewrite rules, such as nginx. Ingress. Kubernetes. IO/rewrite – target: / $1.

4 summarizes

After a time, the pit is really many. Using subdomains feels like a better approach. In addition, Ingress also has a pit, which is to implement HTTP/HTTPS forwarding, but TCP does not work, for example, I installed a MySQL database in Kubernetes, need to expose the address and 3306 to the outside in TCP way, it is more difficult, we will discuss later.


Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…

Read more and share more; Write more. Organize more.