AKS集群中通过Helm安装配置Istio出口网关的技术咨询
你好!我来帮你梳理下AKS里用Helm部署和配置Istio出口网关的步骤,其实这个过程并没有想象中复杂,咱们一步步来:
一、用Helm部署Istio出口网关
首先明确:Istio并没有单独的出口网关Helm Chart,你完全可以复用已经用过的istio/gateway Chart来部署出口网关,只需要在安装时指定差异化的配置参数,让Istio识别这是一个出口网关实例。
执行以下Helm命令即可部署:
helm install istio-egressgateway istio/gateway \ --namespace istio-system \ --set "labels.app=istio-egressgateway" \ --set "labels.istio=egressgateway" \ --set service.type=ClusterIP \ --set autoscaleEnabled=false
参数说明:
- 指定和Istio其他组件(base、istiod)相同的命名空间
istio-system,方便统一管理 - 给网关设置独特的标签,后续配置规则时能精准识别这个出口网关实例
- 服务类型设为
ClusterIP,因为出口网关只需要在集群内部接收流量,不需要对外暴露 - 关闭自动扩缩容(
autoscaleEnabled=false),如果你的业务需要弹性伸缩可以去掉这个参数
二、配置集群流量通过出口网关并限制访问
要实现所有集群流量走出口网关+限制特定主机访问,需要配合几个Istio自定义资源来完成:
1. 定义允许访问的外部服务(ServiceEntry)
先通过ServiceEntry声明集群内可以访问的外部主机,补全你给出的Dropbox示例如下:
apiVersion: networking.istio.io/v1 kind: ServiceEntry metadata: name: external-svc-https namespace: testing spec: hosts: - api.dropboxapi.com - content.dropboxapi.com ports: - number: 443 name: https protocol: HTTPS resolution: DNS location: MESH_EXTERNAL
这个资源会告诉Istio:这两个主机是允许访问的外部服务,用DNS解析地址,端口是443。
2. 配置目标规则指向出口网关(DestinationRule)
创建DestinationRule,把要转发的流量导向刚才部署的出口网关:
apiVersion: networking.istio.io/v1 kind: DestinationRule metadata: name: egressgateway-for-dropbox namespace: istio-system spec: host: istio-egressgateway.istio-system.svc.cluster.local subsets: - name: dropbox
这里的host是出口网关的集群内部服务地址,定义的dropbox子集方便后续路由规则引用。
3. 配置虚拟服务路由流量(VirtualService)
最后用VirtualService把集群内的流量先转发到出口网关,再由出口网关转发到外部服务:
apiVersion: networking.istio.io/v1 kind: VirtualService metadata: name: direct-dropbox-through-egress namespace: testing spec: hosts: - api.dropboxapi.com - content.dropboxapi.com gateways: - mesh # 代表集群内所有Sidecar发出的流量 - istio-egressgateway # 指向我们部署的出口网关 tls: - match: - gateways: - mesh port: 443 sniHosts: - api.dropboxapi.com - content.dropboxapi.com route: - destination: host: istio-egressgateway.istio-system.svc.cluster.local subset: dropbox port: number: 443 weight: 100 - match: - gateways: - istio-egressgateway port: 443 sniHosts: - api.dropboxapi.com - content.dropboxapi.com route: - destination: host: api.dropboxapi.com port: number: 443 weight: 50 - destination: host: content.dropboxapi.com port: number: 443 weight: 50
这个配置分两段:
- 第一段:把集群内Sidecar发往Dropbox的流量全部转发到出口网关
- 第二段:让出口网关把收到的流量分别转发到对应的Dropbox服务主机
4. 全局强制所有出站流量走出口网关(可选)
如果希望整个集群的所有外部流量都必须走出口网关,且只能访问你在ServiceEntry里定义的主机,可以修改Istio的全局流量策略:
helm upgrade istiod istio/istiod \ --namespace istio-system \ --set meshConfig.outboundTrafficPolicy.mode=REGISTRY_ONLY
REGISTRY_ONLY模式下,Istio会拒绝所有未在ServiceEntry中声明的外部服务访问请求,完美实现你“限制特定主机访问”的需求。
三、验证配置是否生效
你可以在testing命名空间的任意Pod中执行curl命令测试访问:
curl -v https://api.dropboxapi.com/2/users/get_current_account
然后用Istio的命令查看流量路由是否正确:
istioctl pc routes <你的Pod名称>.testing --direction outbound
如果输出中能看到指向istio-egressgateway的路由规则,就说明配置生效了。
备注:内容来源于stack exchange,提问作者Sotiris Sotiriou




