Skip to content

Latest commit

 

History

History
315 lines (226 loc) · 8.99 KB

03-state-config-and-jobs.md

File metadata and controls

315 lines (226 loc) · 8.99 KB

Lab 03 - State, config and jobs


Prerequisites

  • You have completed Lab 02 - Pods, Deployments and Services.

  • All operations in this exercise should be performed in the default namespace.

    Top tip: You can set 'default' as the default namespace.

    kubectl config set-context $(kubectl config current-context) --namespace=default

State Persistance

The Kubernetes Volume is simply a directory on disk mapped to the pod that allows you to store and share data usually beyond the lifetime of a pod.

Mounting emptyDir

  1. Create busyboxvol pod with two containers (c1 and c2), each one will have the image busybox and will run the 'sleep 3600' command. Make both containers mount an emptyDir at '/etc/foo'.

    # Create Pod from the script file
    kubectl apply -f ~/ckad/labs/scripts/04-state-emptyDir.yaml

    Open YAML File look how Volume and Volume mounts are performed

  2. Connect to the first container c1 , write current date time in the file /etc/foo/mydata.txt

    kubectl exec -it busyboxvol -c c1 -- /bin/sh
    ls /etc/foo/ # confirm dir is empty
    echo $(date) > /etc/foo/mydata.txt
    cat /etc/foo/mydata.txt # confirm that stuff has been written successfully
    exit

    Notice /etc/foo/ directory has been mounted onto the container

  3. Connect to the second container c2 and read /etc/foo/mydata.txt file to standard output.

    kubectl exec -it busyboxvol -c c2 -- /bin/sh
    cat /etc/foo/mydata.txt
    exit

    Notice that two containers within pod busyboxvol share the directory

Storage Classes

  1. List all the storage class available on your cluster

    kubectl get sc
  2. Create a PersistentVolumeClaim for azure storage class default, called mypvc, a request of 1Gi with an access mode of ReadWriteOnce.

    # Create PersistentVolumeClaim from the script file
    kubectl apply -f ~/ckad/labs/scripts/04-state-mypvc.yaml

    Open YAML File look how PersistentVolumeClaim is written.

  3. Show the PersistentVolumes and PersistentVolumeClaims of the cluster

    # creation can take time, press ctrl+c to exit watch loop once pv and pvc are created
    kubectl get pv -w
    kubectl get pvc -w
  4. Create a nginxvol pod running nginx image and Mount the PersistentVolumeClaim to '/etc/foo'.

    # Create Pod from the script file
    kubectl apply -f ~/ckad/labs/scripts/04-state-mount-pvc.yaml

    Open YAML File look how Volume and Volume mounts are performed

  5. Connect to the 'nginxvol' pod, and copy the '/etc/passwd' file to '/etc/foo'

    kubectl exec nginxvol -it -- cp /etc/passwd /etc/foo/passwd
  6. Delete nginxvol pod

    kubectl delete po nginxvol
  7. Recreate nginxvol pod running nginx image and Mount the PersistentVolumeClaim to '/etc/foo'.

    # Create Pod from the script file
    kubectl apply -f ~/ckad/labs/scripts/04-state-mount-pvc.yaml
  8. Connect to the 'nginxvol' pod, and list all files in '/etc/foo'

    kubectl exec nginxvol ls /etc/foo

    Notice files persisted, even after pod was deleted and recreated.

Config

ConfigMaps and Secretes

  1. Create a configmap named myconfig with values foo=lala,foo2=lolo

    kubectl create configmap myconfig --from-literal=foo=lala --from-literal=foo2=lolo
  2. Create a secret called mysecret with the values password=mypass

    kubectl create secret generic mysecret --from-literal=password=mypass
  3. Create a new nginx pod that loads the value from configmap myconfig -> foo in an env variable called 'option'. Also load secret 'mysecret' as a volume inside an nginx pod on path /etc/secrets.

    # Create Pod from the script file
    kubectl apply -f ~/ckad/labs/scripts/04-state-configs.yaml
  4. Check environment variable option and /etc/secrets has expected values

    kubectl exec -it nginx -- env | grep option
    kubectl exec -it nginx -- ls /etc/secrets

