Different types of services in Kubernetes

There are four types of Kubernetes services — ClusterIP, NodePort, LoadBalancer and ExternalName. The type property under Spec helps to determines which service is exposed to the network.

ClusterIP

ClusterIP is the most commonly used service type in Kubernetes. It is the default Kubernetes service.
We can define type as ClusterIP or leave blank in the service definition file when we need to configure our services as a ClusterIP.
Kubernetes will assign an internal IP to ClusterIP service. The scope of this service is with in the cluster only and it can’t be accessible directly from the outside of the Cluster. To expose these services to outside we need to use ingress controller.

ClusterIP Kubernetes Service
apiVersion: v1
kind: Service
metadata:
  name: "service-devops"
spec:
  selector:
    app: devops
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

NodePort

Exposes the service on each Node’s IP at a static port (The NodePort). By using, you can access the NodePort service from outside the cluster.

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

Mainly, there are two differences between NodePort and ClusterIP service definition file. The first is, You need to be defined type as NodePort and an additional property NodePort need to be defined. If you don’t specify, then it will pick a random ports between 30000–32767.

LoadBalancer

A LoadBalancer service is the standard and easiest way of exposing a service to outside the cluster using a cloud provider’s load balancer. Each cloud providers (AWS, GCP, Azure, Digital Ocean etc.) has its own load balancer services which all the upcoming request routes to kubernetes services.
LoadBalancer service is an extension of NodePort service.

LoadBalancer Kubernetes Service
apiVersion: v1
kind: Service
metadata:
  name: example-service
spec:
  selector:
    app: example
  ports:
    - port: 8765
      targetPort: 9376
  type: LoadBalancer

ExternalName

Services with type ExternalName work as other kubernetes services, but when you want to access to that service name, instead of returning cluster-ip of this service, it returns CNAME record with value that mentioned in externalName: parameter of service.

ExternalName Kubernetes Service
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: ExternalName
  externalName: my.database.example.com
0 Shares:
You May Also Like