Mastering Kubernetes Components: Namespace, ConfigMap, and Secrets
Mesrar
Posted on February 1, 2024
In Kubernetes, effective management of namespaces, ConfigMaps, and Secrets is crucial for maintaining a well-organized and secure environment. Let's explore key commands and concepts related to these components.
Namespace Operations
View Namespaces
List all namespaces in the current cluster:
kubectl get ns
Create a Namespace
Create a new namespace, either imperatively or declaratively:
kubectl create ns <namespace-name>
kubectl apply -f <namespace.yaml>
View Pods in a Namespace
Check pods in a specific namespace:
kubectl get po -n kube-system
Describe a Namespace
Get detailed information about a specific namespace:
kubectl describe ns <namespace-name>
Delete a Namespace
To delete a specific namespace:
kubectl delete ns <namespace-name>
Switch Namespace Permanently
Set the current context to a specific namespace:
kubectl config set-context $(kubectl config current-context) --namespace=<namespace-name>
View Pods in All Namespaces
List all pods across all namespaces:
kubectl get po -A
Create a Pod in a Specific Namespace
Run a pod in a designated namespace:
kubectl run redis --image=redis -n <namespace-name>
Create Namespace Declaratively
Create a namespace from a YAML file:
kubectl create ns <namespace-name> --dry-run=client -o yaml > namespace.yaml
kubectl apply -f namespace.yaml
ConfigMap Operations
View ConfigMaps
List all ConfigMaps in the current namespace:
kubectl get cm
Describe a ConfigMap
Get detailed information about a specific ConfigMap:
kubectl describe cm <cm-name>
Create a ConfigMap
Create a ConfigMap from files or literals:
kubectl create cm <cm-name> --from-file=<path to file>
kubectl create cm <cm-name> --from-literal=<key1>=<value1> --from-literal=<key2>=<value2>
kubectl apply -f cm.yaml
Secret Operations
View Secrets
List all secrets in the current namespace:
kubectl get secrets
Describe a Secret
Get detailed information about a specific Secret:
kubectl describe secret <secret-name>
Create a Secret
Create a Secret from files or literals:
kubectl create secret generic <secret-name> --from-file=<path to file>
kubectl create secret generic <secret-name> --from-literal=<key1>=<value1> --from-literal=<key2>=<value2>
Encoding and Decoding Secrets
Use base64 for encoding and decoding secrets:
echo -n 'string' | base64
echo -n 'encoded string' | base64 --decode
Ensure proper management of namespaces, ConfigMaps, and Secrets to achieve effective resource isolation, configuration management, and sensitive data protection in your Kubernetes cluster.
Happy Kuberneting!
Posted on February 1, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.