- Azure Command-Line Interface (Az CLI) on PowerShell, Bash or the Cloud Shell.
During the lab, you will need the name of your instances several times. To make it easier, we will start defining the environment variable to keep those names handy. If for any reason, your session closes, you will need to come back to this task and define these variables again.
NUMBER=$RANDOM
echo $NUMBER
If you loose your session during the lab, you should come back to this section and define variable number with the number that was randomly generated the first time
AKS_NAME="k8s-cluster"$NUMBER
LOCATION="westus"
REGISTRY="containerregistry"$NUMBER
RESOURCE_GROUP="aksworkshop-RG"
SUBNET_NAME="aks-subnet"
VNET_NAME="aks-vnet"
Verify the name of your instances with the following command:
echo $AKS_NAME
echo $LOCATION
echo $REGISTRY
echo $RESOURCE_GROUP
echo $SUBNET_NAME
echo $VNET_NAME
To get access to your subscription you need to login
az login
With your browser, go to http://aka.ms/devicelogin and provide the code to authenticate in your Azure Subscription.
az group create -n $RESOURCE_GROUP -l $LOCATION
az acr create \
--resource-group $RESOURCE_GROUP \
--location $LOCATION \
--name $REGISTRY \
--sku Basic
Clone the repository
git clone https://github.com/carlosalexei/aks-workshop-ratings-api.git
Change into the cloned directory
cd aks-workshop-ratings-api
Build the image by using the Azure Container Registry task
az acr build \
--resource-group $RESOURCE_GROUP \
--registry $REGISTRY \
--image ratings-api:v1 .
Change back to the home directory
cd
Clone the rating-web repo
git clone https://github.com/MicrosoftDocs/mslearn-aks-workshop-ratings-web.git
Change into the cloned directory
cd mslearn-aks-workshop-ratings-web
Build the image by using the Azure Container Registry task
az acr build \
--resource-group $RESOURCE_GROUP \
--registry $REGISTRY \
--image ratings-web:v1 .
az acr repository list \
--name $REGISTRY \
--output table
The goal of this exercise is to deploy a sample application using a container orchestrator. Since Kubernetes is the facto standard in the industry you will create a Kubernetes cluster using Azure Kubernetes Service.
az network vnet create -g $RESOURCE_GROUP \
-l $LOCATION \
--name $VNET_NAME \
--address-prefixes 10.0.0.0/8 \
--subnet-name $SUBNET_NAME \
--subnet-prefix 10.240.0.0/16
SUBNET_ID=$(az network vnet subnet show \
--resource-group $RESOURCE_GROUP \
--vnet-name $VNET_NAME \
--name $SUBNET_NAME \
--query id -o tsv)
echo $SUBNET_ID
The following command will get the latest, non-preview, Kubernetes version
VERSION=$(az aks get-versions -l $LOCATION --query 'min_by(values, &version).version' -o tsv)
echo $VERSION
az aks create \
-g $RESOURCE_GROUP \
-n $AKS_NAME \
-l $LOCATION \
--vm-set-type VirtualMachineScaleSets \
--node-count 2 \
--load-balancer-sku standard \
--kubernetes-version $VERSION \
--network-plugin azure \
--vnet-subnet-id $SUBNET_ID \
--service-cidr 10.2.0.0/24 \
--dns-service-ip 10.2.0.10 \
--docker-bridge-address 172.17.0.1/16 \
--generate-ssh-keys
az aks update \
--name $AKS_NAME \
--resource-group $RESOURCE_GROUP \
--attach-acr $REGISTRY
sudo az aks install-cli
az aks get-credentials \
-g $RESOURCE_GROUP \
-n $AKS_NAME
Run kubectl to list all nodes
kubectl get nodes
Get the list of pods in the default namespace
kubectl get pods
Get the list of pods in all namespaces
kubectl get pods --all-namespaces
List the current namespaces in the cluster
kubectl get namespace
Create a namespace for the application
kubectl create namespace ratingsapp
helm repo add bitnami https://charts.bitnami.com/bitnami
helm search repo bitnami
MONGO_USER="Mongo.User"
MONGO_PASSWORD="Mongo.Password123"
echo $MONGO_USER
echo $MONGO_PASSWORD
helm install ratings bitnami/mongodb \
--namespace ratingsapp \
--set auth.username=$MONGO_USER,auth.password=$MONGO_PASSWORD,auth.database=ratingsdb
Create a secret to stoge the MongoDB connection
kubectl create secret generic mongosecret \
--namespace ratingsapp \
--from-literal=MONGOCONNECTION="mongodb://$MONGO_USER:$MONGO_PASSWORD@ratings-mongodb.ratingsapp:27017/ratingsdb"
Validate the secret
kubectl describe secret mongosecret --namespace ratingsapp
Use the following commands to check the resources created by the Helm chart:
List deployments
kubectl get deployments -n ratingsapp
Get the list of pods
kubectl get pods -n ratingsapp
Get services
kubectl get services -n ratingsapp
vi ratings-api-deployment.yaml
Paste the following text in the file
apiVersion: apps/v1
kind: Deployment
metadata:
name: ratings-api
spec:
selector:
matchLabels:
app: ratings-api
template:
metadata:
labels:
app: ratings-api # the label for the pods and the deployments
spec:
containers:
- name: ratings-api
image: <ACR>.azurecr.io/ratings-api:v1 # IMPORTANT: update with your own repository
imagePullPolicy: Always
ports:
- containerPort: 3000 # the application listens to this port
env:
- name: MONGODB_URI # the application expects to find the MongoDB connection details in this environment variable
valueFrom:
secretKeyRef:
name: mongosecret # the name of the Kubernetes secret containing the data
key: MONGOCONNECTION # the key inside the Kubernetes secret containing the data
resources:
requests: # minimum resources required
cpu: 250m
memory: 64Mi
limits: # maximum resources allocated
cpu: 500m
memory: 256Mi
readinessProbe: # is the container ready to receive traffic?
httpGet:
port: 3000
path: /healthz
livenessProbe: # is the container healthy?
httpGet:
port: 3000
path: /healthz
Update the <ACR>
value in the image
key with the name of your Azure Container Registry instance.
sed -i 's+<ACR>+'"$REGISTRY"'+g' ratings-api-deployment.yaml
kubectl apply \
--namespace ratingsapp \
-f ratings-api-deployment.yaml
vi ratings-api-service.yaml
Paste the following text in the file
apiVersion: v1
kind: Service
metadata:
name: ratings-api
spec:
selector:
app: ratings-api
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: ClusterIP
kubectl apply \
--namespace ratingsapp \
-f ratings-api-service.yaml
vi ratings-web-deployment.yaml
Paste the following text in the file
apiVersion: apps/v1
kind: Deployment
metadata:
name: ratings-web
spec:
selector:
matchLabels:
app: ratings-web
template:
metadata:
labels:
app: ratings-web # the label for the pods and the deployments
spec:
containers:
- name: ratings-web
image: <ACR>.azurecr.io/ratings-web:v1 # IMPORTANT: update with your own repository
imagePullPolicy: Always
ports:
- containerPort: 8080 # the application listens to this port
env:
- name: API # the application expects to connect to the API at this endpoint
value: http://ratings-api.ratingsapp.svc.cluster.local
resources:
requests: # minimum resources required
cpu: 250m
memory: 64Mi
limits: # maximum resources allocated
cpu: 500m
memory: 512Mi
Update the <ACR>
value in the image
key with the name of your Azure Container Registry instance.
sed -i 's+<ACR>+'"$REGISTRY"'+g' ratings-web-deployment.yaml
kubectl apply \
--namespace ratingsapp \
-f ratings-web-deployment.yaml
vi ratings-web-service.yaml
Paste the following text in the file
apiVersion: v1
kind: Service
metadata:
name: ratings-web
spec:
selector:
app: ratings-web
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
kubectl apply \
--namespace ratingsapp \
-f ratings-web-service.yaml
kubectl get services --namespace ratingsapp
Navigate to the IP Address displayed on the EXTERNAL-IP
column