Kubernetes Using Cloud Shell: Deploy a Spring Boot Application
In this tutorial, you use an Oracle Cloud Infrastructure account to set up a
Kubernetes cluster. Then, you deploy a Spring Boot application to your cluster.
Key tasks include how to:
Create a compartment.
Set up a Kubernetes cluster on OCI.
Build a Spring Boot application and Docker image.
Push your image to OCI Container Registry.
Deploy your Docker application to your cluster using Cloud Shell.
Repeat for Scope:<second-availability-domain> and
<third-availability-domain>. Each region must have
at least 50 GB of block volume available.
Find out how many Flexible Load Balancers you have available:
Filter for the following options:
Service: LbaaS
Scope:<your-region>. Example:
us-ashburn-1
Resource:<blank>
Compartment:<tenancy-name> (root)
Find the count for the following shapes
Limit Name:lb-flexible-bandwidth-count
Available: minimum 1
Note
This tutorial creates three compute instances with a VM.Standard.E2.1 shape for
the cluster nodes. To use another shape, filter for its core count. For example,
for VM.Standard2.4, filter for Cores for Standard2 based VM and BM
Instances and get the count.
This tutorial creates three compute instances with a VM.Standard.E3.Flex shape
for the cluster nodes. To use another shape, filter for its core count. For
example, for VM.Standard2.4, filter for Cores for Standard2 based VM and BM
Instances and get the count.
This tutorial uses a 'Quick Create' workflow to create a cluster with a public
regional subnet that hosts a flexible load balancer. To use a different load
balancer, you can use a custom workflow to explicitly specify which existing network
resources to use, including the existing subnets in which to create the load
balancers.
To use another bandwidth for the load balancer, filter for its count, for
example 100-Mbps bandwidth or 400-Mbps bandwidth.
After you create a Kubernetes cluster, set up Cloud Shell to access the
cluster.
Sign in to the Oracle Cloud Infrastructure Console.
Open the navigation menu and click Developer Services. Under Containers & Artifacts, click Kubernetes Clusters (OKE).
Click the link to <your-cluster>.
The information about your cluster is displayed.
Click Access Cluster.
Click Cloud Shell Access. Follow the steps in the
dialog. The following steps are provided for your reference.
Click Launch Cloud Shell. Alternatively, from the main
menu, you can click the Cloud Shell icon () and start a
session.
Check your oci CLI version and verify that Cloud Shell is
working.
Copy
oci -v
Create kubeconfig file for your setup. Use the information from Access Your
Cluster dialog.
oci ce cluster create-kubeconfig <use data from dialog>
You get a message
that:
New config written to the Kubeconfig file <your-home-directory>/.kube/config
Note
If the config file is not stored in its default location
(~/.kube/config, you must export the
KUBECONFIG environment variable to point to the
location.
Copy
export KUBECONFIG=$HOME/<new-location>/config
Note
When working with more than one cluster, you specify a specific config
file on the command line. Example:
A Docker image holds an application, its dependencies, and instructions to run the
application.
Change into the gs-spring-boot-docker/initial directory.
Create a file named Dockerfile.
Copy
FROM openjdk:8-jdk
RUN addgroup --system spring && adduser --system spring -ingroup spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
If it works for you, remove the sudo in the following
docker commands.
Build the Docker image:
Copy
docker build -t spring-boot-hello .
Example output:
Successfully built xxxxxxxxxxxx
Successfully tagged spring-boot-hello:latest
Run the Docker image:
Copy
docker run -p 8080:8080 -t spring-boot-hello &
You get the same result as running the local app.
Stop the running application.
Congratulations! You have successfully created a Spring Boot Docker
image.
Open the navigation menu and click Developer Services. Under Containers & Artifacts, click Container Registry.
In the left navigation, select <your-compartment-name>.
Click Create Repository.
Create a private repository with your choice of repo name:
Copy
<repo-name> = <image-path-name>/<image-name>
Example: spring-projects/spring-boot-hello-app
You are now ready to push your local image to OCI registry.
Note
Before
you push an image into a compartment, you must create a repository in
that compartment.
Note
The slash in a repository name does
not represent a hierarchical directory structure. The optional
<image-path-name> helps to organize your
repositories.
<image-folder-name>/<image-name> from
the Create a Docker Repository section.
Example:
docker tag spring-boot-hello iad.ocir.io/my-namespace/spring-projects/spring-boot-hello-app
In this example, the components are:
Repo URL:iad.ocir.io/my-namespace/
Repo name:spring-projects/spring-boot-hello-app
Note
OCI Container Registry now supports creating a registry repo in any
compartment rather than only in the root compartment (tenancy). To push the
image to the repo you created, combine the registry URL with the exact repo
name. OCI Container Registry matches the unique repo name and pushes your
image.
Check your Docker images to see if the image is copied.
Copy
docker images
The tagged or the copied image has the same image ID
as your local image.
After you deploy your app, it might take the load balancer a few seconds to
load.
Check if the load balancer is live:
Copy
kubectl get service
Repeat the command until load balancer is assigned an IP address.
Note
While waiting for the load balancer to deploy, you can check the status of
your cluster with these commands:
Get each pods status: kubectl get pods
Get app status: kubectl get deployment
Use the load balancer IP address to connect to your app in a browser:
Copy
http://<load-balancer-IP-address>:8080
The browser displays: Spring Boot Hello World!
Undeploy your application from the cluster. (Optional) To remove your
application run this command:
Copy
kubectl delete -f sb-app.yaml
Output:
deployment.apps/sbapp deleted
service "sbapp-lb" deleted
Your application is now removed from your cluster.
What's Next 🔗
You have successfully created a Hello World application, deployed it to a Kubernetes cluster
and made it accessible on the internet, using Spring Boot and OCI Cloud Shell.
To explore more information about development with Oracle products, check out these sites: