Installing and Configuring kubectl
1. Installing kubectl CLI
| Platform | Installation Method | Notes |
|---|---|---|
| macOS | brew install kubectl |
Recommended for Homebrew users |
| Linux (x86_64) | curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" |
Download binary directly |
| Windows (x86_64) | curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/windows/amd64/kubectl.exe" |
Download executable directly |
| Linux (ARM64) | curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl" |
For ARM-based systems |
Note: Ensure kubectl is in your PATH after installation.
2. Configuring kubeconfig File
Example: Basic kubeconfig structure
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: <cert-data>
server: https://kubernetes-api.example.com
name: kubernetes-cluster
contexts:
- context:
cluster: kubernetes-cluster
user: admin-user
name: production-context
current-context: production-context
users:
- name: admin-user
user:
client-certificate-data: <cert-data>
client-key-data: <key-data>
| Field | Description | Values |
|---|---|---|
| apiVersion | API version of the kubeconfig file | v1 |
| clusters | List of cluster definitions | Array of cluster objects |
| contexts | List of context definitions | Array of context objects |
| current-context | Name of the current context | String matching a context name |
| users | List of user definitions | Array of user objects |
Note: The
kubeconfig file is typically located at ~/.kube/config.
Example: Adding a new cluster
kubectl config set-cluster my-cluster \
--server=https://my-k8s-api.example.com \
--certificate-authority=/path/to/ca.crt
Example: Setting a user credential
kubectl config set-credentials my-user \
--client-certificate=/path/to/client.crt \
--client-key=/path/to/client.key
3. Managing Multiple Contexts
| Command | Description | Flags |
|---|---|---|
| kubectl config current-context | Prints the name of the current context | --v=0 |
| kubectl config get-contexts | Lists all available contexts | -o wide, --no-headers |
| kubectl config use-context | Sets the current context to the specified one | --v=0 |
Note: Contexts are defined in the kubeconfig file, typically located at
~/.kube/config.
Warning: Switching contexts does not change the actual cluster; it only changes which cluster
kubectl targets.
4. Setting Default Namespace
| Command | Description | Example |
|---|---|---|
| kubectl config set-context | Sets the default namespace for a context | kubectl config set-context --current --namespace=production |
| kubectl config view | Displays current configuration including namespace | kubectl config view |
Note: The default namespace is applied only to the current context. To set it for a specific context, use
kubectl config set-context <context-name> --namespace=<namespace>.5. Viewing Cluster Information
| Command | Description | Notes |
|---|---|---|
| kubectl cluster-info | Display basic information about the cluster | Shows API server address and other cluster details |
| kubectl version | Display the Kubernetes version | Shows both client and server versions |
| kubectl get nodes | List all nodes in the cluster | Shows node status, roles, and taints |
| kubectl get pods --all-namespaces | List all pods across all namespaces | Useful for cluster-wide pod monitoring |
| kubectl get services --all-namespaces | List all services across all namespaces | Shows service endpoints and types |
| kubectl get deployments --all-namespaces | List all deployments across all namespaces | Displays deployment status and replicas |
Example: kubectl cluster-info Output
Kubernetes control plane is running at https://192.168.1.100:6443
KubeDNS is running at https://192.168.1.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Example: kubectl get nodes Output
NAME STATUS ROLES AGE VERSION
node-1 Ready <none> 2d v1.28.0
node-2 Ready <none> 2d v1.28.0
node-3 Ready <none> 2d v1.28.0
Note: The
--all-namespaces flag is useful for getting a global view of cluster resources.
6. Checking kubectl Version
| Command | Description | Output |
|---|---|---|
| kubectl version | Displays the kubectl client and server versions | |
| kubectl version --short | Displays only the version numbers without additional metadata | v1.28.0 |
| kubectl version --client | Displays only the client version | v1.28.0 |
7. Enabling kubectl Autocompletion
Kubernetes provides shell autocompletion for kubectl to improve productivity by suggesting commands, flags, and resource names as you type.
Example: Bash Autocompletion Setup
# Enable kubectl autocompletion in bash
source <(kubectl completion bash)
# To make it permanent, add to your ~/.bashrc
echo "source <(kubectl completion bash)" >> ~/.bashrc
Example: Zsh Autocompletion Setup
# Enable kubectl autocompletion in zsh
source <(kubectl completion zsh)
# To make it permanent, add to your ~/.zshrc
echo "source <(kubectl completion zsh)" >> ~/.zshrc
| Shell | Command | Notes |
|---|---|---|
| Bash | source <(kubectl completion bash) |
Add to ~/.bashrc for persistence |
| Zsh | source <(kubectl completion zsh) |
Add to ~/.zshrc for persistence |
| Fish | kubectl completion fish | source |
Add to ~/.config/fish/config.fish |
Note: Autocompletion requires
kubectl to be installed and accessible in your PATH.
8. Using kubectl Aliases
Kubernetes provides a way to create aliases for frequently used commands using the kubectl alias command. This feature allows users to define shortcuts for long or complex commands, improving efficiency and reducing typing.
| Alias Name | Command | Description |
|---|---|---|
| kubectl alias | kubectl alias <alias-name>="<command>" |
Creates a new alias for a kubectl command |
Note: Aliases created with
kubectl alias are stored in the user's shell configuration file (e.g., .bashrc, .zshrc) and will persist across sessions.
9. Installing kubectl Plugins
Kubernetes kubectl plugins extend the functionality of the kubectl command-line tool. Plugins are executed as separate binaries and must be installed in a location that is discoverable by kubectl.
| Installation Method | Description | Notes |
|---|---|---|
| kubectl plugin install | Use the kubectl plugin install command to install plugins from a registry or local binary. |
This is the preferred method for installing official plugins. |
| Manual Binary Placement | Place the plugin binary in a directory included in the $PATH and ensure it is executable. |
The binary name must start with kubectl- to be recognized as a plugin. |
Example: Manual Plugin Installation
# Create directory and place plugin binary
mkdir -p ~/.local/bin
cp kubectl-whoami ~/.local/bin/
chmod +x ~/.local/bin/kubectl-whoami
Note: Ensure that the plugin binary is executable and located in a directory included in your
$PATH.
10. Configuring Authentication Methods
| Method | Description | Configuration | Notes |
|---|---|---|---|
| Client Certificates | Uses X.509 certificates for client authentication | client-certificate and client-key |
Requires CA to sign certificates |
| Token Authentication | Uses bearer tokens for authentication | token field in kubeconfig |
Commonly used with service accounts or OIDC |
| Username/Password | Basic authentication with username and password | username and password |
Not recommended for production due to security concerns |
| AWS IAM | Uses AWS IAM roles for authentication in EKS clusters | exec with aws-iam-authenticator |
Requires aws-iam-authenticator binary to be installed |
| Azure Active Directory | Uses Azure AD for authentication in AKS clusters | exec with kubelogin |
Requires kubelogin to be installed and configured |
| OIDC (OpenID Connect) | Uses OpenID Connect for authentication | exec with oidc-login |
Supports integration with identity providers like Google or Auth0 |
Example: Token Authentication
clusters:
- cluster:
certificate-authority-data: <CA_DATA>
server: https://kubernetes-api.example.com
name: example-cluster
contexts:
- context:
cluster: example-cluster
user: example-user
name: example-context
current-context: example-context
users:
- name: example-user
user:
token: <JWT_TOKEN>
Example: Client Certificate Authentication
clusters:
- cluster:
certificate-authority-data: <CA_DATA>
server: https://kubernetes-api.example.com
name: example-cluster
contexts:
- context:
cluster: example-cluster
user: example-user
name: example-context
current-context: example-context
users:
- name: example-user
user:
client-certificate-data: <CERT_DATA>
client-key-data: <KEY_DATA>
Note: For OIDC and AWS IAM, ensure the respective binaries (
kubelogin, aws-iam-authenticator) are installed and accessible in your PATH.