Create and Manage Namespaces Using Declarative Approach¶
Let's see how you can create and manage kubernetes namespaces declaratively.
Step 1: Create a Namespace¶
Let's create two namespaces dev and prod as follows:
Apply the manifest files to create namespaces:
# Create dev namespace
kubectl apply -f dev-namespace.yml
# Create prod namespace
kubectl apply -f prod-namespace.yml
Step 2: List Namespaces¶
List all the namespaces:
Step 3: Describe a Namespace¶
Describe dev namespace:
Describe prod namespace:
Step 4: Create Kubernetes Resources in a Namespace¶
Let's create two pods. One in dev namespace and another in prod namespace.
Apply the manifest files to create pods:
Similarly you can create Deployment, ReplicaSet, Service etc. in a namespace. You just have to specify namespace in the metadata section of the resource definition.
Resources will be configured in the default namespace if the namespace field is not set in the metadata section of the resource definition.
Step 5: List Resources in a Namespace¶
List all the pods in the dev namespace:
List all the pods in the prod namespace:
Also, let's start a shell session inside the nginx container of both the pods to verify if the application is running as expected.
-
Verify nginx container of
dev-podin thedevnamespace: -
Verify nginx container of
prod-podin theprodnamespace:
Step 6: Delete a Resource in a Namespace¶
Delete dev-pod from dev namespace:
Delete prod-pod from prod namespace:
Step 7: Delete a Namespace¶
Delete dev namespace:
Delete prod namespace: