下边开始来实际跑个demo,看下具体在使用时pod identity能达到什么样的效果,这是pod identity官方给的一个sample,要做的工作主要分为几个步骤
- 部署pod identity
- 创建Azure user assigned identity
- 准备yaml文件,部署Azure identity和Azure identity binding
- 部署测试用的yaml
环境准备
需要准备的实验环境很简单,主要包括以下资源和工具
- 使用Azure CNI网络模型的AKS集群
- kubectl
- Azure CLI
实践环节
准备环境变量
$RESOURCE_GROUP='Apache'
$CLUSTER_NAME='SSLAKS'
$IDENTITY_RESOURCE_GROUP="$(az aks show -g $RESOURCE_GROUP -n $CLUSTER_NAME --query nodeResourceGroup -o tsv)"
$IDENTITY_NAME="demo"
部署pod identity component
pod identity的部署基本和其他产品差不多,完全通过yaml文件直接部署即可
kubectl apply -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/deployment-rbac.yaml
# For AKS clusters, deploy the MIC and AKS add-on exception by running -
kubectl apply -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/mic-exception.yaml
部署完成之后,即可看到系统中的crd资源
kubectl get crd -A
也出现了新的API Resource
kubectl api-resources|?{$_ -like '*identity*'}
还会部署mic和nmi两个核心组件的pod
创建user assigned identity
pod identity本身并不会知道应该给谁获取什么样的权限,而是需要用户告诉pod identity希望获取什么样的权限,而要做到这点,就需要指定在pod identity中要是用的是service principal,还是managed identity,然后把对应的信息写在Azure identity的yaml文件里
因为是个sample的环境,所以我们直接新创建一个user assigned identity来给pod identity用
az identity create -g $IDENTITY_RESOURCE_GROUP -n $IDENTITY_NAME
其实就是个user assigned managed identity
获取client id和resource id
$IDENTITY_CLIENT_ID="$(az identity show -g $IDENTITY_RESOURCE_GROUP -n $IDENTITY_NAME --query clientId -o tsv)"
$IDENTITY_RESOURCE_ID="$(az identity show -g $IDENTITY_RESOURCE_GROUP -n $IDENTITY_NAME --query id -o tsv)"
准备工作做好之后,即可开始准备pod identity测试用的部署文件
创建Azure identity
apiVersion"aadpodidentity.k8s.io/v1"
kind AzureIdentity
metadata
name $ IDENTITY_NAME
spec
type0
resourceID $ IDENTITY_RESOURCE_ID
clientID $ IDENTITY_CLIENT_ID
Type 可以有多种取值范围,分别代表不同含义,部署用的就是0,也就是user assigned identity:
- type: 0 user-assigned MSI
- type: 1 Service Principal with client secret
- type: 2 Service Principal with certificate
kubectl apply -f "D:\UserData\Desktop\Pod Identity\AzureIdentitySample.yml"
kubectl get azureidentity
创建AzureIdentityBinding
准备yaml文件,把变量替换为实际环境的值
apiVersion"aadpodidentity.k8s.io/v1"
kind AzureIdentityBinding
metadata
name $ IDENTITY_NAME -binding
spec
azureIdentity $ IDENTITY_NAME
selector $ IDENTITY_NAME
kubectl apply -f "D:\UserData\Desktop\Pod Identity\AzureIdentityBindingSample.yml"
kubectl get azureidentitybinding
部署测试用pod
准备yaml文件,把变量替换为实际环境的值
哪些pod可以使用identitybinding是靠pod中aadpodidbinding这个字段去区分的,这个字段的值就是之前identitybinding yaml文件中selector字段对应的值,两者要匹配起来
apiVersion v1
kind Pod
metadata
name demo
labels
aadpodidbinding $IDENTITY_NAME
spec
containers
name demo
image mcr.microsoft.com/oss/azure/aad-pod-identity/demo v1.8.4
args
--subscription-id=$ SUBSCRIPTION_ID
--resource-group=$ IDENTITY_RESOURCE_GROUP
--identity-client-id=$ IDENTITY_CLIENT_ID
nodeSelector
kubernetes.io/os linux
开始部署
kubectl apply -f "D:\UserData\Desktop\Pod Identity\AzureIdentitySamplePod.yml"
kubectl get po|? $_ -like 'pod*'
查看部署pod的log,可以找到获取到token这种字眼
kubectl logs pod-identity-demo