记一次k8s的ingress配置问题
记一次k8s的ingress配置问题
我的环境是k8s 1.16 版本,然后根据官方文档的说明,域名分列是这样的效果:
foo.bar.com -> 178.91.123.132 -> / foo    service1:4200
                                 / bar    service2:8080
官方的文档配置是这样的:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /foo
        backend:
          serviceName: service1
          servicePort: 4200
      - path: /bar
        backend:
          serviceName: service2
          servicePort: 8080
然后 kubectl apply -f 使配置生效。
很遗憾,经过我1天半的反复测试,这个例子是错误的!!!!!
按照官网的这个方式去配置域名分列,是无法达到预期的效果的!!!!!
以下是k8s官网例子的链接:
正确的域名分列配置
正确的域名分列配置:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - backend:
          serviceName: service1
          servicePort: 4200
        path: /foo(/|$)(.*)
      - backend:
          serviceName: service2
          servicePort: 8080
        path: /bar(/|$)(.*)  
以上配置进入访问时最终将转发解析到foo.bar.com下:
foo.bar.com/foorewrites tofoo.bar.com/foo.bar.com/bar/rewrites tofoo.bar.com/foo.bar.com/foo/newrewrites tofoo.bar.com/new
以上才是域名分列的正解!!!!!