Jobs

  1. Create CronJob called sleepycron that runs pod busy box with sleep 5 command every minute

    kubectl create cronjob sleepycron --image=busybox --schedule "*/1 * * * *" -- sleep 5

    Try command below if the above fails, it is because you are using older version of AZ CLI

    kubectl run sleepycron --image=busybox --restart=OnFailure --schedule="*/1 * * * *" -- sleep 5
  2. List all the CronJob, Jobs and Pods

    kubectl get cj,job,pod -w # observe every minute job and pods will be created

Fireworks scenario

SignalR based application that allows website users to light fireworks and display on all the connected site users. You can light single or multi shot using the app. There is also a button that can stimulate a crash /home/admin. Pressing the button again will make the application run again.

Port Exposed

  • 80
  • Green: kunalbabre/fireworks:green
  • Blue: kunalbabre/fireworks:blue
  • Red: kunalbabre/fireworks:red
  • Yellow: kunalbabre/fireworks:yellow

Trigger fireworks manually

  • Trigger Single - /home/singleshot
  • Trigger Multishot - /home/multishot

Environment variables

  • SIGNALR_CS : (optional) if you wish to scale-out you can provide connection string for Redis or Azure SignalR
  • APP_COLOR: (works with latest tag): you can specify theme color for the app (red,green, blue, yellow)

Health Monitoring

  • Liveness - /home/isRunning

    • returns HTTP 200 if the application is alive
    • returns HTTP 400 if the application has crashed
  • Readiness - /home/isRunning

    • returns HTTP 200 if the application is alive
    • returns HTTP 400 if the application has crashed

Core goals

  1. Configure Backplane for Fireworks App to use Azure SignalR Download Encrypted File Here.

    Fireworks app supports Signalr backplane allowing it to scale out and can be specified using an envirnment variable called SIGNALR_CS. The value should be a connection string for Redis or Azure SignalR

    hint

    apiVersion: v1
    kind: Pod
    metadata:
    name: sample-pod
    spec:
    containers:
    - name: mycontainer
        image: someImage
        env: #
        - name: SomeName #
          value: SomeValue #

  2. Simulate shooting of 10's of multishot every 1 minute, take advantage of parallel and completions properties of Job (tip: create CronJob)

    hint

    # generate YAML for CronJob to get started
    
    kubectl run sleepycron --image=busybox --restart=OnFailure --schedule="*/1 * * * *" --dry-run -o yaml -- /bin/sh -c "for i in 1 2 3 4 5;do wget fireservice:80/home/multiShot; sleep 1;done" > firecron.yaml
    
    # modify file and add completion and parallel attribute
    code firecron.yaml
    
    # apply changes
    kubectl apply -f firecron.yaml

    Sample CronJob with parallelism and Completion attributes.

    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
    name: sleepycronjob
    spec:
      parallelism: 2
      completions: 60
      template:
        metadata:
        spec:
          containers:
          - command:
            - /bin/sh
            - -c
            - for i in 1 2 3 4 5;do wget foo:80/home/multiShot; sleep 1;done
            image: busybox
            name: firecron
            resources: {}
          restartPolicy: OnFailure
        schedule: '*/1 * * * *'

Stretch goals

  1. Modify the deployment to populate SIGNALR_CS env variable from Secrets config

    hint

    # create Secret 
    kubectl create secret generic mysecret --from-literal=<name>=<value>
    
    # modify your deployment and add env variable from secret under  
    kubectl edit deploy 

    Sample Pod using environment variable from secret

    apiVersion: v1
    kind: Pod
    metadata:
    name: sample-cret-env-pod
    spec:
    containers:
    - name: mycontainer
        image: someImage
        env: #
        - name: SECRET_USERNAME #
            valueFrom: #
            secretKeyRef: #
                name: mysecret #
                key: username #