Skip to main content
In this lesson, we will walk through the solution for the lab on Services. You will learn how to inspect different aspects of services in a Kubernetes cluster and create a service to expose a web application. This guide is designed to help you understand Kubernetes Services, their types, endpoints, and best practices for deployment.

Listing Kubernetes Services

To start, list the services in your cluster. You can use either of the following commands:
or
For example, running:
produces the output:
This output indicates that only the default Kubernetes service is present. Below is an extended example using both commands:
The default Kubernetes service is automatically created and managed by the system.

Inspecting the Default Kubernetes Service

Next, inspect the default Kubernetes service to review its type and configuration. Running the following commands reveals that the service type is ClusterIP:
To inspect the target port configured on this service, use the kubectl describe command as shown below:
From this output, you can see that the target port is set to 6443.

Understanding Labels and Endpoints

The Kubernetes service is labeled with key-value pairs that identify the API server:
  • component=apiserver
  • provider=kubernetes
These labels help in managing and identifying the components of the cluster. Endpoints, on the other hand, define the pods that receive traffic from the service. Even if multiple pods match the selector criteria, the service lists all endpoints. In our case, the Kubernetes service shows one endpoint:
Endpoints represent the set of pod IP addresses and ports that receive traffic from the service. If the service’s selector mistakenly does not match any pods, the endpoints list will be empty.
An illustrative diagram outlines how endpoints connect to pods:
A hand-drawn diagram with a triangle connected to four circles, labeled "app: FE" and "app: FG," possibly representing a network or system architecture.

Inspecting Deployments

Now, let’s examine deployments within the default namespace. To list all deployments, run:
This output indicates that a deployment named simple-webapp-deployment exists.

Reviewing Deployment Details

To check details—such as the container image used—describe the deployment:
The output confirms that the image used is kodekloud/simple-webapp:red.

Accessing the Web Application UI

If you attempt to access the web application UI without first exposing it through a service, you may encounter a “Bad Gateway” error. This occurs because no service directs traffic to your pods.
To resolve this, create a service that exposes the web application.

Creating a Service for the Web Application

The next step is to create a NodePort service that makes the web application accessible. The diagram below shows the service definition framework:
The image shows a terminal interface with instructions to create a NodePort service for a web application, specifying ports and a selector.
Below is the basic framework for the service YAML file:
For further guidance, consult the Kubernetes documentation on services. An example service YAML provided in the docs is shown below:
For this lab, create a NodePort service named webapp-service that exposes your web application on port 8080. The final service definition is as follows:
Save this configuration as your service definition file (for example, service-definition-1.yaml) and then run the following commands to create the service:
After creating the service, accessing the web application UI should work as expected.

Imperative Commands for Creating Services

In the subsequent sections, we will discuss additional imperative commands for creating services. Below are some sample commands that you might find useful for quickly generating service definitions:

This concludes the lab for this lesson. In upcoming lessons, we will dive deeper into imperative commands and additional details on managing Kubernetes services effectively. For further reading, check out these resources:

Watch Video