This article is a translation and practice of ".NET Tutorial-Deploy a microservice to Azure" . The entry-level step-by-step practice, please avoid the k8s boss, so as not to waste your precious time.
Introduction
The purpose of this article is: by using a previously DockerHub and Azure Kubernetes Service (AKS) using .NET and Docker built micro-services deployed on Microsoft's Azure cloud, learn the basics of micro process deployment services.
Push to Docker Hub
Docker Hub is the world's largest container mirror library and community. Many products, including Microsoft Azure, can create containers based on images in Docker Hub.
Log in to Docker Hub
If you do not have a Docker Hub account, you can register one at https://hub.docker.com/ . For the registration steps, please refer to the instructions in Docker Quick Start (3) .
In the command prompt window, run the following command:
docker login
Enter your Docker ID and password, if the output is as follows:
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username:xxxxxx
Password:
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout
Indicates login timeout. You can try to set the preferred DNS server to 8.8.8.8
(free DNS provided by Google). The command line to modify DNS is:
# 使用时请将 "WLAN" 改为实际的本地链接名称,需要以管理员身份运行命令提示符窗口
netsh interface ip set dnsservers "WLAN" static 8.8.8.8 primary
Then log in again, if the output is output Login Succeeded
, it means that the login was successful.
Push the image to Docker Hub
Relabel (rename) your Docker image based on your Docker ID and push it to Docker Hub using the following command:
docker tag mymicroservice [YOUR DOCKER ID]/mymicroservice
docker push [YOUR DOCKER ID]/mymicroservice
Wait for the push to complete, visit your repository https://hub.docker.com/repositories in Docker Hub, you can see the image you just pushed, as shown below:
After the image is pushed, if the DNS has been modified before, be sure to change the DNS address back to the original dynamic acquisition, otherwise it may affect the network access speed:
# 使用时请将 "WLAN" 改为实际的本地连接名称,需要以管理员身份运行命令提示符窗口
# 改为动态获取 DNS 地址
netsh interface ip set dnsservers "WLAN" source=dhcp
# 或者将 DNS 改为 114.114.114.114(国内移动、电信和联通通用的DNS)
netsh interface ip set dnsservers "WLAN" static 114.114.114.114 primary
# 还可以添加第二个 DNS 地址
netsh interface ip add dnsservers "WLAN" 8.8.8.8 index=2
Install Azure tools
Create an Azure account
If you are new to Azure cloud, you can create a free account. If you have an existing account, you can skip this step.
For the steps to create an account, please see " Create a free Azure account "
When registering, you need to fill in some personal information such as your name, email address, mobile phone number, credit card, etc. After successful registration, $1 is deducted, and then a credit line of $200 is given away for a free trial.
Install Azure CLI
Azure Command Line Interface (CLI) provides tools for managing Azure accounts.
For the steps to install Azure CLI, please see " Install Azure CLI for Windows "
If downloading Azure CLI from the official website is slow, you can download it here:
Link: https://pan.baidu.com/s/1FZhkAFX2o4GRCqSWYmYvmA Extraction code: fi8x
After the installation is complete, open a new command prompt window, run the az --version
command to verify that the installation was successful.
Sign in to Azure
At the command prompt, run the az login
command log in to your Azure account:
C:\WINDOWS\system32>az login
# 会提示弹出一个登录网页,登录成功后输出如下信息:
You have logged in. Now let us find all the subscriptions to which you have access...
[
{
"cloudName": "AzureCloud",
"homeTenantId": "7cfff80b-cb8f-461f-8bb4-19bd80xxxxxx",
"id": "0123237e-4c5e-4eb5-a4cc-205b0cxxxxxx",
"isDefault": true,
"managedByTenants": [],
"name": "免费试用",
"state": "Enabled",
"tenantId": "7cfff80b-cb8f-461f-8bb4-19bd80xxxxxx",
"user": {
"name": "[email protected]",
"type": "user"
}
}
]
Failed to install AKS CLI! Use online Azure Cloud Shell instead
Kubernetes
Is a container orchestration platform. The orchestrator is responsible for running, distributing, scaling, and repairing applications composed of a collection of containers. Azure Kubernetes Service
( AKS
) Will be Kubernetes
offered as a hosted service.
Run the following command to install the command line tool for AKS.
az aks install-cli
However, this command was retried many times and failed to execute due to network problems. Finally, I gave up installing AKS CLI on this machine and used the online Azure Cloud Shell directly. For Azure Cloud Shell, please refer to the document: https://docs.microsoft .com/en-us/azure/cloud-shell/overview
The method to open Azure Cloud Shell in Azure Portal is:
Create Azure resources
Create resource group
A resource group is used to organize a group of resources related to a single application.
Run the following command in the local command prompt window to create a resource group:
az group create --name myMicroserviceResources --location eastasia
The execution results are as follows:
C:\Users\xxx>az group create --name myMicroserviceResources --location eastasia
{
"id": "/subscriptions/0123237e-4c5e-4eb5-a4cc-205b0cxxxxxx/resourceGroups/myMicroserviceResources",
"location": "eastasia",
"managedBy": null,
"name": "myMicroserviceResources",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
After execution, click "Resource groups" in Azure Portal to view:
Create an AKS cluster using Azure Cloud Shell
Use Azure Cloud Shell to run the following command to create an AKS cluster in the resource group:
This command usually takes a few minutes to complete.
az aks create --resource-group myMicroserviceResources --name myMicroserviceCluster --node-count 1 --enable-addons http_application_routing --generate-ssh-keys
After the execution is complete, check the resource group list, you can see that there is an AKS cluster resource group and a network observation resource group:
Use Azure Cloud Shell to run the following command to download the credentials to be deployed to the AKS cluster:
az aks get-credentials --resource-group myMicroserviceResources --name myMicroserviceCluster
Deploy to Azure
Like with Kubernetes, AKS use .yaml
file to define how to deploy container.
Use Azure Cloud Shell to create a deployment file
Azure Cloud Shell window opens, running in Azure Portal in cd clouddrive
order to open the clouddrive
catalog,
Run the following command to create an empty deploy-myMicroservice.yaml
file:
echo . > deploy-myMicroservice.yaml
Then run the vim deploy-myMicroservice.yaml
command to edit deploy-myMicroservice.yaml
the file, the contents will be replaced with the following:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mymicroservice
spec:
replicas: 1
template:
metadata:
labels:
app: mymicroservice
spec:
containers:
- name: mymicroservice
image: [YOUR DOCKER ID]/mymicroservice:latest
ports:
- containerPort: 80
env:
- name: ASPNETCORE_URLS
value: http://*:80
selector:
matchLabels:
app: mymicroservice
---
apiVersion: v1
kind: Service
metadata:
name: mymicroservice
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: mymicroservice
Press the Esc
key to re-enter :wq
to save and exit vim
the command.
Opens "cloud-shell-storage-southeastasia " resource groups, which can be seen more than a deploy-myMicroservice.yaml
file, as shown:
Run deployment
Positioned Azure Cloud Shell in the clouddrive
directory, run the following command, according to the deploy-helloMicroservice.yaml
deployment settings:
kubectl apply -f deploy-myMicroservice.yaml
Test the deployed service
Run the following command in Azure Cloud Shell to view the details of the deployed service:
kubectl get service mymicroservice --watch
In addition, in front of the kubectl get service
command will display the external IP address of the service available ( EXTERNAL-IP
).
Use this external IP address to browse " http://[YOUR EXTERNAL IP ADDRESS]/WeatherForecast
" in the browser .
If
EXTERNAL-IP
marked<pending>
, then after the allocation of external IP, a new line will automatically appear to display.
Service scaling
Run the following command to extend the service to two instances:
kubectl scale --replicas=2 deployment/mymicroservice
# 输出如下信息:
deployment.apps/mymicroservice scaled
Good Job! The microservice has now been deployed to Azure and scaled.
to sum up
Operating experience: Docker Hub, slow! Azure, slow! The operation was as fierce as a tiger, the steps were as slow as an ox, and it was still a snail.
You can use Azure Container Registry instead of Docker Hub to manage images.
Although Azure is good, it is difficult to solve network problems when used in China, which greatly affects the experience of use!
In the era of the popularity of microservices and DevOps, more and more large-scale service providers provide support for Kubernetes, Azure’s "Azure Kubernetes Service (AKS)" and "Azure Container Registry", and there are Alibaba Cloud’s "Alibaba Cloud Registry" in China. Cloud Container Service Kubernetes Edition (ACK)" and "Alibaba Cloud Container Image Service (ACR)" are benchmarked. In addition, Tencent Cloud and Huawei Cloud also provide cloud container engine services.
References and some related products and documents
-
The core concepts of Kubernetes in Azure Kubernetes Service (AKS)
-
Alibaba Cloud Container Service for Kubernetes ACK (Alibaba Cloud Container Service for Kubernetes)
-
Alibaba Cloud Container Registry (Alibaba Cloud Container Registry) , which is the Alibaba Cloud Container Registry , is currently in use. The pull push speed is relatively fast, and it is better to use this speed in China.
Author: Technical Zemin
Publisher: Technical Translation Station