Around Go: Kubernetes · 3 MIN READ

kubectl first steps: context, namespace and permissions

Inspect the target cluster and explicitly scope commands before making changes.

kubectl reads a kubeconfig and sends requests to its selected cluster as a configured identity. A namespace narrows many workload operations but does not select a cluster. These lessons assume an existing disposable development cluster with a context named dev-cluster; substitute your actual development context.

01

Identify the context before querying workloads

config get-contexts lists saved contexts, and current-context prints the default. A context bundles cluster, identity and an optional namespace. These commands inspect local configuration; they do not establish whether the remote API is reachable.

bash
kubectl config get-contexts
kubectl config current-context
kubectl --context dev-cluster cluster-info
Read the code

Replace dev-cluster with the development context you inspected. Avoid config view --raw in shared output because it can reveal credentials. Supplying --context on later commands avoids silently targeting a different default cluster.

02

Find namespaces and discover resource types

get namespaces shows available namespaces when your identity is allowed to list them. api-resources tells you the kinds the server exposes. auth can-i checks authorization for one requested action; it is not a proof that the action will otherwise succeed.

bash
kubectl --context dev-cluster get namespaces
kubectl --context dev-cluster api-resources --namespaced=true
kubectl --context dev-cluster auth can-i create deployments -n go-lab
Read the code

A no response from can-i means your identity lacks that permission. An absent go-lab namespace must be created deliberately by an authorized user before the later exercises. Do not switch to a more privileged production credential as an automatic workaround.

03

Scope a development namespace explicitly

Create go-lab only on the inspected development cluster if it does not exist. -n selects the namespace for the request. Pod queries can be watched with -w; Ctrl-C stops watching without deleting any workload.

bash
# One-time setup if go-lab does not exist:
kubectl --context dev-cluster create namespace go-lab
kubectl --context dev-cluster -n go-lab get pods -o wide
kubectl --context dev-cluster -n go-lab get pods -w
Read the code

An empty namespace correctly reports no resources. AlreadyExists from create means the setup already happened; inspect before reusing a namespace someone else owns. Namespace deletion later would delete its namespaced resources, so it is not a casual cleanup command.

TAKE THIS WITH YOU

Know the context, identity and namespace before working on a cluster. Keep the development context and namespace explicit in commands; a namespace flag alone cannot protect against targeting the wrong cluster.

Check your understanding

What does kubectl --context select for one invocation?

Go to the source

Progress stays in this browser. No account needed.