Mastering Namespaces and Services in Kubernetes | Day 33 of | 90 Days of DevOps
Welcome to another exciting journey in the world of DevOps and Kubernetes! In this blog post, we're going to cover two essential tasks that every DevOps engineer should be familiar with. First, we'll learn how to create a Namespace for your Deployment, and then we'll delve into the world of Services, Load Balancing, and Networking in Kubernetes. ๐ ๏ธ
What is Namespace and service in K8s?
Kubernetes (K8s) is a powerful container orchestration platform that allows you to manage and deploy containerized applications at scale. Two important concepts in Kubernetes are "Namespaces" and "Services."
Namespaces:
Definition: Namespaces are a way to organize and isolate resources within a Kubernetes cluster. They provide a scope for naming objects, such as pods, services, and volumes, to avoid naming conflicts.
Use Case: Namespaces are used to logically divide the resources in a cluster. They can be used to separate applications or teams within the same cluster, providing isolation and management boundaries.
Example Image:
In the image above, you can see how multiple namespaces (e.g., "dev," "test," "prod") are used to separate applications and resources. Each namespace has its own set of pods, services, and other objects, which helps in organizing and managing different environments or projects.
Services:
Definition: Services are an abstraction that defines a set of pods and how to access them. They provide a stable endpoint (IP address and DNS name) for connecting to a group of pods, even as the pods themselves may come and go.
Use Case: Services are used to ensure that applications running in a Kubernetes cluster can communicate with each other reliably, regardless of the underlying network or the dynamic nature of pods.
Example Image:
In the image above, you can see a service that acts as a load balancer for a set of pods. Clients can access the service's IP address to reach any of the pods behind it. This abstraction simplifies network communication and makes it possible to scale pods without affecting how clients connect to them.
Task 1: Creating a Namespace for Your Deployment ๐๏ธ
Namespaces in Kubernetes are a way to divide cluster resources among multiple users, teams, or projects. They provide a scope for names, allowing you to have resources with the same name within different namespaces. Let's go through the steps to create a Namespace for your Deployment.
Step 1: Create a Namespace
To create a Namespace, use the following command:
kubectl create namespace <namespace-name>
Replace <namespace-name>
with the desired name for your Namespace.
Example:
kubectl create namespace nginx
Step 2: Update the Deployment YAML
Now, you need to specify the Namespace for your Deployment. In your deployment.yml
file, add the namespace
field under the metadata
section:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx # Add the namespace here
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
Step 3: Apply the Updated Deployment
Apply the updated Deployment using the following command:
kubectl apply -f deployment.yml -n <namespace-name>
Example:
kubectl apply -f deployment.yml -n nginx
This will create the Deployment and its associated ReplicaSet and Pods.
Step 4: Verify the Deployment is created by running:
kubectl get deployments -n nginx
Step 5: Verify the Namespace
To check if the Namespace has been successfully created, you can run the following command:
kubectl get namespaces
You should see your newly created Namespace, in this case, "nginx," in the list.
Task 2: Services, Load Balancing, and Networking in Kubernetes ๐
Now that we have our Deployment set up in a Namespace, let's dive into the world of Services, Load Balancing, and Networking in Kubernetes.
In Kubernetes, a Service is an abstraction that defines a logical set of Pods and a policy for accessing them. Services enable network access to a set of Pods, and they provide load balancing among those Pods.
A common use case is to expose a Deployment to the external world. To do this, you would create a Service of type "LoadBalancer." The Kubernetes cloud provider (like AWS or GCP) takes care of provisioning a load balancer and configuring it to route traffic to the Service.
Here's an example of how you can create a LoadBalancer Service:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
type: NodePort
ports:
- protocol: TCP
port: 80 #port of pod
targetPort: 80 #for exposing local
nodePort: 30007 #for expose to user this node port must be range between 30000 to 32767
type: LoadBalancer
Step1:
After saving this service file apply the following command to apply the services :
kubectl apply -f service.yml
Step 2:
Verify that the Service is created by running:
kubectl get services -n nginx
By specifying type: LoadBalancer
, Kubernetes will take care of the load balancing configuration for you.
This Service will route external traffic to the Pods labeled with app: nginx
and forward requests to port 80.
That's it for this blog post! You've learned how to create a Namespace for your Deployment and explored the basics of Services, Load Balancing, and Networking in Kubernetes. These are fundamental skills for any DevOps engineer working with containerized applications. ๐
Happy Kubernetes-ing! If you have any questions or need further assistance, feel free to leave a comment below. ๐๐ฉโ๐ป๐จโ๐ป