ASP.NET Core在Azure Kubernetes Service中的部署和管理 [TOC] 目標 部署:掌握將aspnetcore程式成功發佈到Azure Kubernetes Service(AKS)上 管理:掌握將AKS上的aspnetcore程式擴容、更新版本 準備工作 註冊 Azu ...
目錄
- ASP.NET Core在Azure Kubernetes Service中的部署和管理
ASP.NET Core在Azure Kubernetes Service中的部署和管理
目標
部署:掌握將aspnetcore程式成功發佈到Azure Kubernetes Service(AKS)上
管理:掌握將AKS上的aspnetcore程式擴容、更新版本
準備工作
註冊 Azure 賬戶
Azure 免費帳戶僅適用於新用戶,並且僅限每個客戶一個免費帳戶。
AKS文檔
Azure有兩種管理方式 Azure Cli 和 Azure 門戶。
進入Azure門戶(控制台)
搜索AKS
,選中Azure Kubernetes Service,進入AKS控制台。
安裝 Azure Cli
主要使用Cli方式管理Azure。
安裝 Docker
進入正題
Azure 相關概念
資源組
創建資源組
az group create --name myResourceGroup --location eastasia
刪除資源組
az group delete --name myResourceGroup --yes --no-wait
容器註冊表 Azure Container Register (ACR)
使用 ACR 管理 Docker 鏡像。
創建 ACR
az acr create --resource-group boot-camp-2019 --name azurebootcamp2019 --sku Basic
登錄 ACR
az acr login --name azurebootcamp2019
服務主體 service principle
創建服務主體
az ad sp create-for-rbac --skip-assignment
記下返回信息 appId 和 password,返回格式如下
{
"appId": "d67dc2f9-d8d1-4a2c-a2ef-df15cc3710c1",
"displayName": "azure-cli-2019-04-21-11-46-32",
"name": "http://azure-cli-2019-04-21-11-46-32",
"password": "4488581b-d297-4488-ac4a-154400df8acd",
"tenant": "16cdead3-aec0-4dcb-acc4-d9c862f105d3"
}
給服務主體配置 ACR 的pull許可權
查詢 ACR 的 arcId
az acr show --resource-group boot-camp-2019 --name azurebootcamp2019 --query "id" --output tsv
給服務主體分配 AcrPull 角色
# az role assignment create --assignee <appId> --scope <acrId> --role acrpull
az role assignment create --assignee d67dc2f9-d8d1-4a2c-a2ef-df15cc3710c1 --scope /subscriptions/5c029b59-2c2e-4b8b-b76b-8afde2753164/resourceGroups/boot-camp-2019/providers/Microsoft.ContainerRegistry/registries/azurebootcamp2019 --role acrpull
K8s服務集群 Azure Kubernetes Service(AKS)
創建AKS集群
# az aks create \
# --resource-group boot-camp-2019 \
# --name k8s-bootcamp2019 \
# --node-count 1 \
# --enable-addons monitoring \
# --service-principal <appId> \
# --client-secret <password> \
# --generate-ssh-keys
az aks create \
--resource-group boot-camp-2019 \
--name k8s-bootcamp2019 \
--node-count 1 \
--enable-addons monitoring \
--service-principal d67dc2f9-d8d1-4a2c-a2ef-df15cc3710c1 \
--client-secret 4488581b-d297-4488-ac4a-154400df8acd \
--generate-ssh-keys
連接AKS集群
使用 kubectl 連接AKS集群,如果沒有安裝 kubectl ,使用如下指令安裝。
az aks install-cli
將 kubectl 配置為連接到 Kubernetes 群集,如下命令將會創建集群配置以及 Kubernetes Context
az aks get-credentials --resource-group boot-camp-2019 --name k8s-bootcamp2019
驗證到群集的連接
kubectl get nodes
刪除Context
kubectl config delete-cluster k8s-bootcamp2019
kubectl config delete-context k8s-bootcamp2019
打包 Docker 鏡像
可以直接使用Docker Hub中的鏡像。也可以將鏡像上傳到ACR(推薦)。
docker 入門
dotnetcore docker 示例
Docker Hub 國內鏡像
ASP.NET Core Sample
git clone https://github.com/dotnet/dotnet-docker
cd dotnet-docker/samples/aspnetapp/
docker build -t aspnetapp .
docker run -it --rm -p 5000:80 --name aspnetcore_sample aspnetapp
標記容器映像
查詢acrLoginServer,需先登錄ACR
az acr list --resource-group boot-camp-2019 --query "[].{acrLoginServer:loginServer}" --output table
標記鏡像
# docker tag aspnetapp <acrLoginServer>/bootcamp2019web:v1
docker tag aspnetapp azurebootcamp2019.azurecr.io/bootcamp2019web:v1
docker images
推送 Docker Image 到 ACR
# docker push <acrLoginServer>/bootcamp2019web:v1
docker push azurebootcamp2019.azurecr.io/bootcamp2019web:v1
查詢 ACR 實例的映像列表
az acr repository list --name azurebootcamp2019 --output table
發佈
deployment配置文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: boot-camp-2019-web
spec:
replicas: 1
selector:
matchLabels:
app: boot-camp-2019-web
template:
metadata:
labels:
app: boot-camp-2019-web
spec:
containers:
- name: boot-camp-2019-web
image: azurebootcamp2019.azurecr.io/bootcamp2019web:v1
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 250m
memory: 256Mi
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: boot-camp-2019-web
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: boot-camp-2019-web
發佈
# kubectl apply -f <配置.yaml>
kubectl apply -f ~/boot-camp-2019-web.yaml
kubectl get service boot-camp-2019-web --watch
擴容
kubectl get pods
kubectl scale --replicas=3 deployment/boot-camp-2019-web
kubectl get pods
更新
kubectl set image deployment boot-camp-2019-web boot-camp-2019-web=azurebootcamp2019.azurecr.io/bootcamp2019web:v2
dashboard
az aks browse --resource-group boot-camp-2019 --name k8s-bootcamp2019
許可權問題
kubectl create clusterrolebinding kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard