Microservices with pebl — a complete and free cloud platform: How to Grow the Fuck Up: A Guide to Humans

Kelvin

Hatched by Kelvin

Apr 22, 2024

6 min read

0

Microservices with pebl — a complete and free cloud platform: How to Grow the Fuck Up: A Guide to Humans

Introduction

In this article, we will explore the concept of microservices using pebl, a complete and free cloud platform. We will also delve into the importance of personal growth and maturity in our lives. While these topics may seem unrelated, they both share a common thread - the need for structure, scalability, and adaptability.

Getting Started with pebl

To begin our journey with pebl, we need to sign up for a free account at pebl.io. By choosing your desired identity provider and following the steps, you can claim your free *.pebl.rocks subdomain. This subdomain will be used throughout the tutorial.

Before we dive into the technical aspects of pebl, it's important to ensure that you have Docker and the pebl CLI installed. Visit the Docker website for installation instructions if you don't have it already. Once Docker is set up, head over to pebl's setup docs for the correct link to download the pebl CLI.

Once you have everything set up, verify that the CLI was installed correctly by running the command "pebl" without any arguments. This will display the available commands and confirm that pebl is ready to use.

The Power of Microservices

Now that we have pebl up and running, let's explore the core building block it provides - the microservice. A microservice is a server that can respond to requests, providing a modular and scalable architecture for cloud applications. Pebl makes it easy to create microservices by embedding cloud capabilities within your application through language-specific SDKs.

To illustrate this, let's create a simple "Hello World" microservice using Python. Create a project folder and a subfolder named "hello." In the hello folder, create a file named "main.py" and paste the following code:

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

This code defines a basic Flask application with a single handler for the root path. By calling pebl.service(app, "hey.pebl.rocks"), we declare that this Flask application should be a microservice hosted at your *.pebl.rocks endpoint. With just this simple method call, we have defined a microservice using pebl.

To run the microservice locally, use the pebl CLI to start a local cluster with the command "pebl up." Once the cluster is configured, navigate to the hello subfolder and execute "pebl run." This will build a Docker image and start a container for your microservice. You can then use the "pebl info" command to see the state of the running cluster and send a request to your server using curl.

Deploying to the Cloud

One of the key benefits of pebl is the ability to deploy the same application to different runtimes without any modifications. Pebl incorporates cloud infrastructure usage into your application, eliminating the need for environment-specific checks or multiple config files for local and production deploys.

To deploy your microservice to the cloud runtime, simply run "pebl deploy" in a similar fashion to "pebl run." Pebl handles the deployment process and ensures zero downtime updates to your services. Once the deployment is complete, you can send requests to your cloud-hosted microservice using the appropriate URL.

Expanding with Internal Services

In addition to external microservices, pebl also supports internal services. Internal services are similar to regular microservices but are not configured for external traffic. They are only exposed to other workloads within the pebl cluster, providing a secure and scalable way to structure your cloud workloads.

To demonstrate the concept of internal services, let's explore adding an internal service using Go instead of Python. Create a new subfolder in your project directory and initialize a Go project using "go mod init." Within this folder, create a file named "main.go" and paste the following code:

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 Go server object using the net/http package and pass it to the pebl.InternalService function. This function declares that this Go application should be an internal service accessible within the pebl cluster.

Running this internal service locally is similar to running the external microservice. Use the "pebl run" command in the Go project directory to start the service. You can then send requests to the internal endpoint using curl, but note that these requests will only work locally and not in the cloud runtime.

Utilizing Redis for Persistence

To support a wider range of applications, pebl provides persistence through Redis. By incorporating Redis into your application using the pebl SDK, you can easily manage and store data in the cloud.

Let's update our Go service to incorporate Redis for data storage. Modify the "main.go" file as follows:

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")  
}  

In this updated code, we import the "github.com/redis/go-redis/v9" package and utilize the pebl.Redis function to retrieve connection information for a Redis instance named "users." We then create a Redis client and use it to store and retrieve user data.

Before running the updated Go service, make sure to update the module dependencies using "go mod tidy" to automatically download the required packages. Once the dependencies are updated, use the "pebl run" command to start the Go service. You can now send requests to the internal endpoint, and the service will retrieve user data from the Redis instance.

Proxying Requests

To utilize the internal service from our external Python microservice, we can proxy a subset of requests from the external service to the internal service. Update the "main.py" file in the Python project directory 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")  

In this updated code, we import the "requests" library to send requests to the internal Go service. We define a new route "/user/<name>" that proxies the request to the internal service at "http://go.internal?name={name}". This allows us to access the internal service functionality from the external microservice seamlessly.

Before running the updated Python microservice, make sure to update the Dockerfile to include the "requests" library. Once the Dockerfile is updated, use the "pebl run" command to start the Python microservice. You can now send requests to both the root endpoint and the "/user/<name>" endpoint to access the internal service.

Conclusion and Next Steps

In this article, we explored the power of pebl as a complete and free cloud platform for building microservices. We learned how to create both external and internal services, incorporate Redis for persistence, and proxy requests between services. Pebl's language-specific SDKs make it easy to embed cloud capabilities into your applications, providing scalability and adaptability.

As we wrap up, here are three actionable pieces of advice to keep in mind:

  1. Embrace microservices: Break down your applications into smaller, modular services to achieve scalability and flexibility.

  2. Continuously grow and mature: Personal growth and maturity are essential for success and happiness in life. Embrace challenges, learn from failures, and strive to become the best version of yourself.

  3. Be adaptable: Just as microservices allow for easy deployment to different runtimes, be adaptable in your approach to life. Embrace change, learn new skills, and be open to new opportunities.

By incorporating these principles into your work and personal life, you can create a solid foundation for success and happiness.

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 🐣