Deploy an application in Red Hat OpenShift on your laptop
One of the benefits of Kubernetes is that it allows you to run your applications in the exact same way in your test environment as in your production environment. If you followed along with my first article on this topic, How to install Red Hat OpenShift Local on your laptop, you now have a local OpenShift environment. You can use that environment to make a test deployment of an application. After you've confirmed that it works properly, you can then deploy that application in a production environment, whether it's another on-premises cluster or a Red Hat OpenShift service running on a cloud provider.
Kubernetes, and by extension Red Hat OpenShift Container Platform, allows you to deploy your application in different ways, depending on the complexity and uniqueness of your requirements. You can use a pod definition, a deployment for relatively simple applications, or a pipeline for more advanced scenarios.
Whether you're deploying locally or remotely, one common element is that you need a container image to run an application in your cluster. In many cases, you don't need a full pipeline to build and deploy your application. For small applications, OpenShift provides the new-app feature that allows you to build and deploy your application straight from your Git repository.
In this article, I'll walk you through deploying a sample application on an OpenShift Local cluster. You can, of course, use the same procedure to deploy applications on any other OpenShift environment.
[ Learn the basics of using Kubernetes in this cheat sheet. ]
Start OpenShift Local
If you followed along with my previous article, you have an OpenShift Local cluster installed on your computer. Because it runs on your local laptop or desktop computer, this cluster is probably not running all the time. You can stop it to save resources and it stops automatically on shutdown. In case the cluster is not running, start it with the command crc start
:
$ crc start
After a few minutes, the cluster is up and running and crc
prints the connection information:
Started the OpenShift cluster.
The server is accessible via web console at:
https://console-openshift-console.apps-crc.testing
Log in as administrator:
Username: kubeadmin
Password: ahYhw-xJNMn-NyxMT-47t22
Log in as user:
Username: developer
Password: developer
Use the 'oc' command line interface:
$ eval $(crc oc-env)
$ oc login -u developer https://api.crc.testing:6443
Deploy a sample application
When your local OpenShift cluster is up and running, you can access it to deploy applications. The crc setup
command you used to configure your machine also downloaded additional command line tools like oc
so that you can connect to your cluster from the command line. To use these tools, you need to set up your environment to make sure they can find your cluster:
$ eval $(crc oc-env)
Now you can connect to the cluster using the developer
account. This account simulates a regular (unprivileged) user account in OpenShift:
$ oc login -u developer https://api.crc.testing:6443
If you need to connect as administrator, you can use the kubeadmin
account, but to deploy an application, developer
is enough.
Now, create a project called hello-world
to host your sample application:
$ oc new-project hello-world
Next, use the new-app
OpenShift command to automatically build and deploy an application straight from your Git repository. If you don't have a test application handy, you can use this simple Go API I created:
$ oc new-app https://github.com/rgerardi/hellogo.git
The oc new-app
command detects the programming language used to develop your application and uses a recipe to build a container image for it. This feature is compatible with many popular languages such as Node, PHP, Go, and more. This command creates a BuildConfig object and starts to build (compile) your application. You can see the progress by checking logs or using the status
command:
$ oc logs -f bc/hellogo
$ oc status
When the build completes, oc new-app
automatically deploys the application for you using the container image it built. Once the status command says the application container is running, you can expose it for external access using OpenShift default router:
$ oc expose deploy hellogo --port 3000
$ oc expose service hellogo
Now, use oc get route
to obtain the external hostname generated for your application and use curl
to test it:
$ oc get route hellogo --template '{{ .spec.host }}'
$ curl http://hellogo-hello-world.apps-crc.testing
API: This request is being served by server hellogo-57859b97dc-gnjmg
You can also do that with a single command:
$ curl "http://$(oc get route hellogo --template '{{ .spec.host }}')"
API: This request is being served by server hellogo-57859b97dc-gnjmg
The application returns the name of the server running it, which in this case is the name of the pod
running in OpenShift:
$ oc get pod -l deployment=hellogo
NAME READY STATUS RESTARTS AGE
hellogo-57859b97dc-gnjmg 1/1 Running 0 2m40s
[ Read Red Hat OpenShift Service on AWS (ROSA) explained ]
What's next
Now that you have your application up and running, you can use the exposed route to perform tests using your OpenShift Local instance. Later, you can use the same procedure to deploy the app in production.
Keep in mind that when using OpenShift Local's default configuration, you can access your cluster only on the same local machine where you installed it.
Ricardo Gerardi
Ricardo Gerardi is Technical Community Advocate for Enable Sysadmin and Enable Architect. He was previously a senior consultant at Red Hat Canada, where he specialized in IT automation with Ansible and OpenShift. More about me
Try Red Hat OpenShift, the enterprise Kubernetes application platform that helps teams focus on the work that matters.