What are services in K8s?
In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.
An example of a service in Kubernetes:
Let's say you have a Kubernetes cluster running multiple pods, and you want to expose these pods as a stable network endpoint so that other applications can access them. You can achieve this by creating a Kubernetes service.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
Why d we need Kubernetes Services ?
In Kubernetes, Pods are non-permanent resources - they can appear and disappear as needed. This is because Kubernetes constantly checks to make sure the cluster is running the desired number of replicas (copies) of your app. And Pods are created or destroyed to match this desired state.
Think of it this way: if you need more replicas of your app because of an increase in incoming traffic (more demand), Kubernetes will spin up some new Pods to handle it. If a Pod fails for some reason, no worries - Kubernetes will quickly create a new one to replace it. And if you want to update your app, Kubernetes can destroy old Pods and create new ones with the updated code. So the set of Pods running at one moment could be totally different from the set running a moment later.
But here's the thing - if you want to access your app, how do you keep track of which Pod to connect to with all these changing IP addresses of Pods?
That's where Services come in. They provide an unchanging location for a group ofPods. So even though the Pods themselves are dynamic, the Services make sure you always have a central location to access your app.
Services in Kubernetes play a crucial role in enabling communication and load balancing between different components within a cluster. They provide a stable network endpoint for accessing pods and allow you to easily scale and manage your applications. The specific type of service you choose depends on your use case and networking requirements.
Kubernetes Service Command:
a) List Service:
kubectl get service
b) Describe a Service
kubectl describe service <service-name>
c) Expose a Service
kubectl expose deployment my-deployment --name=my-service --port=80 --target-port=8080
d) Delete a Service
kubectl delete service <service-name>
e) Port Forward to a Service
kubectl port-forward service/my-service 8080:80
f) Check Endpoints
kubectl get endpoints <service-name>
g) Get ClusterIP
kubectl get service my-service -o=jsonpath='{.spec.clusterIP}'
h) Test Server Connectivity
kubectl run --rm -it --restart=Never --image=alpine testpod -- wget -q -O - my-service.default.svc.cluster.local:80
Task1:
Create a Service for your todo-app Deployment from Day-32
Create a Service definition for your todo-app Deployment in a YAML file.
Apply the Service definition to your K8s (minikube) cluster using the
kubectl apply -f service.yml -n <namespace-name>
command.Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace
Step 1:**
Create a Service for your todo-app Deployment from Day-32
#todo-app Deployment from Day-32 apiVersion: apps/v1 kind: Deployment metadata: name: todo-app labels: app: todo namespace: todo-app spec: replicas: 2 selector: matchLabels: app: todo template: metadata: labels: app: todo spec: containers: - name: todo image: chandanhdaam/todoapp:app1 ports: - containerPort: 8001
Step2:
Apply the deployment.yaml file to run the pod.
kubectl apply -f deployment.yaml kubectl get pods -n=todo-app
Create a Service definition for your todo-app Deployment in a YAML file.
To create services for the above app deployment, you can define a Kubernetes Service manifest file.
apiVersion: v1 kind: Service metadata: name: todo-app-service namespace: todo-app spec: type: NodePort selector: app: todo ports: - port: 80 targetPort: 8001 # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767) nodePort: 30007
kubectl apply -f service.yaml
Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.
To access the
todo-app
using the service's IP and port within your namespace, you can follow these steps:Check the IP address of the service by running the following
kubectl get service -n todo-app
Task-2:
Create a ClusterIP Service for accessing the todo-app from within the cluster
Create a ClusterIP Service definition for your todo-app Deployment in a YAML file.
Apply the ClusterIP Service definition to your K8s (minikube) cluster using the
kubectl apply -f cluster-ip-service.yml -n <namespace-name>
command.Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.
Create a ClusterIP Service for accessing the todo-app from within the cluster
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
labels:
app: todo
namespace: todo-app
spec:
type: ClusterIP
selector:
app: todo
ports:
- protocol: TCP
port: 80
targetPort: 8001
In the above example, the Service is named todo-cluster and is of type ClusterIP. The selector app: todo is used to determine which Pods to associate with the Service.
Apply the ClusterIP Service definition to your K8s (kuebadm) cluster using
kubectl apply -f service.yml -n <namespace-name> # -n use when namspace in not mention in service file
Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.
Task -3
LoadBalancer Service in K8s
Create a LoadBalancer Service for accessing the todo-app from outside the cluster .
Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file
apiVersion: v1
kind: Service
metadata:
name: todo-app-service
namespace: todo-app
spec:
type: LoadBalancer
selector:
app: todo
ports:
- protocol: TCP
port: 80
targetPort: 8001
Apply the LoadBalancer Service to your K8s cluster using the kubectl apply -f load-balancer-service.yml -n command.
Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.
Thank you for 📖reading my blog, 👍Like it and share it 🔄 with your friends.
Happy learning😊😊 !!!