Subham9126 commited on
Commit
0122e2f
Β·
verified Β·
1 Parent(s): 453ef85

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +30 -0
  2. init-topics.sh +38 -0
  3. nginx.conf +115 -0
  4. redpanda.yaml +66 -0
  5. start.sh +71 -0
Dockerfile ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM docker.redpanda.com/redpandadata/redpanda:v23.3.11 AS redpanda
2
+ FROM nginx:1.25-alpine AS final
3
+
4
+ # ── Install Redpanda into nginx image ─────────────────────────────────────────
5
+ COPY --from=redpanda /usr/bin/redpanda /usr/bin/redpanda
6
+ COPY --from=redpanda /usr/bin/rpk /usr/bin/rpk
7
+ COPY --from=redpanda /usr/lib/redpanda /usr/lib/redpanda
8
+ COPY --from=redpanda /etc/redpanda /etc/redpanda
9
+
10
+ # ── Install runtime deps ───────────────────────────────────────────────────────
11
+ RUN apk add --no-cache bash curl netcat-openbsd gettext
12
+
13
+ # ── Copy all config files from root ───────────────────────────────────────────
14
+ COPY redpanda.yaml /etc/redpanda/redpanda.yaml
15
+ COPY nginx.conf /etc/nginx/nginx.conf
16
+ COPY start.sh /start.sh
17
+ COPY init-topics.sh /init-topics.sh
18
+
19
+ RUN chmod +x /start.sh /init-topics.sh
20
+
21
+ # ── Data dir ──────────────────────────────────────────────────────────────────
22
+ RUN mkdir -p /var/lib/redpanda/data && \
23
+ chown -R 1000:1000 /var/lib/redpanda /etc/redpanda /start.sh /init-topics.sh
24
+
25
+ # HF Spaces only exposes 7860 β€” everything goes through nginx
26
+ EXPOSE 7860
27
+
28
+ USER 1000
29
+
30
+ ENTRYPOINT ["/start.sh"]
init-topics.sh ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+
4
+ RPK="rpk --brokers 127.0.0.1:9092"
5
+
6
+ # ── Edit this array to define your topics ────────────────────────────────────
7
+ # Format: "topic-name:partitions"
8
+ TOPICS=(
9
+ "events:4"
10
+ "raw-data:4"
11
+ "processed:4"
12
+ "dead-letter:1"
13
+ )
14
+
15
+ RETENTION_MS=3600000 # 1 hour β€” consume and store yourself
16
+ RETENTION_BYTES=536870912 # 512MB per partition max
17
+
18
+ for entry in "${TOPICS[@]}"; do
19
+ IFS=':' read -r name partitions <<< "$entry"
20
+
21
+ if $RPK topic list 2>/dev/null | grep -q "^${name}"; then
22
+ echo " ↩ ${name} already exists"
23
+ continue
24
+ fi
25
+
26
+ $RPK topic create "$name" \
27
+ --partitions "$partitions" \
28
+ --replicas 1 \
29
+ --topic-config "retention.ms=${RETENTION_MS}" \
30
+ --topic-config "retention.bytes=${RETENTION_BYTES}" \
31
+ --topic-config "cleanup.policy=delete" \
32
+ --topic-config "compression.type=snappy"
33
+
34
+ echo " βœ“ ${name} (${partitions} partitions)"
35
+ done
36
+
37
+ echo "Topics ready:"
38
+ $RPK topic list
nginx.conf ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ worker_processes auto;
2
+ error_log /dev/stderr warn;
3
+ pid /tmp/nginx.pid;
4
+
5
+ events {
6
+ worker_connections 1024;
7
+ }
8
+
9
+ http {
10
+ access_log /dev/stdout;
11
+ client_max_body_size 100M;
12
+
13
+ # ── Status dashboard at / ──────────────────────────────────────────────────
14
+ server {
15
+ listen 7860;
16
+
17
+ # ── Root β†’ HTML dashboard ──────────────────────────────────────────────
18
+ location = / {
19
+ default_type text/html;
20
+ return 200 '<!DOCTYPE html>
21
+ <html>
22
+ <head>
23
+ <title>Redpanda</title>
24
+ <meta charset="utf-8">
25
+ <meta http-equiv="refresh" content="15">
26
+ <style>
27
+ *{box-sizing:border-box;margin:0;padding:0}
28
+ body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;background:#0a0a0a;color:#e5e5e5;padding:40px 32px}
29
+ h1{font-size:1.9rem;font-weight:700;margin-bottom:6px}
30
+ .sub{color:#666;margin-bottom:28px;font-size:.95rem}
31
+ .grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:14px;margin-bottom:28px}
32
+ .card{background:#141414;border:1px solid #242424;border-radius:10px;padding:18px}
33
+ .card h3{font-size:.7rem;text-transform:uppercase;letter-spacing:.08em;color:#555;margin-bottom:10px}
34
+ .card .val{font-size:1.05rem;font-weight:600;color:#fff;font-family:monospace}
35
+ .card .desc{font-size:.78rem;color:#555;margin-top:4px}
36
+ table{width:100%;border-collapse:collapse;background:#141414;border:1px solid #242424;border-radius:10px;overflow:hidden;margin-bottom:28px}
37
+ th{text-align:left;padding:12px 16px;font-size:.75rem;text-transform:uppercase;letter-spacing:.07em;color:#555;border-bottom:1px solid #242424}
38
+ td{padding:11px 16px;font-size:.88rem;border-bottom:1px solid #1a1a1a}
39
+ tr:last-child td{border-bottom:none}
40
+ code{background:#1e1e1e;padding:2px 8px;border-radius:5px;font-size:.82rem;color:#a78bfa;font-family:monospace}
41
+ .badge{display:inline-block;padding:2px 10px;border-radius:999px;font-size:.75rem;font-weight:600;background:#22c55e18;color:#22c55e;border:1px solid #22c55e44}
42
+ </style>
43
+ </head>
44
+ <body>
45
+ <h1>πŸ”΄ Redpanda Broker</h1>
46
+ <p class="sub">All services accessible via path-based routing on this port</p>
47
+
48
+ <div class="grid">
49
+ <div class="card"><h3>Kafka REST Proxy</h3><div class="val">/kafka/</div><div class="desc">Produce &amp; consume over HTTP</div></div>
50
+ <div class="card"><h3>Admin API</h3><div class="val">/admin/</div><div class="desc">Cluster management &amp; metrics</div></div>
51
+ <div class="card"><h3>Schema Registry</h3><div class="val">/schema/</div><div class="desc">Avro / Protobuf schemas</div></div>
52
+ <div class="card"><h3>Health</h3><div class="val">/health</div><div class="desc">Broker ready check</div></div>
53
+ </div>
54
+
55
+ <table>
56
+ <tr><th>Action</th><th>Method</th><th>Path</th></tr>
57
+ <tr><td>List topics</td><td><code>GET</code></td><td><code>/admin/v1/topics</code></td></tr>
58
+ <tr><td>Produce messages</td><td><code>POST</code></td><td><code>/kafka/topics/{topic}</code></td></tr>
59
+ <tr><td>Create consumer</td><td><code>POST</code></td><td><code>/kafka/consumers/{group}</code></td></tr>
60
+ <tr><td>Subscribe consumer</td><td><code>POST</code></td><td><code>/kafka/consumers/{group}/instances/{id}/subscription</code></td></tr>
61
+ <tr><td>Consume records</td><td><code>GET</code></td><td><code>/kafka/consumers/{group}/instances/{id}/records</code></td></tr>
62
+ <tr><td>List schemas</td><td><code>GET</code></td><td><code>/schema/subjects</code></td></tr>
63
+ <tr><td>Cluster health</td><td><code>GET</code></td><td><code>/health</code></td></tr>
64
+ </table>
65
+ </body>
66
+ </html>';
67
+ }
68
+
69
+ # ── /health β†’ Redpanda ready check ────────────────────────────────────
70
+ location = /health {
71
+ proxy_pass http://127.0.0.1:9644/v1/status/ready;
72
+ proxy_set_header Host $host;
73
+ }
74
+
75
+ # ── /kafka/ β†’ Pandaproxy HTTP REST (port 8082) ────────────────────────
76
+ location /kafka/ {
77
+ rewrite ^/kafka/(.*) /$1 break;
78
+ proxy_pass http://127.0.0.1:8082;
79
+ proxy_set_header Host $host;
80
+ proxy_set_header X-Real-IP $remote_addr;
81
+ proxy_read_timeout 120s;
82
+ proxy_buffering off;
83
+ }
84
+
85
+ # ── /admin/ β†’ Redpanda Admin API (port 9644) ──────────────────────────
86
+ location /admin/ {
87
+ rewrite ^/admin/(.*) /$1 break;
88
+ proxy_pass http://127.0.0.1:9644;
89
+ proxy_set_header Host $host;
90
+ proxy_set_header X-Real-IP $remote_addr;
91
+ proxy_read_timeout 120s;
92
+ }
93
+
94
+ # ── /schema/ β†’ Schema Registry (port 8081) ────────────────────────────
95
+ location /schema/ {
96
+ rewrite ^/schema/(.*) /$1 break;
97
+ proxy_pass http://127.0.0.1:8081;
98
+ proxy_set_header Host $host;
99
+ proxy_set_header X-Real-IP $remote_addr;
100
+ proxy_read_timeout 60s;
101
+ }
102
+ }
103
+ }
104
+
105
+ # ── TCP stream proxy for native Kafka protocol (port 9092) ────────────────────
106
+ # NOTE: HF Spaces only exposes port 7860 via HTTPS externally.
107
+ # The Kafka binary protocol (TCP) requires a direct TCP connection.
108
+ # Use the HTTP proxy paths above for external access.
109
+ # For same-network (another HF Space or internal service), connect to :9092 directly.
110
+ stream {
111
+ server {
112
+ listen 7861;
113
+ proxy_pass 127.0.0.1:9092;
114
+ }
115
+ }
redpanda.yaml ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ redpanda:
2
+ node_id: 0
3
+ empty_seed_starts_cluster: true
4
+ data_directory: /var/lib/redpanda/data
5
+
6
+ kafka_api:
7
+ - address: 0.0.0.0
8
+ port: 9092
9
+ name: internal
10
+
11
+ advertised_kafka_api:
12
+ - address: 0.0.0.0
13
+ port: 9092
14
+ name: internal
15
+
16
+ rpc_server:
17
+ address: 0.0.0.0
18
+ port: 33145
19
+
20
+ admin:
21
+ - address: 0.0.0.0
22
+ port: 9644
23
+
24
+ # ── HF Spaces resource limits ───────────────────────────────────────────────
25
+ developer_mode: true
26
+ overprovisioned: true
27
+
28
+ # ── Retention: short since you store data yourself ──────────────────────────
29
+ log_retention_ms: 3600000 # 1 hour
30
+ log_retention_bytes: 536870912 # 512MB per partition
31
+ log_segment_size: 67108864 # 64MB segments
32
+ log_cleanup_policy: delete
33
+
34
+ rpk:
35
+ kafka_api:
36
+ brokers:
37
+ - 127.0.0.1:9092
38
+ admin_api:
39
+ addresses:
40
+ - 127.0.0.1:9644
41
+
42
+ pandaproxy:
43
+ pandaproxy_api:
44
+ - address: 0.0.0.0
45
+ port: 8082
46
+ name: internal
47
+ advertised_pandaproxy_api:
48
+ - address: 0.0.0.0
49
+ port: 8082
50
+ name: internal
51
+
52
+ pandaproxy_client:
53
+ brokers:
54
+ - address: 127.0.0.1
55
+ port: 9092
56
+
57
+ schema_registry:
58
+ schema_registry_api:
59
+ - address: 0.0.0.0
60
+ port: 8081
61
+ name: internal
62
+
63
+ schema_registry_client:
64
+ brokers:
65
+ - address: 127.0.0.1
66
+ port: 9092
start.sh ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+
4
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
5
+ echo " Redpanda β†’ HF Spaces"
6
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
7
+
8
+ # ── 1. Start Redpanda ─────────────────────────────────────────────────────────
9
+ echo "[1/4] Starting Redpanda..."
10
+ /usr/bin/redpanda start \
11
+ --overprovisioned \
12
+ --smp 1 \
13
+ --memory 512M \
14
+ --reserve-memory 0M \
15
+ --node-id 0 \
16
+ --check=false \
17
+ --kafka-addr PLAINTEXT://0.0.0.0:9092 \
18
+ --advertise-kafka-addr PLAINTEXT://0.0.0.0:9092 \
19
+ --pandaproxy-addr 0.0.0.0:8082 \
20
+ --advertise-pandaproxy-addr 0.0.0.0:8082 \
21
+ --schema-registry-addr 0.0.0.0:8081 \
22
+ --rpc-addr 0.0.0.0:33145 \
23
+ --advertise-rpc-addr 0.0.0.0:33145 \
24
+ --mode dev-container &
25
+
26
+ REDPANDA_PID=$!
27
+
28
+ # ── 2. Wait for broker ready ──────────────────────────────────────────────────
29
+ echo "[2/4] Waiting for Redpanda..."
30
+ ATTEMPTS=0
31
+ until curl -sf http://127.0.0.1:9644/v1/status/ready > /dev/null 2>&1; do
32
+ ATTEMPTS=$((ATTEMPTS+1))
33
+ if [ $ATTEMPTS -gt 60 ]; then
34
+ echo "ERROR: Redpanda failed to start after 120s"
35
+ exit 1
36
+ fi
37
+ echo " ... waiting (${ATTEMPTS}/60)"
38
+ sleep 2
39
+ done
40
+ echo " βœ“ Redpanda ready"
41
+
42
+ # ── 3. Create topics ──────────────────────────────────────────────────────────
43
+ echo "[3/4] Initializing topics..."
44
+ /init-topics.sh
45
+
46
+ # ── 4. Start nginx (routes everything through port 7860) ─────────────────────
47
+ echo "[4/4] Starting nginx on :7860..."
48
+ nginx -g "daemon off;" &
49
+ NGINX_PID=$!
50
+
51
+ echo ""
52
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
53
+ echo " βœ… All services up on port 7860"
54
+ echo ""
55
+ echo " GET /health β†’ broker status"
56
+ echo " * /kafka/... β†’ HTTP REST proxy"
57
+ echo " * /admin/... β†’ admin API"
58
+ echo " * /schema/... β†’ schema registry"
59
+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
60
+
61
+ # ── Trap shutdown signals ─────────────────────────────────────────────────────
62
+ cleanup() {
63
+ echo "Shutting down..."
64
+ kill $NGINX_PID 2>/dev/null || true
65
+ kill $REDPANDA_PID 2>/dev/null || true
66
+ exit 0
67
+ }
68
+ trap cleanup SIGTERM SIGINT
69
+
70
+ # ── Keep alive ────────────────────────────────────────────────────────────────
71
+ wait $REDPANDA_PID