今天再来分享个AKS相关的内容,主要是网络相关的,如果AKS cluster到internet的流量要经过firewall的话,在部署的时候是需要有一些特殊的选项的,而且目前portal上也不支持这种模式,需要用terraform或者cli之类的来部署,所以特地来分享下
首先来说,AKS的出站类型,也就是连接到Internet的方式有几种
- Load Balancer
- 默认选项
- AKS cluster部署时会自动部署一个external load balancer, 并创建outbound rule,AKS cluster的出站会从LB出去
- NAT Gateway
- 可以在部署cluster时自动创建NAT Gateway或者用自己的NAT Gateway
- 出站会由NAT Gateway出去
- user-defined routing
- AKS Cluster所在subnet必须绑定route table,并且必须设置默认路由
- 出站会从防火墙出去
今天要部署的就是最后一种,测试的场景是AKS前边会部署一个Azure firewall,到internet的流量会经过firewall
整个架构很简单,比较特殊的就是部署AKS cluster的步骤,我这里是用CLI来部署的,有一些参数会和正常在portal部署不太一样
subnet_id=$(az network vnet subnet show --resource-group xx -n xx --vnet-name xx-vnet --query id|tr -d '"')
az aks create -g TEMPAKS \
-n tempAKS \
--node-osdisk-size 100 \
--admin-username xx \
--kubernetes-version 1.27.7 \
--location eastasia \
--network-plugin kubenet \
--node-count 1 \
--node-vm-size Standard_DS2_v2 \
--nodepool-name systempool \
--pod-cidr 10.200.0.0/16 \
--service-cidr 10.201.0.0/16 \
--vm-set-type VirtualMachineScaleSets \
--vnet-subnet-id $subnet_id \
--dns-service-ip 10.201.0.10 \
--dns-name-prefix tempmxyAKS \
--outbound-type userDefinedRouting \
--tier free \
--generate-ssh-keys \
--assign-identity /subscriptions/xxxx/resourcegroups/TEMPAKS/providers/Microsoft.ManagedIdentity/userAssignedIdentities/tempaks
这里要注意的参数有两个
- --outbound-type,这个就是上边列出的几种出站类型,需要指定为userDefinedRouting
- assign-identity,正常AKS Cluster是不需要指定这个的,但是今天创建的这种AKS cluster不支持system assigned managd identity,只能手动指定user assigned managed identity
除此之外,还有一些需要注意的地方,首先AKS Subnet必须绑定Route Table,并且user assigned managed identity还必须对这个route table有写的权限,否则创建cluster的时候会报错
另外Route Table还必须创建0.0.0.0/0的默认路由,创建cluster的时候会检测这个,没有的话也会创建失败
cluster创建完成后,尝试部署了一个应用,可以看到整个cluster其实是不太正常的
而在firewall的log里可以看到有一些流量被deny了
在firewall的network rule里添加对应的policy来允许流量
之后可以看到cluster算是正常了,只不过到image registry的流量因为还没被允许,所以pull image失败了
但是流量可以确认是经过firewall的