Implement a blue/green switch on a single nginx host with a script.
AdvancedScenarioScenarios: deployments, user management and fleet operations
Answer
Situation read → Two app instances (blue on 8081, green on 8082); nginx points at one through an include file.
What I'd do →
active=$(grep -oE '808[12]' /etc/nginx/conf.d/upstream-active.conf)
target=$([[ $active == 8081 ]] && echo 8082 || echo 8081)
deploy_to_port "$target"
wait_for_http "http://127.0.0.1:$target/readyz" 60 || die "new color unhealthy; live traffic untouched"
tmp=$(mktemp /etc/nginx/conf.d/.upstream.XXXXXX)
printf 'upstream app { server 127.0.0.1:%s; }\n' "$target" > "$tmp"
chmod 0644 "$tmp"
mv -f -- "$tmp" /etc/nginx/conf.d/upstream-active.conf
nginx -t && systemctl reload nginx || { echo "nginx config invalid" >&2; exit 1; }
echo "switched $active -> $target; rollback = rerun with previous color"
- The idle color is deployed and tested before any traffic moves.
nginx reloadswaps workers gracefully; existing connections finish on old workers.
Why → Rollback is just pointing back at the old color, which is still running.
Interview tip → Mention database migrations: they must be backward compatible so both colors can run against the same schema.
More on Scenarios: deployments, user management and fleet operations
Write the core of a deploy script with release directories, an atomic symlink switch and rollback.IntermediateDeploy to 10 servers behind an AWS ALB one at a time, removing each from the target group during its deploy. Outline the script.AdvancedAfter a deploy, the health check fails intermittently. Your script must roll back automatically, but only if the failure persists. How?IntermediateOnboard 30 engineers: create accounts and install their SSH keys from their GitHub usernames, idempotently.IntermediateAn engineer leaves the company. Write the offboarding steps for Linux servers.IntermediateYou are asked to audit a server for risky accounts. What does your script check?Intermediate