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 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

kubectl文档

打包 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

原文地址:https://www.cnblogs.com/binking338/p/aspnetcore_on_k8s.html

 
 

.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com 
640?wx_fmt=jpeg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值