In this part of this series, I am going to deploy our previously developed spring boot application to Minishift. You may wonder why I choose Minishift over Red Hat CodeReady Containers? The answer is simple, Red Hat CodeReady Containers consumes a lot of resources including CPU/RAM for running a simple application. So, deploy to Minishift will be our short and the quickest way to run Red Hat Openshift cluster locally on a single host for scaling up our Spring boot HelloWorld application to multiple instances running on the same host.
Note that, Minishift is not an OpenShift distribution in itself, instead, a tool that you can run to create a minimal VM that includes a container service.
So, we start with the installation of the Minishift and then configure our HelloWorld. Extract the archive somewhere in your file system. Add the location of Minishift executable to the path.
Step 1. Download the Minishift distribution
To download the latest Minishift release, visit the MiniShift releases pages. I am using macOS, so, in this section, I am going to describe the entire process of installing Minishift on macOS. If you are using a different operating system, please ensure you check out the installation instructions for any prerequisites your system must satisfy.
Download the latest version of the Minishift binary distribution from the GitHub repo. Extract the archive somewhere in your file system. Add the location of Minishift executable to path.
export MINISHIFT_HOME=/YOUR_DIRECTORY_PATH/minishift-1.34.2-darwin-amd64 export PATH=$MINISHIFT_HOME:$PATH
Step 2. Install Minishift
By default, Minishift uses a hypervisor to run the VM containing OpenShift. If you already have the Docker Desktop installed in your machine you already have hyperkit. Anyway, in addition to this, you will also need to install docker-machine-driver-hyperkit. Run the following command in your favorite terminal (assume that you already installed HomeBrew).
brew install docker-machine-driver-hyperkit sudo chown root:wheel /usr/local/opt/docker-machine-driver-hyperkit/bin/docker-machine-driver-hyperkit sudo chmod u+s /usr/local/opt/docker-machine-driver-hyperkit/bin/docker-machine-driver-hyperkit brew services start docker-machine
Step 3. Start Minishift
With the Minishift application installed, you can begin installation and startup the OpenShift cluster by running the minishift start command as follows:
$ minishift start --vm-driver=hyperkit -- Starting the OpenShift cluster using 'hyperkit' hypervisor ... -- Starting Minishift VM ................... OK -- Checking for IP address ... OK -- Checking for nameservers ... OK -- Checking if external host is reachable from the Minishift VM ... Pinging 184.108.40.206 ... OK -- Checking HTTP connectivity from the VM ... Retrieving http://minishift.io/index.html ... OK -- Checking if persistent storage volume is mounted ... OK -- Checking available disk space ... 30% used OK -- Writing current configuration for static assignment of IP address ... OK -- OpenShift cluster will be configured with ... Version: v3.11.0
At first, Minishift attempts to use the hyperkit virtualization driver. When minishift is run the first time, it will automatically download and install the OpenShift oc client binary specific to your operating system.
Moreover, minishift ships with a web console that allows users to perform the actions need to deploy and run containers. To find the URL for the minishift web console, please check the output of the console. You should have a similar output as shown below:
OpenShift server started. The server is accessible via web console at: https://192.168.64.8:8443/console You are logged in as: User: developer Password: <any value> To login as administrator: oc login -u system:admin
Note that, IP address of the URL may vary for your operating system. For now, open the minishift web console through a web browser and use the login (developer) from the console. Use any alphabet combination as a password.
Step 4. Add cluster role to user "developer"
Run the following command, which will display the command you need to type into your terminal in order to add the oc binary to your PATH environment variable.
Additionally, we need to give permissions for the user "developer" before using the Minishift to deploy and run containers.
oc adm policy --as system:admin add-cluster-role-to-user cluster-admin developer
Step 5. Build & deploy the project
At this moment, our container orchestration platform is ready for use and we can get back to our simple Spring boot HelloWorld application. Add the following maven plugin into the plugin section of the pom.xml file of the project as follows:
<!-- fabric8 kubernates/Openshift plugin--> <plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <version>4.4.1</version> </plugin>
That's it. Now we are ready to build and deploy the project into the Minishift cluster. the fabric8-maven-plugin plugin is tightly coupled with Maven and provides the following tasks:
Creating resource descriptors in YAML (deployments, services, routes) for OpenShift and Kubernetes.
Deploying the resource descriptors into the cluster.
Furthermore, the plugin can be used in two separate modes: zero-config setup and more advanced configuration through XML. For simplicity, we are going to use the Zero-Config setup which is very suitable for a quick start.
Build the image with the following maven command:
Under the hood, the above command will build a docker image containing the Spring boot Helloworld application and push the image to the registry. The result of the above command is shown below.
[INFO] --- fabric8-maven-plugin:4.4.1:build (default-cli) @ springboot-hello-world --- [INFO] F8: Running in OpenShift mode [INFO] F8: Using OpenShift build with strategy S2I [INFO] F8: Running generator spring-boot [INFO] F8: spring-boot: Using Container image fabric8/s2i-java:2.3 as base / builder [INFO] Copying files to /springboot-hello-world/target/docker/std/springboot-hello-world/latest/build/maven [INFO] Building tar: /springboot-hello-world/target/docker/std/springboot-hello-world/latest/tmp/docker-build.tar [INFO] F8: [std/springboot-hello-world:latest] "spring-boot": Created docker source tar /springboot-hello-world/target/docker/std/springboot-hello-world/latest/tmp/docker-build.tar [INFO] F8: Creating BuildServiceConfig springboot-hello-world-s2i for Source build [INFO] F8: Creating ImageStream springboot-hello-world [INFO] F8: Starting Build springboot-hello-world-s2i [INFO] F8: Waiting for build springboot-hello-world-s2i-1 to complete... [INFO] F8: Using fabric8/s2i-java:2.3 as the s2i builder image [INFO] F8: ================================================================== [INFO] F8: Starting S2I Java Build ..... [INFO] F8: S2I binary build from fabric8-maven-plugin detected [INFO] F8: Copying binaries from /tmp/src/maven to /deployments ... [INFO] F8: Checking for fat jar archive... [INFO] F8: Found springboot-hello-world-1.0-SNAPSHOT.jar... [INFO] F8: ... done [INFO] F8: Pushing image 172.30.1.1:5000/myproject/springboot-hello-world:latest ... [INFO] F8: Pushed 0/27 layers, 0% complete [INFO] F8: Pushed 1/27 layers, 5% complete [INFO] F8: Pushed 2/27 layers, 8% complete [INFO] F8: Pushed 3/27 layers, 12% complete [INFO] F8: Pushed 4/27 layers, 15% complete [INFO] F8: Pushed 5/27 layers, 21% complete [INFO] F8: Pushed 6/27 layers, 24% complete
Note that, the plugin directly pushed the image as an image stream to Minishift. Now, create the resource descriptors by using the following command.
*--service.yml: provides instructions on how Minishift will load balancing internal traffic for the
[INFO] F8: validating /springboot-hello-world/target/classes/META-INF/fabric8/openshift/springboot-hello-world-deploymentconfig.yml resource [INFO] F8: validating /springboot-hello-world/target/classes/META-INF/fabric8/openshift/myproject-project.yml resource [INFO] F8: validating /springboot-hello-world/target/classes/META-INF/fabric8/openshift/springboot-hello-world-route.yml resource [INFO] F8: validating /springboot-hello-world/target/classes/META-INF/fabric8/openshift/springboot-hello-world-service.yml resource
*-deploymentconfig.yml: is a configuration template for the appropriate running application.
*--service.yml: provides instructions how Minishift will load balancing internal traffic for the springboot-hello-world application.
*-route.yml: specify how Minishift enables external access to the service.
Now, deploy the images. Use the fabric8:deploy maven command to deploy the image and resources into the cluster.
You should have a similar output as shown below:
[INFO] --- fabric8-maven-plugin:4.4.1:deploy (default-cli) @ springboot-hello-world --- [INFO] F8: Using OpenShift at https://192.168.64.8:8443/ in namespace myproject with manifest /springboot-hello-world/target/classes/META-INF/fabric8/openshift.yml [INFO] OpenShift platform detected [INFO] F8: Using project: myproject [INFO] F8: Using project: myproject [INFO] F8: Creating a Service from openshift.yml namespace myproject name springboot-hello-world [INFO] F8: Created Service: target/fabric8/applyJson/myproject/service-springboot-hello-world.json [INFO] F8: Using project: myproject [INFO] F8: Creating a DeploymentConfig from openshift.yml namespace myproject name springboot-hello-world [INFO] F8: Created DeploymentConfig: target/fabric8/applyJson/myproject/deploymentconfig-springboot-hello-world.json [INFO] F8: Creating Route myproject:springboot-hello-world host: null [INFO] F8: HINT: Use the command `oc get pods -w` to watch your pods start up [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
At this point, you can use the command 'oc get pods -w' to watch your pods start-up process or go to the overview section of the My Project namespace which will also show you the entire status of the application. After a few moments, the application should be ready to use.
Note that, the deployed HelloWorld application also has an associated route, which actually is the DNS name for the application. In this case, the URL is http://springboot-hello-world-myproject.192.168.64.8.nip.io. Now, let's invoke our getQuote REST API through curl (web browser/PostMan)
curl http://springboot-hello-world-myproject.192.168.64.8.nip.io/getQuote; echo
Congratulations! you have just built and deployed a Spring boot application into to Minishift cluster. From here, we can control (increase/decrease) replicas associated with the HelloWorld application. Execute the following command to scaling the HelloWorld application into two replicas.
oc scale --replicas=2 dc springboot-hello-world
In this blog post, we learned how to install and configure the Minishift cluster. Next, we used the fabric8-maven-plugin maven plugin to build and deploy an image into Minishift cluster.
Use the generated resource descriptors to deploy the application into the Minishift cluster through oc command.
Scaling out the HelloWorld application through the oc scale command.