5 min read

Kubernetes Day to Day - kubectl, Namespaces, ConfigMaps and Secrets

1. Recap: From Concepts to Commands

In the first part of this series we saw what Kubernetes is, the problem it solves and the three core concepts — Pods, Deployments and Services — applying them in a minimal Nginx Deployment. Now that the theory is in place, this article is practical: kubectl day to day, how to organize resources with namespaces, how to manage configuration and secrets with ConfigMaps and Secrets, and a first deploy of a real application (not just a sample Nginx).

2. kubectl Beyond apply and get

The previous article already used kubectl apply and kubectl get. Day to day, a few other commands show up all the time:

# Describe a resource in detail (events, status, full configuration)
kubectl describe pod nginx-7d9f8c6b4-x2z9k

# View the logs of a container inside a Pod
kubectl logs nginx-7d9f8c6b4-x2z9k
kubectl logs -f nginx-7d9f8c6b4-x2z9k    # follow the log in real time

# Open a shell inside a running container
kubectl exec -it nginx-7d9f8c6b4-x2z9k -- bash

# Remove a resource
kubectl delete -f nginx-deployment.yaml

# Forward a local port to a Pod, without exposing an external Service
kubectl port-forward pod/nginx-7d9f8c6b4-x2z9k 8080:80

kubectl describe is usually the first command you run when investigating a Pod that doesn’t come up as expected — the Events section at the end of the output usually points to the cause (image not found, not enough resources in the cluster, a failing readiness probe, etc.) even before you look at the application logs.

3. Organizing Resources with Namespaces

A namespace is a way to split a cluster into isolated logical spaces — teams, environments (dev, staging, prod) or different applications can live in separate namespaces, with resource names that don’t collide with each other and (with the right policies) no network visibility into one another.

# Create a namespace
kubectl create namespace my-app

# List all namespaces in the cluster
kubectl get namespaces

# Apply a manifest to a specific namespace
kubectl apply -f deployment.yaml -n my-app

# List resources in a specific namespace
kubectl get pods -n my-app

# List resources across ALL namespaces
kubectl get pods -A

Without -n, every kubectl command operates on the default namespace — which works for experiments, but quickly becomes confusing in a real cluster with several applications. The recommended practice is to always create one namespace per application or per team, and declare it explicitly in the manifests:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: my-app
spec:
  # ...

4. ConfigMaps: Configuration Outside the Image

A good container practice (also stressed in this blog’s Docker series) is to never hardcode configuration inside the image — it should be able to run in any environment, receiving its configuration from outside. The ConfigMap is the Kubernetes object for that: a set of non-sensitive key-value pairs, injected into Pods as environment variables or files.

apiVersion: v1
kind: ConfigMap
metadata:
  name: api-config
  namespace: my-app
data:
  LOG_LEVEL: "info"
  FEATURE_FLAG_NEW_CHECKOUT: "true"

Consuming the ConfigMap in a Deployment, as environment variables:

spec:
  containers:
    - name: api
      image: my-app/api:1.0
      envFrom:
        - configMapRef:
            name: api-config

5. Secrets: Sensitive Configuration

Secrets are used the same way as ConfigMaps, but they’re meant for sensitive data — passwords, tokens, API keys. The most important difference isn’t technical (by default, a Secret’s content is only base64-encoded, not encrypted) but one of intent: Kubernetes treats Secrets differently in a few places (they’re not shown by default in kubectl describe, for example), and it’s the right object to integrate with encryption at rest or external vaults (Vault, AWS Secrets Manager) in production clusters.

# Create a Secret directly from the CLI, without writing the password in a versioned YAML
kubectl create secret generic api-db-credentials \
  --from-literal=DB_USER=app \
  --from-literal=DB_PASSWORD='replace-with-a-real-value' \
  -n my-app

Consuming the Secret, also as environment variables:

spec:
  containers:
    - name: api
      image: my-app/api:1.0
      envFrom:
        - configMapRef:
            name: api-config
        - secretRef:
            name: api-db-credentials

An important point — and one that comes back strongly in Article 3 of this series, on GitOps: never commit a Secret with real values in plain text to a Git repository, not even a private one. Tools like Sealed Secrets or integrations with external vaults exist precisely to let you version secrets safely within a GitOps flow.

6. A Real Deploy: API with Configuration and Database

Putting the concepts together, an example closer to a real application: an API that reads configuration from a ConfigMap and credentials from a Secret, and is exposed inside the cluster by a Service:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: my-app/api:1.0
          ports:
            - containerPort: 8080
          envFrom:
            - configMapRef:
                name: api-config
            - secretRef:
                name: api-db-credentials
---
apiVersion: v1
kind: Service
metadata:
  name: api
  namespace: my-app
spec:
  selector:
    app: api
  ports:
    - port: 80
      targetPort: 8080
kubectl apply -f api-deployment.yaml
kubectl rollout status deployment/api -n my-app

kubectl rollout status follows the deploy until every replica is ready (or points out the error, if one fails) — useful both in a manual terminal and as a verification step in a CI/CD pipeline.

7. Conclusion and Next Steps

With kubectl describe/logs/exec to investigate, namespaces to organize, and ConfigMaps/Secrets to separate configuration from application code, you can already make real, organized deploys to a Kubernetes cluster. So far, though, every kubectl apply has been run by hand, from the machine of whoever operates the cluster — which doesn’t scale well for teams and leaves no reliable trail of what was applied and when. In the next article, the series gets into GitOps: what it is, why it makes sense to treat infrastructure as versioned code, and an introduction to ArgoCD to automatically apply what’s in a Git repository to the cluster.


References:

  1. Kubernetes Documentation — kubectl Reference
  2. Kubernetes Documentation — Namespaces
  3. Kubernetes Documentation — ConfigMaps
  4. Kubernetes Documentation — Secrets