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.
Sobre o autor
Ricardo Gerardi is a Principal Consultant at Red Hat, having transitioned from his previous role as a Technical Community Advocate for Enable Sysadmin. He's been at Red Hat since 2018, specializing in IT automation using Ansible and OpenShift.
With over 25 years of industry experience and 20+ years as a Linux and open source enthusiast and contributor, Ricardo is passionate about technology. He is particularly interested in hacking with the Go programming language and is the author of Powerful Command-Line Applications in Go and Automate Your Home Using Go. Ricardo also writes regularly for Red Hat and other blogs, covering topics like Linux, Vim, Ansible, Containers, Kubernetes, and command-line applications.
Outside of work, Ricardo enjoys spending time with his daughters, reading science fiction books, and playing video games.
Mais como este
Red Hat and Sylva unify the future for telco cloud
Bridging the gap: Secure virtual and container workloads with Red Hat OpenShift and Palo Alto Networks
Scaling For Complexity With Container Adoption | Code Comments
Kubernetes and the quest for a control plane | Technically Speaking
Navegue por canal
Automação
Últimas novidades em automação de TI para empresas de tecnologia, equipes e ambientes
Inteligência artificial
Descubra as atualizações nas plataformas que proporcionam aos clientes executar suas cargas de trabalho de IA em qualquer ambiente
Nuvem híbrida aberta
Veja como construímos um futuro mais flexível com a nuvem híbrida
Segurança
Veja as últimas novidades sobre como reduzimos riscos em ambientes e tecnologias
Edge computing
Saiba quais são as atualizações nas plataformas que simplificam as operações na borda
Infraestrutura
Saiba o que há de mais recente na plataforma Linux empresarial líder mundial
Aplicações
Conheça nossas soluções desenvolvidas para ajudar você a superar os desafios mais complexos de aplicações
Virtualização
O futuro da virtualização empresarial para suas cargas de trabalho on-premise ou na nuvem