This page shows how to create a Kubernetes Service object that external clients can use to access an application running in a cluster. The Service provides load balancing for an application that has two running instances.
Here is the configuration file for the application Deployment:
{{% code_sample file="service/access/hello-application.yaml" %}}
Run a Hello World application in your cluster: Create the application Deployment using the file above:
The preceding command creates a {{< glossary_tooltip text="Deployment" term_id="deployment" >}} and an associated {{< glossary_tooltip term_id="replica-set" text="ReplicaSet" >}}. The ReplicaSet has two {{< glossary_tooltip text="Pods" term_id="pod" >}} each of which runs the Hello World application.
Display information about the Deployment:
Display information about your ReplicaSet objects:
Create a Service object that exposes the deployment:
Display information about the Service:
The output is similar to this:
Make a note of the NodePort value for the Service. For example, in the preceding output, the NodePort value is 31496.
List the pods that are running the Hello World application:
The output is similar to this:
Get the public IP address of one of your nodes that is running
a Hello World pod. How you get this address depends on how you set
up your cluster. For example, if you are using Minikube, you can
see the node address by running kubectl cluster-info
. If you are
using Google Compute Engine instances, you can use the
gcloud compute instances list
command to see the public addresses of your
nodes.
On your chosen node, create a firewall rule that allows TCP traffic on your node port. For example, if your Service has a NodePort value of 31568, create a firewall rule that allows TCP traffic on port 31568. Different cloud providers offer different ways of configuring firewall rules.
Use the node address and node port to access the Hello World application:
where <public-node-ip>
is the public IP address of your node,
and <node-port>
is the NodePort value for your service. The
response to a successful request is a hello message:
As an alternative to using kubectl expose
, you can use a
service configuration file
to create a Service.
To delete the Service, enter this command:
To delete the Deployment, the ReplicaSet, and the Pods that are running the Hello World application, enter this command:
Follow the Connecting Applications with Services tutorial.