Deploying An Asp.Net Core WebApi and MySql DataBase Container to Azure Kubernetes Service
Tags: kubernetes, azureapril, mysql, docker
Published: 2021-04-02 17:14:57

Azure Kubernetes Service(AKS) provides a fast and efficient way to deploy a containerized application to Azure. In this tutorial, we're going to be walking through how to deploy a.Netcore web API and MySQL database to AKS.

This is a  follow-up to my previous article on Deploying a. Netcore web API and  MySQL on Kubernetes. You can read the previous articles [here](https://dev.to/gbengelebs/deploying-an-asp-net-webapi-and-mysql-database-container-to-kubernetes-part-1-setup-1ll9)

###Prerequisites
1. Azure CLI. Install it [here](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
2. An Azure account. You can create it for free [here](https://portal.azure.com/free). 
_Ps you get 200dollars free Azure credits and 12month's pay as you go._
3. Docker [docker](https://docs.docker.com/get-docker/)
4. VSCODE [vscode](https://code.visualstudio.com/download)
5. GitHub branch finished [here] (https://github.com/GbengaElebsDev/TestApi.git)

![Azure Aks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ncmaybf0r0pwh1hrxgy.png)
This is an infographic of the different stages involved in an azure kubernetes deployment. We will be skipping the Azure Pipeline in this article. I will be covering it in the next article.
###STEPS
**1. Clone the git hub project using this command**
```
$ git clone --single-branch --branch Finished https://github.com/GbengaElebsDev/TestApi.git
```
run a dotnet restore, and build the images using
```
$ docker-compose -f docker-compose.yml up

$ mac@Gbengas-MBP TestApi % docker-compose -f docker-compose.yml up
testapi_database_1 is up-to-date
Recreating testapi_api_1 ... done
Attaching to testapi_database_1, testapi_api_1
```
**2. Login to your Azure account**

- Create a Resource and Search for -> Container Registry
**A container registry is an online repository for your containers. Fill in the details and create a new resource group.**
_A resource group is a collection of all the resources (Virtual machines, databases, and web-apps)for a project._
![Azure Container Registry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5wzky0kg05ywi77r755.png)

**3. Go to VSCode terminal and login to azure with this command** 
```
$ az login
```
- You will be redirected to a browser login

- Check if you are in the right subscription
```
$ az account show
```

**4.Create an Azure service principal with this command and copy your appid, displayName and password**

```
$ az ad sp create-for-rbac --skip-assignment
```
_An Azure service principal is an identity created for use with applications, hosted services,  and automated tools to access Azure resources. This access is restricted by the roles assigned to the service principal, giving you control over which resources can be accessed and at which level._
You can look at it like an admin for your app resource which you configure its authorization level. Kubernetes can use this service principal to authenticate and pull from the Azure Container Registry. 

_Role-based access control (RBAC) is a method of restricting network access based on the roles of individual users within an enterprise._

- use --skip-assignment to avoid creating a role assignment.

**5. Assign this role to the resource group**
Click on the notifications icon -> Go to resource -> Add role assignments
**Select the Acr role**. We want to assign the lowest permission level for a role to perform its duty and in this project, we want the service principal to be able to access our Container Registry. 
- Search for the service principal using the display name and input it.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xx0tx4rat8k4ri2ypz7.png)

**6. Create Azure Kubernetes Service** 
Search the marketplace for Azure Kubernetes Service and fill in the details
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhlzjmlg8sgir0xkn2oi.png)
- Select the same resource group as the container registry.
- Give your cluster a name
- Since this is a demo. Let's change the node count to one and the node size to the lowest cost option which in my case is **Standard B2s**
- Click on authentication and select use existing service principal.
- Configure service principal using the app id and password gotten from 4 above.
![Aks Service Principal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ago94kmallfbwcxa9hq.png)

- Go to the vscode terminal and login to the ACR using the name of the ACR you created.
```
$ az acr login --name <nameofacr>

Login Succeeded
```

**7. Retag the images to use the ACR login server**
In my case
```
$ docker tag testapi_api gbengaelebs.azurecr.io/testapi_api 
```
```
$ docker tag mysql:8.0.22 gbengaelebs.azurecr.io/mysql:8.0.22
```
check that the images have been retagged
```
$ docker images

gbengaelebs.azurecr.io/testapi_api   latest                                                  4c9790679e3f   2 months ago    220MB
```

**8. Push the images to the container registry**
```
$  docker push gbengaelebs.azurecr.io/testapi_api
$  docker push gbengaelebs.azurecr.io/mysql:8.0.22 
```

- Navigate to repositories and check for your images.
![Azure Repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49xpcouxuj2pq3hja32v.png)

- Amend the deployment files, change the container images to point to the azure container repo, and comment out the imagePullPolicy line as shown below.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0b639868a0j4wrohixye.png)

**9. Go to your Azure Kubernetes cluster and connect to it** 

```
$ az aks get-credentials --resource-group <resourcegroup> --name <resourcegroup>
```
In my case 
```
$ az aks get-credentials --resource-group aksdemo --name aksdemo

Merged "aksdemo" as current context in /Users/mac/.kube/config

```
**10. Run the following commands to deploy the MYSQL and webapi deployment to Kubernetes**
```
$ kubectl apply -f mysql-pv-claim.yaml

$ kubectl apply -f mysqlDeployment.yaml

$ kubectl apply -f mysqlService.yaml 

$ kubectl apply -f deployment.yaml

$ kubectl apply -f service.yaml 
```
Run this command to check  services
```
$ kubectl get services

NAME              TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
kubernetes        ClusterIP      10.0.0.1       <none>          443/TCP          152m
mysql8-service    NodePort       10.0.83.21     <none>          3306:30405/TCP   2m36s
testapi-service   LoadBalancer   10.0.228.251   52.149.190.22   8080:31787/TCP   89s
```
**11. Copy the external IP and navigate to the URL**
(http://external-ip:8080/swagger/index.html)
Mine is (http://52.149.190.22:8080/swagger/index.html). Test the API deployment using the swagger page as shown. Our containerized API is now running inside the Azure Kubernetes service and exposed to the internet via the Load Balancer Service. In the next article, we will be automating the whole deployment process using Azure Pipelines via Azure DevOps.
![Swagger Api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otfqolpcguc38w9k1i9q.png)

**In this article we have:**
- Created and configured an Azure Container Registry
- Created and configured an Azure Kubernetes Service
- Deployed our local images to Azure Container Registry
- Deployed our ACR images to Azure Kubernetes Service
- Exposed our API service to Kubernetes via a Load Balancer

Follow me here and across my social media for more content like this [Twitter](https://twitter.com/ElegberunDaniel?s=09) [Linkedin](https://www.linkedin.com/in/olugbenga-elegberun/)