Skip to content

Commit 9d4dcda

Browse files
Merge pull request #426 from norman465/squid_proxy
Add squid-proxy transparent HTTP proxy fleet example
2 parents a8d8ad8 + a07e673 commit 9d4dcda

3 files changed

Lines changed: 119 additions & 1 deletion

File tree

serverless-fleets/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Using both the intuitive graphical user interface and the command line, you will
1919
- [Tutorial: Docling](./tutorials/docling/README.md)
2020
- [Tutorial: Batch Inferencing](./tutorials/inferencing/README.md)
2121
- [Tutorial: Monte Carlo Simulation](./tutorials/simulation/README.md)
22+
- [Example: HTTP egress control](#http-egress-control)
2223
- [HowTo](#howto)
2324
- [Troubleshooting](#troubleshooting)
2425

@@ -522,6 +523,13 @@ Download the results from the output COS bucket to `./data/output`
522523
- [Tutorial: Simulation](./tutorials/simulation/README.md)
523524
524525
526+
## Examples
527+
528+
### HTTP egress control
529+
530+
In order to control/monitor/capture HTTP egress traffic on your tasks for which security group based network controls are not sufficient i.e. hostname based controls: you can use after startup hooks to deploy a HTTP proxy on each worker node that intercepts all HTTP traffic of the tasks running on that worker node. An example using squid proxy to allowlist only traffic to a single domain can be found in `run_hook_squid_http_proxy`.
531+
532+
525533
## HowTo
526534
527535
### How to use your own container and image
@@ -551,7 +559,6 @@ Once the push is complete, you can run the fleet by modifying `./run` and replac
551559
- the environment variables, e.g. `--env foo=bar`
552560
553561
554-
555562
### How to access logs
556563
557564
An IBM Cloud Logs instance is being setup and enabled by default during the automated One Time Setup. Each fleet worker will ingest logs to the IBM Cloud Logs instance by default. [Navigating to the UI](https://cloud.ibm.com/docs/cloud-logs?topic=cloud-logs-instance-launch) and use [Using Livetail](https://cloud.ibm.com/docs/cloud-logs?topic=cloud-logs-livetail) or [Filtering log data](https://cloud.ibm.com/docs/cloud-logs?topic=cloud-logs-query-data-filter) to view the logs.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
uuid=$(uuidgen | tr '[:upper:]' '[:lower:]' | awk -F- '{print $1}')
2+
3+
PREHOOK=$(cat <<'OUTER'
4+
#!/usr/bin/env bash
5+
set -Eeuo pipefail
6+
7+
### ===== User-tunable variables =====
8+
9+
export NETWORK_NAME="podman"
10+
export SUBNET_CIDR="10.88.0.0/16"
11+
export GATEWAY_IP="10.88.0.1"
12+
13+
export SQUID_IP="10.88.0.10"
14+
export SQUID_CONTAINER="http-proxy"
15+
export SQUID_IMAGE="docker.io/ubuntu/squid:latest"
16+
export SQUID_HTTP_PORT="3129"
17+
18+
export WORKDIR="$PWD/podman-transparent-proxy-lab"
19+
20+
### ===== Derived variables =====
21+
22+
SQUID_CONF_DIR="/etc/squid"
23+
24+
mkdir -p "$SQUID_CONF_DIR"
25+
26+
echo "==> Checking dependencies"
27+
command -v podman >/dev/null
28+
command -v sudo >/dev/null
29+
command -v iptables >/dev/null
30+
31+
echo "==> Writing squid.conf"
32+
cat > "${SQUID_CONF_DIR}/squid.conf" <<INNER
33+
http_port 3128
34+
http_port 3129 intercept
35+
36+
acl localnet src ${SUBNET_CIDR}
37+
acl allowed_http dstdomain example.com
38+
39+
acl Safe_ports port 80
40+
acl CONNECT method CONNECT
41+
42+
http_access deny !Safe_ports
43+
http_access allow localnet allowed_http
44+
http_access deny all
45+
46+
pid_filename /tmp/squid.pid
47+
coredump_dir /tmp
48+
cache_log /dev/stderr
49+
access_log stdio:/dev/stdout
50+
# cache_dir ufs ${SQUID_CONF_DIR}/cache 100 16 256
51+
52+
cache deny all
53+
via off
54+
forwarded_for delete
55+
request_header_access X-Forwarded-For deny all
56+
request_header_access Via deny all
57+
visible_hostname localhost
58+
59+
INNER
60+
61+
echo "==> Creating Podman network if needed"
62+
if ! podman network exists "${NETWORK_NAME}"; then
63+
podman network create \
64+
--subnet "${SUBNET_CIDR}" \
65+
--gateway "${GATEWAY_IP}" \
66+
"${NETWORK_NAME}"
67+
fi
68+
69+
echo "==> Removing old containers if present"
70+
podman rm -f "${SQUID_CONTAINER}" 2>/dev/null || true
71+
72+
echo "==> Preparing Squid cache and logs"
73+
mkdir -p "${SQUID_CONF_DIR}/cache" "${SQUID_CONF_DIR}/logs"
74+
75+
echo "==> Starting Squid HTTP transparent proxy"
76+
podman run -d \
77+
--name "${SQUID_CONTAINER}" \
78+
--network host \
79+
--entrypoint sh \
80+
-v "${SQUID_CONF_DIR}/:/etc/squid/:Z,rw,rbind" \
81+
"${SQUID_IMAGE}" \
82+
-c 'exec squid -N -f /etc/squid/squid.conf'
83+
84+
echo "==> Enabling IPv4 forwarding on host"
85+
sudo sysctl -w net.ipv4.ip_forward=1 >/dev/null
86+
87+
echo "==> Preparing iptables chain"
88+
sudo iptables -t nat -N SQUID_PROXY 2>/dev/null || true
89+
sudo iptables -t nat -F SQUID_PROXY
90+
91+
sudo iptables -t nat -A SQUID_PROXY -d 127.0.0.0/8 -j RETURN
92+
sudo iptables -t nat -A SQUID_PROXY -d ${SUBNET_CIDR} -j RETURN
93+
sudo iptables -t nat -A SQUID_PROXY -p tcp --dport 80 -j REDIRECT --to-ports 3129
94+
95+
sudo iptables -t nat -A PREROUTING -s ${SUBNET_CIDR} -p tcp -j SQUID_PROXY
96+
OUTER
97+
)
98+
99+
ibmcloud ce fleet create --name "fleet-${uuid}" \
100+
--tasks-state-store fleet-task-store \
101+
--image registry.access.redhat.com/ubi10/ubi-minimal \
102+
--cpu "2" \
103+
--memory "4G" \
104+
--tasks-from-local-file run_hook_squid_http_proxy_commands.jsonl \
105+
--max-scale 2 \
106+
--retrylimit 0 \
107+
--subnetpool-name fleet-subnetpool \
108+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}"
109+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "command":"curl", "args": ["--silent", "--write-out", "%{url} %{http_code}", "--output", "/dev/null", "http://example.com"]}
2+
{ "command":"curl", "args": ["--silent", "--write-out", "%{url} %{http_code}", "--output", "/dev/null", "http://google.com"]}

0 commit comments

Comments
 (0)