Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Latest commit

 

History

History
144 lines (105 loc) · 3.48 KB

namespaces.md

File metadata and controls

144 lines (105 loc) · 3.48 KB

Kubernetes Namespaces

Overview

In Kubernetes, namespaces provide a way to group resources together and apply certain levels of control, like role based access control and even some default resource limits. You can read all about namespaces in the upstream Kubernetes document listed below. We'll focus on a few of the basic commands for creation and management of namespaces in this lab.

Kubernetes Namespaces

Namespace Creation and Deletion

Imperitive Approach

# List Namespaces
kubectl get ns

# Sample Output
NAME              STATUS   AGE
default           Active   11m
kube-node-lease   Active   11m
kube-public       Active   11m
kube-system       Active   11m
# Create Namesapce - Imperitive Method
kubectl create ns lab
# Describe Namesapce
kubectl describe ns lab

# Sample Output
Name:         lab
Labels:       kubernetes.io/metadata.name=lab
Annotations:  <none>
Status:       Active

No resource quota.

No LimitRange resource.
# Delete Namespace - Imperitive Method
kubectl delete ns lab

Declarative Approach

Create a new file called lab-namespace.yaml with the following contents:

apiVersion: v1
kind: Namespace
metadata:
  name: lab
# Apply the manifest file
kubectl apply -f lab-namespace.yaml
# Check the namespaces was created
kubectl get ns

# Sample Output
NAME              STATUS   AGE
default           Active   25m
kube-node-lease   Active   25m
kube-public       Active   25m
kube-system       Active   25m
lab               Active   4s

Run a pod in a namespace

# Start a pod in the lab namespace
kubectl run nginx --image=nginx --namespace lab
# Get pods
kubectl get pods

# Sample Output
No resources found in default namespace.

To view resource deployed into a namespace you need to provide the namespace or update your config context to target your preferred namespace. When no namespace is provided, or set in context the 'default' namespace will be assumed.

# Check the namespace in your current context
kubectl config get-contexts

# Sample Output
CURRENT   NAME           CLUSTER   AUTHINFO                         NAMESPACE
*         reddog-admin   reddog    clusterAdmin_EphLabPrep_reddog   

Let's re-run the commands both by passing in a namespace and by setting it in context.

# Get pods passing in namesapce
kubectl get pods -n lab

# Sample Output
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          8s
# Update the namespace in context
kubectl config set-context --current --namespace lab

# Check the context namespace is set
kubectl config get-contexts

# Sample Output
CURRENT   NAME           CLUSTER   AUTHINFO                         NAMESPACE
*         reddog-admin   reddog    clusterAdmin_EphLabPrep_reddog   lab

# Now get pods without passing the namesapce parameter
kubectl get pods

# Sample Output
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2m2s

Conclusion

You should now have a basic undersatnding of the use of namespaces. You can see more details at the following articles:

Next Lab:

Kubernetes Pods