With the running application, we want to access one service. Let’s create a ClusterIP type of service. We can:
Like all Kubernetes objects, services are defined in YAML or JSON. The YAML file consists of the following parts:
Now, we will look into an example of YAML file:
The service ‘service-backend’ will be created, and any pod in the cluster can access it on their port 333 via http://service-backend:4000, or at the cluster’s IP address using port 4000.
Kubernetes services can also be created using the ‘kubectl expose’ command, which does not require a YAML file. The same service can be created using the command:
kubectl expose deployment deployment-backend – – port=333- – target-port=4000 –name=service-backend
Once you’ve got the configuration complete, create the service in your cluster as below:
kubectl apply -f service.yaml
To check that your service is running, you can execute the following command:
kubectl get services
You can see your new service in the list of services returned by this command. You can now access your pods using the service.