It's common to have many replica services serving applications in production in order to offer a good user experience. However, no matter how many hosts or images are involved in this process, we need to offer a unique entry point for all this functionality: this is what Kubernetes services are intended for.
A Kubernetes service acts as both an endpoint and load balancer for a specific application. Since a service is located in front of a group of replicated pods, it will distribute the traffic across all the available instances.
Let's look at an example of a configuration file for a Kubernetes service:
apiVersion: v1
kind: Service
metadata:
name: application-xyz-service
spec:
ports:
port:80
targetPort: 80
protocol: TCP
selector:
tier:front-end
The kind configuration entry in line 2 has a new value—in this case, the value is Service. The selector indicates the replica container associated with this service, and the rest of the configuration parameters are self-explanatory. Using this file, you can use the kubectl create command as follows:
kubectl create -f application-xyz-service.yaml
Furthermore, if you don't want to create a file for a service, you can directly expose an existing replica controller using the following command:
kubectl expose rc application-xyz-rc