Deploy WordPress with Traefik Load Balancer on Docker Swarm Cluster using Docker Stack

Deploy WordPress dengan Traefik Load Balancer pada Docker Swarm Cluster menggunakan Docker Stack

Pastikan sudah menginstall docker swarm cluster. Cek disini

Menggunakan docker image: WordPress 6 php8.1-fpm, Nginx 1.22, MariaDB 10.10, Traefik 2.9

Deploy Docker Stack

  • Clone repository dari github
git clone https://github.com/sideka-cloud/docker.git
cd docker/docker-swarm-wordpress-with-traefik
  • Ganti domain ‘sys-ops.hore.host’ menjadi nama domain sesuai keinginan Anda pada file docker-stack.yml
sed -i -e 's/sys-ops.hore.host/yourdomain.com/g' ./docker-stack.yml
  • Ganti default user dan password pada wordpress dan database
version: "3.7"
services:
  mysql:
....... 
   environment:
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
        MYSQL_ROOT_PASSWORD: wordpressroot
....... 
  wordpress:
    environment:
        WORDPRESS_DB_HOST: mysql
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
        WORDPRESS_DB_NAME: wordpress
        WORDPRESS_TABLE_PREFIX: wp_
        # WORDPRESS_DEBUG: 1
....... 
  • Buat folder untuk menyimpan data wordpress dan database
mkdir -p /data/{wp,mysql,letsencrypt}_data
  • Buat docker network dengan driver overlay
docker network create --driver overlay --scope swarm nw-web
docker network create --driver overlay --scope swarm nw-backend
  • Deploy docker stack
  • Akses wordpress dengan port 80/443 dan dasboard traefik dengan port 8080
docker stack deploy -c docker-stack.yml my_wordpress
  • Cek status docker service, pastikan semua service sudah running dan replicated 1/1
docker service ls
---<output>---
ID             NAME                     MODE         REPLICAS   IMAGE                           PORTS
hjjyl67lu2w9   my_wordpress_mysql       replicated   1/1        mariadb:10.10
t536ambkk352   my_wordpress_nginx       replicated   1/1        sysopsid/nginx:1.22-alpine
vqe1qpj0hwee   my_wordpress_traefik     replicated   1/1        traefik:2.9
5fegxu7fvrpc   my_wordpress_wordpress   replicated   1/1        wordpress:6-php8.1-fpm-alpine
docker stack ps my_wordpress
---<output>---
ID             NAME                       IMAGE                           NODE      DESIRED STATE   CURRENT STATE          ERROR     PORTS
qcrv28kcz337   my_wordpress_mysql.1       mariadb:10.10                   Docker    Running         Running 3 minutes ago
r1vdabawj94y   my_wordpress_nginx.1       sysopsid/nginx:1.22-alpine      Docker    Running         Running 3 minutes ago
0qxx0v22h8wi   my_wordpress_traefik.1     traefik:2.9                     Docker    Running         Running 3 minutes ago            *:8080->8080/tcp,*:8080->8080/tcp,*:443->443/tcp,*:443->443/tcp,*:80->80/tcp,*:80->80/tcp
q4pzh65gcyle   my_wordpress_wordpress.1   wordpress:6-php8.1-fpm-alpine   Docker    Running         Running 3 minutes ago
  • Cek log nginx
docker service logs -f my_wordpress_nginx
---<output>---
my_wordpress_nginx.1.ulcou8q6grmx@Docker    | 116.206.14.31 - - [24/Mar/2023:15:48:20 +0000] "POST /wp-admin/install.php?step=1 HTTP/1.1" 200 7103 "https://sys-ops.hore.host/wp-admin/install.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
my_wordpress_nginx.1.ulcou8q6grmx@Docker    | 116.206.14.31 - - [24/Mar/2023:15:48:21 +0000] "GET /wp-admin/js/password-strength-meter.min.js?ver=6.1.1 HTTP/1.1" 200 1123 "https://sys-ops.hore.host/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
my_wordpress_nginx.1.ulcou8q6grmx@Docker    | 116.206.14.31 - - [24/Mar/2023:15:48:21 +0000] "GET /wp-includes/js/dist/vendor/wp-polyfill.min.js?ver=3.15.0 HTTP/1.1" 200 17823 "https://sys-ops.hore.host/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
my_wordpress_nginx.1.ulcou8q6grmx@Docker    | 116.206.14.31 - - [24/Mar/2023:15:48:21 +0000] "GET /wp-admin/js/user-profile.min.js?ver=6.1.1 HTTP/1.1" 200 6187 "https://sys-ops.hore.host/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
  • Akses wordpress dengan domain yang sebelumnya sudah di sesuaikan pada file docker-stack.yml.
  • Url: https://sys-ops.hore.host – free ssl dari Let’s Encrypt sudah aktif
  • Akses dashboard Traefik web ui dengan url: http://sys-ops.hore.host:8080

Scaling Docker Service

  • Scale out/in docker service
docker service scale my_wordpress_nginx=3
---<output>---
my_wordpress_nginx scaled to 3
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
--------------------------------------------------------------------
docker service scale my_wordpress_wordpress=3
---<output>---
my_wordpress_wordpress scaled to 3
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
  • Cek status docker service
docker service ls
---<output>---
ID             NAME                     MODE         REPLICAS   IMAGE                           PORTS
hjjyl67lu2w9   my_wordpress_mysql       replicated   1/1        mariadb:10.10
t536ambkk352   my_wordpress_nginx       replicated   3/3        sysopsid/nginx:1.22-alpine
vqe1qpj0hwee   my_wordpress_traefik     replicated   1/1        traefik:2.9
5fegxu7fvrpc   my_wordpress_wordpress   replicated   3/3        wordpress:6-php8.1-fpm-alpine

SSL Certificate

  • Untuk mendapatkan free ssl dari Let’s Encrypt, gunakan Cloudflare API Token variable “CF_DNS_API_TOKEN” pada file docker-stack.yml
  • Jika ingin menggunakan self sign certificate bisa hapus tanda komentar (#) pada baris berikut
#- "--certificatesResolvers.myCloudflare.acme.caServer=https://acme-staging-v02.api.letsencrypt.org/directory"

PHP

  • File php.ini untuk wordpress di mount di folder docker/docker-swarm-wordpress-with-traefik/apps/wordpress/php.ini

Volume

  • Data file wordpress dan database di simpan di local storage pada folder:
  • /data/wp_data : berisi file wordpress
  • /data/mysql_data : berisi file database
  • /data/letsencrypt_data : berisi file certificate

  • Source code
https://github.com/sideka-cloud/docker/tree/main/docker-swarm-wordpress-with-traefik

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.