What is Kubernetes Services?
Kubernetes services enable communication between two various components within the cluster or outside the cluster. By using services you can enable connectivity between groups of pods. It allows us to access our services outside the cluster in the real world.
Services help us to connect applications with other applications.
Feature Provided by Kubernetes Services:
Service Discovery: Kubernetes Services enable service discovery within the cluster. Other pods can discover and communicate with a service using its DNS name or IP address.
Load Balancing: Services automatically load balance traffic among the pods that are part of the service, distributing requests evenly across the available instances.
External Service Exposure: Services can be exposed externally to the cluster, making applications accessible from outside. There are different types of services for various use cases, such as NodePort, LoadBalancer, and Ingress.
Service Types: Kubernetes supports various types of services, including ClusterIP (default, accessible only within the cluster), NodePort (exposes the service on each node's IP at a static port), LoadBalancer (provisions an external load balancer), and External Name (maps the service to a DNS name).
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
Types of Services in K8S:
In K8S different types of services are available.
a) ClusterIP Service
b) NodePort Service
c) LoadBalacer Service
d) Headless Service
ClusterIP service:
ClusterIP service is a type of service that exposes a set of pods within the cluster to other pods in the same cluster. It provides a stable internal IP address and a DNS name that can be used for communication between different parts of an application.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ClusterIP
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
NodePort Service:
In NodePort Service, Service Listen on the port of a node and forwards requests to the port of the pod. This service helps us by mapping a port on the node to a port on the pod.
This service uses three ports.
a) port: It refers to the port of the service itself.
b) targetport: It refers to the port on the pod.
c) nodeport: It refers to the port of the node.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: NodePort
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodeport: 3008
LoadBalancer Service:
LoadBalancer service is a type of service that exposes an application to the external traffic of the cluster. This type of service is useful when you want to make your application accessible from outside the cluster, such as from the internet.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: LoadBalancer
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodeport: 3008
Headless Service:
In Kubernetes, headless services are used for statefulset applications to maintain the state. A Kubernetes headless Service allows a client to connect to whichever Pod it prefers, directly. When a client sends a request to a headless Service, it will get back a list of all the Pods that this Service represents (in this case, the ones with the label "all: my-web-app"). The Service now lets the client decide on how it wants to connect to the Pods.
❄Tasks:
Create a Service definition for your nginx-app Deployment in a YAML file. Apply the Service definition to your K8s cluster using the kubectl apply -f service.yml -n <namespace-name>
command. Verify that the Service is working by accessing the nginx using the Service's IP and Port in your Namespace.
Here I am using the Play-with-k8s cluster which is freely provided by Docker for practice.
Step 1: Create deployment by using deployment.yaml file and then Create a Service by using service.yaml. In this using NodePort Service.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx-app
template:
metadata:
name: nginx-app
labels:
app: nginx-app
spec:
containers:
- name: nginx-app
image: nginx
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx-app
ports:
- port: 80
targetPort: 80
kubectl get service
curl -L http://192.168.0.18:30246
Create a ClusterIP Service for accessing the Nginx app from within the cluster. Create a ClusterIP Service definition for your Nginx-app Deployment in a YAML file. Apply the ClusterIP Service definition to your K8s cluster using the kubectl apply -f cluster-ip-service.yml -n <namespace-name>
command. Verify that the ClusterIP Service is working by accessing the nginx-app from another Pod in the cluster in your Namespace.
Step 1: Create a service as a ClusterIP.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: ClusterIP
selector:
app: nginx-app
ports:
- port: 80
targetPort: 80
kubectl get service
curl -L 10.107.191.40
Create a LoadBalancer Service for accessing the Nginx app from outside the cluster. Create a LoadBalancer Service definition for your nginx-app Deployment in a YAML file. Apply the LoadBalancer Service definition to your K8s cluster using the kubectl apply -f load-balancer-service.yml -n <namespace-name>
command. Verify that the LoadBalancer Service is working by accessing the Nginx app from outside the cluster in your Namespace.
Step 1: Create a Service as a Load Balancer.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: LoadBalancer
selector:
app: nginx-app
ports:
- port: 80
targetPort: 80
kubectl get service
curl -L 192.168.0.18:30265
Create a Headless Service with nginx-deployment and try to access it. Verify when you do nslookup it gives back to you a list of all pods.
Step 1: Create a Headless service by using the service.yaml.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
clusterIP : None
selector:
app: nginx-app
ports:
- port: 80
targetPort: 80
Step 2: Apply service and Create another pod nginx-2 and go inside the different pod.
kubectl get svc
kubectl run nginx-2 --image=nginx
kubectl get pods
kubectl exec -it nginx-2 -- bash
Step 3: Do nslookup to get the list of all the pods that serve the nginx service.
nslookup nginx-service
In the Next Article, we will go deep down in K8S Secrets and configmap.......
Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on LinkedIn Unnati Gupta. Happy Learning !!!