ASP.NET Core在Azure Kubernetes Service中的部署和管理
目標
部署:掌握將aspnetcore程序成功發布到Azure Kubernetes Service(AKS)上
管理:掌握將AKS上的aspnetcore程序擴容、更新版本
準備工作
注冊 Azure 賬戶
官網
免費帳戶
Azure 免費帳戶僅適用于新用戶,并且僅限每個客戶一個免費帳戶。
AKS文檔
AKS文檔首頁
azure中文文檔
Azure有兩種管理方式 Azure Cli 和 Azure 門戶。
進入Azure門戶(控制臺)
門戶(控制臺)
搜索AKS,選中Azure Kubernetes Service,進入AKS控制臺。
安裝 Azure Cli
安裝文檔
主要使用Cli方式管理Azure。
安裝 Docker
Docker首頁
DockerHub
進入正題
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 acrpullaz 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-bootcamp2019kubectl config delete-context k8s-bootcamp2019
kubectl文檔
打包 Docker 鏡像
可以直接使用Docker Hub中的鏡像。也可以將鏡像上傳到ACR(推薦)。
docker 入門
dotnetcore docker 示例
Docker Hub 國內鏡像
ASP.NET Core Sample
git clone https://github.com/dotnet/dotnet-dockercd 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:v1docker tag aspnetapp azurebootcamp2019.azurecr.io/bootcamp2019web:v1
docker images
推送 Docker Image 到 ACR
# docker push <acrLoginServer>/bootcamp2019web:v1docker push azurebootcamp2019.azurecr.io/bootcamp2019web:v1
查詢 ACR 實例的映像列表
az acr repository list --name azurebootcamp2019 --output table發布
deployment配置文件
apiVersion: apps/v1kind: 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 ~/boot-camp-2019-web.yaml
kubectl get service boot-camp-2019-web --watch
擴容
kubectl get podskubectl 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:v2dashboard
az aks browse --resource-group boot-camp-2019 --name k8s-bootcamp2019權限問題
kubectl create clusterrolebinding kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard原文地址:https://www.cnblogs.com/binking338/p/aspnetcore_on_k8s.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總?http://www.csharpkit.com?
總結
以上是生活随笔為你收集整理的ASP.NET Core在Azure Kubernetes Service中的部署和管理的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 网络数据采集(AngleSharp)-使
- 下一篇: 深入研究 Mini ASP.NET Co
