lab4 minimum moved to lab5

This commit is contained in:
2026-11-22 15:47:13 +03:00
parent 276dbdad8a
commit c10bf08feb
29 changed files with 2772 additions and 3 deletions
+92
View File
@@ -0,0 +1,92 @@
# Client -> HAProxy -> api x N. Replicas are discovered through Docker DNS and checked actively.
#
# Timing contract (keep in sync with compose.yaml and app settings):
# check period inter 1s, timeout check 1s
# marked DOWN after fall 2 failed checks -> hung/draining replica leaves the pool in <= 3 s
# marked UP after rise 2 good checks -> new replica gets traffic ~2 s after it is ready
# DNS refresh every 1 s (hold valid 1s); a removed container disappears within ~1-2 s
# crashed process connect refused -> request redispatched to another replica at once
# app DRAIN_SECONDS=4 > 3 s, so a replica receiving SIGTERM is out of rotation before it closes its socket.
global
log stdout format raw local0 info
maxconn 2048
# Runtime API for evidence (scripts/lb-status.sh); loopback inside the container only.
stats socket ipv4@127.0.0.1:9999 level operator
resolvers docker
nameserver dns 127.0.0.11:53
accepted_payload_size 8192
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold valid 1s
hold other 1s
hold refused 1s
hold nx 1s
hold timeout 1s
hold obsolete 0s
defaults
mode http
log global
option httplog
option dontlognull
option forwardfor
timeout connect 1s
timeout client 30s
timeout server 10s
# Body arrival for a 10 MiB attachment; JSON posts finish well under this.
timeout http-request 30s
timeout http-keep-alive 5s
timeout queue 2s
timeout check 1s
# A request whose connection to a replica failed is sent to another replica.
retries 2
option redispatch 1
frontend public
bind :8080
monitor-uri /lb/health
# Attachment uploads are raw bytes, up to 10 MiB. A larger Content-Length is
# rejected here, before the body is read or an API replica is contacted.
# The body matches the API error envelope; request_id echoes a safe client
# X-Request-Id and otherwise stays "lb", like the other gateway errors.
acl upload_too_large req.hdr_val(content-length) gt 10485760
acl safe_request_id req.hdr(X-Request-Id) -m reg ^[A-Za-z0-9._:-]{1,128}$
http-request set-var(txn.reqid) req.hdr(X-Request-Id) if upload_too_large safe_request_id
http-request set-var(txn.reqid) str(lb) if upload_too_large !safe_request_id
http-request return status 413 content-type application/json lf-string "{\"error\":{\"code\":\"payload_too_large\",\"message\":\"Файл больше 10 МиБ\",\"request_id\":\"%[var(txn.reqid)]\"}}" hdr X-Request-Id %[var(txn.reqid)] hdr X-Instance-Id %[str(lb)] if upload_too_large
# Message POST carries a mandatory Idempotency-Key, so replaying it on another replica is safe.
# Other POSTs (users, chats, sessions) are retried only when the connection was never established.
acl message_post method POST
acl message_post_path path_reg ^/api/v1/chats/[^/]+/messages$
http-request disable-l7-retry if message_post !message_post_path
default_backend api
backend api
# Round robin, no stickiness: any replica serves any session from PostgreSQL.
# Attachment PUT to S3 can outlast the default 10 s server timeout under concurrent load.
acl attachment_upload method POST
acl attachment_upload_path path_reg ^/api/v1/chats/[^/]+/attachments$
http-request set-timeout server 30s if attachment_upload attachment_upload_path
balance roundrobin
option httpchk
http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host lb
http-check expect status 200
retry-on conn-failure empty-response response-timeout
http-error status 503 content-type application/json string "{\"error\":{\"code\":\"unavailable\",\"message\":\"No API replica is available\",\"request_id\":\"lb\"}}"
http-error status 502 content-type application/json string "{\"error\":{\"code\":\"internal_error\",\"message\":\"Bad gateway\",\"request_id\":\"lb\"}}"
http-error status 504 content-type application/json string "{\"error\":{\"code\":\"internal_error\",\"message\":\"Gateway timeout\",\"request_id\":\"lb\"}}"
default-server check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr none
server-template api 1-10 api:8080
# Read-only stats for evidence; not published, reachable only inside the container.
frontend stats
bind 127.0.0.1:8404
stats enable
stats uri /stats
stats refresh 5s
http-request set-log-level silent
+116
View File
@@ -0,0 +1,116 @@
#!/bin/sh
# Idempotent private-bucket bootstrap for MinIO. Root credentials are read from
# files and are not printed. A second run leaves the bucket, policy and API user in place.
set -eu
bucket=${S3_BUCKET:?S3_BUCKET is required}
region=${S3_REGION:-us-east-1}
endpoint=${MINIO_ENDPOINT:-http://minio:9000}
policy_name=attachments-api
case "$bucket" in
[a-z0-9][a-z0-9.-]*[a-z0-9]) ;;
*)
echo "S3_BUCKET must be a DNS-style name" >&2
exit 1
;;
esac
read_secret() {
tr -d '\r\n' < "$1"
}
root_user=$(read_secret /run/secrets/minio_root_user)
root_password=$(read_secret /run/secrets/minio_root_password)
access_key=$(read_secret /run/secrets/s3_access_key_id)
secret_key=$(read_secret /run/secrets/s3_secret_access_key)
if [ -z "$root_user" ] || [ -z "$root_password" ] || [ -z "$access_key" ] || [ -z "$secret_key" ]; then
echo "MinIO bootstrap secrets are empty" >&2
exit 1
fi
i=0
until mc alias set local "$endpoint" "$root_user" "$root_password" >/dev/null; do
i=$((i + 1))
if [ "$i" -ge 30 ]; then
echo "MinIO is not accepting the root credentials" >&2
exit 1
fi
sleep 1
done
# A new MinIO bucket has no anonymous policy. Set none explicitly so a later
# public policy cannot survive a repeated bootstrap.
mc mb --ignore-existing --region "$region" "local/${bucket}" >/dev/null
mc anonymous set none "local/${bucket}" >/dev/null
anon=$(mc anonymous get "local/${bucket}")
case "$anon" in
*private*) ;;
*)
echo "bucket ${bucket} is not private" >&2
exit 1
;;
esac
probe_key=".bootstrap/private-probe"
printf 'probe' | mc pipe "local/${bucket}/${probe_key}" >/dev/null
code=$(curl -s -o /dev/null -w '%{http_code}' "${endpoint}/${bucket}/${probe_key}")
mc rm --force "local/${bucket}/${probe_key}" >/dev/null
if [ "$code" != "403" ]; then
echo "unauthenticated GET returned ${code}, expected 403" >&2
exit 1
fi
policy_file=/tmp/attachments-api-policy.json
cat > "$policy_file" <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetBucketLocation", "s3:ListBucket", "s3:ListBucketMultipartUploads"],
"Resource": ["arn:aws:s3:::${bucket}"]
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts"
],
"Resource": ["arn:aws:s3:::${bucket}/*"]
}
]
}
EOF
# Recreate the canned policy on every run so added actions (multipart list) apply.
if mc admin user info local "$access_key" >/dev/null 2>&1; then
mc admin policy detach local "$policy_name" --user "$access_key" >/dev/null 2>&1 || true
fi
mc admin policy remove local "$policy_name" >/dev/null 2>&1 || \
mc admin policy rm local "$policy_name" >/dev/null 2>&1 || true
mc admin policy create local "$policy_name" "$policy_file" >/dev/null
if ! mc admin user info local "$access_key" >/dev/null 2>&1; then
mc admin user add local "$access_key" "$secret_key" >/dev/null
fi
mc admin policy attach local "$policy_name" --user "$access_key" >/dev/null
mc alias set api "$endpoint" "$access_key" "$secret_key" >/dev/null
api_key=".bootstrap/api-probe"
printf 'ok' | mc pipe "api/${bucket}/${api_key}" >/dev/null
mc cat "api/${bucket}/${api_key}" >/dev/null
mc rm --force "api/${bucket}/${api_key}" >/dev/null
if mc ls "api/not-this-bucket" >/dev/null 2>&1; then
echo "API credentials can list a different bucket" >&2
exit 1
fi
echo "bucket ${bucket} is private"