Why Use Kubernetes Namespaces?
A single GKE cluster can host multiple environments or teams. Namespaces help you:- Enforce resource quotas and limits per team or environment
- Simplify Role-Based Access Control (RBAC)
- Keep workloads organized and isolated
Namespaces are a native Kubernetes feature. Learn more in the Kubernetes Namespaces documentation.
Prerequisites
- A Google Cloud project (e.g.,
kodekloud-gcp-training) - A GKE cluster named
gcp-devops-projectin zoneus-central1-c - Access to Cloud Shell, or local installation of
gcloudandkubectl
Step 1. Connect to Your GKE Cluster
-
Open Cloud Shell in the GCP Console:
Kubernetes Engine > Clusters > Connect > Run in Cloud Shell. - Authorize Cloud Shell by pressing Enter when prompted.
-
Fetch cluster credentials:
kubeconfig is now configured to interact with the gcp-devops-project cluster.
Step 2. List Existing Namespaces
Run:Step 3. Create a New Namespace
Create thegcp-devops-prod namespace for production workloads:
Step 4. Verify Your New Namespace
List namespaces again:Step 5. (Optional) Set the New Namespace as Default
To avoid specifying-n gcp-devops-prod on every command, update your context:
Switching the default namespace affects all subsequent
kubectl commands in this session.Step 6. View Namespaced Workloads in GCP Console
- In the GCP Console, go to Kubernetes Engine > Workloads.
- Click Show system workloads, then open the Filter pane.
- Select gcp-devops-prod under Namespace and apply.
Command Reference Table
| Step | Description | Command |
|---|---|---|
| 1 | Authenticate to GKE cluster | gcloud container clusters get-credentials gcp-devops-project --zone us-central1-c --project kodekloud-gcp-training |
| 2 | List all namespaces | kubectl get namespaces |
| 3 | Create a new namespace | kubectl create namespace gcp-devops-prod |
| 5 | Set new namespace as default (optional) | kubectl config set-context --current --namespace=gcp-devops-prod |
Links and References
That’s it! You’ve successfully created, verified, and configured a namespace in your GKE cluster. You can now deploy applications intogcp-devops-prod.