Getting Started with Okteto Pipelines
This tutorial provides a step by step guide to configure an Okteto Pipeline. Okteto Pipelines allow anyone on your team or your open source community to deploy a realistic environment for your application on Okteto in just one click.
Overview
The tutorial is composed of the following steps:
- Step 1: Code the Hello World application
- Step 2: Define a Dockerfile for building the Docker image of the Hello World application
- Step 3: Create Kubernetes manifests to deploy the Hello World application on Okteto
- Step 4: Automate the deployment of the Hello World application with an Okteto Pipeline
- Step 5: Deployment time!
Let's get started!
Step 1: Code the Hello World application
We will use a simple Hello World application to illustrate how to configure an Okteto Pipeline. The Hello World application is a simple web service that responds "Hello World!" to every request. It is written in Go, but Okteto can be used with any application that runs on Kubernetes.
First, make a new directory called hello-world
and move inside it:
$ mkdir hello-world
$ cd hello-world
Create a new file under the name main.go
with the following content:
package main
import (
"fmt"
"net/http"
)
func main() {
fmt.Println("Starting hello-world server...")
http.HandleFunc("/", helloServer)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func helloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello world!")
}
main.go
implements a Golang web server that listens on port 8080 and responds to every Http request with the message Hello world!
Next, initialize your go module running the following command:
$ go mod init go-getting-started
Step 2: Define a Dockerfile for building the Docker image of the Hello World application
You need to build a Docker image and push it to a Docker registry before Okteto can run your application.
To do that, you need to define a Dockerfile
.
A Dockerfile
file is a sequence of instructions to build the Docker image of your application.
If you're not familiar with Docker, we strongly recommend you learn about it.
Open a new file under the name Dockerfile
with the following content:
FROM golang:buster
WORKDIR /app
ADD . .
RUN go build -o app
EXPOSE 8080
CMD ["./app"]
Step 3: Create Kubernetes manifests to deploy the Hello World application on Okteto
To deploy an application to Okteto, you need to define it using Kubernetes manifests.
Let's start by creating a new folder for the Kubernetes manifests:
$ mkdir k8s
When you use a Kubernetes manifest, you tell Kubernetes how you want your application to run. This time, you'll create a deployment object. Create a new file k8s/deployment.yaml
with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
selector:
matchLabels:
app: hello-world
replicas: 1
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: okteto.dev/hello-world:latest
ports:
- containerPort: 8080
The deployment manifest has three main sections:
metadata
defines the name for your deployment.replicas
defines how many copies of it you want running.template
tells Kubernetes what to deploy, and what labels to add. In this case, a single container, with theokteto.dev/hello-world:latest
image we will build in our Okteto Pipeline, listening on port 8080, and with theapp: hello-world
label. Note that this label is the same used in theselector
section.
If you want to know more about Kubernetes deployment objects, check the official docs.
You'll now need a way to access your application. You can expose an application on Kubernetes by creating a service object. Create a new file called k8s/service.yaml
with the following content:
apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
type: LoadBalancer
ports:
- name: http
port: 8080
selector:
app: hello-world
The service manifest has four main sections:
metadata
tells Kubernetes how to name your servicetype
tells Kubernetes how you want to expose your service. In this case, it will expose it externally through a Load Balancer. In you want to expose your application using ingress, check the docs.ports
tells Kubernetes which ports you want to exposeselector
tells Kubernetes how to direct traffic. In this case, any pod with theapp: hello-world
label will receive traffic.
Kubernetes manifests can get complex to manage. As your application grows, we recommend you pack your application using Helm.
You now have everything ready to define the Okteto Pipeline of the Hello World application.
Step 4: Automate the deployment of the Hello World application with an Okteto Pipeline
You can automate the deployment of the Hello World application with an Okteto Pipeline to provide a one-click deployment experience for anyone in your team or your open source community.
To define your Okteto Pipeline, create a new file okteto-pipeline.yml
with the following content:
deploy:
- okteto build -t okteto.dev/hello-world:latest
- kubectl apply -f k8s
The Okteto Pipeline manifest has a deploy
section to define the sequence of commands to deploy the Hello World application.
-
The first command builds the Docker image of the Hello World application and pushes the image to the Okteto Registry. Note that
okteto.dev/hello-world:latest
is expanded to the current user image space to make this instruction portable between namespaces. -
The second command deploys the Hello World application using
kubectl
and the Kubernetes manifests you created on Step 2.
You can make use of tools such as kubectl
, helm
, okteto
, and kustomize
in your Okteto Pipeline.
Read the Okteto Pipeline documentation to learn more about the Okteto Pipeline yaml syntax, available tools, and environment variables that you can use as part of your Okteto Pipeline.
Step 5: Deployment time!
Log into your Okteto instance (eg. https://okteto.example.com). Click on the Deploy button on the top left of the Okteto dashboard, select "Git URL" as the source and type the URL of your Git repository to deploy the Hello World application:
After a few seconds, your application will be ready. You can access the Hello World application by clicking its endpoint:
You can make the deployment experience even smoother by adding a Develop on Okteto button to the
README.md
file of your Git repository.
Next steps
Congratulations, you just configured your first Okteto Pipeline on Okteto 🚀.
We have covered how to deploy realistic environments for cloud-native applications in just one-click. The full source code used on this tutorial is available here.
Head over to our samples for Go, ASP.NET, Java, Node.js, PHP, Python, or Ruby to see how to use Okteto to live-update your application with different programming languages and debuggers.