Skip to content

Instantly share code, notes, and snippets.

@murarisumit
Last active May 15, 2018 03:04
Show Gist options
  • Save murarisumit/42e7be266195f8711ec59814d4db5b1e to your computer and use it in GitHub Desktop.
Save murarisumit/42e7be266195f8711ec59814d4db5b1e to your computer and use it in GitHub Desktop.
Kubernetes Namespace #kubernetes
  • Namespace are for logical division of resources in kubernetes.

  • Get all namespace:

    • kubectl get namespaces o kubectl get namespaces --show-labels

Creating namespaces

  • To create namespace:
    • Create json file namespace-dev with content:

      {
        "kind": "Namespace",
        "apiVersion": "v1",
        "metadata": {
          "name": "dev",
          "labels": {
            "name": "dev"
          }
        }
      }
      
    • Execute this kubectl create -f namespace-dev.json

    • Similary for creating namespace for prod-env, create namespace-prod.json with content

      {
        "kind": "Namespace",
        "apiVersion": "v1",
        "metadata": {
            "name": "prod",
            "labels": {
            "name": "prod"
            }
        }
      }
      
    • Execute this kubectl create -f namespace-prod.json

Working with namespace using kubectl command

To execute command in specific namespace, we create a context and when to switch to that context, our kubectl command would create/update/delete objects in that namespace.

  • Creating the context to work in namespace, setting context to view

    • kubectl config set-context dev --namespace=dev
    • kubectl config set-context prod --namespace=prod
  • To switch the context:

    • kubectl config use-context dev
    • kubectl config use-context prod
  • View all the context:

    • kubectl config view | grep namespace
    • or kubectl config list-contexts
  • View current context:

    • kubectl config current-context
  • Delete a namespace

    • kubectl delete namespaces dev

Ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment