~/kubernetes/how-set-namespace-no-need-mention-again-command-line.md

Set a Default Kubernetes Namespace So You Stop Repeating --namespace

1 min readMay 05, 2022by Gorav Singal#kubernetes#k-8-s#kubectl
// TL;DR

Use kubectl config set-context with the --namespace flag to bind a default namespace to your current context, eliminating repetitive --namespace flags.

Set a Default Kubernetes Namespace So You Stop Repeating --namespace

Introduction

In some of the cases, we need to specify namespace name along with kubectl commands.

Example of get pods command with namespace:

kubectl --namespace my-namespace get pods

Its very tedius to specify these two strings every time you are running kubectl commands. Especially, when you have multiple namespaces. Its frustrating to copy-paste them each time.

How to Set Namespace With the Context

Assumming, the name of your namespace is my-namespace.

Command to set namespace with a context:

kubectl config set-context --current --namespace my-namespace

Now, if you do: kubectl config get-contexts

You will see a namespace name along with the context name, something like:

* cluster-name cluster-name xyz my-namespace

Run Commands without Specifying Namespace

Now, you can run normal kubectl commands without mentioning namespace.

kubectl config get-contexts
kubectl get pods
kubectl apply -f <path to yaml>

Thanks for reading…

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Kubernetes - How to Solve Gateway Timeout with Http Statuscode Error 504

Kubernetes - How to Solve Gateway Timeout with Http Statuscode Error 504

Introduction You have a running kubernetes setup, and have a webservice (exposed…

Kubernetes - How to Configure Docker Repository to Pull Image and Configure Secret

Kubernetes - How to Configure Docker Repository to Pull Image and Configure Secret

Introduction In most of cases, you are not pulling images from docker hub public…

How to configure Grafana (Free version) with oAuth Okta, with SSL on Kubernetes

How to configure Grafana (Free version) with oAuth Okta, with SSL on Kubernetes

Introduction In our previous post How to configure Grafana on docker, we saw how…

Claude Code Skills — Build a Better Engineering Workflow

Claude Code Skills — Build a Better Engineering Workflow

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…