Skip to content

Part 4: Using Services

This is Part 4 of a multi-part, self-paced quick start exercise.

Note

This exercise requires MicroK8s. If you do not have these already installed and running, see "Prerequisites".


What Will You Do

In part 4, you will:

  • Use services on your cluster.

Estimated Time

Estimated time for this exercise is 10 minutes. Watch a video of this exercise below.


Using Services

Kubernetes services allow exposing an application as a network service. The service is a static IP address that can be attached to a pod. The service and the pod are separate, so if a pod dies, the service and IP address remain.

Service YAML file

Create a service using a YAML file, which is a configuration file. You could create a YAML file from the command line, but for this exercise, you can just use a text editor. Or you can download the service YAML file from this public Git repository

  1. Open the Terminal.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    touch service.yaml
    
  4. Use the nano text editor in the Terminal.
    nano service.yaml
    
  5. Copy and paste the configuration below into the text editor.
  6. Press Cmd + X, then type Y and press Return to save the service.yaml file.
  1. Open the command prompt.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    copy NUL service.yaml
    
  4. Open the service.yaml file with a text editor. For example, use Notepad++ to edit the YAML file.
  5. Copy and paste the configuration below into the text editor.
  6. Save the service.yaml file.
  1. Open the Terminal.
  2. Navigate to the Downloads folder.
    cd ./Downloads
    
  3. Use the following command to create an empty YAML file in your Downloads folder.
    touch service.yaml
    
  4. Use the nano text editor in the Terminal.
    nano service.yaml
    
  5. Copy and paste the configuration below into the text editor.
  6. Press Ctrl + X, then type Y and press Enter to save the service.yaml file.

Add a Service

  1. In the Terminal or Command Prompt, add the service to your environment using a YAML file.
    microk8s kubectl create -f service.yaml
    
  2. List the services and see that development is now listed. You will need the NodePort port number. Example: 30007.
    microk8s kubectl get services
    
  3. Get the IP address for the microk8s-vm. It will be an IPv4 address. Example: 192.168.64.2.
    multipass list
    
  4. Test that the service is available using the IP address and the NodePort. Example: curl http://192.168.64.2:30007. A message should display and include the pod name; this is from the Deployment YAML file ("Hello from...").
    curl http://ip-address:nodeport 
    

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: test-service
spec:
  type: NodePort
  selector:
    app: test-pod
  ports:
  - port: 4000
    targetPort: 80
    nodePort: 30007