Updated April 17, 2023
Introduction to kubernetes service types
A service in Kubernetes is a logical abstraction which helps us to expose the application which is running of the pods or set of pods. If we want to use any service in the application then we do not need to modify it, if the service is unfamiliar. In Kubernetes service is known as an abstraction because it defines the pods and many policies to use them further. In order to identify the set of pods that is being pointed by the service, it uses a selector. As service provide abstraction, hence it helps to provide decoupling it is one of the good things to be followed while programming. In the coming section of the tutorial, we will see its internal working and how it can be implemented in the application to expose it for beginners to understand it better.
How do Kubernetes services work?
As we already discussed it is an abstraction layer which is used to define the set of pods, in this section we will see how it work internally Kubernetes, we can define service as a simple REST object, we can create all the them, and POST a service definition to the server in order to create the new instance from it. Below is the definition by which we can define a service in the Kubernetes see below;
e.g. :
apiVersion: v1
kind: Service
metadata:
name: your_name
spec:
selector:
app: name
ports:
- protocol: TCP
port: 80
targetPort: 9376
As you can see we are trying to define a service here, where we are defining name, version, posts, etc. to define it. Also, we need to mention what kind of REST object it is to use the ‘kind’ keyword to define it line above syntax.
1) first it assigns them in Service IP, this IP is used by the service proxy further.
2) We also have the controller for service who is responsible to scan the Pods which are matching with the selector of the service. After matching it sends any updates to that endpoint.
3) Default protocol used by the service is TCP.
4) With the help of service we can expose as many ports on the service object it is supported by Kubernetes.
What are the Kubernetes service types?
In this section we will see various types of the Kubernetes service, first, take a look at the component then we will see its type which makes them understand in a better way, let’s get started;
1) label selector: it helps to locate the pods
2) cluster IP: assigned the IP address and port number
3) port definition
4) mapping: it is an optional mapping to map ports to the targeted ports.
Now let’s get started with the types of Kubernetes service in detail to understand it better let’s get started;
We mainly have 4 different types of Kubernetes service which are mentioned below;
a) clusterIP: the main purpose of this type of service is it helps to expose a service that can be accessible from the given cluster.
b) NodePort: this type of service helps us to expose the service through the static port.
c) LoadBalancer: This type of service helps us to expose the service by using the cloud provider.
d)ExtrenalName: This type of service helps us to expose or map the service by using a predefined name ‘externalName’ filed.
As above we have seen the one-liner for each of the types of service provided by Kubernetes but now we will have to look at them in detail for better clarity so, Let’s discuss each of them in detail now, see below;
1) Kubernetes ClusterIP service: This is the default service provided by Kubernetes, it mainly uses the IP address to expose the service. But it has one restriction here is that it helps us to expose the service with the help of an IP address but it will be internal to the cluster. That means we can access the exposed service within the same cluster itself not from outside that cluster. This can be used when we do not want another external service to use our expose service, hence increases the accessibility part here. In short, we can say that it helps to expose the service on internal cluster IP. If we choose this service then we will only access the service with the cluster.
2) Kubernetes NodePort service: As this name suggests this type of service helps to expose the service on every node port. that means it helps us to open ports on every cluster node. So it will navigate the traffic to every node of the service, even if the service is not running on the running on that particular node, thus helps us to handle the traffic as well by navigating it to a different node. It is one of the high-level methods which can be used in development. If we want to connect to the NodePort from outside of any cluster then we can use this format to connect to them;
<NodeIP>:<NodePort>
3) Kubernetes ExternalName service: As the name suggests here, this type of service can be accessed by the external name assigned to them. Rather than access them via cluster IP etc. In short, if we want to access this service then we have to use the externalName field which we define when creating the service. It returns a CNAME record that contains the value of the externalName parameter. In this type of service, no proxy is set up.
4) Kubernetes Load Balancer service: This type of service helps us to expose the service to the cloud provider. Suppose we have a cluster that is running on any of the public clouds for example AZURE, Aws, so by creating a load balancer service, it will help us equivalent access like a cluster Ip, by expanding this to the external load balancer that will turn specifically to the cloud provider. So Kubernetes will automatically create the environment with all required things like firewall, also it will help us to populate the service with an external IP address provided by the cloud provider, creation of the load balancer, etc.
Conclusion
As we have seen the types, working and its components in detail we can now decide how to use this within the application, also we have seen how we can define a service, it is easy to use, handle, and maintainable by the developers as well. Also, provide decoupling through abstraction.
Recommended Articles
We hope that this EDUCBA information on “kubernetes service types” was beneficial to you. You can view EDUCBA’s recommended articles for more information.