Kubernetes Fundamentals · Module 02 of 14

kubectl, kubeconfig and namespaces

Use kubectl safely, understand contexts, and isolate course resources in a namespace rather than operating in the default namespace by accident.

Learning objectives

By the end of this module, you should be able to:

  • Read the current kubectl context and cluster identity.
  • Create and target a namespace deliberately.
  • Use discovery and explain commands before applying unfamiliar manifests.

kubectl is an API client

kubectl reads kubeconfig to determine the cluster, user credentials and current context. Commands such as get, describe, apply, logs and exec translate operator intent into Kubernetes API requests.

Contexts prevent accidental targeting

A kubeconfig context combines a cluster, user and optional default namespace. In real operations, checking the current context before write operations is a basic safety habit.

Namespaces are scopes, not virtual clusters

Namespaces scope many namespaced objects such as Pods, Deployments and Services. Nodes, PersistentVolumes and StorageClasses are cluster-scoped. Namespaces help organization and policy, but do not by themselves provide complete security isolation.

Worked examples

See the idea in practice.

Safe discovery

kubectl config current-context
kubectl api-resources
kubectl explain deployment.spec.template.spec.containers
  • Check the target context first.
  • api-resources shows what the API server supports.
  • kubectl explain reads API schema information instead of relying on memory.
Hands-on lab

Lab — Create a safe course namespace

Goal: Create the namespace used by subsequent labs and prove which resources are namespaced.

Before you start

  • kubectl is installed and can reach the lab cluster.
STEP 1

Confirm the current context

kubectl config current-context
STEP 2

Create the lab namespace

kubectl create namespace gnu-k8s-fundamentals --dry-run=client -o yaml | kubectl apply -f -
STEP 3

List namespaced and cluster-scoped resources

kubectl api-resources --namespaced=true | head
kubectl api-resources --namespaced=false | head
STEP 4

Verify the namespace

kubectl get namespace gnu-k8s-fundamentals

Verify

  • The namespace exists.
  • The current context is known before changes are made.
  • Learners can identify examples of namespaced and cluster-scoped resources.

Expected outcome

  • kubectl reports the namespace as Active.

If it fails

  • If namespace creation is forbidden, the current identity lacks the required RBAC permission.
  • Do not switch contexts or namespaces blindly on shared clusters.
Real-world connection

Teams commonly separate environments or organizational scopes with namespaces and then layer RBAC, quotas and network policies on top.

Avoid these traps

Common mistakes

  • Assuming every Kubernetes object belongs to a namespace.
  • Running destructive commands before checking the current context.
  • Treating namespace separation as equivalent to a separate cluster.
Knowledge check

Can you explain it without looking back?

Open each answer only after you have tried to answer the question yourself.

1What does a kubectl context select?

A cluster, user identity and optionally a default namespace.

2Is a Node namespaced?

No. Nodes are cluster-scoped.

3Why use kubectl explain?

To inspect the API schema supported by the target cluster.