Deploying an application¶
This guide will take you through the process of getting a simple NAIS application up and running and is intended for people without any previous experience with NAIS and the tools we use.
Questions or feedback?
If you have any questions or feedback, please reach out to us on Slack or GitHub Issues. We are happy to help!
Introduction¶
NAIS is a platform for running applications in the cloud. It is built on top of Kubernetes and provides a set of tools and services that makes it easy to build, deploy and operate applications in a secure and scalable way. We often say "It is like Heroku, but for the Norwegian public sector".
Prerequisites¶
Access¶
Before you get started, you need to make sure you have the following:
- You have a GitHub account
- The account should be connected to your GitHub organization (e.g.
navikt
)
- The account should be connected to your GitHub organization (e.g.
- You have a Google Cloud Platform account
- In most cases this should already be set up so that you can log in with single sign-on
- You can check your access by attempting to log in to the Google Cloud Console. Use the same email address as your work email
- If you do not have access, try to request the "Google Cloud Platform" app through MyApps
- If all else fails, please reach out for further assistance
- You have a working nais device
- You are part of a NAIS team
- You have a NAIS API key
Tools¶
You will need the following tools installed on your computer:
- kubectl - Kubernetes command-line tool
- nais-cli - NAIS command-line tool
- Run
nais kubeconfig
after installation to generate the configuration thatkubectl
needs.
- Run
- gh-cli - GitHub command-line tool
- gcloud - Google Cloud command-line tool
-
JDK - Java Development Kit
Version 17 or higher is recommended. To check, run
java -version
: -
Docker-cli via one of these alternatives
A little bit about Docker¶
As you can see above there is a bunch of different tools to run Docker. The main reason for this is that Docker decided to stop their free version for non-private usage, so a bunch of alternatives started to pop up. The most used one in NAV (at least for Mac users) is Colima, while Docker Desktop is the most used for Windows users.
If you want to use Docker Desktop you need give yourself access do Docker hub using MyApps.
For Colima you can follow the Getting started-guide.
Remember to run colima start
before using the Docker-cli.
Conventions¶
Throughout this guide, we will use the following conventions:
<my-app>
- The name of your NAIS application (e.g.joannas-first
)<my-org>
- Your GitHub organization (e.g.navikt
)<my-team>
- The name of your NAIS team (e.g.onboarding
)
NB! Choose names with lowercase letters, numbers and dashes only.
Step 1: Create your own GitHub repository¶
Create your own repo using the nais/getting-started as a template.
You create a new repository through either the GitHub UI or through the GitHub CLI:
Step 2: Grant the onboarding team access to your repository¶
This has to be done in the GitHub UI.
Go to your repository and click on Settings
-> Collaborators and teams
-> Add teams
.
Select the team named onboarding
, and grant them the Write
role.
Visit the NAIS Teams page for the onboarding
team and press the Syncrhonize
button.
This ensures that the proper permissions are set up by the time you're doing the steps that require them.
Normally, these are automatically synchronized every 15 minutes.
Step 3: Familiarize yourself with the files used¶
Check out the files in your repository to see what they contain.
nais.yaml
is the configuration for your application. This file contains the configuration for your application, such as the name of the application, the team that owns the application, and the port that the application listens on. You can read more about the configuration options in the NAIS documentation.
Dockerfile
describes the steps needed to build your docker image.
.github/workflows/main.yaml
is the GitHub Actions workflow that will build and deploy your application. This file contains the steps that will build your application and deploy it to NAIS. You can read more about the GitHub Actions workflow in the GitHub Actions documentation.
Step 4: Check that your application is working¶
Before proceeding, let's make sure that your application is working. Run the following command to start your application:
Visit http://localhost:8080 to see your application running.
Step 5: Build a Docker image with your application¶
Now that we have a working application, we need to build a Docker image with our application in order to run it on NAIS.
First, we need to prepare and package the application so that it can be distributed:
Then, run the following command to build a Docker image with your application:
The --platform=linux/amd64
flag is to instruct Docker to build a image for Linux. The default might not work.
Step 6: Push your image to our image registry¶
To make the image available to NAIS, we need to push it to our image registry. We will use the Google Cloud Container Registry for this.
Step 6.1: Configure how Docker authenticates to our image registry¶
Step 6.2: Google login¶
Step 6.3: Tag your image¶
Step 6.4: Push your image¶
Step 7: Manually deploy your application to NAIS¶
Open the nais.yaml
file. It should look something like this:
apiVersion: nais.io/v1alpha1
kind: Application
metadata:
labels:
team: onboarding
name: <my-app>
namespace: onboarding
spec:
ingresses:
- https://<my-app>.intern.dev.nav.no
image: {{image}}
port: 8080
ttl: 3h
replicas:
max: 1
min: 1
resources:
requests:
cpu: 50m
memory: 32Mi
For the highlighted lines (line 6, 10 and 11):
- Replace all occurrences of
<my-app>
with the name of your application, for example: - Replace the
{{image}}
underspec.image
to point to the image you just pushed, for example:
Ensure you are connected to the correct cluster (dev-gcp):
And that you are in the correct namespace:
Now you can deploy your application to NAIS by running the following command:
Ensure that your application is running by running the following command:
You should see a pod with the name <my-app>-<random-string>
. You can inspect the logs from this pod by running the following command:
When the application is running, you can visit the application on the following URL:
Congratulations! You have now deployed an application to NAIS. The next step is to automate this process using GitHub Actions.
Step 8: Configure deployment API key¶
For the GitHub Actions workflow to be able to deploy your application to NAIS, it needs to be able to authenticate with the NAIS Deploy API. Add our team's API key as a secret in the repository.
Paste in your NAIS Deploy API key as the value when asked.
Step 9: Make some changes to your application¶
Open the src/main/kotlin/Main.kt
file and change the Hello, world!
message to something else.
Step 9.5: Reset image reference in nais.yaml
¶
Previously we changed the {{image}}
to point to the image we pushed to our image registry. Now we need to change it back to {{image}}
so that the GitHub Actions workflow can replace this for us.
Step 10: Commit and push your changes¶
Step 11: Observe the GitHub Actions workflow¶
Once you have pushed your changes to GitHub, you can observe the GitHub Actions workflow by running the following command:
...or open the Actions tab on GitHub.
Step 12: Visit your application¶
When the workflow is finished, you can visit the application on the following URL:
Step 13: Epilogue / Cleanup¶
When you are done; delete the application by running the following command:
When you are finished with this guide you can delete your repository:
Created: 2023-06-14