Author: LemonNan

Kubernetes series

  • Kubernetes Series (1) Pod
  • Kubernetes series (2) Service
  • Kubernetes Series (III) Deployment
  • Kubernetes series (4) StatefulSet

Ingress

In the Service section, load balancing based on layer 4 network is introduced. Layer 4 network forwarding is not flexible enough to carry out routing and forwarding according to the information related to the interface path. So, today, layer 7 routing and forwarding based on Ingress is introduced.


introduce

Ingress is a way of exposing services in K8s, and the Ingress Controller and Ingress (load balancing rules) are required for the exposed services to be available externally. Requests are forwarded to the corresponding backend service if they meet the Ingress configuration rules, or to the default backend of the Ingress Controller if there is no matching rule or the corresponding backend service does not exist.


Fundamentals of operation

Recent articles are based on the operation of minikube, so from time to time we need to enter minikube for some operation verification, so the operation of entering minikube is recorded here.

#Minikube default username: docker password: tcuser, my address is 192.168.99.100SSH [email protected]Copy the code


Path type of the Ingress

  • ImplementationSpecific (default): For this type, matching depends on IngressClass. Concrete implementations can treat this as a separatepathTypeTo deal with or deal with.PrefixExactDo the same for type.
  • Exact: Accurately matches URL paths and is case-sensitive.
  • Prefix: based on the in/The delimited URL path prefix matches. Matching is case sensitive and is done element by element in the path. The path element is defined by/A list of labels in a path separated by a delimiter. If eachpAll request pathspElement prefix, request and pathpMatching.


use

Start the Ingress Controller

Since you are in a minikube environment, you only need to execute the following command to start an Ingress Controller.

#Start the Ingress controller
minikube addons enable ingress
#To view
kubectl get pods -n kube-system
Copy the code

When you view it, you’ll get output like the following, the penultimate being the Ingress Controller (when it was first turned on, god knows how long I waited to turn it on)

NAME READY STATUS RESTARTS AGE coredns-d5947d4b-fsjhr 1/1 Running 57 65d coredns-d5947d4b-kqdfl 1/1 Running 57 65d default-http-backend-68449d5b4-wfxgl 1/1 Running 2 15h etcd-minikube 1/1 Running 31 65d kube-addon-manager-minikube 1/1 Running 32 65d kube-apiserver-minikube 1/1 Running 31 65d kube-controller-manager-minikube 1/1 Running 29 58d kube-proxy-z4qhh 1/1 Running 31 65d kube-scheduler-minikube 1/1 Running 32 65d kubernetes-dashboard-6bb54dcddd-c8ksr 1/1  Running 55 65d nginx-ingress-controller-586cdc477c-txz8q 1/1 Running 1 15h storage-provisioner 1/1 Running 57 65dCopy the code


Configure an Ingress rule

# Ingress.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
spec:
  rules:
  - http:
      paths:
      - path: /nginx    Interfaces starting with # nginx are routed to the corresponding POD under SVC-nginx
        backend:
          serviceName: svc-nginx
          servicePort: 80
      - path: /tomcat   Interfaces starting with tomcat are routed to the corresponding POD under SVC - Tomcat
        backend:
          serviceName: svc-tomcat
          servicePort: 80
Copy the code


Create a Service

nginx

Create nginx Pods and Services

# nginx.yaml
apiVersion: v1          
kind: Pod               Create a Pod type
metadata:               # Metadata, some basic information
  name: pod-nginx       # the name
  labels:
    name: label-pod-nginx # label
spec:
  containers:
  - name: pod-nginx     # container name
    image: nginx:latest # mirror used
    ports:
    - containerPort: 80 The port number monitored by the container
---
apiVersion: v1
kind: Service
metadata:
  name: svc-nginx
  namespace: default      # namespace. Default is default
  labels:
    name: label-svc-nginx # service tag
  annotations:
    name: anno-nginx
