This example demonstrates step by step to create and deploy a sample MVC application with AAD in kubernetes.
- You have Visual Studio Code.
- You have an Azure Subscription. Free $200 Azure Credit
- You have an image repository (this example uses Azure container registry)
dotnet new mvc -au SingleOrg --client-id "xxxxxxxxxxxxxxxxxxx" --tenant-id "xxxxxxxxxxxxxxxxxxxxxxxx" --domain "xxxxxxxx"
Add below code in startup.cs ---> Configure program.cs
app.Use((context, next) =>
{
context.Request.Scheme = "https";
return next();
});
dotnet run
touch Dockerfile (see DockerFile from SampleApp)
docker build -t myapp:v1 .
docker image ls
az login
az acr login --name acrname
docker tag myapp:v1 acrname.azurecr.io/myapp:v1
docker push acrname.azurecr.io/myapp:v1
Skip this step, If you already have AKS created and configured
- Create Azure kubernetes Cluster
az aks create -g <resourceGroupName> --name <kubernetes-cluster-name> --service-principal <servicePrincipalId> --client-secret <clientSecret>
- Create a public (static) IP address in the resource group MC_resourceGroupName_location and note the dns name, which gives
- Configure the route traffic to the ingress controller
helm install stable/nginx-ingress \
--namespace ingress-basic \
--set controller.replicaCount=1 \
--set controller.image.repository= quay.io/kubernetes-ingress-controller/nginx-ingress-controller \
--set controller.service.loadBalancerIP="<your static Ip address>"
- Configure a DNS name: For the HTTPS certificates to work correctly, configure an FQDN for the ingress controller IP address. Update the following script with the IP address of your ingress controller and a unique name that you would like to use for the FQDN. (This step is not always necessary but good to be sure)
# Public IP address of static ip address
IP="<your static IP>"
# Name to associate with public IP address
DNSNAME="<dns name>"
# Get the resource-id of the public ip
PUBLICIPID=$(az network public-ip list --query "[?ipAddress!=null]|[?contains(ipAddress, '$IP')].[id]" --output tsv)
# Update public ip address with DNS name
az network public-ip update --ids $PUBLICIPID --dns-name $DNSNAME
- Add Reidirect URI in Azure ---> AAD -- AppRegistration https:///signin-oidc
kubectl create secret docker-registry <secret-name> --docker-server=<youracr.azurecr.io> --docker-username=<acrusername> --docker-password=<acr-password> --docker-email=<youremailaddress>
kubectl create -f ingress.yaml
A few things to note:
- We've tagged the ingress with the annotation nginx.ingress.kubernetes.io/ssl-redirect: "true".
- backend serviceName should match the name defined in file app-service.yaml
- Host should be your FQDN (fqdn from create static ip)
kubectl create -f app-deployment.yaml
A few things to note:
- image pull secret should be present if container registry is private
- image should be of format <youracr.azurecr.io>/myapp:v1 (if you are using Azure container registry)
kubectl create -f kubectl create -f app-service.yaml
you can run following commands to check deployment, services and pods
kubectl get ing -n ingress-basic
kubectl get deployment
kubectl get pods
kubectl logs <pod_name> -f