Take a previously created microservice and deploy it to Azure, using DockerHub and Azure Kubernetes Service (AKS).
Push to Docker Hub
Docker Hub is a central place to upload Docker images. Many products, including Microsoft Azure, can create containers based on images in Docker Hub.
Sign in to Docker Hub
In your command prompt, run the following command:
In your terminal, run the following command:
Command prompt
docker login
Use the username and password created when you downloaded Docker in the previous tutorial. You can visit the Docker Hub website to reset your password if needed.
Upload image to Docker Hub
Re-tag (rename) your Docker image under your username and push it to Docker Hub using the following commands:
Command prompt
docker tag mymicroservice [YOUR DOCKER USERNAME]/mymicroservicedocker push [YOUR DOCKER USERNAME]/mymicroservice
The push command will upload your image to Docker Hub, which may take some time.
Set up Azure tools
Create an Azure account
If you're new to Azure, you can create a free account. If you have an existing account, you can skip this step.
Once you've installed, open a new command prompt and sign in to your Azure account by running the following command:
Once you've installed, open a new terminal and sign in to your Azure account by running the following command:
Command prompt
az login
Install AKS CLI
Kubernetes is a container orchestration platform. An orchestrator is responsible for running, distributing, scaling, and healing apps comprised of a collection of containers. Azure Kubernetes Service (AKS) provides Kubernetes as a managed service.
Run the following command to install the command-line tools for AKS:
Command prompt
az aks install-cli
You may be alerted with recommendations to set system PATH variables. These are not required for this tutorial.
Create Azure resources
Create a resource group
A resource group is used to organize a set of resources related to a single app.
Run the following command to create a resource group on the West US region:
Command prompt
az group create --name MyMicroserviceResources --location westus
If you want to use a different location on the previous command, you can run the following command to see which regions are available on your account and pick one closer to you:
Command prompt
az account list-locations -o table
If you want to use a different subscription for the session you can get a list of all subscriptions by running the following command:
Command prompt
az account list --all
Then you can run the following command to set a specific subscription for the session:
Command prompt
az account set -s NAME_OR_ID
Create an AKS cluster
Run the following command to create an AKS cluster in the resource group:
It's normal for this command to take several minutes to complete.
Command prompt
az aks create --resource-group MyMicroserviceResources --name MyMicroserviceCluster --node-count 1 --enable-addons http_application_routing --generate-ssh-keys
Run the following command to download the credentials to deploy to your AKS cluster:
Command prompt
az aks get-credentials --resource-group MyMicroserviceResources --name MyMicroserviceCluster
Deploy to Azure
Return to app directory
Since you opened a new command prompt in the previous step, you'll need to return to the directory you created your service in.
Since you opened a new terminal in the previous step, you'll need to return to the directory you created your service in.
Command prompt
cd MyMicroservice
Create a deployment file
The AKS tools use a .yaml file to define how to deploy your container.
Create a file called deploy.yaml with this command:
Command prompt
touch deploy.yaml
Command prompt
fsutil file createnew deploy.yaml 0
You can then open it in your favorite text editor.
You can then open it in your favorite text editor manually or with this command:
Command prompt
open deploy.yaml
Command prompt
start deploy.yaml
Replace the content of the deploy.yaml to the following in the text editor, making sure to replace [YOUR DOCKER ID] with your actual Docker ID.
Run the following command to deploy your microservice based on the settings in deploy.yaml:
Command prompt
kubectl apply -f deploy.yaml
Test your deployed service
Run the following command to see the details of your deployed service:
Command prompt
kubectl get service mymicroservice --watch
Among other things, the previous command will show the external IP address that your service is available on (EXTERNAL-IP).
Using the external IP address, open a new browser window and navigate to http://[YOUR EXTERNAL IP ADDRESS]/weatherforecast
If the EXTERNAL-IP is marked as <pending>, a new line will automatically appear once the external IP has been allocated.
Press CTRL+C on your command prompt to end the kubectl get service command.
Press CTRL+C on your terminal to end the kubectl get service command
Congratulations! You've deployed a microservice to Azure.
Scale your service
A benefit of using Kubernetes is that you can easily scale up a deployment to multiple instances to handle additional load. Currently, there's only a single instance, so let's scale to two instances.
Run the following command to scale your service up to two instances: