Kubernetes Services
what is a Kubernetes service?
A service is a type of Kubernetes resource that causes a proxy to be configured to forward requests to a set of pods. The set of pods that will receive traffic is determined by the selector, which matches labels assigned to the pods when they were created. A service is a REST object in Kubernetes whose definition can be posted to Kubernetes apiServer on the Kubernetes master to create a new instance.
Service types:
ClusterIP − This helps in restricting the service within the cluster. It exposes the service within the defined Kubernetes cluster.
NodePort − It will expose the service on a static port on the deployed node. A ClusterIP service, to which NodePort service will route, is automatically created. The service can be accessed from outside the cluster using the NodeIP:nodePort.
Load Balancer − It uses cloud providers’ load balancer. NodePort and ClusterIP services are created automatically to which the external load balancer will route.
Ingress − Kubernetes Ingress is an API object that provides routing rules to manage external users’ access to the services in a Kubernetes cluster, typically via HTTPS/HTTP. Ingress allows you to configure and manage these capabilities inside the cluster.
How to create a service?
You can create a service for an existing deployment. I have mentioned how to create a deployment in my previous story →Link
using command prompt:
kubectl expose deployment my-web --port=8080 --target-port=80 --type=LoadBalancer
using yml file
After creating that deployment let us create a LoadBaalance type of service in that same folder and for that, you have to create a service.yml file and paste the below command.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- protocol: TCP
port: 8080
targetPort: 80
At the selector we are giving our app name is nginx as we have deployed in that name and the port should be 8080, as I mentioned above we have given our type as LoadBalancer.
after that run this command in your terminal
kubectl apply -f service.yml
After running this command go to the browser and type localhost:8080 and you can see the welcome page of nginx.
you can check the services using this command
kubectl get svc
That’s it for today. I hope you understand everything and thank you for reading.