Start the First Pod
Now we are prepared to start our first pod.
Create YAML file
In the current console tab we have still the kubectl proxy
command running. We will use a second
console tab to run all further commands. Click on the +
icon to add a second shell.
Create your first YAML file which will later on be used to create the first Kubernetes resource.
mkdir k8s-files
touch k8s-files/backend.yml
You can now edit the backend.yml
using the Cloud Shell IDE above your shell.
Write the pod definition
We will write the configuration of a pod to launch the backend. To get started copy the contents
of kubernetes-workshop/k8s-examples/pod.yml
into your own backend.yml
.
The example contains some values we need to change:
- Set the name of the pod to
calculator-backend
. - Change the image of the container to
quay.io/kubernetes-workshop/calculator-backend:v1
. - The backend service does run on port
8080
change this in the configuration.
The resources definition is also fine for our service.
Apply the pod definition
The CLI for Kubernetes allows us to send all configuration files of a directory to Kubernetes.
kubectl apply -f k8s-files/
The console should display the following:
pod "calculator-backend" created
Monitor the startup
You can now monitor the startup of the pod with kubectl get pods
.
After a successful startup the output should look like this:
NAME READY STATUS RESTARTS AGE
calculator-backend 1/1 Running 0 3s
During the startup the console would look like this:
NAME READY STATUS RESTARTS AGE
calculator-backend 0/1 Pending 0 0s
Access the pod
You can now use the proxy we set up previously to access the pod.
Use the link to access the pod:
$GCLOUD_SHELL_URL/api/v1/namespaces/default/pods/calculator-backend/proxy/
The browser should now display the Swagger UI of the backend service.
Execute a calculation
To run your calculation against the API you can use the Swagger UI.
- Click on
GET /calculate
- Click on
Try it out
on the right side - Enter a valid expression into the field, e.g.
100 * sqrt(3)
- Click on
Execute
- The UI displays the response below:
Trigger an error
The service does not handle invalid expressions well. It crashes when an invalid expression is entered.
- Verify that the pod is still running:
kubectl get pods
- The output should look like this:
NAME READY STATUS RESTARTS AGE calculator-backend 1/1 Running 0 7m
- Execute an invalid expression, e.g.
sqrt(Hello World)
- The UI displays the error:
- Now try a valid expression, there will still be an error.