DEV Community

Mesrar
Mesrar

Posted on

Mastering Kubernetes Components: Namespace, ConfigMap, and Secrets

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
Enter fullscreen mode Exit fullscreen mode

Create a Namespace
Create a new namespace, either imperatively or declaratively:


kubectl create ns <namespace-name>
kubectl apply -f <namespace.yaml>
Enter fullscreen mode Exit fullscreen mode

View Pods in a Namespace
Check pods in a specific namespace:

kubectl get po -n kube-system

Enter fullscreen mode Exit fullscreen mode

Describe a Namespace
Get detailed information about a specific namespace:

kubectl describe ns <namespace-name>

Enter fullscreen mode Exit fullscreen mode

Delete a Namespace
To delete a specific namespace:

kubectl delete ns <namespace-name>

Enter fullscreen mode Exit fullscreen mode

Switch Namespace Permanently
Set the current context to a specific namespace:

kubectl config set-context $(kubectl config current-context) --namespace=<namespace-name>

Enter fullscreen mode Exit fullscreen mode

View Pods in All Namespaces
List all pods across all namespaces:

kubectl get po -A
Enter fullscreen mode Exit fullscreen mode

Create a Pod in a Specific Namespace
Run a pod in a designated namespace:

kubectl run redis --image=redis -n <namespace-name>

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

ConfigMap Operations

View ConfigMaps
List all ConfigMaps in the current namespace:

kubectl get cm
Enter fullscreen mode Exit fullscreen mode

Describe a ConfigMap
Get detailed information about a specific ConfigMap:

kubectl describe cm <cm-name>

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Secret Operations

View Secrets
List all secrets in the current namespace:

kubectl get secrets
Enter fullscreen mode Exit fullscreen mode

Describe a Secret
Get detailed information about a specific Secret:

kubectl describe secret <secret-name>

Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Encoding and Decoding Secrets

Use base64 for encoding and decoding secrets:

echo -n 'string' | base64
echo -n 'encoded string' | base64 --decode
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)