Setting up an Azure Kubernetes Cluster for a .NET Core MVC app

  • An Azure subscription
  • A .NET MVC Core app
  • Docker or Docker Toolbox
  • A private container registry (for instance Azure Container Registry )

Step 1: Create a Kubernetes cluster

az group create -l westeurope -n k8s
az aks create --resource-group k8s --name cluster01 --generate-ssh-keys --node-vm-size Standard_A1_v2 --node-count 1 --enable-addons http_application_routing
az aks get-credentials --resource-group=k8s --name=cluster01
kubectl get nodes
NAME STATUS ROLES AGE VERSION
aks-nodepool1-12041136-0 Ready agent 10m v1.9.6

Step 2: Dockerize the .NET Core MVC app

FROM microsoft/aspnetcore-build:2.0 AS build-env 
WORKDIR /app COPY . ./
RUN dotnet publish ./OMT.Web/OMT.Web.csproj -c Release -o out
# Build runtime image FROM microsoft/aspnetcore:2.0
WORKDIR /app EXPOSE 80
COPY --from=build-env /app/OMT.Web/out .
ENTRYPOINT ["dotnet", "OMT.Web.dll"]
docker build -t omt.web . -f Web
docker login marcmathijssen.azurecr.io -u xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -p myPassword
docker tag omt.web:latest marcmathijssen.azurecr.io/omt/web docker push marcmathijssen.azurecr.io/omt/web
The push refers to a repository [marcmathijssen.azurecr.io/omt/web]
672fff0c4445: Pushing [=====================================> ] 31.99MB/43.13MB

Step 3: Deploy the container in Kubernetes

  • Deployment: Configuring and creating of all of the below
  • Pods: One or multiple containers that will be guaranteed to run on the same node (handy if you have multiple containers that depend on each other presence on the same node)
  • Services: Allows traffic to your pods
  • Ingress: Allows traffic to your services through domain names
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: omtweb
spec:
template:
metadata:
labels:
app: omtweb
spec:
containers:
- image: marcmathijssen.azurecr.io/omt/web
name: omtweb
ports:
- containerPort: 80
imagePullSecrets:
- name: marcmathijssencr
---
apiVersion: v1
kind: Service
metadata:
name: omtweb
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: omtweb
type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: omtweb
annotations:
kubernetes.io/ingress.class: addon-http-application-routing
spec:
rules:
- host: ourmixtape.net
http:
paths:
- backend:
serviceName: omtweb
servicePort: 80
path: /
- host: www.ourmixtape.net
http:
paths:
- backend:
serviceName: omtweb
servicePort: 80
path: /
kubectl create secret docker-registry marcmathijssencr --docker-server=marcmathijssen.azurecr.io --docker-username=marcmathijssen --docker-password=<my-key> --docker-email=marcmathijssen@gmail.com
kubectl create -f omt.yaml
deployment "omtweb" created 
service "omtweb" created
ingress "omtweb" created
kubectl get ingNAME HOSTS ADDRESS PORTS AGE
omtweb ourmixtape.net,www.ourmixtape.net 13.80.99.67 80 10m

Tips

  • The tutorials on the Kubernetes website are pretty good if you need more knowledge on Kubernetes itself.
  • You can view your Azure Kubernetes dashboard by using the Azure CLI powershell
  • Test your container on your machine first before you push it to the registry (it’s harder to debug it there :-))

--

--

--

.NET developer living in The Netherlands. I write stories because it helps me sharing and remembering knowledge.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create your first PHP page

Kotlin for Android Development

CS 373 Spring 2020: Namit Agrawal

Setting Up Queue Worker in Laravel to a server eg Digital Ocean

Sustainable Test Automation: From Garden To Ecosystem

Weekly Recap Aug 8 — Aug 15

🔎 GEMS Airdrop 💵 Get 2.5 USDT (for 1k random users)

When Worlds Collide: Collaborative Docs and Work Management

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marc Mathijssen

Marc Mathijssen

.NET developer living in The Netherlands. I write stories because it helps me sharing and remembering knowledge.

More from Medium

What is Kubernetes?

Integrating Apache Kafka with Mule-4 & Anypoint Studio 7.8 on Windows OS

CICD with Jenkins Pipelines Running in Docker

Injecting Fault in Azure Cache for Redis using Azure Chaos Studio through Rest API (Part 2)