Local Development With OpenShift and Tilt
How to use Tilt to facilitate local OpenShift development. If you develop containerized applications on OpenShift, this post is for you.
Join the DZone community and get the full member experience.
Join For Freein this article, i am going to show you how to use tilt to facilitate local openshift development. tilt's capabilities will be demonstrated in a practical example that uses buildah and codeready containers. if you develop containerized applications on openshift, this blog post is for you.
how does tilt facilitate local development?
the diagram below depicts a development workflow orchestrated by tilt. after you execute the
tilt up
command on your development machine, tilt will keep running while performing the following actions:
- tilt watches for changes in the source code made by the developer on the local machine.
- after a change has been detected, tilt executes buildah to update the container image. after the build completes, the updated container image is pushed to the internal registry in codeready containers.
- tilt watches kubernetes manifests on the local machine and keeps them in sync with codeready containers. any changes made to the manifests are instantly applied to codeready containers.
- tilt forwards local ports to the application pod running in codeready containers. this allows the developer to conveniently access the application on localhost.
tilt helps the developer automate many of the manual steps made during the development of containerized applications. it speeds up the edit-compile-run loop considerably. interested to trying it out? follow me to the next section, where we will implement the workflow depicted in the above diagram.
using tilt for developing a sample application
in this section, we are going to use tilt to orchestrate the development of a sample application. as i didn't want to reinvent the wheel by designing a custom application, i grabbed the plain old static html example that comes with tilt and can be found on github . this example is described in the tilt's documentation and you may already be familiar with it. it consists of a very simple shell script that serves a static html. in contrast to tilt's example which leverages docker and upstream kubernetes, i will be using developer tools from the red hat's portfolio:
in order to be able to use the red hat developer tools, i had to modify the original sample code. the sample code used in this tutorial can be found on github . i recommend that you go ahead and briefly review it.
the overall setup consists of a fedora 32 development machine where i installed buildah and coreready containers (crc) version 1.10. i installed tilt version 0.13.4 which is the latest release of tilt available at the time of this writing. if you plan to use tilt along with codeready containers, i recommend grabbing this or any future versions of tilt, as this version includes a patch that makes tilt work with codeready containers without the need for further configuration. the tilt binary can be downloaded from github .
having the required tools in place, let's start by logging in into crc and creating a new project:
$ oc login
$ oc new-project tilt-example
let's now focus on configuring buildah to be able the pull and push images from the container registries. as you can see in the
dockerfile
, our application uses the
registry.redhat.io/ubi8/ubi
container image as the base image. in order for buildah to be able to pull this image from the registry, we need to log in to this registry using the red hat customer portal credentials:
xxxxxxxxxx
$ buildah login registry.redhat.io
next, let's configure buildah to be able to push images into the crc internal registry. the crc registry endpoint uses a self-signed certificate. buildah will refuse to communicate with the internal registry as the certificate is signed by an unknown authority. in order for buildah to be able to push images into the internal registry, you will need to add this registry to the list of insecure registries. on your development machine, edit
/etc/containers/registries.conf
and add the crc internal registry to the list of insecure registries.
xxxxxxxxxx
[registries.insecure]
registries = [ 'default-route-openshift-image-registry.apps-crc.testing' ]
in order for buildah to be able to push images into the crc registry, we need to log in to this registry. for that, use the
oc
command to grab a token used for authentication against the registry:
xxxxxxxxxx
$ oc whoami --show-token
7getdza6mqa-nexwetxtofujtehocvshkl5yxtxqeb0
log in to the registry using the authentication token:
xxxxxxxxxx
$ buildah login \
--username unused \
--password 7getdza6mqa-nexwetxtofujtehocvshkl5yxtxqeb0 \
default-route-openshift-image-registry.apps-crc.testing
login succeeded!
after successfully logging into the crc internal registry, the buildah configuration is now complete. finally, we can turn our attention to tilt. first, let's review the
tiltfile
which describes how tilt will orchestrate our development workflow:
xxxxxxxxxx
# push the container image to the crc internal registry, project tilt-example
default_registry(
'default-route-openshift-image-registry.apps-crc.testing/tilt-example',
host_from_cluster='image-registry.openshift-image-registry.svc:5000/tilt-example')
# use buildah to build and push the container image
custom_build(
'example-html-image',
'buildah build-using-dockerfile --tag $expected_ref . && buildah push $expected_ref',
['.'],
skips_local_docker=true)
# deploy kubernetes resource
k8s_yaml('kubernetes.yaml')
# make the application available on localhost:8000
k8s_resource('example-html', port_forwards=8000)
i annotated the
tiltfile
with comments that explain the meaning of individual tilt instructions. ready to give it a shot? just clone the git repository and run tilt:
xxxxxxxxxx
$ git clone https://github.com/noseka1/local-development-with-openshift-and-tilt
$ cd local-development-with-openshift-and-tilt/
$ tilt up
after tilt comes up, it will call buildah to pull the base image, build the application, and push the resulting image to the crc internal registry. it will also deploy the application on kubernetes by applying the
kubernetes.yaml
manifest referenced in the
tiltfile
. if everything worked well, and the application pod starts up, you will see the "serving files on port 8000" log message in the bottom pane:
at this point, you should be able to reach the running application on
localhost:8000
:
xxxxxxxxxx
$ curl localhost:8000
<!doctype html>
<html>
<body style="font-size: 30px; font-family: sans-serif; margin: 0;">
<div style="display: flex; flex-direction: column; width: 100vw; height: 100vh; align-items: center; justify-content: center;">
<img src="pets.png" style="max-width: 30vw; max-height: 30vh;">
<div>hello cats!</div>
</div>
</body>
</html>
to experience how tilt facilitates the local development, change the content of the
index.html
file. after you save your changes, tilt will instantly re-run the loop and deploy the updated application.
conclusion
in this blog, we described how tilt can facilitate the local development of containerized applications. we demonstrated tilt's capabilities in a practical example that showed tilt working along with buildah and codeready containers. in this introductory article, we were able to only scratch the surface. there is much more that tilt has to offer, including live updates that can update the application without restarting the container, and which can drastically speed up the edit-compile-run loop. i encourage you to read through tilt's documentation to learn more about this tool.
do you use tilt for development on openshift or kubernetes? what's your opinion on tilt? i would be happy to hear about your experiences. you can leave your comments in the comment section below.
Published at DZone with permission of Ales Nosek. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments