Microservices with pebl — a complete and free cloud platform: Unleashing the Power of Cloud Infrastructure as Code

Kelvin

Hatched by Kelvin

Oct 09, 2023

6 min read

0

Microservices with pebl — a complete and free cloud platform: Unleashing the Power of Cloud Infrastructure as Code

Overview
In today's fast-paced digital landscape, cloud computing has become a necessity for businesses and developers. It provides scalability, flexibility, and cost-efficiency, allowing organizations to focus on their core competencies. In this guide, we will introduce you to pebl, a complete and free cloud platform that revolutionizes the way we approach infrastructure as code. Unlike traditional methods that rely on declaring YAML files to control your cloud, pebl embeds cloud capabilities within your application through language-specific SDKs.

Getting Started: Signing Up and Installing pebl CLI
To get started with pebl, you first need to 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 will be using it throughout the tutorial.

Next, ensure that you have Docker and the pebl CLI installed on your system. If you don't have Docker, head over to the Docker website and follow their installation instructions. For the pebl CLI, refer to the setup documentation on the pebl website and download the correct link for your operating system. Verify the installation by running the command "pebl" without any arguments. You should see a list of available commands.

Creating a Simple Service with Python and Flask
The core building block of pebl is a service, similar to a serverless application. Let's dive into pebl by creating a simple "Hello World" service using Python and Flask.

First, create a project folder on your system, such as ~/work/scratch. Inside this folder, create a subfolder named "hello" to hold our service. In the hello folder, create a file named "main.py" and add 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")   put your own domain here  

This code defines a basic Flask application with a single handler on the root path ("/"). The pebl.service() method is used to declare that this Flask application should be a service hosted at your *.pebl.rocks endpoint.

To run this service locally, we will use pebl's base Docker image to create a reusable container. Create a file named "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 the command "pebl up" in the terminal. Once the cluster is configured, navigate to the hello subfolder and execute "pebl run". This will build the Docker project and start the container.

You can now access your service by sending a request to http://localhost:32807. You should see the response "hello, world!".

Deploying to the Cloud Runtime
One of the key benefits of pebl is its ability to deploy the same application to different runtimes without any modifications. This eliminates the need for environment-specific checks or multiple configuration files. To deploy your application to the cloud runtime, simply run "pebl deploy" in a similar fashion to "pebl run". Make sure to authenticate if prompted.

Once the deployment is successful, you can send requests to your service using your *.pebl.rocks domain. For example, you can use the command "curl https://hey.pebl.rocks" to access your deployed service.

Exploring Microservices with Internal Services
In cloud computing, it is common to break down workloads into smaller services, each serving a specific part of the larger system. With pebl, you can achieve this by utilizing internal services. Internal services are similar to regular services, but they are only exposed to other workloads within your pebl cluster and not configured for external traffic.

To demonstrate this, let's add an internal service using Go instead of Python. Create a subfolder named "go" within the scratch folder. Initialize a Go project using "go mod init scratch" in the go folder.

Next, create a file named "main.go" in the go folder and add 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")  
}  

This code creates an HTTP server using Go's net/http package and registers a handler for the root path ("/"). The pebl.InternalService() method is used to declare this as an internal service with the endpoint "go.internal".

You can run this internal Go service locally by executing "pebl run" within the go folder. Access the service at http://localhost:32808 to see the response "this is an internal service!".

Incorporating Redis for Persistence
To support a wider range of applications, pebl provides persistence through Redis. You can easily incorporate Redis into your pebl services using the SDK. Let's update the Go service to utilize Redis for managing user data.

First, import the necessary packages in the main.go file:

import (  
    "context"  
    "encoding/json"  
    "fmt"  
    "net/http"  
    "net/url"  
    "github.com/peblcloud/go"  
    "github.com/redis/go-redis/v9"  
)  

Next, update the main() function to include Redis functionality:

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 connects to a Redis instance named "users" and retrieves user data based on the query parameter "name".

To run the updated Go service locally, execute "pebl run" within the go folder. You can now send requests to http://localhost:32808, including the query parameter "?name=alice" or "?name=bob" to retrieve user data.

To manage the Redis data in the cloud runtime, you can use the pebl CLI's "pebl tunnel" command. This command establishes a tunnel to your Redis instance in the cloud, allowing you to use the redis-cli to connect and manage the data.

Next Steps and Conclusion
In this guide, we have explored the power of pebl as a complete and free cloud platform for microservices and infrastructure as code. We have created a simple service using Python and Flask, demonstrated the use of internal services with Go, and incorporated Redis for persistence.

To further explore the capabilities of pebl, we recommend diving into the SDK reference and exploring the many guides available on the pebl website. As you continue your cloud journey, remember to leverage the flexibility and scalability provided by microservices and infrastructure as code to optimize your applications and workflows.

Actionable Advice:

  1. Experiment with different languages and frameworks: Pebl allows you to incorporate cloud capabilities into your application regardless of the programming language or framework you choose. Take advantage of this flexibility to explore and compare different options that best fit your project requirements.
  2. Optimize performance and scalability: As you develop and deploy your microservices, pay attention to performance and scalability considerations. Utilize caching mechanisms, load balancing, and other techniques to ensure your services can handle increasing traffic and maintain fast response times.
  3. Implement thorough testing and monitoring: With a distributed microservices architecture, it is crucial to have robust testing and monitoring practices in place. Implement automated tests, continuous integration, and comprehensive monitoring solutions to detect and resolve issues proactively.

In conclusion, pebl provides a unique approach to cloud infrastructure as code, embedding cloud capabilities within your application through language-specific SDKs. By utilizing pebl, you can easily create and deploy microservices, incorporate external services like Redis, and optimize your cloud-based applications for scalability and performance. Embrace the power of pebl and unlock the full potential of cloud computing for your projects.

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 🐣