DEV Community

Cover image for K8s Exercise: Persistent State
Akshay Rao
Akshay Rao

Posted on

K8s Exercise: Persistent State

Introduction
Hi, I am Akshay Rao, will be starting a exercise series on k8s.
In this blog there will not explanation only problems and solutions.if you want explanation have a look at this series:-
https://dev.to/aksrao1998/series/24887

Pre-requisite
have minikube or kind running in the local machine.

Note:- k is alias for kubectl.

Let's Start

Problem 1
Create busybox pod with two containers, each one will have the image busybox and will run the 'sleep 3600' command. Make both containers mount an emptyDir at '/etc/foo'. Connect to the second busybox, write the first column of '/etc/passwd' file to '/etc/foo/passwd'. Connect to the first busybox and write '/etc/foo/passwd' file to standard output. Delete pod.

Solution

[k8s-ckad (⎈|minikube:mynamespace)]$ k run busybox --image=busybox --dry-run=client -o yaml > pod2.yaml

#edit the pod2.yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  dnsPolicy: ClusterFirst
  restartPolicy: Never
  containers:
  - image: busybox
    name: busybox0
    command: ["/bin/sh","-c","sleep 3600"]
    volumeMounts:
    - mountPath: /etc/foo
      name: anyvolume
    resources: {}
  - image: busybox
    name: busybox1
    command: ["/bin/sh","-c","sleep 3600"]
    volumeMounts:
    - mountPath: /etc/foo
      name: anyvolume
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  volumes:
  - name: anyvolume
    emptyDir: {}
status: {}

[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f pod2.yaml
pod/busybox created

# login in container
[k8s-ckad (⎈|minikube:mynamespace)]$ k exec -it busybox -c busybox0  -- /bin/sh
/etc # cd /etc/foo/
/etc/foo # ls
/etc/foo # cat /etc/passwd 
root:x:0:0:root:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/false
bin:x:2:2:bin:/bin:/bin/false
sys:x:3:3:sys:/dev:/bin/false
sync:x:4:100:sync:/bin:/bin/sync
mail:x:8:8:mail:/var/spool/mail:/bin/false
www-data:x:33:33:www-data:/var/www:/bin/false
operator:x:37:37:Operator:/var:/bin/false
nobody:x:65534:65534:nobody:/home:/bin/false
/etc/foo # cat /etc/passwd | awk '{print $1}' > /etc/foo/passwd
/etc/foo # ls
passwd
/etc/foo # cat passwd 
root:x:0:0:root:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/false
bin:x:2:2:bin:/bin:/bin/false
sys:x:3:3:sys:/dev:/bin/false
sync:x:4:100:sync:/bin:/bin/sync
mail:x:8:8:mail:/var/spool/mail:/bin/false
www-data:x:33:33:www-data:/var/www:/bin/false
operator:x:37:37:Operator:/var:/bin/false
nobody:x:65534:65534:nobody:/home:/bin/false
/etc/foo # 

# verify this is updated in the other container automatically 

[k8s-ckad (⎈|minikube:mynamespace)]$ k exec -it busybox -c busybox1  -- /bin/sh
/ # 
/ # cat /etc/foo/passwd 
root:x:0:0:root:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/false
bin:x:2:2:bin:/bin:/bin/false
sys:x:3:3:sys:/dev:/bin/false
sync:x:4:100:sync:/bin:/bin/sync
mail:x:8:8:mail:/var/spool/mail:/bin/false
www-data:x:33:33:www-data:/var/www:/bin/false
operator:x:37:37:Operator:/var:/bin/false
nobody:x:65534:65534:nobody:/home:/bin/false
Enter fullscreen mode Exit fullscreen mode

Problem 2
Create a PersistentVolume of 10Gi, called 'myvolume'. Make it have accessMode of 'ReadWriteOnce' and 'ReadWriteMany', storageClassName 'normal', mounted on hostPath '/etc/foo'. Save it on pv.yaml, add it to the cluster. Show the PersistentVolumes that exist on the cluster.

Solution

# create a file name pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: myvolume
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: normal
  hostPath:
    path: /etc/foo

# verify
[k8s-ckad (⎈|minikube:mynamespace)]$ k get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
myvolume   10Gi       RWO,RWX        Recycle          Available           normal                  32s
Enter fullscreen mode Exit fullscreen mode

Problem 3
Create a PersistentVolumeClaim for this storage class, called 'mypvc', a request of 4Gi and an accessMode of ReadWriteOnce, with the storageClassName of normal, and save it on pvc.yaml. Create it on the cluster. Show the PersistentVolumeClaims of the cluster.

Solution

# create a pvc.yaml file
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi
  storageClassName: normal

# verify
[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f pvc.yaml
persistentvolumeclaim/mypvc created
[k8s-ckad (⎈|minikube:mynamespace)]$ k get pvc
NAME    STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mypvc   Bound    myvolume   10Gi       RWO,RWX        normal         5s
Enter fullscreen mode Exit fullscreen mode

Problem 3

  • Create a busybox pod with command 'sleep 3600', save it on pod.yaml. Mount the PersistentVolumeClaim to '/etc/foo'. Connect to the 'busybox' pod, and copy the '/etc/passwd' file to '/etc/foo/passwd'.
  • Create a second pod which is identical with the one you just created (you can easily do it by changing the 'name' property on pod.yaml). Connect to it and verify that '/etc/foo' contains the 'passwd' file. Delete pods to cleanup. Note: If you can't see the file from the second pod, can you figure out why? What would you do to fix that?

Solution

Part-1

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  dnsPolicy: ClusterFirst
  restartPolicy: Never
  containers:
  - image: busybox
    name: busybox0
    command: ["/bin/sh","-c","sleep 3600"]
    volumeMounts:
    - mountPath: /etc/foo
      name: anyvolume
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  volumes:
    - name: anyvolume
      persistentVolumeClaim:
        claimName: mypvc
status: {}

[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f pod.yaml
pod/busybox created

Copy the passwd file
[k8s-ckad (⎈|minikube:mynamespace)]$ k exec -it busybox -- /bin/sh 
/ # cp /etc/passwd /etc/foo/
/ # ls /etc/foo
passwd

Part-2
# change the labels to busybox1
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybo1
  name: busybox1
spec:
  dnsPolicy: ClusterFirst
  restartPolicy: Never
  containers:
  - image: busybox
    name: busybox1
    command: ["/bin/sh","-c","sleep 3600"]
    volumeMounts:
    - mountPath: /etc/foo
      name: anyvolume
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  volumes:
    - name: anyvolume
      persistentVolumeClaim:
        claimName: mypvc
status: {}

[k8s-ckad (⎈|minikube:mynamespace)]$ k create -f pod3.yaml
pod/busybox1 created

#verify 

[k8s-ckad (⎈|minikube:mynamespace)]$ k exec -it busybox1 -- /bin/sh
/ # 
/ # ls /etc/foo
passwd
/ # 

# delete the resources

[k8s-ckad (⎈|minikube:mynamespace)]$ k delete po  busybox
pod "busybox" deleted
[k8s-ckad (⎈|minikube:mynamespace)]$ k delete po  busybox1
pod "busybox1" deleted
[k8s-ckad (⎈|minikube:mynamespace)]$ k delete pv myvolume
persistentvolume "myvolume" deleted
[k8s-ckad (⎈|minikube:mynamespace)]$ k delete pvc mypvc
persistentvolumeclaim "mypvc" deleted

Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more