Services
Module
A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them.
Overview
At the end of the module, you will :
Learn how to expose a Pod internally
Learn how the domain name resolution works on a Kubernetes cluster
Learn how to expose a Pod externally
Prerequisites
Create the directory data/services in your home folder to manage the YAML file needed in this module.
mkdir ~/data/servicesCreate
Kubernetes Pods are mortal. They are born and when they die, they are not resurrected. ReplicaSets in particular create and destroy Pods dynamically. While each Pod gets its own IP address, even those IP addresses cannot be relied upon to be stable over time. This leads to a problem.
A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them. The set of Pods targeted by a Service is determined by a label selector.
Kubernetes manage four kinds of services depending on the exposition needed for the Pods attached :
Type
Description
ClusterIP
Only accessed within the Kubernetes cluster
NodePort
Host exposition accessible from the local network
LoadBalancer
External exposition, Internet access
Endpoint
External endpoint definition to easily use it in the cluster
The create command can directly ask the API resource to create a Service in command line or create a Service object based on a yaml file definition.
Exercise n°1
Create an Nginx Deployement and expose the Nginx Deployemnt to be able to access it from the local network on HTTP (80) port in command line.
Exercise n°2
Update the Nginx Pod in the namespace app-demo to be able to access it on HTTP (80) and HTTPS (443) port thanks to a YAML file.
Update the service with the new yaml file definition.
Get
The get command list the object asked. It could be a single object or a list of multiple objects comma separated. This command is useful to get the status of each object. The output can be formatted to only display some information based on some json search or external tools like tr, sort, uniq.
The default output display some useful information about each services :
Name : the name of the newly created resource
Type : the type of exposition defined at service creation (see previous table).
Cluster-IP : the internal cluster IP of the resources
External-IP : the external resources IP of the resources, field configured when LoadBalancer type is used
Port(s) : a list of ports opened in the pods managed by the service
Age : the age since his creation
Exercise n°1
Get the list of existing service in the default namespace.
Describe
Once an object is running, it is inevitably a need to debug problems or check the configuration deployed.
The describe command display a lot of configuration information about the Service(s) (labels, resource requirements, etc.) or any other Kubernetes objects, as well as status information about the Service(s) and Pod (state, readiness, restart count, events, etc.).
This command is really useful to introspect and debug an object deployed in a cluster.
Exercise n°1
Describe one of the existing service in the default namespace.
Explain
Kubernetes come with a lot of documentation about his objects and the available options in each one. Those information can be fin easily in command line or in the official Kubernetes documentation.
The explain command allows to directly ask the API resource via the command line tools to display information about each Kubernetes objects and their architecture.
Exercise n°1
Get the documentation of a specific field of a resource.
Add the --recursive flag to display all of the fields at once without descriptions.
Delete
The delete command delete resources by filenames, stdin, resources and names, or by resources and label selector.
Deleting a Service will have no effect on the Pod state. It only delete the access to each resources managed by the service.
Note that the delete command does NOT do resource version checks, so if someone submits an update to a resource right when you submit a delete, their update will be lost along with the rest of the resource.
Exercise n°1
Delete the service and the deployment created previously in the default namespace.
Module exercise
The purpose of this section is to manage each steps of the lifecycle of an application to better understand each concepts of the Kubernetes course.
The main objective in this module is to understand how to expose a service depending on the security level needed.
For more information about the application used all along the course, please refer to the Exercise App > Voting App link in the left panel.
Based on the principles explain in this module, try by your own to handle this steps. The development of a yaml file is recommended.
The file developed has to be stored in this directory : ~/data/votingapp/04_services
Manage the access of each part of the Voting App based on those conditions :
The PostgreSQL database Deployment must only be access from the Kubernetes cluster on port 5432.
The Redis queue Deployment must only be access from the Kubernetes cluster on port 6379.
The vote and result Deployment must be publicly access on port 8080.
The worker Pods must not be exposed.
Try to access the vote web site
Try to access the result web site
An example of yaml definition file to manage the Pods access with Services resources.
Create the Services resources based on the previous yaml file definition.
External documentation
Those documentations can help you to go further in this topic :
Kubernetes official documentation on services
Last updated
Was this helpful?