Ingress 是管理从集群外部访问集群内部服务的入口规则(HTTP 和 HTTPS)的 API 对象,核心功能是将外部到达集群的 HTTP/HTTPS 请求,根据主机名(host)和路径(path)转发到不同的后端 Service,一起来看看如何使用吧!
1、概念
1.1、Ingress(资源)
- 在 K8s 中创建的 YAML 文件,只是一份规则声明,定义 “当访问 a.example.com 时,将流量转发给 service-a” 之类的规则
1.2、Ingress Controller(控制器)
- 这是规则的实际执行者,是一个独立的 Pod,负责监听集群中的 Ingress 资源变化,并根据规则配置底层的负载均衡器(如 Nginx、Envoy、Traefik 等)
- 常见的 Ingress Controller 有Nginx Ingress Controller、Traefik、HAProxy Ingress、Istio Ingress Gateway 等
- 必须先部署一个 Ingress Controller,创建的 Ingress 才能生效
2、示例
- 下方使用 Nginx Ingress
- 部署参考:https://blog.csdn.net/mm1234556/article/details/147892844
**2.1、**基于主机名路由
- 需求: 通过不同的主机名访问不同的服务
- frontend.local -> 前端,api.local -> 后端
- 部署:kubectl apply -f ingress-simple-host.yaml
- 通过 http://frontend.local 和 http://api.local 访问不同的服务
1# ingress-simple-host.yaml 2apiVersion: networking.k8s.io/v1 3kind: Ingress 4metadata: 5 name: simple-host-ingress 6 annotations: 7 kubernetes.io/ingress.class: "nginx" 8spec: 9 rules: 10 - host: frontend.local 11 http: 12 paths: 13 - path: / 14 pathType: Prefix 15 backend: 16 service: 17 name: frontend-service 18 port: 19 number: 80 20 - host: api.local 21 http: 22 paths: 23 - path: / 24 pathType: Prefix 25 backend: 26 service: 27 name: backend-service 28 port: 29 number: 8080
2.2、基于路径路由
- 需求: 使用同一个域名,根据 URL 路径将流量分发到不同的微服务
- myapp.com/web -> 用户UI,myapp.com/api -> API网关,myapp.com/static -> 静态文件
- 部署:kubectl apply -f ingress-path-based.yaml
1# ingress-path-based.yaml 2apiVersion: networking.k8s.io/v1 3kind: Ingress 4metadata: 5 name: path-based-ingress 6 annotations: 7 kubernetes.io/ingress.class: "nginx" 8 # 若需路径重写,可取消下方注释 9 # nginx.ingress.kubernetes.io/rewrite-target: / 10spec: 11 rules: 12 - host: myapp.com 13 http: 14 paths: 15 - path: /web 16 pathType: Prefix 17 backend: 18 service: 19 name: web-ui-service 20 port: 21 number: 3000 22 - path: /api 23 pathType: Prefix 24 backend: 25 service: 26 name: api-gateway-service 27 port: 28 number: 8080 29 - path: /static 30 pathType: Prefix 31 backend: 32 service: 33 name: static-file-service 34 port: 35 number: 80
2.3、基于主机名路由 TLS
- 需求: 为 myapp.example.com 提供 HTTPS 服务,将所有流量转发到 my-web-service
- 部署:kubectl apply -f ingress-tls-simple.yaml
- 将域名 myapp.example.com 解析到 Ingress Controller 服务的外部 IP(若是云厂商的 LoadBalancer,会自动分配一个外部 IP)
- 访问 https://myapp.example.com,浏览器会显示安全的 HTTPS 连接
1# ingress-tls-simple.yaml 2apiVersion: networking.k8s.io/v1 3kind: Ingress 4metadata: 5 name: simple-tls-ingress 6 annotations: 7 kubernetes.io/ingress.class: "nginx" 8spec: 9 tls: 10 - hosts: 11 - myapp.example.com # 指定需要使用 TLS 的域名 12 secretName: my-tls-secret # 指定存储证书的 Secret 13 rules: 14 - host: myapp.example.com # 基于主机名的路由规则 15 http: 16 paths: 17 - path: / 18 pathType: Prefix 19 backend: 20 service: 21 name: my-web-service # 后端服务名 22 port: 23 number: 80 # 后端服务端口
2.4、基于路径路由 TLS
- 需求:使用 HTTPS,同一个域名下,根据 URL 路径将流量路由到不同的服务
- 访问 www.myapp.com/web 进入前端 Web 界面
- 访问 www.myapp.com/api 进入后端 API
1apiVersion: networking.k8s.io/v1 2kind: Ingress 3metadata: 4 name: path-based-ingress 5spec: 6 tls: 7 - hosts: 8 - www.myapp.com 9 secretName: myapp-tls-secret 10 rules: 11 - host: www.mycompany.com 12 http: 13 paths: 14 - path: /web 15 pathType: Prefix 16 backend: 17 service: 18 name: frontend-web-service 19 port: 20 number: 80 21 - path: /api 22 pathType: Prefix 23 backend: 24 service: 25 name: backend-api-service 26 port: 27 number: 8080
2.5、重定向 HTTP -> HTTPS
- 需求:用户访问 http://myapp.com 会被 301 重定向到 https://myapp.com
- 强制将所有 HTTP 流量重定向到 HTTPS,提升安全性,通过 Ingress Controller 的特定注解Annotation 实现
1apiVersion: networking.k8s.io/v1 2kind: Ingress 3metadata: 4 name: redirect-https-ingress 5 annotations: 6 kubernetes.io/ingress.class: "nginx" 7 nginx.ingress.kubernetes.io/ssl-redirect: "true" # 强制SSL重定向 8 nginx.ingress.kubernetes.io/force-ssl-redirect: "true" # 即使没有 TLS 规则也重定向 9spec: 10 tls: 11 - hosts: 12 - myapp.com 13 secretName: myapp-tls-secret 14 rules: 15 - host: myapp.com 16 http: 17 paths: 18 - path: / 19 pathType: Prefix 20 backend: 21 service: 22 name: myapp-service 23 port: 24 number: 80
《【Kubernetes】K8s 集群 Ingress 入口规则》 是转载文章,点击查看原文。
