Home » Backend Dev » kubernetes » 30 Days kubernetes » Day 7: Kubernetes – Services in Kubernetes

Day 7: Kubernetes – Services in Kubernetes

Introduction to Services in Kubernetes

In Kubernetes, Services act as stable endpoints that connect applications running in a cluster to each other or to external clients. They enable seamless communication between pods, which are ephemeral and can be created or destroyed frequently.

This article explores the types of services in Kubernetes and demonstrates how to expose your application to the external world.


What Are Kubernetes Services?

Kubernetes Services abstract and expose application workloads to ensure reliable network communication. They provide load balancing, discovery, and stable IPs for pods, regardless of pod lifecycle changes.

Key Features of Kubernetes Services:

  1. Stable Network Identity: Services assign a consistent DNS name and IP address to pods.
  2. Load Balancing: Distributes traffic among multiple pod replicas.
  3. Discovery: Pods can easily find and communicate with each other using service names.
  4. Port Mapping: Maps external traffic to internal pod ports.

Types of Kubernetes Services

Kubernetes supports several service types, depending on the traffic routing requirements.

1. ClusterIP (Default)

  • Exposes the service within the cluster.
  • Pods can communicate using the service’s internal IP.
  • Suitable for internal workloads.

Example:

apiVersion: v1
kind: Service
metadata:
  name: my-clusterip-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
  type: ClusterIP

Apply the service:

kubectl apply -f my-clusterip-service.yaml

2. NodePort

  • Exposes the service on a specific port of each node in the cluster.
  • Accessible from outside the cluster using <NodeIP>:<NodePort>.
  • Best suited for development or testing.

Example:

apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
    nodePort: 30007
  type: NodePort

Apply the service:

kubectl apply -f my-nodeport-service.yaml

3. LoadBalancer

  • Exposes the service to the external world via a cloud provider’s load balancer.
  • Ideal for production workloads.

Example:

apiVersion: v1
kind: Service
metadata:
  name: my-loadbalancer-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
  type: LoadBalancer

Apply the service:

kubectl apply -f my-loadbalancer-service.yaml

4. ExternalName

  • Maps a service to an external DNS name.
  • Does not require selector or pod management.

Example:

apiVersion: v1
kind: Service
metadata:
  name: my-externalname-service
spec:
  type: ExternalName
  externalName: example.com

Apply the service:

kubectl apply -f my-externalname-service.yaml

Exposing Applications to External Traffic

Kubernetes Services can expose applications to external traffic using NodePort or LoadBalancer.

Using NodePort:

  1. Create a NodePort service.
  2. Access the application via <NodeIP>:<NodePort>.

Using LoadBalancer:

  1. Deploy the LoadBalancer service.
  2. Use the external IP provided by the cloud provider to access the application.

Verify the Service:

kubectl get services

Check the external IP or node port details.


Monitoring and Troubleshooting Services

View Service Details:

kubectl describe service <service-name>

Debugging Issues:

  1. Ensure pods are running and labeled correctly:
kubectl get pods --selector=app=my-app
  1. Check logs for errors:
kubectl logs <pod-name>
  1. Test connectivity using kubectl port-forward:
kubectl port-forward service/my-clusterip-service 8080:80

Conclusion

Kubernetes Services play a crucial role in ensuring smooth communication between applications and external clients. By understanding the different service types, you can effectively expose and manage your workloads.


References

*** Your support will help me continue to bring new Content. Love Coding *** ❤️


Feedback and Discussion

Have questions or feedback? Comment below! Let’s build a collaborative learning environment. Check out more articles on Node.js, Express.js, and System Design.

Leave a Comment

Your email address will not be published. Required fields are marked *