Demystifying Services in Kubernetes: Providing Stable Network Identities
Introduction to Kubernetes Services
In the dynamic world of container orchestration, Kubernetes (K8s) plays a pivotal role in managing containerized applications. One of the key features that Kubernetes provides is Services. These are essential objects that bestow stable network identities upon Pods, while abstracting away the intricacies of individual Pod IP addresses. Services empower Pods to seamlessly receive traffic from other Pods, Services, and external clients, facilitating efficient communication within a cluster.
Task 1: Creating a Service for your todo-app Deployment
Step 1: Service Definition
To begin, create a Service definition for your todo-app Deployment in a YAML file. This file will outline the desired properties of the Service.
COPY
COPY
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
Step 2: Applying the Service Definition
Next, apply the Service definition to your K8s (minikube) cluster using the following command:
COPY
COPY
kubectl apply -f service.yml -n <namespace-name>
Step 3: Verification
Verify that the Service is operational by accessing the todo-app using the Service's IP and Port within your Namespace.
Task 2: Creating a ClusterIP Service for Internal Access
Step 1: ClusterIP Service Definition
Now, let's create a ClusterIP Service definition for your todo-app Deployment in a YAML file.
COPY
COPY
apiVersion: v1
kind: Service
metadata:
name: todo-app-clusterip-service
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
Step 2: Applying the ClusterIP Service Definition
Apply the ClusterIP Service definition to your K8s (minikube) cluster using the following command:
COPY
COPY
kubectl apply -f cluster-ip-service.yml -n <namespace-name>
Step 3: Verification
To ensure that the ClusterIP Service is functioning correctly, access the todo-app from another Pod within the cluster in your Namespace.
Task 3: Creating a LoadBalancer Service for External Access
Step 1: LoadBalancer Service Definition
Now, let's create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.
COPY
COPY
apiVersion: v1
kind: Service
metadata:
name: todo-app-loadbalancer-service
spec:
selector:
app: todo-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
Step 2: Applying the LoadBalancer Service Definition
Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the following command:
COPY
COPY
kubectl apply -f load-balancer-service.yml -n <namespace-name>
Step 3: Verification
To verify the functionality of the LoadBalancer Service, access the todo-app from outside the cluster in your Namespace.
By completing these tasks, you have gained valuable hands-on experience in creating and configuring different types of Services in Kubernetes. This knowledge is fundamental for ensuring seamless communication and accessibility within your cluster, whether for internal operations or external interactions.