1.
ssh-keygen -t ed25519 -C "dev@you.com",然后复制公钥:ssh-copy-id user@your-tw-vps-ip。确认可以免密登录:ssh user@your-tw-vps-ip。如使用云商面板,可在创建实例时直接贴入公钥。
2.
sudo apt update && sudo apt upgrade -y(以 Ubuntu 为例)。新增非 root 用户并赋予 sudo:sudo adduser devuser && sudo usermod -aG sudo devuser。配置基本防火墙:sudo ufw allow OpenSSH && sudo ufw enable。
3.
bootstrap-docker.sh,示例内容:#!/bin/bash
apt update
apt install -y apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt update && apt install -y docker-ce docker-ce-cli containerd.io
usermod -aG docker devuser
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose,赋予可执行并运行:chmod +x bootstrap-docker.sh && sudo ./bootstrap-docker.sh。
4.
deploy.sh:#!/bin/bash
cd /home/devuser
git clone git@github.com:you/your-repo.git || (cd your-repo && git pull)
cp your-repo/.env.example your-repo/.env
# 编辑 .env 或用 envsubst 替换机密变量
docker-compose -f your-repo/docker-compose.yml up -d --build。将脚本加入可执行并测试。
5.
/etc/systemd/system/dev-deploy.service,示例核心:[Unit]
Description=Deploy app
After=network.target
[Service]
Type=oneshot
User=devuser
ExecStart=/home/devuser/deploy.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target,然后启用:sudo systemctl daemon-reload && sudo systemctl enable --now dev-deploy.service。用于开机自动拉取并启动容器。
6.
0 3 * * * /home/devuser/backup.sh。备份脚本包含 docker exec db_container pg_dumpall -U postgres > /home/devuser/backups/$(date +%F).sql。使用 certbot 自动续期并重载 nginx:certbot renew --post-hook "systemctl reload nginx",并将 renew 加入 cron 或 timer。
7.
- hosts: tw_servers
become: true
tasks:
- name: update apt
apt: update_cache=yes
- name: copy deploy script
copy: src=deploy.sh dest=/home/devuser/deploy.sh mode=0755
- name: run deploy
command: /home/devuser/deploy.sh。执行:ansible-playbook -i inventory deploy.yml。
8.
docker-compose logs -f;systemd 日志 journalctl -u dev-deploy.service -b;SSH 连接问题检查 ss -tulpen | grep ssh。将关键日志轮转并定期上传到远端存储或对象存储,防止磁盘耗尽。
9.
答:主要差异在网络延迟与镜像源,建议把 apt、Docker 镜像源切换到亚太或台湾附近的镜像,加速包与镜像拉取,另外注意云商防火墙规则与可用区特色,脚本中应加入重试与超时设置以应对不稳定网络。
10.
答:不要把明文写入脚本或 git,使用环境变量注入或使用云提供的密钥管理服务(如 KMS、Vault),在部署时用 CI/CD 把机密注入目标主机的 .env 文件,并限制文件权限为 600。
11.
答:在本地或测试实例先跑完全流程,加入 idempotent(幂等)设计并对关键步骤加检查点(如校验容器是否启动、健康检查接口返回 200),配合 CI 做回归测试与模拟断网重试场景。