spec:
  selector: 
    name : label-pod-nginx  # Manage pods with nginx tags
  type: ClusterIP   ClusterIP = ClusterIP = ClusterIP = ClusterIP = ClusterIP = ClusterIP = ClusterIP = ClusterIP
  # clusterIP: 172.18.0.1 # Virtual IP address, type = clusterIP
                        This must be specified when # type=LoadBalancer
  sessionAffinity:  ClientIP # Whether to support session. Default is null. ClientIP is optional, which requests the same client (depending on the ClientIP) to a Pod
  ports:
  - name: p80       # port name
    protocol: TCP   # agreement
    port: 80      Service Specifies the exposed port
    targetPort: 80  # Back-end Pod port
Copy the code


tomcat

Create the Tomcat Pod and Service. Note that the default tomcat port is 8080

apiVersion: v1          
kind: Pod               Create a Pod type
metadata:               # Metadata, some basic information
  name: pod-tomcat       # the name
  labels:
    name: label-pod-tomcat # label
spec:
  containers:
  - name: pod-tomcat     # container name
    image: tomcat:latest # mirror used
    ports:
    - containerPort: 80 The port number monitored by the container
---
apiVersion: v1
kind: Service
metadata:
  name: svc-tomcat
  namespace: default      # namespace. Default is default
  labels:
    name: label-svc-tomcat # service tag
  annotations:
    name: anno-tomcat
spec:
  selector: 
    name : label-pod-tomcat  Manage pods with tomcat tags
  type: ClusterIP   ClusterIP = ClusterIP = ClusterIP = ClusterIP = ClusterIP = ClusterIP = ClusterIP = ClusterIP
  # clusterIP: 172.18.0.1 # Virtual IP address, type = clusterIP
                        This must be specified when # type=LoadBalancer
  sessionAffinity:  ClientIP # Whether to support session. Default is null. ClientIP is optional, which requests the same client (depending on the ClientIP) to a Pod
  ports:
  - name: p80       # port name
    protocol: TCP   # agreement
    port: 80      Service Specifies the exposed port
    targetPort: 8080  Tomcat defaults to port 8080
Copy the code


Access the service

curl

After successful creation, access to the service is started through the Ingress Controller.

Curl curl curl curl curl curl curl curl

-l is used to make the Ingress Controller visit again after returning the redirected Http status code. The 308 status code indicates that the target resource has been permanently moved to a new URI, and any future references to the resource should use the new URI, and the original Http Method(such as Post) cannot be modified.

-k indicates that the Https certificate is not verified



With no arguments, the following output appears, yielding a 308 status code and no redirection

#The curl 192.168.99.100 / tomcat/xx<html> <head><title>308 Permanent Redirect</title></head> <body> <center><h1>308 Permanent Redirect</h1></center> < hr > < center > nginx / 1.15.9 < / center > < / body > < / HTML >Copy the code


The browser

Both of these are 404 because there is no service provided under the corresponding path.

nginx


tomcat


Routing by requested domain name

Next, start modifying the configuration file so that you can route to different back-end services based on the requested domain name.

Instead of routing according to the interface path under the same domain name, this time routing is based on the domain name in the request header. Different domain names are routed to different back-end services.

First you need to add two lines to the hosts file:

192.168.99.100 tomcat.whn.com 192.168.99.100 nginx.whn.com

Here the Ingress name is the same as before, so we need to delete the previous Ingress first.

kubectl delete ingress test-ingress


Create a new ingress

# Ingress-host.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
spec:
  rules:
  - host: nginx.whn.com	The address of the browser
    http:
      paths:
      - path: 
        backend:
          serviceName: svc-nginx
          servicePort: 80
  - host: tomcat.whn.com 	The address of the browser
    http:
      paths:
      - path: /
        backend:
          serviceName: svc-tomcat
          servicePort: 80
Copy the code

Next, pass through the browsernginx.whn.comtomcat.whn.comAccess the back-end Nginx service and tomcat service respectively


Visit nginx



Visit the tomcat

Tomcat, latest is Tomcat9, the default is empty under webapps, at that time, I also confirmed that there is no configuration error, later found that there is no content under Webapps, content under another webapps.dist, Just move webapps.dist to Webapps or rename the folder and visit tomcat.whn.com in your browser to access the Tomcat home page.

The last

At this point, the Ingress function that I want to introduce is over. I haven’t decided on the following content yet.

Few words said

Have fun on National Day! !