Use a Service to Access an Application in a Cluster

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.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

Objectives

  • Run two instances of a Hello World application.
  • Create a Service object that exposes a node port.
  • Use the Service object to access the running application.

Creating a service for an application running in two pods

Here is the configuration file for the application Deployment:

service/access/hello-application.yaml Use a Service to Access an Application in a Cluster - 图1

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: hello-world
  5. spec:
  6. selector:
  7. matchLabels:
  8. run: load-balancer-example
  9. replicas: 2
  10. template:
  11. metadata:
  12. labels:
  13. run: load-balancer-example
  14. spec:
  15. containers:
  16. - name: hello-world
  17. image: gcr.io/google-samples/node-hello:1.0
  18. ports:
  19. - containerPort: 8080
  20. protocol: TCP
  1. Run a Hello World application in your cluster: Create the application Deployment using the file above:

    1. kubectl apply -f https://k8s.io/examples/service/access/hello-application.yaml

    The preceding command creates a Deployment and an associated ReplicaSet. The ReplicaSet has two Pods each of which runs the Hello World application.

  2. Display information about the Deployment:

    1. kubectl get deployments hello-world
    2. kubectl describe deployments hello-world
  3. Display information about your ReplicaSet objects:

    1. kubectl get replicasets
    2. kubectl describe replicasets
  4. Create a Service object that exposes the deployment:

    1. kubectl expose deployment hello-world --type=NodePort --name=example-service
  5. Display information about the Service:

    1. kubectl describe services example-service

    The output is similar to this:

    1. Name: example-service
    2. Namespace: default
    3. Labels: run=load-balancer-example
    4. Annotations: <none>
    5. Selector: run=load-balancer-example
    6. Type: NodePort
    7. IP: 10.32.0.16
    8. Port: <unset> 8080/TCP
    9. TargetPort: 8080/TCP
    10. NodePort: <unset> 31496/TCP
    11. Endpoints: 10.200.1.4:8080,10.200.2.5:8080
    12. Session Affinity: None
    13. Events: <none>

    Make a note of the NodePort value for the service. For example, in the preceding output, the NodePort value is 31496.

  6. List the pods that are running the Hello World application:

    1. kubectl get pods --selector="run=load-balancer-example" --output=wide

    The output is similar to this:

    1. NAME READY STATUS ... IP NODE
    2. hello-world-2895499144-bsbk5 1/1 Running ... 10.200.1.4 worker1
    3. hello-world-2895499144-m1pwt 1/1 Running ... 10.200.2.5 worker2
  7. 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.

  8. 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.

  9. Use the node address and node port to access the Hello World application:

    1. curl http://<public-node-ip>:<node-port>

    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:

    1. Hello Kubernetes!

Using a service configuration file

As an alternative to using kubectl expose, you can use a service configuration file to create a Service.

Cleaning up

To delete the Service, enter this command:

  1. kubectl delete services example-service

To delete the Deployment, the ReplicaSet, and the Pods that are running the Hello World application, enter this command:

  1. kubectl delete deployment hello-world

What’s next

Learn more about connecting applications with services.