Skip to content

Latest commit

 

History

History
257 lines (202 loc) · 6.63 KB

7. Volumes and Configs.md

File metadata and controls

257 lines (202 loc) · 6.63 KB

Volumes and Configs

Prerequisites

  • 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'.

    hint

    apiVersion: v1
    kind: Pod
    metadata:
     name: busyboxvol
    spec:
     volumes: # specify the volumes
        - name: myvolume # this name will be used for reference inside the container
          emptyDir: {}
     containers:
        - image: busybox
          imagePullPolicy: IfNotPresent
          name: c1
          command: ['sleep']
          args: ['3600']
          volumeMounts: # your volume mounts are listed here
          - name: myvolume # the name that you specified in pod.spec.volumes.name
            mountPath: /etc/foo # the path inside your container   
        - image: busybox
          imagePullPolicy: IfNotPresent
          name: c2
          command: ['sleep']
          args: ['3600']
          volumeMounts: # your volume mounts are listed here
          - name: myvolume # the name that you specified in pod.spec.volumes.name
            mountPath: /etc/foo # the path inside your container   

  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.

    hint

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
     name: mypvc
    spec:
     accessModes:
     - ReadWriteOnce
     storageClassName: default
     resources:
      requests:
       storage: 1Gi

  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'.

    hint

    apiVersion: v1
    kind: Pod
    metadata:
     name: nginxvol
    spec:
     volumes:
      - name: my-volume
        persistentVolumeClaim: #
         claimName: mypvc #
     containers:
     - image: nginx:1.15.5
       name: mypod
       volumeMounts:
       - name: my-volume
         mountPath: /etc/foo #
       resources:
        limits:
         memory: "64Mi"
         cpu: "100m"          

  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'.

    hint

    apiVersion: v1
    kind: Pod
    metadata:
     name: nginxvol
    spec:
     volumes:
      - name: my-volume
        persistentVolumeClaim: #
         claimName: mypvc #
     containers:
     - image: nginx:1.15.5
       name: mypod
       volumeMounts:
       - name: my-volume
         mountPath: /etc/foo #
       resources:
        limits:
         memory: "64Mi"
         cpu: "100m"          

  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 Secrets

  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.

    hint

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      volumes: # specify the volumes
      - name: myvolume # this name will be used for reference inside the container
        secret: # we want a secret
          secretName: mysecret # name of the secret - this must already exist on pod creation
    
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
    
        volumeMounts: # your volume mounts are listed here
        - name: myvolume # the name that you specified in pod.spec.volumes.name
          mountPath: /etc/secrets # the path inside your container   
    
        env:
        - name: option # name of the env variable
          valueFrom:
            configMapKeyRef:
              name: myconfig # name of config map
              key: foo # name of the entity in config map
    
        resources:
          limits:
           memory: "64Mi"
           cpu: "100m"         

  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