Kubernetes Service provides communication between components inside or outside the application. These Kubernetes Services will help connect applications to each other and connect to users as well.
For example, our application has groups that run from several parts, for example a frontend group or a backend group process or a third party group that are connected to communicate with each other with external data. With Kubernetes Services we can connect all groups connected to Kubernetes.
There are 3 types of Kubernetes Services, namely: a. NodePort, a service created for internal ports on Nodes.
b. ClusterIP, virtual IP address in the Cluster to enable communication within it. c. LoadBalancer, a service that can connect sets for example frontend and backend in our application.NodePort Implementation
The following is an example of blinding a service by implementing NodePort
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30008
selector:
app: myapp
type: front-end
As seen in the YAAML file above, we get a new field Port
which targets the node port and others.
Next, we try to create the service with the command below.
➜ kubectl create -f code/services/service-nodeport-definition.yaml
service/myaapp-service created
And we will see that when it is made it will look like below.
➜ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24d
myaapp-service NodePort 10.101.173.39 <none> 80:30008/TCP 71s
We can also see the diagram below which depicts the NodePort service configuration that we have implemented.
And when we use the nodeport for multiple nodes, the picture will be like below.
To see our service running with the URL that has been published by Minikube, we need to see the URL with the command below.
➜ minikube service myapp-service --url
http://127.0.0.1:50569
ClusterIP explanation
As explained briefly above, we can use ClusterIP to divide the IP address allocation so that it becomes a cluster that gathers together according to each other’s needs. For example, if we want to make the clusterIP something like backend
, frontend
, and redis
as shown below.
And how can we create the ClusterIP, here is an example of creating a YAML file.
apiVersion: v1
kind: Service
metadata:
name: back-end
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
selector:
app: myapp
type: back-end
then we create the service with the command below.
➜ kubectl create -f code/services/service-clusterip-definition.yaml
service/back-end created
And we will see whether the cluster we defined has been created using the command below.
➜ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
back-end ClusterIP 10.97.219.53 <none> 80/TCP 7s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24d
myapp-service NodePort 10.106.237.142 <none> 80:30008/TCP 18m
LoadBalancer implementation
The last type is LoadBalancer, which is a type that can combine each node for each application that we have created. So how do we do it if we want to create a Loadbalancer? Look at the code below.
apiVersion: v1
kind: Service
metadata:
name: myapp-loadbalancer
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
nodePort: 30009
then we create the service with the command below.
➜ kubectl create -f code/services/service-loadbalancer-definition.yaml
service/myapp-loadbalancer created
And we will see whether the cluster we defined has been created using the command below.
➜ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
back-end ClusterIP 10.97.219.53 <none> 80/TCP 8m45s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24d
myapp-loadbalancer LoadBalancer 10.99.140.200 <pending> 80:30009/TCP 21s
myapp-service NodePort 10.106.237.142 <none> 80:30008/TCP 27m