We’re going to be deploying our web services to k8s using Docker, so first off we need to create a registry (if you don’t already have one) on Azure.
- Go to the Azure Dashboard and select Create a resource
- Locate and click on Container Registry
- Click on Create
- Supply the Resource Group you want to use, I’m using the one I created for my previous post Creating and using the Azure Service Bus
- Create a registry name, mines apptestregistry
- Select your location and SKU, I’ve gone Basic on the SKU
Now click Review + create. Review your options and if all looks correct then click Create. Now wait for the Deployment to complete and go to the resource’s dashboard where you’ll see (at least on the current Dashboard) options to Push a Container image, Deployment a container image etc.
Adding a Kubernetes service
We now need to return to the main dashboard to select Containers | Kubernetes services or just type Kubernetes services into the Dashboard search bar.
- In Kubernetes services click Create
- Click Create a Kubernetes cluster
- Supply a Resource Group
- For Cluster preset configuration choose Dev/Test for now
- Enter a Kubernetes cluster name, mine’s testappcluster
- Fill in the rest of the options to suite your location etc.
Now click Review + create.
Stop, don’t press create yet. Before we click create, go to the Integrations tab and set the Container Registry to the one we created – if you don’t do this then you’ll get 401’s when trying to deploy from your registry into K8s.
Note: There is a way to create this integration between k8s and your registry later, but it is so much simpler letting the Dashboard do the work for us.
Now, review your options and if happy all looks correct, click Create.
Note: I kept getting an error around quota’s on the setup above, I found if you reduce the autoscaling slider/values (as mine showed it would be maxed out) then this should pass the review phase.
Once the deployment in complete (and it may take a little while) we’ll need something to push to it…
Creating a simple set of microservices
- Using Visual Studio, create a new ASP.NET Core Web API, make sure to have Docker support checked
- Delete the Weatherforecast controller and domain object
- Right mouse click on Controllers and select Add | Controller
- Select an empty controller
Note: We’re going to use the Azure CLI tools, so if you’ve not got the latest, go to https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli#install-or-update and install from here
Now let’s dockerize our WebApi…
Note: Replace apptestregistry with your registry name and I’m not going to include th az login etc. and my service within the registry is called myname, so replace with something meaningful. Also ver01: is the version I’m assigning to the image.
- Copy your Docker file for your WebApi into the solution folder of your project
- Run az acr build -t myname:ver01 -r apptestregistry .
That’s all there is to it, repeat for each WebApi you want to deploy.
To check all went well. We’re going to use the following az command to list the Azure registry (change the registry name to yours)
az acr repository list --name apptestregistry
Or you can go to the Azure container registry dashboard and select Repositories
Deploying to Kubernetes
At this point we’ve created our WebApi, dockerized it and deployed to the Azure registry of our choice, but to get it into k8s we need to create the k8s manifests.
We create a YML file, for example kubernetes-manifest-myname.yaml (change myname to the name of your service as well is with the YML below, don’t forget to change apptestregistry to your registry name also)
apiVersion: apps/v1
kind: Deployment
metadata:
name: myname
labels:
app: myname
spec:
selector:
matchLabels:
app: myname
replicas: 1
template:
metadata:
labels:
app: myname
spec:
containers:
- name: myname
image: apptestregistry.azurecr.io/myname:ver01
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 200m
memory: 200Mi
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: myname
spec:
ports:
- port: 80
selector:
app: myname
---
We need to set up kubectl access from Terminal/PowerShell using
az aks get-credentials -n testappcluster -g test-app-cluster
kubectl apply -f .\kubernetes-your-manifest-file-yaml
We can check that the pods exist using
kubectl get pods
If you notice Status or ImagePullBackOff then it’s possible you’ve not set up the integration of k8s and your registry, to be sure type “kubectl describe pod” along with your pod name to get more informations.
and finally, let’s check the status of our services using
kubectl get services
In the Azure Kubernetes services dashboard you can select Services and ingresses to view the newly added service(s).
We can see Cluster IP address which is an internal IP address. So for example if our myname service has Cluster IP 10.0.40.100 then other services deployed to the cluster can interact with the myname service by this IP.
External facing service
We’ve created an service which is hosted in a pod within k8s but we have no external interface to it. A simple way to create an external IP from our service is by declaring a service as a load balancer which routes calls to the various services we’ve deployed.
So let’s assume we created a new WebApi, with Docker support and we added the following controller which routes operations to a single endpoint in this case, but ofcourse it could route to any WebApi that we’ve deployed via it’s Cluster IP
[ApiController]
[Route("[controller]")]
public class MathController : Controller
{
[HttpGet("Get")]
public async Task<string> Get(string op)
{
var httpClient = new HttpClient();
using var response = await httpClient.GetAsync($"http://10.0.40.100/myname/get?operation={op}");
return await response.Content.ReadAsStringAsync();
}
}
Once we have an External IP associate with this load balance type of service, then we can access from the web i.e. http://{external-ip}/myservice/get?op=display