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

ASP.NET Core在Azure Kubernetes Service中的部署和管理的更多相关文章

  1. 如何托管ASP.NET Core应用到Windows Service中

    (此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...

  2. ASP.NET Core应用到Windows Service中

    托管到Windows Service中 众所周知,ASP.NET Core采用了和传统ASP.NET不同的托管和HTTP处理方式,即把服务器和托管环境完全解耦. ASP.NET Core内置了两个HT ...

  3. Azure Kubernetes Service 入门

    一,引言 上一节,我们使用Azure CLI 创建了Azure Resource Group 和 Azure Container Registry 资源,并且将本地的一个叫 “k8s.net.demo ...

  4. 【Azure 云服务】如何从Azure Cloud Service中获取项目的部署文件

    问题描述 在历史已经部署的云服务(Azure Cloud Service)中,如何获取到项目在很久以前的部署包文件呢? 解决办法 1)如果部署云服务是通过门户上传部署包到存储账号中,则可以直接从存储账 ...

  5. 尝新体验ASP.NET Core 6预览版本中发布的最小Web API(minimal APIS)新特性

    本文首发于<尝新体验ASP.NET Core 6预览版本中发布的最小Web API(minimal APIS)新特性> 概述 .NET开发者们大家好,我是Rector. 几天前(美国时间2 ...

  6. 如何将Azure DevOps中的代码发布到Azure App Service中

    标题:如何将Azure DevOps中的代码发布到Azure App Service中 作者:Lamond Lu 背景 最近做了几个项目一直在用Azure DevOps和Azure App Servi ...

  7. ASP.NET Core MVC的路由参数中:exists后缀有什么作用,顺便谈谈路由匹配机制

    我们在ASP.NET Core MVC中如果要启用Area功能,那么会看到在Startup类的Configure方法中是这么定义Area的路由的: app.UseMvc(routes => { ...

  8. 【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用

    问题描述 在Azure App Service中,有对.Net,Java的WebSocket支持的示例代码,但是没有成功的PHP代码. 以下的步骤则是如何基于Azure App Service实现PH ...

  9. 【应用服务 App Service】当遇见某些域名在Azure App Service中无法解析的错误,可以通过设置指定DNS解析服务器来解决

    问题情形 当访问部署在Azure App Service中的应用返回 "The remote name could not be resolved: ''xxxxxx.com'" ...

随机推荐

  1. openresty + lua-resty-weedfs + weedfs + graphicsmagick动态生成缩略图(类似淘宝方案)

    openresty + lua-resty-weedfs + weedfs + graphicsmagick动态生成缩略图(类似淘宝方案) --大部分的网站都要涉及到图片缩略图的处理,比如新闻配图,电 ...

  2. 类的父类object的一些属性、方法

    # class Test: # """文档字符串""" # name = 'scolia' # # print(Test.__doc__) ...

  3. 第二章 ArrayList源码解析

    一.对于ArrayList需要掌握的七点内容 ArrayList的创建:即构造器 往ArrayList中添加对象:即add(E)方法 获取ArrayList中的单个对象:即get(int index) ...

  4. 学习AD、DA的体会

    AD转换器的转换是指模拟信号输入转化为数字信号输出,而DA转换器是把数字信号转换为模拟信号,在ADC0832.TLC549和TLC5615程序设计中,通过使用中断服务函数每0.5s对ADC0832进行 ...

  5. 洛谷 P1450 解题报告

    P1450.硬币购物 题目描述 硬币购物一共有\(4\)种硬币.面值分别为\(c1,c2,c3,c4\).某人去商店买东西,去了\(tot\)次.每次带\(d_i\)枚\(c_i\)硬币,买\(s_i ...

  6. 适合Python 新手的5大练手项目,你练了么?

    接下来就给大家介绍几种适合新手的练手项目. 0.算法系列-排序与查找 Python写swap很方便,就一句话(a, b = b, a),于是写基于比较的排序能短小精悍.刚上手一门新语言练算法最合适不过 ...

  7. Python_字符串检测与压缩

    ''' center().ljust().rjust(),返回指定宽度的新字符串,原字符串居中.左对齐或右对齐出现在新字符串中, 如果指定宽度大于字符串长度,则使用指定的字符(默认为空格进行填充). ...

  8. 【Quartz】常用方法的使用方式(三)

    前言    总结了一下quartz组件的一些常用方法,以备将来不时之需.哈哈,看着挺简单有些好是值得笔记一下的.好记性不如烂笔头吗? 代码部分: 方法类内容: public class Example ...

  9. SpringMVC--入门案例

    一.SpringMVC介绍 SpringMVC和Struts都属于表现层框架, 是Spring的一部分,Spring的整体结构如下: 1.1 SpringMVC的处理流程 下图是SpringMVC的执 ...

  10. [求教]利用typescript对Javascript做强类型检测提示

    近期在学习typescript的时候,发现ts居然可以对原生js做强检测.发现从v2.3就开始直接了.这让我感叹他的变化,又让我自己感到学习的缓慢.本文章就对他小试牛刀一下. 一.他是的使用与支持 通 ...