Skip to content

Part 1: Using Namespaces

This is Part 1 of a multi-part, self-paced quick start exercise.

Note

This exercise requires MicroK8s. If you do not have these already installed and running, see "Prerequisites".


What Will You Do

In part 1, you will:

  • Use namespaces to organize your cluster.

Estimated Time

Estimated time for this exercise is 10 minutes. Watch a video for this exercise.


Using Namespaces

Kubernetes supports multiple virtual clusters backed by the same physical cluster. These virtual clusters are called Namespaces. A namespace isolates and virtualizes system resources. Processes are restricted to a namespace and can only interact with processes and resources in the same namespace.

Namespaces are useful for larger teams or projects to share a cluster without impacting each other's work. Namespaces also help when access to a namespace needs to be restricted, like creating a namespace for your production site and restricting access to the operations team.

Add Namespace manually

You can create a namespace manually with the following command. The following command will create a namespace titled "production".

  1. Open the Terminal or command prompt.
  2. Use the following command to list the namespaces.

    microk8s kubectl get namespaces
    

    Get Namespaces

  3. Add a namespace titled production to your environment.

    microk8s kubectl create namespace production
    

  4. List the namespaces. Production is now listed.

    microk8s kubectl get namespaces
    

    Get Namespaces Production


Namespace YAML file

You can also create a namespace using a YAML file, which is a configuration file. You could create a YAML file from the command line, but for this exercise, you can just use a text editor. Or you can download the namespace YAML file from this public Git repository.

  1. Open the Terminal.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    touch namespace.yaml
    
  4. Use the nano text editor in the Terminal.
    nano namespace.yaml
    
  5. Copy and paste the configuration below into the text editor.
  6. Press Cmd + X, then type Y and press Return to save the namespace.yaml file.
  1. Open the command prompt.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    copy NUL namespace.yaml
    
  4. Open the namespace.yaml file with a text editor. For example, use Notepad++ to edit the YAML file.
  5. Copy and paste the configuration below into the text editor.
  6. Save the namespace.yaml file.
  1. Open the Terminal.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    touch namespace.yaml
    
  4. Use the nano text editor in the Terminal.
    nano namespace.yaml
    
  5. Copy and paste the configuration below into the text editor. Edit YAML file
  6. Press Ctrl + X, then type Y and press Enter to save the namespace.yaml file.

Add Namespace YAML file

Using a YAML file to create namespaces can be useful if you create multiple clusters that have the same namespaces, like development and production.

  1. In the Terminal or Command Prompt, add the development namespace to your environment using a YAML file.
    microk8s kubectl create -f namespace.yaml
    
  2. List the namespaces and see that development is now listed.
    microk8s kubectl get namespaces
    
  3. Add the NGINX image to your production namespace.
    microk8s kubectl run nginx --image=nginx --namespace=production
    
  4. List pods in the current namespace.
    microk8s kubectl get pods
    
    • You should get the message "No resources found in default namespace."
  5. Change the namespace from default to production.
    microk8s kubectl config set-context --current --namespace=production
    
  6. List the pods. The NGINX pod should appear in the list.

    microk8s kubectl get pods
    

    Get Pods

    • If you use microk8s kubectl get pods -A, this lists all pods in all namespaces in your environment.
    • Change the namespace from production to default.
      microk8s kubectl config set-context --current --namespace=default
      

namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: development
  labels:
    name: development