Exposing Services in Kubernetes

1 hour
  • 2 Learning Objectives

About this Hands-on Lab

With deployments, you can create a dynamically-managed set of replica pods. But this introduces a need for an equally dynamic way to access them. Services provide a layer of abstraction that provides access to pods and other entities, allowing dynamic, high-availability access to the necessary components of your applications. In this lab, you will have the opportunity to work with services by creating a service on top of an existing deployment.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the `auth-svc` service

Examine the auth-deployment. Take note of the labels specified in the pod template, as well as the containerPort exposed by the containers.

kubectl get deployment auth-deployment -o yaml

Create a service descriptor file called auth-svc.yml.

apiVersion: v1
kind: Service
metadata:
  name: auth-svc
spec:
  type: NodePort
  selector:
    app: auth
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80

Create the service in the cluster.

kubectl apply -f auth-svc.yml
Create the `data-svc` service

Examine the data-deployment. Take note of the labels specified in the pod template, as well as the containerPort exposed by the containers.

kubectl get deployment data-deployment -o yaml

Create a service descriptor file called data-svc.yml.

apiVersion: v1
kind: Service
metadata:
  name: data-svc
spec:
  type: ClusterIP
  selector:
    app: data
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80

Create the service in the cluster.

kubectl apply -f data-svc.yml

Additional Resources

Your company has just deployed two components of a web application to a Kubernetes cluster, using deployments with multiple replicas. They need a way to provide dynamic network access to these replicas so that there will be uninterrupted access to the components whenever replicas are created, removed, and replaced. One deployment is called auth-deployment, an authentication provider that needs to be accessible from outside the cluster. The other is called data-deployment, and it is a component designed to be accessed only by other pods within the cluster.

The team wants you to create two services to expose these two components. Examine the two deployments, and create two services that meet the following criteria:

auth-svc

  • The service name is auth-svc.
  • The service exposes the pod replicas managed by the deployment named auth-deployment.
  • The service listens on port 8080 and its targetPort matches the port exposed by the pods.
  • The service type is NodePort.

data-svc

  • The service name is data-svc.
  • The service exposes the pod replicas managed by the deployment named data-deployment.
  • The service listens on port 8080 and its targetPort matches the port exposed by the pods.
  • The service type is ClusterIP.

Note: All work should be done in the default namespace.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?