DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Kubernetes Tutorial

Introduction to Kubernetes
Kubernetes Architecture
Installing Kubernetes
kubectl Basics
Kubernetes Pods
Kubernetes Deployments
Kubernetes Services
ConfigMaps and Secrets
Kubernetes Storage
Kubernetes Namespaces
Kubernetes Resource Management
Helm for Kubernetes
Kubernetes Monitoring

kubectl contexts and namespaces

PreviousPrev
Next

Understand kubeconfig structure, switch safely between clusters, and scope kubectl commands with namespaces.

Why Contexts and Namespaces Matter

One of the easiest ways to make a costly mistake is to run a command against the wrong cluster or namespace. Contexts and namespaces are your safety rails.

kubeconfig Structure

A kubeconfig file usually has three core sections:

SectionMeaning
clustersAPI Server endpoints and certificate data
usersCredentials or auth methods
contextsNamed combinations of cluster + user + namespace

List Contexts

kubectl config get-contexts

Switch Contexts

kubectl config use-context dev-cluster
kubectl config use-context prod-cluster

Always double-check before running write operations in production.

Use a Namespace with a Command

kubectl get pods -n kube-system
kubectl get services -n default

Set a Default Namespace in the Current Context

kubectl config set-context --current --namespace=dev

After that, kubectl commands use dev unless you override with -n.

Why This Helps

Think of a context like the combination of office building, badge, and floor. It tells you where you are going, who you are, and which area you work in by default.

kubens Tool Mention

Many engineers use kubens from the kubectx toolset to switch namespaces quickly. It is not required, but it can make frequent namespace changes easier.

Safe Habits

  • Run kubectl config get-contexts before important changes.
  • Keep production contexts clearly named.
  • Use -n explicitly when you want to be extra careful.
  • Avoid assuming the default namespace is always correct.
Exercise

Context Definition

What does a Kubernetes context usually combine?

Exercise

Current Namespace

What does `kubectl config set-context --current --namespace=dev` do?

PreviousPrev
Next

Continue Learning

kubectl get and describe

Inspect pods, nodes, deployments, and services with kubectl get and use describe output to understand object behavior and events.

18 min·Easy

kubectl apply and delete

Create, update, compare, and remove Kubernetes resources using both declarative manifests and imperative helper commands.

18 min·Easy

kubectl exec, logs, and port-forward

Debug running containers, stream logs, inspect previous crashes, and forward local ports into the cluster.

18 min·Easy

Explore Related Topics

Do

Docker Tutorials

Build the containers Kubernetes orchestrates

Te

Terraform Tutorials

Provision Kubernetes clusters as code

Try the Tool

YAML Validator

Validate Kubernetes manifests and any YAML files instantly.

On This Page

Why Contexts and Namespaces Matterkubeconfig StructureList ContextsSwitch ContextsUse a Namespace with a CommandSet a Default Namespace in the Current ContextWhy This Helps`kubens` Tool MentionSafe Habits