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".
- Open the Terminal or command prompt.
-
Use the following command to list the namespaces.
microk8s kubectl get namespaces
-
Add a namespace titled production to your environment.
microk8s kubectl create namespace production
-
List the namespaces. Production is now listed.
microk8s kubectl get namespaces
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.
- Open the Terminal.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
touch namespace.yaml
- Use the nano text editor in the Terminal.
nano namespace.yaml
- Copy and paste the configuration below into the text editor.
- Press Cmd + X, then type Y and press Return to save the namespace.yaml file.
- Open the command prompt.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
copy NUL namespace.yaml
- Open the namespace.yaml file with a text editor. For example, use Notepad++ to edit the YAML file.
- Copy and paste the configuration below into the text editor.
- Save the namespace.yaml file.
- Open the Terminal.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
touch namespace.yaml
- Use the nano text editor in the Terminal.
nano namespace.yaml
- Copy and paste the configuration below into the text editor.
- 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.
- In the Terminal or Command Prompt, add the development namespace to your environment using a YAML file.
microk8s kubectl create -f namespace.yaml
- List the namespaces and see that development is now listed.
microk8s kubectl get namespaces
- Add the NGINX image to your production namespace.
microk8s kubectl run nginx --image=nginx --namespace=production
- List pods in the current namespace.
microk8s kubectl get pods
- You should get the message "No resources found in default namespace."
- Change the namespace from default to production.
microk8s kubectl config set-context --current --namespace=production
-
List the pods. The NGINX pod should appear in the list.
microk8s kubectl 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
- If you use
namespace.yaml¶
apiVersion: v1
kind: Namespace
metadata:
name: development
labels:
name: development