Istio 网关配置:实现 HTTPS 流量接入 Kubernetes 内部服务

以下是配置 Istio 网关实现外部 HTTPS 流量接入 Kubernetes 内部服务的完整步骤:

核心概念
  1. Gateway:定义入口点,配置监听端口和 TLS 证书
  2. VirtualService:将网关流量路由到具体服务
  3. TLS 终止:网关处理 HTTPS 解密,内部服务使用 HTTP

配置步骤
1. 准备 TLS 证书(假设已有证书)
# 创建 Kubernetes Secret (命名空间需与网关一致)
kubectl create secret tls istio-tls-secret \
  --key key.pem \
  --cert cert.pem \
  -n istio-system

2. 配置 Gateway 资源
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: https-gateway
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway  # 使用默认网关组件
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    tls:
      mode: SIMPLE          # TLS 终止模式
      credentialName: istio-tls-secret  # 引用证书 Secret
    hosts:
    - "yourdomain.com"     # 实际域名

3. 配置 VirtualService 路由
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: web-service-route
  namespace: default        # 服务所在命名空间
spec:
  hosts:
  - "yourdomain.com"        # 必须匹配 Gateway 的 hosts
  gateways:
  - istio-system/https-gateway  # 关联网关
  http:
  - route:
    - destination:
        host: web-service   # Kubernetes 服务名
        port:
          number: 8080      # 服务端口


完整工作流程
  1. 外部用户访问 https://yourdomain.com
  2. Istio 网关处理 TLS 解密
  3. 网关将 HTTP 流量转发到 web-service:8080
  4. Kubernetes 服务接收明文请求

验证配置
# 检查网关状态
istioctl analyze -n istio-system

# 测试 HTTPS 访问
curl -v -k --resolve yourdomain.com:443:$GATEWAY_IP https://yourdomain.com

注意事项
  1. 证书必须与域名完全匹配
  2. Gateway 和 VirtualService 需在相同网格
  3. 生产环境建议使用自动证书管理(如 Cert-Manager)
  4. 如需端到端加密,需配置 TLS origination

关键参数说明

  • tls.mode: SIMPLE:标准 TLS 终止
  • credentialName:指向 Kubernetes TLS Secret
  • hosts:必须与证书 SAN 域名一致
  • gateways:格式为 <namespace>/<gateway-name>

此配置实现了安全的外部 HTTPS 流量接入,同时保持内部服务无需处理加密逻辑。

更多推荐