Okteto with Go
This guide takes the Go Sample App through the whole Okteto workflow, in an application environment in your Kubernetes cluster. The Go Sample App is a hello world service. Its page shows a message, the Go version, and the pod that serves it.
The sample is small, but its configuration is optimized, so you can use it as a reference for your own Go application. Along the way, the guide points out what makes each step fast in a Go project: the cache mounts that the build and the tests share, the ignore files that keep every upload small (.dockerignore, .oktetoignore, and .stignore), and Air and Delve for hot reload and debugging from VS Code.
Prerequisites
- The Okteto CLI, installed and configured to access your Okteto instance. See the installation guide.
- To debug the application: VS Code with the Go extension.
Clone the Go Sample App and move into its folder. Every command in this guide runs from there:
git clone https://github.com/okteto/go-getting-started
cd go-getting-started
Build
The Go Sample App uses two container images. dev is the image you develop in: it has the Go compiler and the tools to test, reload, and debug the application. hello-world is the production image: a small image with nothing but the compiled binary. The build section of the okteto.yml file defines both:
build:
dev:
context: .
target: dev
hello-world:
context: .
target: prod
depends_on:
- dev
Both images come from the same Dockerfile. target picks the stage each image builds, and depends_on tells Okteto to build dev first, so hello-world reuses its cached layers:
FROM golang:1.26.5-trixie AS dev
# The cache mounts keep the Go module cache and the Go build cache in the
# Okteto Build Service, which reuses them between builds. The tests of the
# Okteto Manifest use the same two caches
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
GOBIN=/usr/local/bin go install github.com/air-verse/air@v1.67.4 && \
GOBIN=/usr/local/bin go install github.com/go-delve/delve/cmd/dlv@v1.27.1
WORKDIR /usr/src/app
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -o /usr/local/bin/hello-world
FROM debian:trixie-slim AS prod
COPY --from=dev /usr/local/bin/hello-world /usr/local/bin/hello-world
CMD ["/usr/local/bin/hello-world"]
The Dockerfile installs the tools, Air for hot reload and Delve for debugging, before COPY . ., so a code change doesn't install them again.
Both RUN instructions mount the Go module cache (/go/pkg/mod) and the Go build cache (/root/.cache/go-build) as cache mounts, which the Okteto Build Service keeps between builds. When a code change runs go build again, Go compiles only the packages that changed and doesn't download any module twice.
The build context makes the biggest difference, here and in any application. Every okteto build starts by uploading the Dockerfile's folder to the Okteto Build Service, and the .dockerignore file decides what goes in. The Go Sample App's .dockerignore excludes everything and then allows only what go build reads: the Go files, go.mod, and the static folder with the page.
*
!go.mod
!go.sum
!**/*.go
!static
The upload stays small, and a change to any other file, such as the README or k8s.yml, doesn't rebuild the images.
Deploy
okteto deploy first builds the images that aren't built yet. Okteto pushes them to the Okteto Registry and stores their references in the OKTETO_BUILD_DEV_IMAGE and OKTETO_BUILD_HELLO_WORLD_IMAGE variables.
The Go Sample App deploys with a plain Kubernetes manifest. Its deploy section has a single command, which applies k8s.yml with kubectl:
deploy:
- envsubst < k8s.yml | kubectl apply -f -
In k8s.yml, the Deployment's image is the OKTETO_BUILD_HELLO_WORLD_IMAGE variable. envsubst replaces it with the image Okteto built, and then kubectl applies the file.
The deploy has its own ignore file. okteto deploy uploads your folder to the container that runs the command, and the .oktetoignore file decides what goes in. Its deploy section excludes everything except k8s.yml, the only file the command reads, so there's almost nothing to upload. Its destroy section does the same for okteto destroy:
[deploy]
*
!k8s.yml
[destroy]
*
!k8s.yml
To deploy the application, run okteto deploy:
okteto deploy
Okteto builds the two images, runs the deploy command, and prints the application's endpoint:
i Using cindy @ okteto.example.com as context
✓ Image 'registry.okteto.example.com/cindy/go-getting-started-dev:okteto' successfully pushed
✓ Image 'registry.okteto.example.com/cindy/go-getting-started-hello-world:okteto' successfully pushed
i Running stage 'envsubst < k8s.yml | kubectl apply -f -'
Executing command 'envsubst < k8s.yml | kubectl apply -f -'...
deployment.apps/hello-world created
service/hello-world created
Command 'envsubst < k8s.yml | kubectl apply -f -' successfully executed
i Endpoints available:
- https://hello-world-cindy.okteto.example.com
✓ Development environment 'go-getting-started' successfully deployed
Open the endpoint in your browser. The page shows the application's message, the Go version it runs on, and the pod that served the request:
Test
The Go Sample App has unit tests in main_test.go. The test section of the Okteto Manifest defines a test, unit, that runs them:
test:
unit:
image: ${OKTETO_BUILD_DEV_IMAGE}
caches:
- /go/pkg/mod
- /root/.cache/go-build
commands:
- go test -v -coverprofile=coverage.out ./...
artifacts:
- coverage.out
The test runs with Remote Execution, in a container inside your cluster. The container starts from the development image, so it has the same Go toolchain as the Development Container. If any of the commands exits with a non-zero code, the test fails. When the test finishes, Okteto copies its artifacts, here the coverage profile, to your machine.
The caches are the same two folders that the Dockerfile mounts, and they're the same caches too: Okteto identifies a cache by its repository, context, and path, and the test and the build share all three. So go test starts with the modules the build downloaded and the packages it compiled, and compiles only the test code.
The tests have their own section in the .oktetoignore file, so okteto test uploads only what go test reads:
[test]
*
!go.mod
!go.sum
!**/*.go
!static
Run the unit tests with okteto test:
okteto test
i Using cindy @ okteto.example.com as context
i Images were already built. To rebuild your images run 'okteto build'
i Executing test container 'unit'
i Running 'go test -v -coverprofile=coverage.out ./...'
=== RUN TestHello
--- PASS: TestHello (0.00s)
=== RUN TestPage
--- PASS: TestPage (0.00s)
PASS
coverage: 44.4% of statements
ok github.com/okteto/go-getting-started 0.007s coverage: 44.4% of statements
✓ Command 'go test -v -coverprofile=coverage.out ./...' successfully executed
✓ Test container 'unit' passed
The coverage profile is now in coverage.out, in the root folder of the Go Sample App on your machine. Inspect it with the Go tools:
go tool cover -func=coverage.out
Running okteto deploy and okteto test after each change is usually fast enough, especially when a coding agent writes and tests the code on its own. With the Okteto skills, the agent runs this loop in its own Namespace and reports back the tests it ran, the application's endpoints, and a link to that Namespace.
Develop
A Development Container feels like developing on your machine: every change you save reloads the application, and you can attach a debugger. When you run okteto up, Okteto replaces the hello-world Deployment with the Development Container, keeps your local files synchronized with it, and runs the development image's hot reload and debugging tools in your cluster. This step is optional: coding agents work well with the okteto deploy and okteto test loop and don't need a Development Container.
While okteto up is running, a coding agent with the Okteto skills stops building, deploying, and testing, and only edits the code: File Sync copies each change to the Development Container, and hot reload applies it to the running application.
The dev Section
The dev section of the Okteto Manifest defines the Go Sample App's Development Container:
dev:
hello-world:
image: ${OKTETO_BUILD_DEV_IMAGE}
command: air
sync:
- .:/usr/src/app
persistentVolume:
enabled: false
securityContext:
capabilities:
add:
- SYS_PTRACE
forward:
- 2345:2345
The hello-world key is the name of the Deployment in k8s.yml that the Development Container replaces. This is what each field does:
image: the development image from thebuildsection.command: starts Air, which rebuilds and restarts the application every time a.gofile changes.sync: synchronizes the Go Sample App's root folder with/usr/src/app, the development image's working directory.persistentVolume: disables the Development Container's persistent volume, so there's no volume to create and attach. Okteto synchronizes every file on eachokteto up, and Air compiles the application from scratch when it starts, because the build's cache mounts aren't part of the development image. For a larger project, keep the Development Container's Go caches in a persistent volume or a distributed cache. Caches in Development Containers explains both.securityContext: adds theSYS_PTRACEcapability, which Delve needs.forward: forwards port2345, where Delve listens, to your machine.
The third ignore file is .stignore, for File Sync. okteto up synchronizes your folder with the Development Container in both directions, and this file leaves out what shouldn't travel. That's the Git metadata, which is large and only matters on your machine, and the binaries that Air and Delve generate inside the Development Container, which would otherwise come back to your machine:
.git
/tmp
/go-getting-started
__debug_bin*
*.test
*.out
.vscode
.DS_Store
Hot Reload
Run okteto up to replace the hello-world Deployment with the Development Container:
okteto up
Okteto synchronizes your files and starts Air, which builds and runs the application:
i Using cindy @ okteto.example.com as context
i 'go-getting-started' was already deployed. To redeploy run 'okteto deploy' or 'okteto up --deploy'
✓ Images successfully pulled
✓ Files synchronized
Context: okteto.example.com
Namespace: cindy
Name: hello-world
Forward: 2345 -> 2345
watching .
!exclude tmp
building...
running...
Starting hello-world server...
Now change the code. Open main.go in your IDE, edit the message on line 14, and save the file:
const message = "Built, deployed, and tested on Okteto."
Okteto synchronizes main.go to the Development Container. Air notices the change, rebuilds the application, and restarts it:
main.go has changed
building...
running...
Starting hello-world server...
A moment later, the page in your browser shows the new message. You don't even have to reload it, because the page asks the service for its info every second. Your change is running in your cluster, and you didn't build an image or redeploy the application.
When you're done, press Ctrl+C to stop Air and exit okteto up.
Debugging with VS Code
To debug the application, start the Development Container with Delve instead of Air: Delve builds and runs its own copy of the application. Pass the command after --, and it replaces the command in the Okteto Manifest:
okteto up -- make debug
make debug runs dlv debug --headless --listen=:2345 --log --api-version=2. Delve builds the application but doesn't start it until a debugger connects to port 2345:
API server listening at: [::]:2345
info layer=debugger launching process with args: [./__debug_bin3004072740]
That's the port okteto up forwards to your machine, and the Go Sample App's .vscode/launch.json file tells VS Code to attach to it:
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to okteto",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "/usr/src/app",
"port": 2345,
"host": "127.0.0.1"
}
]
}
remotePath is the path of the code in the Development Container, the same one as in the sync field of the Okteto Manifest. If you synchronize your code to another path, change remotePath to match.
Open the Go Sample App in VS Code, go to the Run and Debug view, and run the Connect to okteto configuration. The application starts. Now add a breakpoint on line 42 of main.go, the first line of the hello function. The page in your browser asks the service for its info every second, so execution stops at the breakpoint right away, and you can inspect the request and the variables:
Ctrl+C doesn't stop Delve. To end the debugging session, run okteto down in another terminal, from the root folder of the Go Sample App. Then run okteto up again to go back to hot reload.
Stopping the Development Container
When you finish developing, run okteto down. It stops the Development Container and brings back the hello-world Deployment with the production image:
okteto down
i Using cindy @ okteto.example.com as context
✓ Development container 'hello-world' deactivated
Destroy
To delete the application, run okteto destroy. It removes the Go Sample App's Deployment and Service from your Namespace:
okteto destroy
i Using cindy @ okteto.example.com as context
i Images were already built. To rebuild your images run 'okteto build'
✓ Development environment 'go-getting-started' successfully destroyed
FAQ
How do I set up my own repository the same way?
Use a coding agent with the Okteto skills. Ask it to set up your repository for Okteto: it discovers your services, writes the Okteto Manifest, and validates it with okteto build and okteto deploy.
How do I make builds, deploys, and okteto up faster?
Upload only the files that each command needs: the context upload is key to performance. Every okteto build, okteto deploy, okteto test, and okteto up uploads files from your machine, and three ignore files decide what gets uploaded: .dockerignore for the image builds, .oktetoignore for okteto deploy, okteto test, and okteto destroy, and .stignore for File Sync. A coding agent with the Okteto skills configures them for your application and measures how much faster your builds, deploys, and okteto up get. Optimize Your Application Environment covers these and other optimizations.
Do I need to deploy the whole application in every Namespace?
No. Okteto supports two models, and you choose one for your application:
- Full replica: every service of the application runs in your Namespace. In a monorepo, the
deploysection of the Okteto Manifest deploys every service, as in the quickstart, where one Helm chart deploys the three services of Kube Movies. In a multirepo, each repository has its own Okteto Manifest and lists the other repositories in itsdependenciessection, as in Deploy a Multi-Repo Application with Dependencies. Full replicas are simpler to configure and give you full isolation: nothing one environment does affects the others. Use them for an application with fewer than 20 services. - Overlay Namespace: your Namespace runs only the services you change, and Divert routes every other request to a Shared Namespace that runs the rest of the application. Overlay Namespaces are more cost-effective, faster to deploy, and more reliable, because each environment builds, deploys, and runs only the services it changes. Use them for an application with 20 services or more.
How do I create a Preview Environment of my application for every pull request?
Add a workflow to your CI pipeline that deploys the application in a Preview Environment when a pull request opens or changes, runs okteto test against it, and destroys it when the pull request closes. The workflow uses the Okteto Manifest, so okteto build, okteto deploy, and okteto test work the same way in your CI pipeline, for coding agents, and on developer machines. Follow the guide for GitHub Actions or for GitLab CI/CD.
How do I start each environment with realistic data?
Load the data with a script in your deploy commands, or copy the data of another application environment with a Volume Snapshot, which creates the volumes of your database from a snapshot of its volumes. Data from Volume Snapshots shows how.
How do I create resources outside Kubernetes, such as a cloud database?
Create them in the deploy and destroy commands with your infrastructure as code tool, so every environment gets its own resources. See the Create Cloud Infrastructure with Terraform tutorial, which creates an Amazon S3 bucket for each environment.
Can I access my application environment with kubectl?
Yes. Run okteto kubeconfig to add the credentials of your Okteto Namespaces to your kubeconfig file, and use kubectl as usual: kubectl get pods, kubectl logs, or kubectl describe. See Kubernetes Credentials for how Okteto generates those credentials.