Introduction
Namespaces in Kubernetes are a way to divide cluster resources between multiple users. They provide a mechanism for isolating groups of resources within a single cluster. Namespaces are intended for use in environments with many users spread across multiple teams, or projects.
Key Concepts
- Isolation: Namespaces provide a scope for names. Resources in different namespaces can have the same name.
- Resource Quotas: You can set resource quotas for each namespace to limit the amount of resources that can be consumed.
- Access Control: Role-Based Access Control (RBAC) can be applied to namespaces to control access to resources.
Creating a Namespace
To create a namespace, you can use the kubectl
command or a YAML configuration file.
Using kubectl
Command
Using a YAML Configuration File
Apply the configuration:
Listing Namespaces
To list all namespaces in your cluster, use the following command:
Switching Between Namespaces
To switch between namespaces, you can use the kubectl config set-context
command:
Using Namespaces in Resource Definitions
When defining resources, you can specify the namespace in the metadata section of the YAML file:
apiVersion: v1 kind: Pod metadata: name: my-pod namespace: my-namespace spec: containers: - name: my-container image: nginx
Practical Example
Let's create a namespace and deploy a pod within that namespace.
Step 1: Create a Namespace
Create a file named namespace.yaml
:
Apply the configuration:
Step 2: Deploy a Pod in the Namespace
Create a file named pod.yaml
:
apiVersion: v1 kind: Pod metadata: name: nginx-pod namespace: dev spec: containers: - name: nginx image: nginx
Apply the configuration:
Step 3: Verify the Pod is Running in the Namespace
Exercises
Exercise 1: Create and Use a Namespace
- Create a namespace named
test
. - Deploy a pod named
test-pod
using thenginx
image in thetest
namespace. - Verify that the pod is running in the
test
namespace.
Solution
- Create the namespace:
kubectl create namespace test
- Create a file named
test-pod.yaml
:
Apply the configuration:apiVersion: v1 kind: Pod metadata: name: test-pod namespace: test spec: containers: - name: nginx image: nginx
kubectl apply -f test-pod.yaml
- Verify the pod:
kubectl get pods -n test
Exercise 2: Set a Resource Quota for a Namespace
- Create a namespace named
quota-ns
. - Set a resource quota in the
quota-ns
namespace to limit the number of pods to 5.
Solution
- Create the namespace:
kubectl create namespace quota-ns
- Create a file named
resource-quota.yaml
:
Apply the configuration:apiVersion: v1 kind: ResourceQuota metadata: name: pod-quota namespace: quota-ns spec: hard: pods: "5"
kubectl apply -f resource-quota.yaml
Conclusion
Namespaces are a powerful feature in Kubernetes that help manage and isolate resources within a cluster. They are essential for organizing resources, applying resource quotas, and implementing access control. By understanding and utilizing namespaces, you can better manage your Kubernetes environment, especially in multi-user and multi-team scenarios.
Kubernetes Course
Module 1: Introduction to Kubernetes
- What is Kubernetes?
- Kubernetes Architecture
- Key Concepts and Terminology
- Setting Up a Kubernetes Cluster
- Kubernetes CLI (kubectl)
Module 2: Core Kubernetes Components
Module 3: Configuration and Secrets Management
Module 4: Networking in Kubernetes
Module 5: Storage in Kubernetes
Module 6: Advanced Kubernetes Concepts
Module 7: Monitoring and Logging
- Monitoring with Prometheus
- Logging with Elasticsearch, Fluentd, and Kibana (EFK)
- Health Checks and Probes
- Metrics Server
Module 8: Security in Kubernetes
Module 9: Scaling and Performance
Module 10: Kubernetes Ecosystem and Tools
Module 11: Case Studies and Real-World Applications
- Deploying a Web Application
- CI/CD with Kubernetes
- Running Stateful Applications
- Multi-Cluster Management