欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ingress rewrite 重定向

程序员文章站 2022-05-29 22:51:22
...

ingress rewrite 重定向

直接看 ingress.yaml 注解
1、运行2个nginx deploy 
kubectl apply -f nginx1.yaml -f nginx2.yaml
进入nginx改 /usr/share/nginx/html/index.html
kubectl exec -it nginx1-XXXX echo nginx1 >  /usr/share/nginx/html/index.html
kubectl exec -it nginx2-XXXX echo nginx2 >  /usr/share/nginx/html/index.html
2、 运行ingress 
kubectl apply -f ingress.yaml
3、 访问
www.test.com   -->  nginx1
www.test.com/ng2  --> nginx2 
nginx2 日志    "GET / HTTP/1.1" 200 26

ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
  name: test-nginxingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    # /$1 这里重定向到 "/"
    # /abc/$1  这表示重定向到服务的  /abc/
spec:
  rules:
    - host: www.test.com
      http:
        paths:
          - path: /
          #- path: /($/.*)
          # 所有/** 重定向到 /abc/**
            backend:
              serviceName: nginx1
              servicePort: 80
          - path: /ng2
            backend:
              serviceName: nginx2 
              servicePort: 80

nginx1.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx1
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  selector:
    k8s-app: nginx1
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx1
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: nginx1
  template:
    metadata:
      labels:
        k8s-app: nginx1
    spec:
      nodeName: k8s-node16
      containers:
      - image: nginx:1.17.0-alpine
        name: nginx1

nginx2.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx2
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  selector:
    k8s-app: nginx2
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx2
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: nginx2
  template:
    metadata:
      labels:
        k8s-app: nginx2
    spec:
      nodeName: k8s-node16
      containers:
      - image: nginx:1.17.0-alpine
        name: nginx2
相关标签: 虚拟化