/
4. Services with a YAML File

4. Services with a YAML File

We learned earlier that PODs without a Deployment object cannot be exposed as Services. In this section we will construct a YAML file that describes the Service of the echoserver

K8 Objects via YAML file

Services

If you haven’t already created the k8_echoserver folder create a new folder called k8_echoserver. Navigate into the k8_echoserver folder.

Create a file called echoserver_service.yaml

Add the followings elements to the file

apiVersion: v1 kind: Service metadata: name: yml-echoservice-entrypoint namespace: default spec: type: NodePort selector: service_id: echo-sever-from-yml ports: - nodePort: 30080 port: 8080 targetPort: 8080

Creating resources from the yaml file

Once you have created the file, from the shell where the file is located execute the command

kubectl apply -f echoserver_deployment.yaml

You should see a message that the Service has been created.

Visit the minikube dashboard, and look at the PODs, Deployments and Services, you will see the POD, Deployment, and Service are there.

YAML Content Description

apiVersion - identifies the version of the schema the object should have

kind - a string that identifies the schema this object should have. Type kubectl api-resources to see the possible options.

metadata - Information that uniquely identifies the object

metadata.name - uniquely identifies this object within the current namespace

metadata.namespace - if not specified defaults to default

spec - What state do you desire for the object, in our example above we want it to have a

  • spec.type - it’s a service with a NodePort (recap on our discussion on NodePorts vs. LoadBalancer

  • spec.selector - In order for this Service to be linked to a Deployment, the label here must match a label of a previously defined Deployment

  • spec.ports - the port bindings

    • spec.ports.NodePort - exposes the service to the cluster by mapping the NodePort to the targetPort

    • spec.ports.port - exposes the Service within the cluster on this port. Other PODs within the same cluster can communicate with the Service through this port

    • sepc.ports.targetPort - the port on which the Service sends requests to, that your POD will be listening on. Your application within the container will need to be listening on this port

      • Very similar to EXPOSE <port no> in the Dockerfile

 

You will need to use the minikube tunnel as we did earlier in order to expose the service to the host if you are uising Darwin, Windows, or WSL

minikube service yml-echoservice-entrypoint

Service with a LoadBalancer

As we saw above, even though we deployed the service, we need to give it a port number that is visible on the outside network. We can do this by using a LoadBalancer.

Add the following to the end of the echoserver_service.yaml file

This will create a new service; a LoadBalancer service that exposes the echoserver POD on 30090 in the host environment.

To remove the resources that have been created type

Your Task

Use the steps from above and create a Deployment for the stream2stream/pingme image. It can be pulled from docker.io.