Azure Kubernetes(AKS)是微软Azure云上托管的Kubernetes群集,可以用于快速部署Kubernetes群集,结合Azure简化日常操作和维护,轻松实现业务应用的弹性。
想必很多小伙伴已经大量使用了AKS服务。然而,目前在使用中不可避免地会发现Kubernetes社区蓬勃发展,版本更新频繁。这其实是一件好事,可以更快地修复各种发现的东西Bug,它还可以通过升级为我们带来各种创新功能。
然而,如何部署已经部署的部署AKS为了获得各种新版本,以获得各种新功能?
对AKS托管版本的升级可以一键升级AKS集群原地升级,或逐个滚动集群中的工作节点。用于小规模或非关键业务集群AKS托管升级功能非常方便;但对于大规模集群或关键业务应用,存在上述一键原地升级方法,,而且如果发生故障,会等局限。
那么,有没有一种方法可以同时兼顾上述两种方法的优点呢?
从事应用程序开发或操作和维护的合作伙伴应该熟悉这个概念。这种应用程序发布模式可以逐渐将用户流量从以前版本的应用程序转移到几乎相同的新版本(旧版本可以称为蓝色环境,新版本可以称为绿色环境)。一旦生产流量完全从蓝色转移到绿色环境,蓝色环境可以改为待机,以便在发生事故时回滚和恢复。观察稳定性一段时间后,可彻底清理,节约资源。
其实对于AKS群集的升级,我们也可以采用类似的思路:新建一个AKS采用蓝绿部署切换升级。这样可以实现秒级切换,备份组可以随时回滚,更快更安全。
然而,该方案的结构和操作有点复杂。本文将带领您逐步建立这样一套切换升级的架构方案。在下面的例子中,我们将使用经典Web但也可以扩展到其他应用场景AKS集群应用场景。
上图显示了一个经典Web对于应用架构图,我们选择了最简洁的资源,使演示和解释尽可能简洁。
将三个子网分离在虚拟网络中,前一个子网放置应用网关作为外部服务的负载平衡器;后两个子网通常只有一个子网和一个子网AKS集群部署业务系统。另一个子网络用于创建新版本升级AKS集群。AKS集群采用高级网络CNI,简化网络模式,方便网关及其通信的应用。
在AKS集群中部署Pod Identity,通过Azure AD的Pod Identity来授权AKS中的Pod应用网关可以管理。
AGIC全称为Application Gateway Ingress Controller,除了将来自应用网关的网络流量分发给相应的网络流量外Pod,还监视部分Kubernetes资源的变化可以在AKS应用网关的后端池在伸缩时自动更新AKS应用网关的后端池同步更新。使用AGIC实现蓝绿部署的核心理念是动态更新应用网关的后端。
首先需要用CLI快速创造当前环境的基础资源。
创建资源组:
AZ_REGION=ChinaNorth2
RESOURCE_GROUP=AKS_Upgrade
az group create -n $RESOURCE_GROUP -l $AZ_REGION
创建VNET和子网:
VNET_NAME=AksVnet
APPGW_SUBNET=AppGwSubnet
AKS_SUBNET=AksSubnet
network vnet create on $VNET_NAME \
-g $RESOURCE_GROUP \
-l $AZ_REGION \
--address-prefix 10.0.0.0/8 \
--subnet-name $APPGW_SUBNET --subnet-prefix 10.1.0.0/16
az network vnet subnet create \
-g $RESOURCE_GROUP \
-n $AKS_SUBNET \
--address-prefixes 10.240.0.0/16 \
--vnet-name $VNET_NAME
创建公有IP:
APPGW_IP=AppGatewayIp
az network public-ip create -n $APPGW_IP \
-g $RESOURCE_GROUP \
--allocation-method Static \
--sku Standard
创建应用网关:
APP_GATEWAY=AppGateway
az network application-gateway create -n $APP_GATEWAY \
-g $RESOURCE_GROUP \
-l $AZ_REGION \
--vnet-name $VNET_NAME \
--subnet $APPGW_SUBNET \
--sku Standard_v2 \
--public-ip-address $APPGW_IP
创建旧AKS集群。使用当前默认主流AKS版本。首先获取之前创建的放置AKS集群的子网ID。
AKS_SUBNET_ID=$(az network vnet subnet show -g $RESOURCE_GROUP --vnet-name $VNET_NAME --name $AKS_SUBNET --query id -o tsv)
创建AKS集群(写这篇文章主流AKS版本为1.19.11):
AKS_OLD=old
az aks create -n $AKS_OLD \
-g $RESOURCE_GROUP \
-l $AZ_REGION \
--generate-ssh-keys \
--network-plugin azure \
--enable-managed-identity \
--vnet-subnet-id $AKS_SUBNET_ID
我们将用Azure授权服务主体AKS集群管理应用网关配置。
首先连接AKS集群:
az aks get-credentials --resource-group $RESOURCE_GROUP --name $AKS_OLD
然后你可以用大家熟悉的了。kubectl来管理AKS集群。
安装Helm并运行以下命令添加application-gateway-kubernetes-ingress Helm包。我们的AKS群集已启用Kubernetes RBAC,因此,可以使用以下命令:
kubectl create serviceaccount --namespace kube-system tiller-sa kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller-sa helm repo add aad-pod-identity https://raw.githubusercontent.com/Azure/aad-pod-identity/master/charts helm install aad-pod-identity aad-pod-identity/aad-pod-identity
上述命令将返回以下结果:
NAME:aad-pod-identity
LAST DEPLOYED: Tue Jun 29 08:14:30 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
You have successfully installed AAD Pod Identity in your Kubernetes cluster!
…
稍等一两分钟后,运行如下命令:
kubectl get po -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
aad-pod-identity-mic-787c5958fd-kmx9b 1/1 Running 0 71s 10.240.0.33 aks-nodepool1-94448771-vmss000000 <none> <none>
aad-pod-identity-mic-787c5958fd-nkpv4 1/1 Running 0 72s 10.240.0.63 aks-nodepool1-94448771-vmss000001 <none> <none>
aad-pod-identity-nmi-mhp86 1/1 Running 0 72s 10.240.0.4 aks-nodepool1-94448771-vmss000000 <none> <none>
aad-pod-identity-nmi-sjpvw 1/1 Running 0 72s 10.240.0.35 aks-nodepool1-94448771-vmss000001 <none> <none>
aad-pod-identity-nmi-xnfxh 1/1 Running 0 72s 10.240.0.66 aks-nodepool1-94448771-vmss000002 <none> <none>
可以看到,相关的几个Pod也已经运行起来了。
随后使用Helm安装Application Gateway Ingress Controller:
helm repo add application-gateway-kubernetes-ingress https://appgwingress.blob.core.windows.net/ingress-azure-helm-package/
helm repo update
复制以下YAML文件,先保存为helm_agic.yaml,用于配置AGIC:
# This file contains the essential configs for the ingress controller helm chart
# Verbosity level of the App Gateway Ingress Controller
verbosityLevel: 3
################################################################################
# Specify which application gateway the ingress controller will manage
#
appgw:
subscriptionId: <subscriptionId>
resourceGroup: <resourceGroupName>
name: <applicationGatewayName>
environment: AzureChinaCloud
# Setting appgw.shared to "true" will create an AzureIngressProhibitedTarget CRD.
# This prohibits AGIC from applying config for any host/path.
# Use "kubectl get AzureIngressProhibitedTargets" to view and change this.
shared: false
################################################################################
# Specify which kubernetes namespace the ingress controller will watch
# Default value is "default"
# Leaving this variable out or setting it to blank or empty string would
# result in Ingress Controller observing all acessible namespaces.
#
# kubernetes:
# watchNamespace: <namespace>
################################################################################
# Specify the authentication with Azure Resource Manager
#
# Two authentication methods are available:
# - Option 1: AAD-Pod-Identity (https://github.com/Azure/aad-pod-identity)
# armAuth:
# type: aadPodIdentity
# identityResourceID: <identityResourceId>
# identityClientID: <identityClientId>
## Alternatively you can use Service Principal credentials
armAuth:
type: servicePrincipal
secretJSON: <<Generate value with: "az ad sp create-for-rbac --sdk-auth | base64 -w0">>
################################################################################
# Specify if the cluster is RBAC enabled or not
rbac:
enabled: true # true/false
我们逐个把上述配置文件中的参数值填写进去。
-
<subscriptionId>:通过az account show --query id -o tsv获取;
-
<resourceGroupName>:值取$RESOURCE_GROUP环境变量;
-
<applicationGatewayName>:值取$APP_GATEWAY环境变量。
secretJSON的值使用az ad sp create-for-rbac --sdk-auth | base64 -w0命令获取,是一段800多字节的base64编码长字符串。
因为我们的AKS集群启用了RBAC,所以最后那个配置rbac设置为true。
最后执行以下命令安装:
helm install agic application-gateway-kubernetes-ingress/ingress-azure -f helm_agic.yaml
上述命令将返回如下结果:
W0629 08:16:47.733467 16087 warnings.go:70] apiextensions.k8s.io/v1beta1 CustomResourceDefinition is deprecated in v1.16+, unavailable in v1.22+; use apiextensions.k8s.io/v1 CustomResourceDefinition
NAME: agic
LAST DEPLOYED: Tue Jun 29 08:16:48 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Thank you for installing ingress-azure:1.4.0.
Your release is named agic.
The controller is deployed in deployment agic-ingress-azure.
Configuration Details:
----------------------
* AzureRM Authentication Method:
- Use AAD-Pod-Identity
* Application Gateway:
- Subscription ID : 3d07553f-f6a8-455f-9de6-876fbcc00bb4
- Resource Group : AKS_Upgrade
- Application Gateway Name : AppGateway
* Kubernetes Ingress Controller:
- Watching All Namespaces
- Verbosity level: 3
随后运行下列命令:
kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
aad-pod-identity-mic-787c5958fd-kmx9b 1/1 Running 0 4m54s 10.240.0.33 aks-nodepool1-94448771-vmss000000 <none> <none>
aad-pod-identity-mic-787c5958fd-nkpv4 1/1 Running 0 4m55s 10.240.0.63 aks-nodepool1-94448771-vmss000001 <none> <none>
aad-pod-identity-nmi-mhp86 1/1 Running 0 4m55s 10.240.0.4 aks-nodepool1-94448771-vmss000000 <none> <none>
aad-pod-identity-nmi-sjpvw 1/1 Running 0 4m55s 10.240.0.35 aks-nodepool1-94448771-vmss000001 <none> <none>
aad-pod-identity-nmi-xnfxh 1/1 Running 0 4m55s 10.240.0.66 aks-nodepool1-94448771-vmss000002 <none> <none>
agic-ingress-azure-8d9d85dd9-z8dwh 1/1 Running 0 2m37s 10.240.0.70 aks-nodepool1-94448771-vmss000002 <none> <none>
发现新建的agic-ingress-azure这个Pod也正常运行起来了。
至此,我们已经成功部署了相关资源并实现了应用网关与AKS的集成。在下篇文章中,将会涉及应用部署、AKS新集群部署以及AKS版本切换三个任务。敬请期待!