Exercise 3: Deploy to the Kubernetes
Last updated
Was this helpful?
Last updated
Was this helpful?
In this exercise we will work in the Kubernetes Web Console and with the Kubernetes CLI.
The following image is a simplified overview of the topics of that lab.
This lab has two parts:
Start build and save the container image
Deploy the application and expose the service
We will define and apply a deployment configuration (yaml) to create a Pod with our Microservice
We will define a service which routes requests to the Pod with our Microservice
The following gif is an animation of the simplified steps above in a sequence.
cloud-native-starter/authors-java-jee
Note: You have cloned the project twice: first to your local machine and second into the Docker image. The code changes you did in execrise 2 on your local computer, don't exist in the running Docker container.
Now we want to build and save a container image in the IBM Cloud Container Registry.
Ensure you logon on to IBM Cloud.
REMEMBER: You should know this from the prerequisites.
You can follow the steps in the Access tab, by starting from After your cluster provision .. and inserting the commands into your terminal session.
Logon to the IBM Cloud Container Registry (Ensure you are in the $ROOT_FOLDER/authors-java-jee
)
List you namespaces inside the IBM Cloud Container Registry
Example output:
Build the container image using buildah.
Now upload the container image to the IBM Cloud Container Registry. We use the information from step 3, where we got the list of namespaces.
Example command:
[YOUR_REGISTRY] = us.icr.io
[YOUR_REGISTRY_NAMESPACE] = cloud-native-suedbro
Optional: Verify the container upload in the IBM Cloud web UI.
List the container images to verify the upload.
Example output:
Copy the REPOSITORY path for the uploaded Authors container image. In this sample case it would be: us.icr.io/cloud-native-suedbro/authors
and save it somewhere, we need this later in the deployment.yaml
configuration.
A Pod is the basic building block of Kubernetes-the smallest and simplest unit in the Kubernetes object model that you create or deploy. A Pod represents processes running on your Cluster .
Here is a simplified image for that topic. The deployment.yaml file points to the container image that needs to be instantiated in the pod.
Definition of kind
defines this as a Deployment
configuration.
In the spec
section we specify an app name and version label.
Then we define a name
for the container and we provide the container image
location, e.g. where the container can be found in the Container Registry.
The containerPort
depends on the port definition inside our Dockerfile and in our server.xml.
We have previously talked about the usage of the HealthEndpoint class for our Authors service and here we see it the livenessProbe
definition.
Ensure you are in the $ROOT_FOLDER/authors-java-jee/deployment
Note: With the specification imagePullPolicy: Always
we force that the image is pulled from the IBM Cloud Container Registry and not cashed image in Kubernetes is possible used, when we change our container image IBM Cloud Container Registry.
REMEMBER: You should have saved the IBM Container Registry information somewhere.
Before:
Sample change:
Now we apply the deployment and we create a new Authors Pod.
Insert this command and verify the output.
Sample output:
Open your Kubernetes Cluster in the IBM Cloud web console
Open the Kubernetes dashbord
In the overview you see the created deployment and the pod
A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service. The set of Pods targeted by a Service is (usually) determined by a Label Selector.
In the service we map the NodePort of the cluster to the port 3000 of the Authors Microservice running in the authors Pod, as we can see in the following picture.
$ROOT_FOLDER/authors-java-jee/deployment
Sample output:
Open your Kubernetes Cluster in the IBM Cloud web console
Open the Kubernetes dashbord
In the overview scroll down until you see the created service
Example output:
Expose a public port on your worker node and use the public IP address of the worker node to access your service in the cluster publicly from the internet.
Get nodeport to access the service (do you remember the mapping?)
Example output:
Open API explorer.
Sample output:
Copy and past the URL in a local browser on your PC:
Execute curl to test the Authors service.
Example output:
Execute following curl command to test the HealthCheck implementation for the Authors service.
Example output:
Optional:
We can also verify that call in the browser.
We can simply delete the deployed Authors Microservice with:
This deployment will deploy a container to a Pod in Kubernetes. For more details we use the for Pods.
Inside the container image cns-workshop-tools
you can use the editor .
Let's start with the deployment yaml. For more details see the for deployments.
This is the full file.
Open the file with a editor and replace the value for the container image location with the path we got from the IBM Container Registry and just replace the authors:1
text, and add following statement imagePullPolicy: Always
and save the file.
After the definition of the Pod we need to define how to access the Pod. For this we use a service in Kubernetes. For more details see the for service.
In the we see a selector of the pod using the label 'app: authors'.
Get cluster (worker node) and show the IP address
Congratulations you have finished this hands-on workshop. Maybe you want to verify your learning in the .