Microservices with pebl — a complete and free cloud platform
Hatched by Kelvin
Oct 06, 2023
7 min read
16 views
Microservices with pebl — a complete and free cloud platform
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 YAML files, pebl integrates cloud capabilities directly into your application using language-specific SDKs.
Getting Started
To begin, sign up for a free account at pebl.io. Choose your preferred identity provider and follow the steps. Don't forget to claim your free *.pebl.rocks subdomain, which we'll be using throughout the tutorial.
Next, ensure you have Docker and the pebl CLI installed. If you don't have Docker, visit the Docker website and follow the installation instructions. For the pebl CLI, refer to the setup documentation and download the correct version for your system. Verify the installation by running the 'pebl' command without arguments.
Simple Service
The core building block in pebl is a service, which can be compared to a serverless application in other platforms. Let's create a simple service using Python to understand how pebl works.
Start by creating a project folder, such as ~/work/scratch, and then create a subfolder called 'hello' inside it. In the 'hello' folder, create a file called '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") put your own domain here
The code above defines a basic Flask application with a single handler for the root path. The last line declares that this Flask application should be a service hosted at your *.pebl.rocks endpoint.
To run the service, 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 the local pebl cluster by running 'pebl up' command. Once the cluster is configured, navigate to the 'hello' subfolder and execute 'pebl run' command. You can use 'pebl info' to see the running cluster's information. Test the service by sending a request to your server using 'curl localhost:<port>'.
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.
To deploy your application to the cloud runtime, use the 'pebl deploy' command. Make sure to run 'pebl auth' if you encounter authentication errors. Once the deployment is successful, test the deployed service using 'curl' command with the appropriate endpoint.
Microservices
Microservices architecture is a popular approach to structuring cloud workloads. With pebl, you can easily break down your application into small services using the internal services capability.
Internal services in pebl are similar to regular services but are only exposed to other workloads within the cluster and not configured for external traffic. Let's explore how to create an internal service using Go.
Start by creating a subfolder called 'go' within the 'scratch' folder. Initialize a Go project using 'go mod init' command. Install the pebl package for Go using 'go get github.com/peblcloud/go'.
To create an internal Go service, create a file called 'main.go' in the 'go' folder 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 server object based on the net/http package and pass it to the pebl.InternalService function. Note that the endpoint for internal services can be anything, but it's recommended to use an easy-to-remember scheme.
To run the internal Go service locally, use the 'pebl run' command. You can send requests to the internal endpoint using 'curl localhost:<port>'.
Redis
To support a wider range of applications, pebl provides persistence through Redis. You can incorporate Redis into your application using the pebl SDK.
Update the Go service by incorporating Redis. Install the required Redis package by running 'go get github.com/redis/go-redis/v9'. 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")
}
This code adds Redis functionality to the internal Go service. Run 'go mod tidy' to update the module dependencies. Run 'pebl run' to start the service locally and test it with 'curl' commands.
Proxying
To utilize the internal service, we can proxy requests from the external 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
The code above uses the 'requests' library to send requests to the internal Go service. Update the Dockerfile in the 'hello' folder to include the 'requests' library. Run the application locally and test the '/user' endpoint using 'curl' commands.
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 ensures zero downtime updates during deployment, so there won't be any dropped requests.
Redis Management
To access Redis in the cloud runtime, use the 'pebl tunnel' command to create a tunnel that proxies local traffic to your Redis instances. Use the 'redis-cli' command with the '-p' argument to connect to the proxied port and manage the data in the cloud Redis instance.
Next Steps
This guide has covered the basics of using pebl, including creating services, internal services, and integrating Redis. You can further explore pebl's capabilities by referring to the SDK reference and following additional guides for common cloud tasks.
Contributing to Open-Source Projects: A Beginner's Guide
"The only way to do great work is to love what you do." - Steve Jobs
Contributing to open-source projects on GitHub is a rewarding experience that allows you to collaborate with others and improve your coding skills. Here is a simplified guide for beginners to get started:
- Fork the Project
Visit the project's GitHub page and click on the Fork button to create a copy of the project under your own GitHub account.
- Clone the Forked Repository
Copy the URL of your forked repository and use the 'git clone' command in your terminal to create a local copy of the repository on your machine.
- Install Dependencies
Navigate to the project directory and follow the installation instructions provided in the README.md or INSTALL.md file. Make sure you have all the necessary dependencies installed.
- Run the Project Locally
Refer to the instructions in the README.md file to run the project locally. Make sure the project runs without any errors and familiarize yourself with its functionality.
- Make Your Changes
Create a new branch for your changes using the 'git checkout -b' command. Make the necessary modifications and improvements to the codebase. Use descriptive commit messages to track your changes.
- Create a Pull Request (Optional)
If you wish to contribute your changes back to the original project, go to the Pull Requests section in the original repository. Click on the New Pull Request button and select your forked repository and branch. Provide a clear description of your changes and submit the pull request.
Remember that contributing to open-source projects is a collaborative process, so be open to feedback and work closely with the project maintainers to get your changes merged.
Conclusion
In this article, we explored the pebl cloud platform and its unique approach to Infrastructure as Code. We learned how to create services, deploy them locally and to the cloud runtime, and incorporate Redis and internal services into our applications. Additionally, we provided a simplified guide for beginners to contribute to open-source projects on GitHub.
Actionable Advice:
- Experiment with pebl and explore its capabilities by creating more complex microservices and integrating other cloud services.
- Contribute to open-source
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 🐣