插一嘴, nginx-ingress的使用

  • Ingress可以理解为Nginx中的nginx.conf配置文件
  • Ingress Controller可以理解为一个监听器,实时监听kube-apiserver,感知后端svc,Pod的变化
  • Ingress资源对象只是一个描述文件,真正工作的是Ingress控制器。
  • Ingress控制器进行Ingress对象资源的解析
  • Ingress通常使用Annotations来配置一些选项,不同的Ingress Controller支持不同的注解
  • Ingress的apiVersion在1.18前后版有差异

控制器的种类包括

  • traefik
  • ApiSix
  • Caddy
  • Haproxy Ingress Controller
  • Kong
  • ingress-nginx
以ingress-nginx作为Ingress控制器讲
ingress-nginx的全局的配置
kubectl get cm -n ingress-nginx ingress-nginx-controller -oyaml
# all 所有的资源大类名称
# --all 每类资源中包含的所有
# ingress 不会删除
k delete  all --all -n csdn-test
kubectl create deployment my-httpd --image=httpd:2.4  --replicas=3 -n csdn-test  --port=80
kubectl create deployment my-nginx --image=nginx      --replicas=3 -n csdn-test --port=80
k expose deploy/my-httpd --name=svc-http --type=ClusterIP --port=81 --target-port=80 -n csdn-test
k expose deploy/my-nginx --name=svc-nginx --type=ClusterIP --port=82 --target-port=80 -n csdn-test
kubectl create deployment svc-a --image=mendhak/http-https-echo:31 --port 8888
kubectl create deployment svc-b --image=mendhak/http-https-echo:31 --port 8888
# 写错了,但是好像暴露出不对的端口也是可以访问的
kubectl expose deployment/svc-a --port=911 --target-port=8080
kubectl expose deployment/svc-b --port=119 --target-port=8080

官网上搜到的相应的,但是需要更改

k get ingressClass # nginx
https://www.google.com/search?q=nip.io+使用
# echo "127.0.0.1 192.169.240.231.nip.io" >> /etc/hosts
# 加入上面的那条解析是因为192.168.240.231.nip.io 这个的需要,
# 不加上,192.168.240.231.nip.io 这个无法访问
cat << eof | k apply -f - -n csdn-test
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: i1
spec:
  ingressClassName: nginx
  rules:
  - host: nginx.192.168.240.231.nip.io
    http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: svc-nginx
            port:
              number: 82
  - host: http.192.168.240.231.nip.io
    http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: svc-http
            port:
              number: 81
eof
curl nginx.192.168.240.231.nip.io
curl http.192.168.240.231.nip.io
cat << eof | k apply -f - -n csdn-test
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: i2
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: woc.192.168.240.231.nip.io
    http:
      paths:
      - pathType: Prefix
        path: /ng
        backend:
          service:
            name: svc-nginx
            port:
              number: 82
      - pathType: Prefix
        path: /ht
        backend:
          service:
            name: svc-http
            port:
              number: 81
eof
curl woc.192.168.240.231.nip.io/ng
curl woc.192.168.240.231.nip.io/ht
kubectl -n csdn-test apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: i3
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  namespace: csdn-test
spec:
  ingressClassName: nginx
  rules:
  - host: woc.192.168.240.231.nip.io
    http:
      paths:
      - backend:
          service:
            name: svc-a
            port:
              number: 911
        path: /t1
        pathType: Prefix
      - backend:
          service:
            name: svc-b
            port:
              number: 119
        path: /t2
        pathType: Prefix
EOF
curl woc.192.168.240.231.nip.io/t1
curl woc.192.168.240.231.nip.io/t2
kubectl -n csdn-test apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: homs-ingress
  annotations:
    kubernetes.io/ingress.class: nginx  
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  ingressClassName: nginx
  rules:
  - host: woc.192.168.240.231.nip.io
    http:
      paths:
      - path: /service1/?(.*)
        pathType: Prefix
        backend:
          service:
            name: svc-a
            port:
              number: 911
      - path: /service2/?(.*)
        pathType: Prefix
        backend:
          service:
            name: svc-b
            port:
              number: 119
EOF
curl woc.192.168.240.231.nip.io/service2/
curl woc.192.168.240.231.nip.io/service1/

观察上述输出,发现转发后的location被重定向了

我们可以修改ingress配置,在路径上添加一段分组匹配,如下:

kubectl -n csdn-test apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: i4
  annotations:
    kubernetes.io/ingress.class: nginx  
    nginx.ingress.kubernetes.io/rewrite-target: /$1$2
    nginx.ingress.kubernetes.io/app-root: /service3
spec:
  ingressClassName: nginx
  rules:
  - host: woc.192.168.240.231.nip.io
    http:
      paths:
      - path: /service3/?(.*)
        pathType: Prefix
        backend:
          service:
            name: svc-a
            port:
              number: 911
      - path: /service4/?(.*)
        pathType: Prefix
        backend:
          service:
            name: svc-b
            port:
              number: 119
EOF
curl http://woc.192.168.240.231.nip.io/service3/
curl woc.192.168.240.231.nip.io/service4/

kubectl -n csdn-test apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx  
    nginx.ingress.kubernetes.io/rewrite-target: /$1$2
    nginx.ingress.kubernetes.io/app-root: /service1
  name: nginx-test-ingress
  namespace: csdn-test
spec:
  ingressClassName: nginx
  rules:
  - host: nginx-test.k8s.com
    http:
      paths:
      - backend:
          service:
            name: svc-nginx
            port:
              number: 82
        path: /
        pathType: Prefix
      - backend:
          service:
            name: svc-http
            port:
              number: 81
        path: /test02
        pathType: Prefix
EOF
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