Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Kubernetes - Using Namespaces

Core Concepts in Kubernetes

Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. This guide provides an understanding of namespaces, one of the core concepts in Kubernetes.

Key Points:

  • Namespaces provide a mechanism for isolating groups of resources within a single Kubernetes cluster.
  • They are intended for use in environments with many users spread across multiple teams, or projects.
  • Namespaces help to organize resources and can be used to manage access and resource allocation.

What is a Namespace?

A namespace is a way to divide cluster resources between multiple users. It is a way to create multiple virtual clusters within a single physical cluster. Namespaces are intended for environments with many users spread across multiple teams, or projects.

Creating and Managing Namespaces

Here are some basic commands to create and manage namespaces:

# List all namespaces
kubectl get namespaces

# Create a new namespace
kubectl create namespace 

# Delete a namespace
kubectl delete namespace 

# Get detailed information about a specific namespace
kubectl describe namespace 
                

Using Namespaces

You can specify the namespace for a resource when creating it, or switch the current context to a specific namespace:

# Create a pod in a specific namespace
kubectl run nginx --image=nginx --namespace=

# Switch the current context to a specific namespace
kubectl config set-context --current --namespace=

# List all pods in the current namespace
kubectl get pods

# List all pods in a specific namespace
kubectl get pods --namespace=
                

Resource Quotas and Limits

Namespaces can be used to set resource quotas and limits to manage resource allocation:

# Example of a resource quota definition
apiVersion: v1
kind: ResourceQuota
metadata:
  name: example-quota
  namespace: 
spec:
  hard:
    pods: "10"
    requests.cpu: "4"
    requests.memory: "8Gi"
    limits.cpu: "10"
    limits.memory: "16Gi"
                

Best Practices

Follow these best practices when working with namespaces:

  • Organize Resources: Use namespaces to organize resources logically by team, project, or environment (e.g., development, staging, production).
  • Set Resource Quotas: Define resource quotas to control resource consumption within namespaces and prevent resource exhaustion.
  • Use Role-Based Access Control (RBAC): Apply RBAC policies to namespaces to control access and permissions for different users and teams.
  • Monitor Resource Usage: Regularly monitor resource usage within namespaces to ensure efficient utilization and to identify any potential issues.

Conclusion

This guide provided an overview of namespaces in Kubernetes, including their creation, management, and use cases. By understanding namespaces, you can effectively organize and manage resources in your Kubernetes cluster, ensuring better isolation, resource allocation, and access control.