Services
- Services interact with various components inside and outside of the application.
- Allows to connect other applications or users together.
- For example, a bunch of pods where a frontend is serving loads to users, a second group for running backup processes and a third group connected to an external data source.
- The
servicesenable the connectivity between the groups of pods. - Allows the frontend application to be made available to end users and helps provide connections between the backend pods as well.
- Also allows connection to data sources (like a storage source for files).
- Pods communicate with each other via internal networking.
- For example, how do we as an external user access the webpage and to access the node at
192.168.1.2. The pod network in a range of10.244.0.0and the pod itself is assigned10.244.0.2- To see the webpage, we
sshinto the node on192.168.1.2and then runcurl 10.244.0.2to access the webpage directly. - However, ideally we want to just access
192.168.1.2and see the webpage information directly.- There needs to be something in the middle to map the node IP request to the container.
- This is where the Kubernetes
serviceis used. Its an object like a replicaset or deployment as an example.- In this case, the
servicelistens for requests on a port of the node and forwards those requests to the pod running the application. This service is called aNodePortservice. The service listens to a port on the node and forwards the request to the pods.
- In this case, the
- This is where the Kubernetes
- There needs to be something in the middle to map the node IP request to the container.
- To see the webpage, we
- For example, how do we as an external user access the webpage and to access the node at
- Multiple Service Types are available:
NodePort- makes an internal pod accessible via a port on the node.ClusterIP- Creates a virtual IP inside the cluster, to facilitate communication between different services such as a set of frontend servers to a set of backend servers.LoadBalancer- provisions a load balancer for the application for supported cloud providers. An example is distributing load across servers.
NodePortin more detail - back to the webpage pod, the port running on the pod is80- this is theTargetPort, as theserviceis forwarding the outside request to this port.- The second port is the port on the
service- it is just known asPort- theservicecan be thought of as a virtual server inside the node. The service has is own IP address and this known as theClusterIPof theservice. - The third port is the port on the Node itself, which is just called
NodePort. The example port that this is set to in this case is30008.NodePortscan only be in the range of30000 - 32767
- The second port is the port on the
- To create the
Service, you need aservice-definition.ymlfile. The file looks like so:apiVersion: v1 kind: Service metadata: name: myapp-service # Most important part of the file is spec to differ the services. spec: type: NodePort ports: - targetPorts: 80 # Port on the service object. The "port" field is mandatory. If you do not provide a nodePort, then one is automatically allocated. Can have multiple port mappings in one service. port: 80 nodePort: 30008 selector: app: myapp type: front-end - Nothing in the
definitionfile that connects the service to the target port.- Can use
labelsandselectorsfor linking. - The pod was created with a label and that same label needs to be brought into the
service definitionfile. Under theselector:, we need to provide the labels from thepod definition file, in the above example this isappandtype.- Once performed, this links the
serviceto thepod.
- Once performed, this links the
- Can use
- Create the
serviceusing thekubectl create -f service-definition.yaml - To see the service, run
kubectl get services.- It will show
Name,TYPE,CLUSTER-IP,EXTERNAL-UP,PORT(S)andAGE
- It will show
- We can then use the port
300008and the IP of the node to successfully access the container running in the node.curl http://192.168.1.2:30008 - What do you do if you have multiple pods. In the above example we had
10.244.0.2, but we also may have two other pods with10.244.0.3and10.244.0.4respectfully. - All of the pods would have the same
labels:labels: app: myapp - The same
labelofapp: myappis used when creating the service. - When the
serviceis then created, it finds three pods, because each pod would have the samelabel.- No additional configuration is required on the
serviceorpodside. The algorithm used isRandonwithSessionAffinityset toYes.- Thus this allows the
serviceto act as aLoad Balancerand distribute the load across different pods.
- Thus this allows the
- No additional configuration is required on the
- What happens if the pods are distributed across multiple nodes.
- Kubernetes automatically creates a
servicethat spans all nodes in the cluster. TheTargetPortis set as the same node port in the whole cluster.- That means you can access the cluster using the IP of any node in the cluster and using the same port number. For example nodes with
192.168.1.2,192.168.1.3and192.168.1.4, all would work if you runcurl 192.168.1.<number>:30008
- That means you can access the cluster using the IP of any node in the cluster and using the same port number. For example nodes with
- Kubernetes automatically creates a
- When
podsare removed or added, theserviceis automatically updated. - Once created, you don’t need to make any configuration changes.