Microservices with pebl — a complete and free cloud platform # pebl # devops # microservices # infrastructureascode

Kelvin

Hatched by Kelvin

Sep 11, 2023

6 min read

0

Microservices with pebl — a complete and free cloud platform pebl devops microservices infrastructureascode

Overview

In this guide, we will explore pebl, a complete and free cloud platform that offers an alternative approach to Infrastructure as Code. Unlike traditional methods that rely on YAML files to control the cloud, pebl integrates cloud capabilities directly into your application through language-specific SDKs.

Getting Started

To begin, sign up for a free account at pebl.io. Choose your desired identity provider and follow the steps. Don't forget to claim your free *.pebl.rocks subdomain, as we'll be using it throughout the tutorial.

Before we proceed, ensure that you have Docker and the pebl CLI installed on your system. If you don't have Docker, visit the Docker website and follow their installation instructions. For the pebl CLI, refer to the pebl setup documentation and download the correct link for your system. To verify the installation, run the "pebl" command without any arguments.

Simple Service

The core feature of pebl is the service, which serves as a building block for your applications. Similar to serverless applications on other platforms, pebl's service is a server that can respond to requests.

Let's create a simple service using Python. Start by creating a project folder and a subfolder for our service. In our example, we'll use the following directory structure:

~/work/scratch/  
~/work/scratch/hello  

Inside the hello folder, create a main.py file with the following content:

from flask import Flask  
  
app = Flask(__name__)  
  
@app.route("/")  
def root():  
    return "hello, world!\n"  
  
import pebl  
pebl.service(app, "hey.pebl.rocks")   put your own domain here  

This code defines a basic Flask application with a single handler for the root path. The pebl.service function is used to declare this Flask application as a service, hosted at your *.pebl.rocks endpoint.

To run this locally, we'll rely on pebl's Docker image. Create a Dockerfile in the hello folder with the following content:

FROM peblcloud/python  
COPY . .  
RUN pip install flask  
ENTRYPOINT python -u main.py  

Now, start a local pebl cluster by running pebl up and navigate to the hello subfolder. Execute pebl run to run the service locally. You can use pebl info to see the state of the running cluster and send a request to your server using curl.

Cloud Runtime

One of the key benefits of pebl is the ability to deploy the same application to different runtimes without modifications. This eliminates the need for environment-specific checks or multiple config files for local and production deploys.

To deploy your application to the cloud runtime, simply run pebl deploy. Make sure to authenticate if you encounter any authentication errors.

Once the deploy is successful, you can send requests to your deployed service using the assigned domain. Don't forget to use HTTPS, as pebl only allows TLS traffic.

Microservices

In cloud architecture, it is common to break workloads into small services that handle specific parts of a larger system. Pebl provides the capability to create internal services, which are only exposed to other workloads and not configured for external traffic.

Let's explore this feature by creating an internal service using Go. First, create a subfolder within the scratch folder to hold the Go setup:

mkdir ~/work/scratch/go  

Initialize a Go project using go mod init:

cd ~/work/scratch/go  
go mod init scratch  

Unlike Python, Go is compiled, so we don't need a Dockerfile. Instead, we need to download the pebl package for Go:

cd ~/work/scratch/go  
go get github.com/peblcloud/go  

Next, create a main.go file with the following content:

package main  
  
import (  
    "github.com/peblcloud/go"  
    "net/http"  
)  
  
func main() {  
    mux := http.NewServeMux()  
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {  
        w.Write([]byte("this is an internal service!\n"))  
    })  
  
    pebl.InternalService(mux, "go.internal")  
}  

In this code, we create a server object based on net/http and pass it to the pebl.InternalService function to define an internal service. The endpoint for internal services can be anything, but it is recommended to use an easy-to-remember scheme, such as a .local or .internal TLD.

To run the Go service locally, execute pebl run in the go subfolder. You can send requests to the internal endpoint using curl.

Redis

To support a wider range of applications, pebl provides persistence through Redis. This capability can be unlocked by incorporating the Redis SDK into your application.

Let's update the Go service (main.go) to include Redis:

package main  
  
import (  
    "context"  
    "encoding/json"  
    "fmt"  
    "net/http"  
    "net/url"  
  
    "github.com/peblcloud/go"  
    "github.com/redis/go-redis/v9"  
)  
  
func main() {  
    connInfo, _ := pebl.Redis("users")  
    client := redis.NewClient(&redis.Options{  
        Addr: fmt.Sprintf("%s:%d", connInfo.Host, connInfo.Port),  
    })  
  
    mux := http.NewServeMux()  
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {  
        query, _ := url.ParseQuery(r.URL.RawQuery)  
        name := query.Get("name")  
        userInfo, _ := client.HGetAll(context.Background(), name).Result()  
        payload, _ := json.Marshal(userInfo)  
        w.Write(payload)  
    })  
  
    pebl.InternalService(mux, "go.internal")  
}  

This code incorporates the Redis SDK and uses it to retrieve user information based on the query parameter. To run this locally, make sure to update your module dependencies using go mod tidy. You can also manually run go get github.com/redis/go-redis/v9 if needed.

Run the Go service locally using pebl run, and send requests with query parameters to populate the Redis instance. You can manage the data using the exposed port shown on the info pane.

Proxying

To utilize the internal service from the external Python service, we can proxy a subset of requests. Update the main.py in the hello folder as follows:

from flask import Flask  
import requests  
  
app = Flask(__name__)  
  
@app.route("/")  
def root():  
    return "hello, world!\n"  
  
@app.route("/user/<name>")  
def user_service(name):  
    r = requests.get(f"http://go.internal?name={name}")  
    return r.text, r.status_code  
  
import pebl  
pebl.service(app, "hey.pebl.rocks")   put your own domain here  

In this code, we use the requests library to send requests to the internal Go service. The power of pebl is its ability to incorporate other Python libraries seamlessly without additional configuration. Update the Dockerfile to include the requests library.

Run the external Python service locally using pebl run and send requests to the /user endpoint to access the internal service.

Deploying

To deploy the changes, first deploy the internal Go service using pebl deploy in the go subproject. Then, invoke pebl deploy in the hello subproject.

Pebl implements zero-downtime updates to services, ensuring that there are no dropped requests during the transition. Once the deploy is complete, you can send requests to the cloud runtime using the assigned domain.

Redis Management

To access Redis in the cloud runtime, you can utilize the pebl tunnel command. This command sets up a tunnel that proxies local traffic to your Redis instances in the cloud. Use the redis-cli with the -p argument to connect to the proxied port and manage the data.

Next Steps

In this guide, we explored the capabilities of pebl, including services, internal services, and Redis integration. To further explore pebl's offerings, refer to the SDK reference and the provided guides for common cloud tasks.

Actionable Advice:

  1. Familiarize yourself with the pebl SDK documentation to fully utilize the capabilities of the platform.
  2. Experiment with different languages and frameworks to leverage pebl's language-agnostic nature.
  3. Explore additional features and services offered by pebl to enhance your cloud applications.

By combining the contents from the two sources, we have created a comprehensive article that covers the use of pebl, a complete and free cloud platform, for microservices development. The article provides a step-by-step guide to getting started with pebl, creating a simple service using Python, deploying to the cloud runtime, incorporating internal services with Go, integrating Redis for persistence, proxying requests between services, and managing Redis in the cloud runtime. The article also includes actionable advice for further exploration and utilization of pebl's capabilities.

Sources

← Back to Library

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)

Start Hatching 🐣