Microservices with pebl — a complete and free cloud platform: Unlocking the Power of Cloud-Native Applications
Hatched by Kelvin
Oct 05, 2023
6 min read
12 views
Microservices with pebl — a complete and free cloud platform: Unlocking the Power of Cloud-Native Applications
Overview
In this guide, we will explore 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 the cloud, pebl embeds cloud capabilities within your application through language-specific SDKs. This unique approach allows for seamless integration and more efficient development processes.
Getting Started: Signing Up and Setting Up
To begin your pebl journey, the first step is to sign up for a free account at pebl.io. Choose your desired identity provider and follow the simple steps. Don't forget to claim your free *.pebl.rocks subdomain, which will be used throughout the tutorial.
Next, ensure that you have Docker and the pebl CLI installed on your system. If Docker is not yet installed, head over to the official Docker website and follow the installation instructions. For the pebl CLI, refer to the pebl setup documentation and choose the appropriate download link for your system.
Once everything is set up, verify that the pebl CLI was installed correctly by running the command "pebl" without any arguments. This command should display the available commands and their descriptions.
Creating a Simple Service with Python
The core building block of pebl is a service, which is similar to a serverless application. To demonstrate pebl's capabilities, let's create a simple "Hello, World!" service using Python.
Start by creating a project folder on your system, such as "~/work/scratch". Within this folder, create a subfolder named "hello" that will contain our hello world 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 for the root path. The last line is where we declare that this Flask application should be a service hosted at your *.pebl.rocks endpoint.
To run the service locally, we will utilize pebl's base 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 cluster using the command "pebl up". Once the cluster is initialized, navigate to the "hello" subfolder and execute "pebl run". This command will build the Docker project and start the container for your service.
To test the locally running service, use the command "curl localhost:{port}" with the correct port number shown in the "pebl info" pane. You should see the "Hello, World!" message as the response.
Deploying to the Cloud
One of the key benefits of pebl is its ability to deploy the exact same application to different runtimes without any modifications. This eliminates the need for environment-specific checks or multiple config files for different deployments.
To deploy your application to the cloud runtime, use the command "pebl deploy" in a similar fashion to "pebl run". If you encounter authentication errors, run "pebl auth" and follow the steps to manage your credentials.
After a successful deployment, you can send requests to your cloud-hosted service using HTTPS and your *.pebl.rocks domain. For example, use the command "curl https://hey.pebl.rocks" to access your deployed service.
Exploring Microservices with Internal Services
A common approach to structuring cloud workloads is to break them into small services, each serving a specific part of the larger system. Pebl provides the capability to create internal services, which are only exposed to other workloads within the cluster and not configured for external traffic.
To demonstrate this capability, let's create an internal Go service that acts as a user service, managing user data.
Create a new folder within your project directory, such as "~/work/scratch/go". Initialize a Go project using "go mod init scratch".
Within the "go" folder, create a file named "main.go" with 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 a server object using the net/http package and passes it to pebl's SDK using the pebl.InternalService call. Note that the endpoint provided ("go.internal") is arbitrary for internal services.
To run the internal Go service locally, use the command "pebl run" within the "go" folder. You can send requests to the internal service using "curl localhost:{port}" with the correct port number shown in the "pebl info" pane.
Adding Redis for Persistence
To support a wider range of applications, pebl provides persistence through Redis. You can incorporate Redis into your application using the pebl SDK, as demonstrated in the following updated Go service code:
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 updated code incorporates Redis into the Go service, allowing you to store and retrieve user data. To ensure the availability of the necessary dependencies, update the module dependencies using "go mod tidy" or "go get github.com/redis/go-redis/v9".
Run the Go service locally using "pebl run" and test the requests with user names. You can manage the Redis data using the exposed port shown in the "pebl info" pane combined with the "redis-cli" command.
Proxying Requests to Internal Services
To utilize the internal service from an external service, we can proxy a subset of requests. Update the main.py file in the "hello" folder with the following code:
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
This code uses the requests library to send requests to the internal Go service at "go.internal:80". The power of pebl is evident in its ability to incorporate other Python libraries seamlessly.
Update the Dockerfile in the "hello" folder to include the requests library:
FROM peblcloud/python
COPY . .
RUN pip install flask requests
ENTRYPOINT python -u main.py
To test the proxying functionality locally, run the "pebl run" command within the "hello" folder. Use the command "curl localhost:{port}/user/{name}" to access the internal service through the external endpoint.
Deploy the updated service using "pebl deploy" in both the Go and Python subprojects. The zero downtime deployment ensures a smooth transition without dropped requests.
Redis Management in the Cloud
To access Redis in the cloud runtime, you can use pebl CLI's "pebl tunnel" command. This command establishes a tunnel that proxies local traffic to your cloud Redis instances.
Use the command "pebl tunnel" and note the tunnel address shown. Connect to the proxied port using "redis-cli -p {port}" to manage the Redis data in the cloud.
Next Steps: Exploring More of pebl's Capabilities
In this guide, we have explored three key capabilities of pebl: services, internal services, and Redis. However, pebl offers much more. You can explore the SDK reference and follow the guides available to discover and utilize additional features for your cloud-native applications.
Actionable Advice:
-
Leverage pebl's unique approach to Infrastructure as Code by incorporating cloud capabilities directly into your application code. This eliminates the need for separate configuration files and allows for seamless integration with other libraries.
-
Take advantage of pebl's ability to deploy the same application to different runtimes without modifications. This simplifies the deployment process and ensures consistency across environments.
-
Utilize pebl's internal services for handling internal communication between services within your application. This helps to organize and modularize your cloud workloads, promoting scalability and maintainability.
In conclusion, pebl offers a complete and free cloud platform that empowers developers to build cloud-native applications with ease. By embedding cloud capabilities into the application code and providing seamless integration with popular libraries, pebl revolutionizes the way we
Sources
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 🐣