Skip to content

Latest commit

 

History

History
495 lines (383 loc) · 10.7 KB

01.basic-aks.md

File metadata and controls

495 lines (383 loc) · 10.7 KB

Hands-on Lab - Day 1

Tools for this Lab

  • Azure Command-Line Interface (Az CLI) on PowerShell, Bash or the Cloud Shell.

Preparation

Task 1 - Define environment variable to keep the names of your instances

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.

Bash
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

Task 2 - Get access to your subscription

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.

Task 3 - Create a new resource group

az group create -n $RESOURCE_GROUP -l $LOCATION

Exercise 1 - Create a private container registry

Task 1 - Create a container registry

az acr create \
    --resource-group $RESOURCE_GROUP \
    --location $LOCATION \
    --name $REGISTRY \
    --sku Basic

Task 2 - Build the ratings-api image

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 .

Task 3 - Build the ratings-web image

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 .

Task 4 - Verify the images

az acr repository list \
    --name $REGISTRY \
    --output table

Exercise 2 - Deploy Kubernetes with AKS

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.

Task 1 - Create a virtual network and a subnet for Pods

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

Task 2 - Retrieve and store the subnet ID

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

Task 3 - Get the latest Kubernetes version

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

Task 4 - Create the AKS cluster

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

Task 5 - Configure the AKS cluster to authenticate to the container registry

az aks update \
    --name $AKS_NAME \
    --resource-group $RESOURCE_GROUP \
    --attach-acr $REGISTRY

Exercise 3 - Managing Kubernetes

Task 1 - Install kubectl (Kubernetes Client)

sudo az aks install-cli

Task 2 - Test cluster connectivity by using kubectl

az aks get-credentials \
    -g $RESOURCE_GROUP \
    -n $AKS_NAME

Task 3 - Use kubectl to manage kubernetes elements

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

Task 4 - Create a Kubernetes namespace for the application

List the current namespaces in the cluster

kubectl get namespace

Create a namespace for the application

kubectl create namespace ratingsapp

Exercise 3a - Deploy MongoDB

Task 1 - Add the Helm bitname repository

helm repo add bitnami https://charts.bitnami.com/bitnami

Task 2 - List available charts

helm search repo bitnami

Task 3 - Define a password for MongoDB

MONGO_USER="Mongo.User"
MONGO_PASSWORD="Mongo.Password123"
echo $MONGO_USER
echo $MONGO_PASSWORD

Task 4 - Install a Helm Chart

helm install ratings bitnami/mongodb \
    --namespace ratingsapp \
    --set auth.username=$MONGO_USER,auth.password=$MONGO_PASSWORD,auth.database=ratingsdb

Task 5 - Create a Kubernetes secret to hold MongoDB connection string

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

Task 6 - Verify elements created by the Helm Chart

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

Exercise 3b - Deploy the ratings API

Task 1 - Create a manifest file for the Kubernetes deployment called ratings-api-deployment.yaml

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

Task 2 - Update the manifest file with your registry (ACR)

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

Task 3 - Apply the configuration represented on the manifest file

kubectl apply \
    --namespace ratingsapp \
    -f ratings-api-deployment.yaml

Task 4 - Create a manifest file for a Kubernetes Service

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

Task 5 - Apply the configuration represented on the manifest file

kubectl apply \
    --namespace ratingsapp \
    -f ratings-api-service.yaml

Exercise 3c - Deploy the ratings front end

Task 1 - Create a manifest file for the Kubernetes deployment called ratings-web-deployment.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

Task 2 - Update the manifest file with your registry (ACR)

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

Task 3 - Apply the configuration represented on the manifest file

kubectl apply \
    --namespace ratingsapp \
    -f ratings-web-deployment.yaml

Task 4 - Create a manifest file for a Kubernetes Service

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

Task 5 - Apply the configuration represented on the manifest file

kubectl apply \
    --namespace ratingsapp \
    -f ratings-web-service.yaml

Task 6 - Check exposed services and assigned IP address

kubectl get services --namespace ratingsapp

Task 7 - Check the application is running

Navigate to the IP Address displayed on the EXTERNAL-IP column