To understand the types of services first lets understand what are services
services are basically collection of different pods having same set of functions. These are the services that are accessed by the clients/users.
You already know there are 3 types of service types:
- ClusterIP
- NodePort
- LoadBalancer
Lets talk about them one by one
ClusterIP:
ClusterIP is the default kubernetes service. This service is created inside a cluster and can only be accessed by other pods in that cluster. So basically we use this type of service when we want to expose a service to other pods within the same cluster.
This service is accessed using kubernetes proxy.
apiVersion: v1
kind: Service
metadata:
name: my-internal-service
spec:
selector:
app: my-app
type: ClusterIP
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
Nodeport:
NodePort opens a specific port on your node/VM and when that port gets traffic, that traffic is forwarded directly to the service.
There are a few limitations and hence its not advised to use NodePort
- only one service per port
- You can only use ports 30000-32767
- Dealing with changing node/VM IP is difficult
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
LoadBalancer:
This is the standard way to expose service to the internet. All the traffic on the port is forwarded to the service. It's designed to assign an external IP to act as a load balancer for the service. There's no filtering, no routing. LoadBalancer uses cloud service
Few limitations with LoadBalancer:
- every service exposed will it's own ip address
- It gets very expensive