Skip to content

Latest commit

 

History

History
213 lines (144 loc) · 3.92 KB

05-kubernetes-best-practices.md

File metadata and controls

213 lines (144 loc) · 3.92 KB

Lab Guide - Kubernetes Best Practices

Exercise 1 - Resource Management

Resource Quota

  1. Create a namesapce called "bp-rq"

    kubectl create ns bp-rq
  2. Apply the Resource Quota

    kubectl apply -f ./assets/lab-05/resource-quota.yaml -n bp-rq
  3. Verify that Used is 0.

    kubectl describe quota -n bp-rq
  4. Create a high-priority pod

    kubectl apply -f ./assets/lab-05/high-priority-pod.yml -n bp-rq
  5. Verify that the quote has updated

    kubectl describe quota -n bp-rq

Limit Ranges

  1. Create a namesapce called "bp-lr"

    kubectl create ns bp-lr
  2. Apply the Limit Range

    kubectl apply -f ./assets/lab-05/limit-range.yaml -n bp-lr
  3. Describe the Limit Range

    kubectl describe limitrange/limit-mem-cpu-per-container -n bp-lr
  4. Create a Pod which define some resource

    kubectl apply -f ./assets/lab-05/resourced-pod.yaml -n bp-lr
  5. Review each container in turn

    kubectl describe po/busybox1 -n bp-lr

Exercise 2 - Auto Scaler

Cluster Autoscaler

  1. Update your AKS cluster to enable the Cluster Autoscaler.

    az aks update \
    --resource-group <aks-resource-group> \
    --name <aks-cluster-name> \
    --enable-cluster-autoscaler \
    --min-count 2 \
    --max-count 5

HPA

  1. Create a namespace called 'wordpress'

    kubectl create ns wordpress
  2. Deploy wordpress using Helm:

    helm install wp-release stable/wordpress --namespace wordpress

    We will explore Helm in detail tomorrow.

  3. Enable autoscaling:

    kubectl autoscale deployment wp-release-wordpress --cpu-percent=50 --min=1 --max=3 -n wordpress
  4. Review HPA:

    kubectl get hpa -n wordpress

Exercise 3 - Backup Your Volumes

Dynamic Provisioning

  1. Create new namespace:

    kubectl create ns bp-pvc
  2. Create a PVC using default storage class, and a pod:

    kubectl apply -f ./assets/lab-05/pvc.yaml -n bp-pvc
  3. Show disk created in Azure portal

  4. Create a pod, and mount the disk.

  5. Get a shell running, and create a new file in the mounted volume:

    kubectl exec -it mypod2 -n bp-pvc -- /bin/bash
    cd /mnt/mydemo
    hostname >> host.txt
    cat host.txt
  6. Exit the shell, and delete the Pod.

    kubectl delete pod mypod2 -n bp-pvc
  7. Recreate the pod

    kubectl apply -f ./assets/lab-05/pvc.yaml -n bp-pvc
  8. Verify the file still exists:

    kubectl exec -it mypod2 -n bp-pvc -- /bin/bash
    cd /mnt/mydemo
    ls
    cat host.txt

Back up a persistent volume

  1. Get the volume

    kubectl get pvc my-data-store2 -n bp-pvc
  2. Query for the disk Id

    az disk list --query '[].id | [?contains(@,`pvc-7f700aa1-a3bd-4017-971c-ee054c3cf26a`)]' -o tsv
  3. Create a snapshot:

    az snapshot create \
        --resource-group <cluster-mv-resource-group> \
        --name pvcSnapshot \
        --source <disk-id>

Restore and use a snapshot

  1. Create a new disk

    az disk create --resource-group <cluster-mv-resource-group> --name pvcRestored --source pvcSnapshot
  2. Get the disk ID

    az disk show --resource-group <cluster-mv-resource-group> --name pvcRestored --query id -o tsv
  3. Update "restored-disk.yaml" with the disk ID. Create a pod, mounting the restored disk:

    code ./assets/lab-05/restored-disk.yaml
    kubectl apply -f ./assets/lab-05/restored-disk.yaml -n bp-pvc
  4. Verify the file still exists:

    kubectl exec -it mypodrestored -n bp-pvc -- /bin/bash
    cd /mnt/mydemo
    ls
    cat host.txt