K8s的ingress-nginx

原理:ingress contronler通过与k8s的api进行交互,动态的去感知k8s集群中ingress服务规则的变化,然后读取它,并按照定义的ingress规则,转发到k8s集群中对应的service,而这个ingress规则写明了哪个域名对应k8s集群中的哪个service,然后再根据ingress-controller中的nginx配置模板,生成一段对应的nginx配置,然后再把该配置动态的写到ingress-controller的pod里,该ingress-controller的pod里面运行着一个nginx服务,控制器会把生成的nginx配置写入到nginx的配置文件中,然后reload一下,使其配置生效。以此来达到域名分配置及动态更新的效果

图解:

ingress.png

github地址:https://github.com/kubernetes/ingress-nginx

#下载
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml

根据版本下载对应的yaml

Ingress-NGINX version

k8s supported version

Alpine Version

Nginx Version

Helm Chart Version

v1.8.1

1.27,1.26, 1.25, 1.24

3.18.2

1.21.6

4.7.*

v1.8.0

1.27,1.26, 1.25, 1.24

3.18.0

1.21.6

4.7.*

v1.7.1

1.27,1.26, 1.25, 1.24

3.17.2

1.21.6

4.6.*

v1.7.0

1.26, 1.25, 1.24

3.17.2

1.21.6

4.6.*

...

...

...

...

...

v1.3.0

1.24, 1.23, 1.22, 1.21, 1.20

3.16.0

1.19.10†

4.2.3

#下载对应版本后更改yaml文件
#在Deployment中
........
  template:
    metadata:
      labels:
        app.kubernetes.io/component: controller
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
    spec:
      hostNetwork: true           #添加
      containers:
      - args:
        - /nginx-ingress-controller
        - --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
        - --election-id=ingress-controller-leader
        - --controller-class=k8s.io/ingress-nginx
        - --ingress-class=nginx
......
        ports:
        - containerPort: 80
          hostPort: 80            #添加
          name: http
          protocol: TCP
        - containerPort: 443
          hostPort: 443           ##添加
          name: https
          protocol: TCP
        - containerPort: 8443
          name: webhook
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
#修改镜像
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.3.0         #改为这个地址的对应版本
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1    #改为这个地址的对应版本

例:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-web
  namespace: work
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.18
          ports:
            - containerPort: 80
          volumeMounts:
            - name: work
              mountPath: /work
            - name: nginx-conf
              mountPath: /etc/nginx/conf.d
          livenessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 15
          readinessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 10
            periodSeconds: 15
      volumes:
        - name: work
          persistentVolumeClaim:
            claimName: nginx-pvc
        - name: nginx-conf
          configMap:
            name: nginx-conf
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: work
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      name: http
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
  namespace: work
spec:
  ingressClassName: nginx
  rules:
  - host: test.ingress.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
#地址重写
举例:原来的访问路径是 /ups/api/v1/user/login  现在需要在/ups前多加一层ups也就是/ups/ups/api/v1/user/login

apiVersion: v1
kind: Service
metadata:
  name: ups-center-service
  namespace: work
spec:
  selector:
    app: ups-center
  ports:
    - protocol: TCP
      port: 7207
      targetPort: 7207
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2    #改为$2
  name: ups
  namespace: work
spec:
  ingressClassName: nginx
  rules:
  - host: test.ingress.com
    http:
      paths:
      - path: /ups(/|$)(.*)    #地址重写
        pathType: Prefix
        backend:
          service:
            name: ups-center-service
            port:
              number: 7207