Ingress 公开了从集群外部到集群内 services 的HTTP和HTTPS路由。 流量路由由 Ingress 资源上定义的规则控制。

    internet
          |
   [ Ingress ]
   --|-----|--
   [ Services ]

参考网站:https://kubernetes.io/zh/docs/concepts/services-networking/ingress/

2.部署ingress-nginx

部署参考:https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md
如果镜像拉不下来换成 阿里云的源即可
ingress-nginx-controller 替换成 阿里云的源:registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller

3. 部署Service

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
spec:
  externalIPs:
  - 192.168.0.200
  - 192.168.0.201 
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP
  - name: https
    port: 443
    targetPort: 443
    protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx

selector不要写错了,这里的selector 针对v0.28.0 yaml 中的deployment,为了方便先用 externalIPs 来访问。

4. 部署一个ingress,用户访问

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /home/$2
spec:
  rules:
  - http:
      paths:
      - path: /java(/|$)(.*)
        backend:
          serviceName: spring-boot-demo-svc
          servicePort: 8080

访问 192.168.0.200/java/index 即可实现访问 spring-boot-demo-svc:8080/home/index
假如我现在有三个胖服务:

  • service1 -> service1:8080/api/xxx
  • service2 ->service2:8080/api/xxx
  • service3 ->service3:8080/api/xxx
    那么我的ingress即可配置为
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /api/$2
spec:
  rules:
  - http:
      paths:
      - path: /service1(/|$)(.*)
        backend:
          serviceName: service1
          servicePort: 80

service2、3同理,
访问即为:192.168.0.200/service1/values -> service1/api/values