Subscribe Us

Example of ConfigMap, Secret and Volume in pod on Kubernetes



Below kubernetes example include ConfigMap, Secret and Volume, added in pod.


Secret data :

User: admin
pass: pass

# echo "admin" | base64
# echo "pass" | base64

Create ConfigMap and Secret imperative way :

kubectl create configmap cname --from-literal=SERVER_URL=https://go.gl
kubectl create secret generic sname --from-literal=user=YWRtaW4K 
--from-literal=pass=cGFzcwo=

Create folder and sample "index.html" file:
sudo mkdir /mnt/data sudo sh -c "echo 'Hello from Kubernetes storage' > /mnt/data/index.html"


Here, Create yml file for PersistentVolume with 100M storage and StorageClass is manual,
refer below:


apiVersion: v1 kind: PersistentVolume metadata: name: task-pv-volume labels: type: local spec: storageClassName: manual capacity: storage: 100M accessModes: - ReadWriteOnce hostPath: path: "/tmp/data"

# kubectl create -f pv.yml


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100M


# kubectl create -f pvc.yml

Create Pod with ConfigMap, Secret, Volume :

apiVersionv1
kindPod
metadata:
  namemypod
spec:
  volumes:
  - nametask-pv-storage
    persistentVolumeClaim:
      claimNametask-pv-claim
  - namemysecret
    secret:
      secretNamesname 
  containers:
    - nametask-pv-container
      imagenginx
      env:
      - nameSERVER_URL_ENV
        valueFrom:
          configMapKeyRef:
            namecname
            keySERVER_URL

      volumeMounts:
      - name:  mysecret
        mountPath"/etc/test"
      - nametask-pv-storage
        mountPath"/usr/share/nginx/html"

# kubectl create -f pod.yml

Testing :

$ kubectl exec -it mypod sh
# cat /etc/test/* | base64 -d # Test secret
admin
pass

# env | grep SERVER_URL_ENV # check configMap env variable check
SERVER_URL=https://go.gl

# df -h # Check volume mount & also check with "mount" command

Done !

Post a Comment

0 Comments