Work With Kubernetes Resources outside The Default Namespace

In Kubernetes, a Namespace is a set of API objects and Kubernetes Objects that share the same accessibility policies. Namespaces are Kubernetes’ way of managing object privileges for different sets of users in an organization.

Kubernetes also provides a default namespace to which all Kubernetes Objects belong by default. This tutorial will teach you how to work with Kubernetes Resources outside the default namespace!

Kubernetes namespaces allow admins to create groups of resources with specific access requirements. Kubernetes namespaces can be used for many different purposes, such as running services on different hosts or limiting network traffic between two sets of pods.

–namespace

To access a Kubernetes object outside the default namespace, we need to use the –namespace command switch and specify the namespace’s name as shown below.

kubectl get pods --namespace prod

To create a new namespace, we use the following command.

kubectl create namespace test

We can use the -n shortcut instead of the –namespace

We use the following command to deploy a manifest file into a different namespace.

kubectl apply -f deploy.yaml --namespace=test

As shown below, we can specify a different namespace inside a YAML file.

apiVersion: v1
kind: Pod
metadata:
  name: mynginx

  namespace: test
  labels:
    env: test
    version: v1
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80

For more information about namespaces in Kubernetes, visit the overview post we published on the topic.

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.