π Introduction:
In this blog, we will explore Kubernetes (k8s) two key concepts Namespaces and Services. These are critical components of the Kubernetes architecture that help you organize your workloads and enable communication between them.
What are Namespaces in Kubernetes (k8s)?
Namespaces are a way to group resources within a Kubernetes cluster. They provide a virtual environment where you can create, update, and manage your Kubernetes objects. By using Namespaces, you can segment your cluster into logical units based on teams, projects, or environments.
The benefits of using Namespaces in Kubernetes are numerous. For example:
Resource isolation: You can prevent resources from interfering with each other by putting them in separate Namespaces. This is particularly useful for teams working on different projects or applications.
Access control: Namespaces provides a way to limit access to Kubernetes objects based on user roles and permissions. This helps to ensure that resources are only accessed by authorized users.
Resource quotas: You can set resource quotas for each Namespace to prevent one team or application from using too many resources and impacting the performance of others.
What are Services in Kubernetes (k8s)?
Services provide a way to expose your applications running in a cluster to other services or external clients. A Service acts as a stable endpoint for your application, allowing other components to discover and communicate with it.
There are different types of Services in Kubernetes, including:
ClusterIP: This is the default type of Service and exposes the Service on a cluster-internal IP. This type of Service is useful for internal communication between components within the same cluster.
NodePort: This type of Service exposes the Service on a port on each node in the cluster. This is useful for exposing your application to external clients.
LoadBalancer: This type of Service creates a load balancer in the cloud provider's infrastructure to distribute traffic to your Service. This is useful for scaling your application horizontally.
The benefits of using Services in Kubernetes are also numerous. For example:
Load balancing: Services provide load-balancing capabilities to distribute traffic across multiple instances of your application.
Service discovery: Services provide a stable endpoint for your application that can be used by other components to discover and communicate with it.
Portability: Services provide a way to expose your application consistently, regardless of where it's deployed.
πΌTask: 1
Create a Namespace for your Deployment
Use the command
kubectl create namespace <namespace-name>
to create a NamespaceUpdate the deployment.yml file to include the Namespace
Apply the updated deployment using the command:
kubectl apply -f deployment.yml -n <namespace-name>
Verify that the Namespace has been created by checking the status of the Namespaces in your cluster.
Steps :
- Create a Namespace for your Deployment
kubectl create namespace todo-namespace
- Update the deployment.yml file to include the Namespace
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-deployment
namespace: todo-namespace
labels:
app: todo-app
spec:
replicas: 2
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: sutish/django-todo-cicd:latest
ports:
- containerPort: 8000
- Apply the updated deployment
kubectl apply -f deployment.yaml -n todo-namespace
- Verify that the Namespace
kubectl get namespaces
- check the status of the deployment and its replicas in the todo-namespace
kubectl get deployments -n todo-namespace
kubectl get pods -n todo-namespace
π Conclusion :
In this blog, we cover Kubernetes (k8s) Namespaces and Services, we will cover some advanced topics.
Thank you for reading!
Contact me on Linkedin π€
Check out my GitHub for more resources π