Microservices with pebl — a complete and free cloud platform # pebl # devops # microservices # infrastructureascode
Hatched by Kelvin
Feb 20, 2024
7 min read
13 views
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 a unique approach to 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
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 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, visit the Docker website and follow their instructions. For the pebl CLI, refer to pebl's setup docs for the correct download link for your system.
Once installed, verify that the pebl CLI was installed correctly by running the command "pebl" without any arguments. You should see a list of available commands.
Simple Service
The core building block of pebl is a service, which is equivalent to a serverless application. A pebl service is a server that can respond to requests. In this section, we will create a simple service using Python.
Start by creating a scratch project folder on your system. For example, you can use "~/work/scratch". Within this folder, create a subfolder called "hello" that will contain our simple hello world service.
In the "hello" folder, create a file called "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 uses the Flask framework to define a simple 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 the service, we will use 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 pebl cluster by running "pebl up" command. Once the cluster is configured, navigate to the "hello" subfolder and execute "pebl run" command. This will build the Docker project and start the container.
You can use the "pebl info" command to see the state of the running cluster and obtain the port number for your server. Send a request to your server using curl to verify that it is working correctly.
Cloud Runtime
One of the key benefits of pebl is the ability to deploy the exact same application to different runtimes without any modifications. Pebl incorporates cloud infrastructure usage into your application, making it easy to migrate your applications to the cloud once you are done testing them locally.
To deploy your application, use the "pebl deploy" command in a similar fashion to "pebl run". If you encounter authentication errors, run "pebl auth" and follow the steps.
Once the deploy is successful, send a request to your deployed application using curl. Make sure to use https as all services in the cloud get TLS by default.
Microservices
A common approach to structuring cloud workloads is to break them into small services, each serving a specific part of the larger system. Pebl allows you to achieve this by utilizing the internal services capability.
Internal services are similar to regular services but are only exposed to other workloads within your pebl cluster. They are not configured for external traffic. In this section, we will explore adding an internal service and utilizing Go instead of Python to highlight the language-agnostic feature of pebl.
Start by creating a subfolder called "go" within your scratch folder. Initialize a Go project by running "go mod init" command.
Create a file called "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")
}
In this code, we create a server object based on the net/http package and pass it to pebl's SDK using the pebl.InternalService call. The "go.internal" domain is used for internal services.
To run the Go service locally, use the "pebl run" command as before. You can send requests to the internal endpoint using curl, but note that internal services cannot be accessed from outside the cluster in the cloud runtime.
Redis
To support a wider range of applications, pebl provides persistence through Redis. You can incorporate Redis into your application using pebl's SDK.
Update the Go service code in the "main.go" file to include Redis functionality:
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 uses the go-redis package and the pebl.Redis method to obtain connection information for a Redis instance named "users". The server retrieves user data from Redis based on the query parameter and returns it as a JSON response.
To run the updated Go service locally, use the "pebl run" command again. You can manage the data in your local Redis instance using the exposed port shown on the info pane. Send requests to the service with different query parameters to retrieve user data.
Proxying
To utilize the internal service from the external Python service, we can proxy a subset of requests from the Python service to the internal Go service.
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 pebl.service method is used to declare that this Flask application should be a service hosted at your *.pebl.rocks endpoint.
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
Now, run the updated Python service locally using the "pebl run" command. You can send requests to the external endpoint at the "/user" path to access the internal service.
Deploying
To deploy the changes, first deploy the internal Go service using the "pebl deploy" command in the "go" subproject. Then, invoke "pebl deploy" in the "hello" subproject.
Pebl automatically implements zero downtime updates to services, ensuring that there won't be any dropped requests during the transition. Once the deploy is complete, send requests to the cloud runtime using curl to verify that the deployed services are working correctly.
Redis Management
To access the Redis instance in the cloud runtime, you can use the "pebl tunnel" command. This command establishes a tunnel that proxies local traffic to your Redis instances in the cloud.
Use the provided redis-cli command to connect to the proxied port and manage the data in your cloud Redis instance. This allows you to set the same data as in the local Redis instance.
Next Steps
In this guide, we have explored three capabilities of pebl: services, internal services, and Redis. Pebl offers many more features and capabilities that you can explore by referring to the SDK reference and following additional guides on common cloud tasks.
Actionable Advice:
-
Experiment with creating different services using pebl and explore the language-agnostic features it offers. This will allow you to build complex cloud applications without the need for separate configuration files for different runtimes.
-
Use pebl's internal services capability to break down your cloud workloads into smaller, more manageable services. This approach can improve scalability, maintainability, and modularity of your applications.
-
Take advantage of pebl's integration with Redis to add persistence to your applications. Redis provides fast, in-memory data storage and retrieval, making it an ideal solution for caching and other data-intensive tasks.
In conclusion, pebl offers a unique and
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 🐣