Deploy an Application to Kubernetes
Let's use Waypoint to deploy applications onto Kubernetes. This tutorial will give you a very quick overview of how Waypoint works.
Prerequisites
In order to complete this tutorial, you will need access to a running Kubernetes
cluster, such as one running in a public cloud, in Docker Desktop, or in
kind
.
You'll need to install the waypoint
binary locally,
and clone the examples repository (detailed in the next section).
Clone the examples repository
This walkthrough uses an example NodeJS application to show you how to build, deploy and release an application using Waypoint.
This example app is part of the Waypoint Examples applications. You will need a local clone of this repository on your computer to complete this walkthrough.
Clone the Waypoint Examples repository and navigate to the Kubernetes NodeJS example directory.
Change into the subdirectory for the Kubernetes project. This project uses NodeJS but the following instructions will work with any language that can be built with a cloud native buildpack.
Create a Kubernetes environment
You can use any current Kubernetes distribution with Waypoint. Set your KubeConfig
with the KUBECONFIG
environment variable to allow access to the cluster.
Docker Desktop for running local Docker resources. It has a built-in Kubernetes feature for running a local Kubernetes cluster.
To enable Kubernetes on your local machine, navigate to the Preferences screen and select the check box labeled "Enable Kubernetes".
Then click the "Apply & Restart" button. After a few moments, Docker Desktop
will restart with and active Kubernetes cluster. You can verify your local
configuration by running any command against kubectl
.
Install the Waypoint server
Now that you have created a Kubernetes environment, install the Waypoint server
on it with the install
command. The -accept-tos
flag confirms that you
accept the terms of service for our application URL
service.
You can verify that the cluster was successfully created by running kubectl get all
.
You should see the waypoint-server-0
pod. The exact output will vary based on
your Kubernetes platform.
Initialize Waypoint
Before you can build and deploy your application, you must initialize it with
the init
command.
When you initialize Waypoint for your application, Waypoint first looks for a
Waypoint configuration file (waypoint.hcl
) for the app in the directory.
The waypoint.hcl
configuration file gives Waypoint instructions for how to
build, deploy, and release your application.
If Waypoint cannot find the app's configuration file when you run waypoint init
, Waypoint will create a starter waypoint.hcl
file that you can customize
for your application.
The remainder of this walkthrough uses the example NodeJS application to show how to initialize an app and then build, deploy, and release it with Waypoint.
Explore waypoint.hcl
You will now explore the waypoint.hcl
file to learn more about the
application's configuration.
In the waypoint-examples/kubernetes/nodejs/
directory, run the following
command:
The Terminal will show the contents of the waypoint.hcl
file.
The build
clause defines how Waypoint will build the application.
The pack
option tells Waypoint to use the most relevant Cloud Native Buildpack
to build the application. Since this example application is written for NodeJS,
Waypoint will use NodeJS Buildpacks.
The deploy
clause defines where Waypoint will deploy the application. The
kubernetes
option tells Waypoint to deploy the application to Kubernetes.
The release
stanza defines how our application will be released to our
environment. For example, in the case of external Kubernetes clusters this would
be where you would configure a load_balancer
on a specific port
.
With these configurations in place, execute the following command in order to initialize Waypoint with this configuration.
Build, deploy and release application
This application is configured to use Cloud Native Buildpacks to detect the type of application running and to launch it within Kubernetes. Because Waypoint uses the appropriate buildpack to create the Dockerfile, you do not need to create the file yourself.
Once Waypoint completes a build, it stores the artifacts in either a local or remote registry.
The registry
clause in the waypoint.hcl
file specifies where to store the
app's artifacts. If the registry
clause is not present, Waypoint will default
to using the local Docker instance to store the application's artifacts.
For this example application, Waypoint will store the image in a remote Docker repository after the application is built.
You can now deploy the application to Kubernetes by running waypoint up
.
Tip
It make take a few minutes to copy the image to the remote server, depending on the speed of your internet connection.
Waypoint will show the progressive status of the build, deploy, and release steps in the Terminal output. As part of the deployment workflow, Waypoint creates a preview URL for your application.
Tip
The preview URL is optional and can be disabled in the Waypoint server configuration.
Waypoint will show the result of your deployment in the Terminal, along with your specific preview URL.
Waypoint creates the preview URL on a HashiCorp service. Because this preview URL is connected to your application and where it has been deployed, the URL will only show your application when it is running. If the Deployment URL does not respond, try the Release URL.
Update and redeploy the application
One of the most powerful parts of Waypoint is its ability to iterate on deployments and quickly redeploy the application with your changes in place.
Open views/pages/index.ejs
in your preferred development editor:
Near line 18
, change the text to anything that you like, such as today's date.
Back in the Terminal, redeploy the application with your new and improved text.
Waypoint has now redeployed your application.
You will notice that the Deployment URL for this second deployment is different from what was created for the first deployment. Waypoint generates unique URLs for each deployment, which means that you can access each deployment by using their unique deployment URLs. This is typically used to preview a deployment before releasing it.
Now, open the new Deployment URL and verify that your modified text is running in the application.