-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcc8b14
commit 75055ec
Showing
10 changed files
with
240 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
pico | ||
!pico/ | ||
|
||
build/ | ||
bin/ | ||
|
||
coverage.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
events { | ||
worker_connections 1024; | ||
} | ||
|
||
http { | ||
upstream backend-proxy { | ||
server pico-1:8000 max_fails=1 fail_timeout=1s; | ||
server pico-2:8000 max_fails=1 fail_timeout=1s; | ||
server pico-3:8000 max_fails=1 fail_timeout=1s backup; | ||
} | ||
|
||
server { | ||
listen 8000; | ||
access_log /dev/null; | ||
location / { | ||
proxy_pass http://backend-proxy; | ||
# Retain the original Host header. | ||
proxy_set_header Host $host; | ||
} | ||
} | ||
|
||
upstream backend-upstream { | ||
server pico-1:8001 max_fails=1 fail_timeout=1s; | ||
server pico-2:8001 max_fails=1 fail_timeout=1s; | ||
server pico-3:8001 max_fails=1 fail_timeout=1s backup; | ||
} | ||
|
||
server { | ||
listen 8001; | ||
access_log /dev/null; | ||
location / { | ||
proxy_pass http://backend-upstream; | ||
# Enable WebSockets. | ||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection "Upgrade"; | ||
proxy_set_header Host $host; | ||
} | ||
} | ||
|
||
upstream backend-admin { | ||
server pico-1:8002 max_fails=1 fail_timeout=1s; | ||
server pico-2:8002 max_fails=1 fail_timeout=1s; | ||
server pico-3:8002 max_fails=1 fail_timeout=1s backup; | ||
} | ||
|
||
server { | ||
listen 8002; | ||
access_log /dev/null; | ||
location / { | ||
proxy_pass http://backend-admin; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
cluster: | ||
join: | ||
- pico-1 | ||
- pico-2 | ||
- pico-3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
global: | ||
scrape_interval: 5s | ||
external_labels: | ||
cluster: demo | ||
namespace: demo | ||
|
||
scrape_configs: | ||
- job_name: demo/pico | ||
static_configs: | ||
- targets: ["pico-1:8002"] | ||
labels: | ||
pod: "pico-1" | ||
- targets: ["pico-2:8002"] | ||
labels: | ||
pod: "pico-2" | ||
- targets: ["pico-3:8002"] | ||
labels: | ||
pod: "pico-3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
version: "3.4" | ||
services: | ||
pico-1: | ||
image: pico:latest | ||
command: | ||
- server | ||
- --config.path | ||
- /etc/pico.yaml | ||
- --cluster.node-id-prefix | ||
- pico-1- | ||
hostname: pico-1 | ||
volumes: | ||
- ./config/pico.yaml:/etc/pico.yaml | ||
|
||
pico-2: | ||
image: pico:latest | ||
command: | ||
- server | ||
- --config.path | ||
- /etc/pico.yaml | ||
- --cluster.node-id-prefix | ||
- pico-2- | ||
hostname: pico-2 | ||
volumes: | ||
- ./config/pico.yaml:/etc/pico.yaml | ||
|
||
pico-3: | ||
image: pico:latest | ||
command: | ||
- server | ||
- --config.path | ||
- /etc/pico.yaml | ||
- --cluster.node-id-prefix | ||
- pico-3- | ||
hostname: pico-3 | ||
volumes: | ||
- ./config/pico.yaml:/etc/pico.yaml | ||
|
||
load-balancer: | ||
image: nginx:latest | ||
volumes: | ||
- ./config/nginx.conf:/etc/nginx/nginx.conf:ro | ||
depends_on: | ||
- "pico-1" | ||
- "pico-2" | ||
- "pico-3" | ||
ports: | ||
- 8000:8000 | ||
- 8001:8001 | ||
- 8002:8002 | ||
|
||
prometheus: | ||
image: prom/prometheus:latest | ||
command: | ||
- --config.file=/etc/prometheus/prometheus.yml | ||
- --log.level=error | ||
volumes: | ||
- ./config/prometheus.yaml:/etc/prometheus/prometheus.yml | ||
depends_on: | ||
- "pico-1" | ||
- "pico-2" | ||
- "pico-3" | ||
ports: | ||
- 9090:9090 | ||
|
||
grafana: | ||
image: grafana/grafana:latest | ||
environment: | ||
- GF_LOG_MODE=console | ||
- GF_LOG_LEVEL=critical | ||
ports: | ||
- 3000:3000 | ||
|
||
networks: | ||
pico-network: | ||
driver: bridge | ||
name: pico-network |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Getting Started | ||
|
||
This example uses `docker-compose` to deploy three Pico server nodes behind | ||
a load balancer, then registers endpoints to forward requests to a local HTTP | ||
server. | ||
|
||
## Server | ||
|
||
Start by cloning Pico and building the Pico Docker image: | ||
```shell | ||
git clone [email protected]:andydunstall/pico.git | ||
make pico | ||
make image | ||
``` | ||
|
||
This will build the Pico binary at `bin/pico` and Docker image `pico:latest`. | ||
|
||
Next start the Pico server cluster: | ||
```shell | ||
cd docs/demo | ||
docker compose up | ||
``` | ||
|
||
This creates a cluster of three server nodes and a load balancer that exposes | ||
the Pico ports. It will also start Prometheus and Grafana to inspect the Pico | ||
metrics. The following ports are exposed: | ||
- Pico proxy (`http://localhost:8000`): Forwards proxy requests to registered | ||
endpoints | ||
- Pico upstream (`http://localhost:8001`): Accepts connections from upstream | ||
endpoints | ||
- Pico admin (`http://localhost:8002`): Accepts admin connections for metrics, | ||
the status API, and health checks | ||
- Prometheus (`http://localhost:9090`) | ||
- Grafana (`http://localhost:3000`) | ||
|
||
To verify Pico has started correctly, run `pico status netmap nodes` which | ||
queries the Pico admin API for the set of known Pico nodes. | ||
|
||
## Agent | ||
|
||
The Pico agent is a lightweight proxy that runs alongside your upstream | ||
services. It connects to the Pico server and registers endpoints, then forwards | ||
incoming requests to your services. | ||
|
||
Create a simple HTTP server to forward requests to, such as | ||
`python3 -m http.server 4000` so serve the files in the local directory on port | ||
`4000`. | ||
|
||
Then run the Pico agent and register endpoint `my-endpoint` using: | ||
```shell | ||
pico agent --endpoints my-endpoint/localhost:4000 | ||
``` | ||
|
||
See `pico agent -h` for the available options. | ||
|
||
You can verify the endpoint has connected, again run `pico status netmap nodes` | ||
and you'll see one of the Pico server nodes reporting endpoint `my-endpoint` | ||
has an active connection. | ||
|
||
### Request | ||
|
||
As described above, when sending a request to Pico you can identify the | ||
endpoint ID using either the `Host` header or the `x-pico-agent`. | ||
|
||
Therefore to send a request to the registered endpoint, `my-endpoint`, use: | ||
```shell | ||
# x-pico-endpoint | ||
curl http://localhost:8000 -H "x-pico-endpoint: my-endpoint" | ||
|
||
# Host | ||
curl --connect-to my-endpoint.example.com:8000:localhost:8000 http://my-endpoint.example.com:8000 | ||
``` | ||
|
||
This request will be load balanced among the Pico servers, then forwarded to | ||
the endpoint registered above. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters