> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ahmadraza.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Kubectx

# Practical Guide – `kubectx` & `kubens`

Managing multiple Kubernetes clusters and namespaces with plain `kubectl` can get repetitive.
Two handy tools – **kubectx** and **kubens** – simplify switching contexts and namespaces.

***

## 1. 🔧 Installation

### Linux / macOS

```bash theme={null}
# Install both with brew
brew install kubectx

# Or manually
git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
sudo ln -s ~/.kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s ~/.kubectx/kubens /usr/local/bin/kubens
```

### Windows (with Chocolatey)

```powershell theme={null}
choco install kubectx
```

***

## 2. ⚙️ Setup & Verify

Ensure `kubectl` works with clusters:

```bash theme={null}
kubectl config get-contexts
```

Then test:

```bash theme={null}
kubectx --help
kubens --help
```

***

## 3. 🧭 Usage

### 🔄 Switch Contexts (`kubectx`)

* List available contexts:

```bash theme={null}
kubectx
```

* Switch to a context:

```bash theme={null}
kubectx my-cluster
```

* Rename a context:

```bash theme={null}
kubectx old-name=new-name
```

* Switch back to previous context:

```bash theme={null}
kubectx -
```

***

### 📂 Switch Namespaces (`kubens`)

* List all namespaces:

```bash theme={null}
kubens
```

* Switch to namespace:

```bash theme={null}
kubens dev
```

* Switch back to previous namespace:

```bash theme={null}
kubens -
```

***

## 4. 📑 Common Workflows

### Example: Change Context + Namespace

```bash theme={null}
kubectx production
kubens payments
```

Now all `kubectl` commands run in `production` cluster, `payments` namespace.

***

## 5. 🎨 Bonus Features

* **Fuzzy search support** if `fzf` is installed → lets you pick context/namespace interactively.

```bash theme={null}
kubectx
kubens
```

Opens an interactive menu to choose from.

* Works seamlessly with `k9s` → if you already use it.

***

## 6. ✅ Summary

* `kubectx` → switch between **clusters/contexts** easily.
* `kubens` → switch between **namespaces** quickly.
* Both save time and reduce human error vs typing full `kubectl config` commands.

***

⚡ These tools are **must-haves** for anyone managing **multiple Kubernetes clusters/namespaces daily**.

```bash theme={null}
| Feature                | kubectl                      | kubectx/kubens               |
|------------------------|------------------------------|-------------------------------|
| Context Switching       | `kubectl config use-context` | `kubectx <context>`          |
| Namespace Switching     | `kubectl config set-context --namespace` | `kubens <namespace>`        |
| List Contexts          | `kubectl config get-contexts`| `kubectx`                    |
| List Namespaces        | `kubectl get namespaces`     | `kubens`                     |
| Interactive Selection   | No                           | Yes (with fzf)               |
```
