Learn Kubernetes 101 - Part 1 - Using Namespaces - Rafay Product Documentation
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.
Kubernetes 101 - Namespaces - YouTube
Kubernetes 101 - Namespaces
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 namespacesAdd a namespace titled production to your environment.
microk8s kubectl create namespace productionList 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 ./DownloadsUse the following command to create an empty YAML file in your Downloads folder.
touch namespace.yamlUse the nano text editor in the Terminal.
nano namespace.yamlCopy and paste the configuration below into the text editor.
Press Cmd + X, then type Y and press Return 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.yamlList the namespaces and see that development is now listed.
microk8s kubectl get namespacesAdd the NGINX image to your production namespace.
microk8s kubectl run nginx --image=nginx --namespace=productionList 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=productionList 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
namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: development
labels:
name: development