Introduction ๐
Welcome to Day 33 of our Kubernetes journey! Today, we'll delve into the world of Namespaces and Services, two essential components for organizing and managing resources within a Kubernetes cluster.
Prerequisites Make instances of t2.medium(check previous blog)
install minikube in ece-instance (check previous blog)
Clone this repository to your local machine:
git clone github.com/mgupt21/django-todo-cicd-Jenkins..
sudo docker build . -t mgupt21/django-todo:latest
Login to your Docker Hub by using your username and password and push your image to Docker Hub as we used it for day 32 of 90 days of DevOps.
pushed the image into the docker hub
Task 1: What are Namespaces in k8s?
In Kubernetes (often abbreviated as k8s), Namespaces and Services are fundamental concepts that contribute to the organization, isolation, and networking capabilities within a cluster.
Namespaces:
Definition: Namespaces are virtual clusters within a Kubernetes cluster, providing a way to partition resources and create isolated environments. They are particularly useful in scenarios where multiple teams or projects share the same Kubernetes cluster.
Purpose:
Isolation: Namespaces allow you to create separate workspaces, preventing naming conflicts between resources like pods, services, and deployments.
Resource Management: They assist in better resource allocation, ensuring that each team or project has its own designated space.
Security: Namespaces contribute to security by isolating resources. Permissions and access control can be applied at the namespace level.
Example:
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
Here, a namespace named "my-namespace" is created.
Task 1.1: Create a Namespace for your Deployment
Namespaces in Kubernetes provide a way to divide cluster resources among multiple users or projects. They allow you to create isolated environments, preventing naming conflicts and providing better resource management.
Task 1.2: Use the command kubectl create namespace <namespace-name> to create a Namespace
Let's kick things off by creating a Namespace for our deployment. Execute the command:
kubectl create namespace <namespace-name>
Replace <namespace-name>
with your desired name.
Task 1.3: Update the deployment.yml file to include the Namespace
Open your deployment YAML file and add the following lines under the metadata section:
codemetadata:
namespace: <namespace-name>
Again, replace <namespace-name>
with the name you chose earlier.
Task 1.4: Apply the updated deployment using the command: kubectl apply -f deployment.yml -n <namespace-name>
Apply the changes to your cluster by running:
kubectl apply -f deployment.yml -n <namespace-name>
Task 1.5: Verify that the Namespace has been created by checking the status of the Namespaces in your cluster.
Ensure that the Namespace is successfully created by checking the status:
kubectl get namespaces
Task 2: Services, Load Balancing, and Networking in Kubernetes ๐
Services play a crucial role in Kubernetes, providing stable endpoints for applications. They enable load balancing, making it easier to manage traffic across pods.
Services:
Definition: Services are an abstraction that defines a set of pods and how they communicate. They provide a stable endpoint for applications, allowing them to be accessed consistently, regardless of the underlying pod instances.
Purpose:
Load Balancing: Services distribute incoming network traffic across multiple pods, ensuring even workload distribution.
Networking: They enable communication between different microservices within the Kubernetes cluster.
Stable Endpoints: Services provide a stable IP address and DNS name, abstracting the underlying pod instances.
Example:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
In this example, a service named "my-service" is created. It selects pods labeled with "app: my-app" and directs traffic to them on port 8080.
In summary, Namespaces help in organizing and isolating resources, while Services provide networking and load balancing capabilities for pods, contributing to the scalability and reliability of applications in Kubernetes.
Conclusion ๐ฅ
Congratulations on successfully working with Namespaces and Services in Kubernetes! Namespaces help organize resources, and Services facilitate networking and load balancing, enhancing the scalability and reliability of your applications. Keep exploring the vast world of Kubernetes, and stay tuned for more exciting tasks! ๐๐ฉโ๐ป๐จโ๐ป