Skip to main content

Deploy a Go Website to Kubernetes in 5 Minutes with Jetpack.io

In this tutorial, we'll build a Go webservice and deploy it to Kubernetes with Jetpack.io – all in 5 minutes.

Prerequisites

If you don't have it already, download and install Go.

Docker is required to package your backend as a container before deploying to the cloud.

If you don't already have Docker installed as part of your development environment, follow one of the guides below before returning to this quickstart:

kubectl, while not required, is a useful tool for inspecting and managing your deployments in Kubernetes. We recommend following the installation directions for your platform:

Building a Go website

In this example we'll build and deploy a simple Go webservice. You could swap out these steps with use an existing Go webservice, or scaffold a service using other frameworks or tools.

  1. Open a terminal in an empty directory.

  2. Initialize a go project by running go mod init jetpack-go

  3. Generate a Go website by creating main.go:

    package main

    import (
    "net/http"
    "os"

    "github.com/labstack/echo/v4"
    "github.com/labstack/echo/v4/middleware"
    )

    func main() {

    e := echo.New()

    e.Use(middleware.Logger())
    e.Use(middleware.Recover())

    e.GET("/", func(c echo.Context) error {
    return c.HTML(http.StatusOK, "Hello, Jetpack")
    })

    httpPort := os.Getenv("HTTP_PORT")
    if httpPort == "" {
    httpPort = "8080"
    }

    e.Logger.Fatal(e.Start(":" + httpPort))
    }
  4. Update your go.mod file with the required packages by running go mod tidy

Now we have a regular Go website. Feel free to customize this site to meet your needs.

Configure the App for Containers

Jetpack.io assumes your web server is running on port 8080 from inside a Docker container. In this section we'll build a Dockerfile to run the web site on port 8080.

  1. Create a new file in the website folder named Dockerfile and add this content:

    FROM golang:alpine
    WORKDIR /app
    COPY . .
    RUN go build -o /goapp
    EXPOSE 8080
    CMD [ "/goapp" ]

    Note: this is a bare-bones Dockerfile. Depending on your app's dependencies, you may need to add more.

Initialize the project with Jetpack.io

Jetpack.io needs some initial configuration to understand the Go web server. We need only do this once per website.

  1. To install the Jetpack CLI, open a terminal and run

    curl https://get.jetpack.io -fsSL | bash

    Jetpack CLI works on Linux, macOS, and on Windows via WSL2.

  2. Login to Jetpack CLI:

    jetpack auth login

    Logging into Jetpack allows you to deploy to the Jetpack.io Kubernetes cluster. You can also run on your own cluster in your Azure, AWS, GCP, or private cloud account.

  3. Initialize the project for Jetpack:

    jetpack init

    This wizard interviews you and configures the Jetpack deployment strategy to match your application. For this quickstart, we'll provide the following answers:

    ? What is the name of this project? jetpack-go
    ? What type of service you would like to add to this project? Web Service
    ? To which cluster do you want to deploy project? jetpack-cloud

    This tells Jetpack what to call our project, that it should be deployed as a Web Service, and that we want to use Jetpack Cloud to deploy our example application

  1. Finish the wizard, and Jetpack CLI automatically generates an appropriate jetconfig.yaml file. You should commit this to source control.

Deploy to Kubernetes using Jetpack.io

Now that the project is configured for Jetpack, deploying is really easy.

  1. Open a terminal in the directory with the Dockerfile and jetconfig.yaml file.

  2. Deploy to Kubernetes:

    jetpack dev

    Now Jetpack makes the deployment really simple. Automatically it will:

    • Build the Docker image
    • Push the image to a private registry
    • Schedule the necessary Kubernetes resources
    • Create a publicly routable URL to test the website
    • Setup port-forwarding from your local machine
    • Stream application logs back to the terminal
  3. Test the website:

    In the console output will be the publicly routable URL. Click this URL to view the web page.

    Jetpack also sets up port-forwarding, so you can also browse to http://localhost:8080/ to view the page.

Next Steps

In this tutorial we scaffolded a webservice used Jetpack.io to deploy to Kubernetes. For next steps, check out the following docks: