Getting started with Opta on GCP.

To use Opta, you first need to create some simple yaml configuration files that describe your needs. In the following guide we will deploy a simple python flask application.

1: Installation

For Opta to work, the prerequisite tools needed are:

Then you can simply install opta using this command (detailed instructions):

/bin/bash -c "$(curl -fsSL https://docs.opta.dev/install.sh)"

2: Environment creation

Before you can deploy your app, you need to first create an environment (like staging, prod etc.) This will sets up the base infrastructure (like network and cluster) that will be the foundation for your app.

Note that it costs around $5 per day to run this on GCP So make sure to destroy it after you’re done (opta has a destroy command so it should be easy :))!

Create this file and name it opta.yaml

# opta.yaml
name: staging # name of the environment
org_name: my-org # A unique identifier for your organization
    region: us-central1
    project: XXXXX # the name of your GCP project
  - type: base
  - type: k8s-cluster
  - type: k8s-base

Now, run:

opta apply

For the first run, this step takes approximately 15 min.
It configures 3 Opta modules:

For more information about what is created, see GCP Architecture.

Once done, the apply command lists all the resource created, for example:

# partial output of opta apply

k8s_cluster_name = [The Kubernetes cluster name]
load_balancer_raw_ip = [Load Balancer IP]

Opta updates complete!

3: Service creation

In this step we will create a service - which is basically a http server packaged in a docker container.
Here is a simple hello world app, the source code is here.

Create a new opta file for your service.

# hello.yaml
name: hello
  - name: staging
    path: "opta.yaml" # the file we created in previous step
  - type: k8s-service
    name: hello
      http: 80
    # from https://github.com/run-x/hello-opta
    image: ghcr.io/run-x/hello-opta/hello-opta:main
    healthcheck_path: "/"
    # path on the load balancer to access this service
    public_uri: "/hello"

Now you are ready to deploy your service.

opta apply -c hello.yaml
# partial output of opta apply -c hello.yaml
hello-hello-k8s-service-586447679-fgmld  * Running on
module.hello.helm_release.k8s-service: Creation complete after 53s [id=staging-hello]

Opta updates complete!

Now, your service is deployed, you can:

  • Access your service using the load balancer (public)
# see output above or run `opta output | grep load_balancer_raw_ip`
export load_balancer_raw_ip=...

# the service is reachable at /hello (set in the `public_uri` property)
curl http://$load_balancer_raw_ip/hello

<p>Hello from Opta.!</p>
  • SSH into the container
opta shell -c hello.yaml

  • See the application logs
opta logs -c hello.yaml             

Showing the logs for server hello-hello-k8s-service-586447679-fgmld of your service
hello-hello-k8s-service-586447679-fgmld  * Running on
hello-hello-k8s-service-586447679-fgmld - - [23/Dec/2021 19:42:18] "GET / HTTP/1.1" 200 -
  • If you have kubectl installed, you can use it to connect to the kubernetes cluster
# configure the kubeconfig file
# note: if you don't want to use the default path $HOME/.kube/config, set the env var KUBECONFIG first
opta configure-kubectl

# Opta created all the kubernetes resources for your service
kubectl get all --namespace hello

NAME                                            READY   STATUS    RESTARTS   AGE
pod/hello-hello-k8s-service-586447679-fgmld   2/2     Running   0          17m

NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/hello   ClusterIP   <none>        80/TCP    17m

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-hello-k8s-service   1/1     1            1           17m

NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-hello-k8s-service-586447679   1         1         1       17m

NAME                                                            REFERENCE                              TARGETS           MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/hello-hello-k8s-service   Deployment/hello-hello-k8s-service   18%/80%, 1%/80%   1         3         1          17m

4: Cleanup

Once you’re finished playing around with these examples, you may clean up by running the following command from the environment directory:

# destroy the service resources
opta destroy -c hello.yaml

# destroy the environment resources
opta destroy -c opta.yaml

5: Next steps

Last modified August 5, 2022 : Cleanup install script (#197) (2175394)