Configuration and Storage
Definition: A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume.
- ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable.
|
Creating a ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
key1: value1
key2: value2
Apply with: kubectl apply -f configmap.yaml
|
Common Commands:
kubectl get configmaps : List all configmaps.
kubectl describe configmap <configmap-name> : Get details about a specific configmap.
kubectl create configmap <configmap-name> --from-literal=key1=value1 --from-literal=key2=value2 : Create a configmap from literals.
|
Definition: A Secret is an API object used to store sensitive information, such as passwords, OAuth tokens, and SSH keys.
- Storing sensitive information in a Secret is safer and more flexible than putting it verbatim in a Pod definition or in a container image.
|
Creating a Secret:
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
username: $(echo -n 'myuser' | base64)
password: $(echo -n 'mypassword' | base64)
Apply with: kubectl apply -f secret.yaml
Note: Data must be base64 encoded.
|
Common Commands:
kubectl get secrets : List all secrets.
kubectl describe secret <secret-name> : Get details about a specific secret.
kubectl create secret generic <secret-name> --from-literal=username=myuser --from-literal=password=mypassword : Create a generic secret.
|
Definition: A Volume is a directory, possibly with some data in it, which is accessible to the containers in a pod.
- Volumes have a lifetime that is tied to the pod, but can persist data through container restarts.
|
Volume Types:
emptyDir : A temporary directory that lasts as long as the Pod is running.
hostPath : Mounts a file or directory from the host node’s filesystem into your Pod.
persistentVolumeClaim : Used to request storage from a PersistentVolume.
|
Using a Volume:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:latest
volumeMounts:
- mountPath: /data
name: my-volume
volumes:
- name: my-volume
emptyDir: {}
|
PersistentVolume (PV): A piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes.
PersistentVolumeClaim (PVC): A request for storage by a user. It is a claim on a PV.
|
Creating a PersistentVolume:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/pv
|
Creating a PersistentVolumeClaim:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
|
Usage: The PVC is then mounted as a volume in a pod.
|