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
- Open the Terminal.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
touch service.yaml
- Use the nano text editor in the Terminal.
nano service.yaml
- Copy and paste the configuration below into the text editor.
- Press Cmd + X, then type Y and press Return to save the service.yaml file.
- Open the command prompt.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
copy NUL service.yaml
- Open the service.yaml file with a text editor. For example, use Notepad++ to edit the YAML file.
- Copy and paste the configuration below into the text editor.
- Save the service.yaml file.
- Open the Terminal.
- Navigate to the Downloads folder.
cd ./Downloads
- Use the following command to create an empty YAML file in your Downloads folder.
touch service.yaml
- Use the nano text editor in the Terminal.
nano service.yaml
- Copy and paste the configuration below into the text editor.
- Press Ctrl + X, then type Y and press Enter to save the service.yaml file.
Add a Service¶
- In the Terminal or Command Prompt, add the service to your environment using a YAML file.
microk8s kubectl create -f service.yaml
- List the services and see that development is now listed. You will need the NodePort port number. Example: 30007.
microk8s kubectl get services
- Get the IP address for the microk8s-vm. It will be an IPv4 address. Example: 192.168.64.2.
multipass list
- 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