Déploiement d'une application Go sur VPS : guide avancé

Formateur Python en ligne pour débutants

Apprenez Python facilement sans surcharge théorique. Résolvez des tâches pratiques avec vérification automatique et écrivez du code directement dans le navigateur.

Start Course

Introduction : Du développement au serveur de production en Go

\n

Vous avez écrit une application web haute performance en Go (Golang). Compilation en un seul binaire, typage statique, gestion réseau intégrée — tout cela donne une longueur d'avance sur Node.js ou Python. Mais « ça marche sur ma machine » n'est pas un argument pour la production. Le déploiement avancé d'une application Go sur VPS ne se résume pas à copier des fichiers via SSH. C'est la configuration d'un pipeline CI/CD, la conteneurisation, le reverse proxy, la mise à jour automatique des certificats, le monitoring et l'arrêt gracieux (graceful shutdown).

\n

Dans cet article, nous allons détailler un scénario complet prêt pour la production pour une application Golang sur Ubuntu 22.04 LTS. Vous apprendrez à configurer une build Docker, à organiser un déploiement zero-downtime via GitHub Actions, à intégrer Nginx avec SSL et à configurer le monitoring des métriques Prometheus.

\n\n

1. Préparation du VPS et configuration de l'environnement

\n

Supposons que vous ayez un VPS (DigitalOcean, Hetzner, Timeweb) avec Ubuntu 22.04. La première chose à faire est de mettre à jour le système et d'installer un ensemble d'outils de base :

\n
ssh root@your_server_ip\napt update && apt upgrade -y\napt install -y git curl htop net-tools ufw
\n

Créons un utilisateur deploy avec les droits sudo (sans mot de passe, uniquement par clé SSH) :

\n
adduser deploy\nusermod -aG sudo deploy\nmkdir /home/deploy/.ssh\ncp ~/.ssh/authorized_keys /home/deploy/.ssh/\nchown -R deploy:deploy /home/deploy/.ssh\nchmod 700 /home/deploy/.ssh && chmod 600 /home/deploy/.ssh/authorized_keys
\n

Configurons le pare-feu UFW :

\n
ufw allow OpenSSH\nufw allow 80/tcp\nufw allow 443/tcp\nufw enable
\n

Important : Désactivez la connexion root dans /etc/ssh/sshd_config (PermitRootLogin no) et redémarrez SSH (systemctl restart sshd).

\n\n

1.1 Installation de Docker et Docker Compose

\n

Pour une application Go, la conteneurisation est un standard de facto. Installez Docker selon les instructions officielles :

\n
curl -fsSL https://get.docker.com -o get-docker.sh\nsh get-docker.sh\nusermod -aG docker deploy
\n

Vérifiez la version : docker --version. Ensuite, installez Docker Compose v2 (plugin) :

\n
apt install -y docker-compose-plugin
\n\n

2. Pipeline CI/CD avec GitHub Actions et Docker

\n

Un déploiement avancé implique l'automatisation. Nous utiliserons GitHub Actions pour construire l'image et l'envoyer sur le serveur via SSH, puis lancer le nouveau conteneur.

\n

Structure du projet Go :

\n
myapp/\n├── main.go\n├── go.mod\n├── Dockerfile\n├── docker-compose.yml\n└── .github/workflows/deploy.yml
\n

Dockerfile (build multi-étapes) :

\n
FROM golang:1.22-alpine AS builder\nWORKDIR /app\nCOPY go.mod go.sum ./\nRUN go mod download\nCOPY . .\nRUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app/myapp .\n\nFROM alpine:latest\nRUN apk --no-cache add ca-certificates tzdata\nWORKDIR /root/\nCOPY --from=builder /app/myapp .\nEXPOSE 8080\nCMD ["./myapp"]
\n

docker-compose.yml (pour la production) :

\n
version: '3.8'\nservices:\n  app:\n    image: ghcr.io/your_username/myapp:latest\n    restart: always\n    ports:\n      - "127.0.0.1:8080:8080"\n    environment:\n      - DB_URL=postgres://user:pass@db:5432/mydb\n    depends_on:\n      - db\n  db:\n    image: postgres:16-alpine\n    restart: always\n    environment:\n      POSTGRES_USER: user\n      POSTGRES_PASSWORD: pass\n    volumes:\n      - pg_data:/var/lib/postgresql/data\nvolumes:\n  pg_data:
\n

Workflow GitHub Actions (.github/workflows/deploy.yml) :

\n
name: Deploy Go App\non:\n  push:\n    branches: [ main ]\njobs:\n  build-and-deploy:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - name: Login to GitHub Container Registry\n        uses: docker/login-action@v3\n        with:\n          registry: ghcr.io\n                

Blogs

Book Recommendations