|
| 1 | +# Run with Docker Compose |
| 2 | + |
| 3 | +For more information about using Docker, see the [Docker Docs](https://docs.docker.com/). Make |
| 4 | +sure that you are using the latest version of Docker. The ones |
| 5 | +provided via `apt` and `yum` may be outdated and cause errors. |
| 6 | + |
| 7 | +We gather [Telemetry data] in the Percona packages and Docker images. |
| 8 | + |
| 9 | +--8<--- "get-help-snip.md" |
| 10 | + |
| 11 | +This guide shows you how to deploy a three-node Percona XtraDB Cluster 8.4 using Docker Compose. You generate SSL certificates on the first node and copy them to the other two nodes to enable secure communication. |
| 12 | + |
| 13 | +!!! note |
| 14 | + |
| 15 | + This setup is for testing and development purposes only. Do not use it in production without configuring proper storage, security, backup, and monitoring systems. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +* Docker and Docker Compose installed |
| 20 | + |
| 21 | +* At least 3 GB of memory per container |
| 22 | + |
| 23 | +* Familiarity with Docker volumes and networks |
| 24 | + |
| 25 | +## Directory Structure |
| 26 | + |
| 27 | +You need to create a separate directory structure to organize your configuration, certificate files, and Docker Compose setup. This keeps your deployment clean and easy to manage. |
| 28 | + |
| 29 | +Run the following commands to create the directory structure: |
| 30 | + |
| 31 | +```{.bash data-prompt="$"} |
| 32 | +$ mkdir -p pxc-cluster/{certs,conf.d,init} |
| 33 | +$ cd pxc-cluster |
| 34 | +``` |
| 35 | + |
| 36 | +After running these commands, your working directory (pxc-cluster/) will contain: |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +This structure helps manage configuration files, TLS/SSL certificates, and setup scripts, ensuring a tidy and easy-to-manage deployment. |
| 41 | +{.power-number} |
| 42 | + |
| 43 | +1. Create conf.d/custom.cnf with minimal SSL settings: |
| 44 | + |
| 45 | + ```ini |
| 46 | + [mysqld] |
| 47 | + ssl-ca=/etc/mysql/certs/ca.pem |
| 48 | + ssl-cert=/etc/mysql/certs/server-cert.pem |
| 49 | + ssl-key=/etc/mysql/certs/server-key.pem |
| 50 | + ``` |
| 51 | + |
| 52 | +2. Create a file named `.env` in the directory root: |
| 53 | + |
| 54 | + ```ini |
| 55 | + MYSQL_ROOT_PASSWORD=rootpass |
| 56 | + XTRABACKUP_PASSWORD=xbpass |
| 57 | + ``` |
| 58 | + |
| 59 | + ⚠️ Security Note: Add .env to your .gitignore file to prevent committing secrets to version control. |
| 60 | + |
| 61 | +3. Create the SSL Certificate Script. Save the following script as init/create-ssl-certs.sh: |
| 62 | + |
| 63 | + ```ini |
| 64 | + #!/bin/bash |
| 65 | + set -e |
| 66 | + |
| 67 | + CERT_DIR=./certs |
| 68 | + mkdir -p "$CERT_DIR" |
| 69 | + cd "$CERT_DIR" |
| 70 | + |
| 71 | + openssl genrsa 2048 > ca-key.pem |
| 72 | + openssl req -new -x509 -nodes -days 3650 \ |
| 73 | + -key ca-key.pem \ |
| 74 | + -subj "/C=XX/ST=State/L=City/O=Organization/CN=RootCA" \ |
| 75 | + -out ca.pem |
| 76 | + |
| 77 | + openssl req -newkey rsa:2048 -days 3650 -nodes \ |
| 78 | + -keyout server-key.pem \ |
| 79 | + -subj "/C=XX/ST=State/L=City/O=Organization/CN=pxc-node" \ |
| 80 | + -out server-req.pem |
| 81 | + |
| 82 | + openssl rsa -in server-key.pem -out server-key.pem |
| 83 | + openssl x509 -req -in server-req.pem -days 3650 \ |
| 84 | + -CA ca.pem -CAkey ca-key.pem -set_serial 01 \ |
| 85 | + -out server-cert.pem |
| 86 | + |
| 87 | + |
| 88 | + chmod 600 *.pem |
| 89 | + ``` |
| 90 | + |
| 91 | +4. Make the script executable: |
| 92 | + |
| 93 | + ```{.bash data-prompt="$"} |
| 94 | + $ chmod +x init/create-ssl-certs.sh |
| 95 | + ``` |
| 96 | + |
| 97 | +5. Run the script to create the certs: |
| 98 | + |
| 99 | + ```{.bash data-prompt="$"} |
| 100 | + $ ./init/create-ssl-certs.sh |
| 101 | + ``` |
| 102 | + |
| 103 | +4. Copy Certificates to All Nodes |
| 104 | + |
| 105 | + All three nodes in the cluster must use the same set of SSL certificates. After generating the certificates in the certs/ directory on node 1, you need to copy them to the directories for node 2 and node 3. |
| 106 | + |
| 107 | + If you are running all containers from a single project directory (like with Docker Compose on one host), you can reuse the same certs/ directory for all nodes. However, if you’re organizing them into separate directories or deploying on separate hosts, you must explicitly copy the certificates. |
| 108 | + |
| 109 | + To create the directories for node 2 and node 3: |
| 110 | + |
| 111 | + ```{.bash data-prompt="$"} |
| 112 | + $ mkdir -p certs-node2 |
| 113 | + $ mkdir -p certs-node3 |
| 114 | + ``` |
| 115 | + |
| 116 | + Then copy the certificates: |
| 117 | + |
| 118 | + ```{.bash data-prompt="$"} |
| 119 | + $ cp -r certs/* certs-node2/ |
| 120 | + $ cp -r certs/* certs-node3/ |
| 121 | + ``` |
| 122 | + |
| 123 | + If you’re deploying on separate machines, run the following from node 1: |
| 124 | + |
| 125 | + ```{.bash data-prompt="$"} |
| 126 | + $ scp -r ./certs/ user@node2-host:/path/to/pxc-cluster/certs |
| 127 | + $ scp -r ./certs/ user@node3-host:/path/to/pxc-cluster/certs |
| 128 | + ``` |
| 129 | + |
| 130 | + Ensure each container mounts its own copy of the certs/ directory. |
| 131 | + |
| 132 | +5. Create docker-compose.yml: |
| 133 | + |
| 134 | + ```ini |
| 135 | + |
| 136 | + |
| 137 | + # Docker Compose Configuration for Percona XtraDB Cluster |
| 138 | + |
| 139 | + This is a Docker Compose file that defines a multi-container application for running a Percona XtraDB Cluster. |
| 140 | + |
| 141 | + ```yaml |
| 142 | + services: |
| 143 | + pxc1: |
| 144 | + image: percona/percona-xtradb-cluster:8.4 |
| 145 | + container_name: pxc1 |
| 146 | + environment: |
| 147 | + - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} |
| 148 | + - CLUSTER_NAME=pxc-cluster |
| 149 | + - CLUSTER_JOIN=pxc2,pxc3 |
| 150 | + - XTRABACKUP_PASSWORD=${XTRABACKUP_PASSWORD} |
| 151 | + volumes: |
| 152 | + - ./certs:/etc/mysql/certs:ro |
| 153 | + - ./conf.d:/etc/percona-xtradb-cluster.conf.d:ro |
| 154 | + networks: |
| 155 | + - pxcnet |
| 156 | + ports: |
| 157 | + - "3306:3306" |
| 158 | + command: ["--wsrep-new-cluster"] |
| 159 | + |
| 160 | + pxc2: |
| 161 | + image: percona/percona-xtradb-cluster:8.4 |
| 162 | + container_name: pxc2 |
| 163 | + environment: |
| 164 | + - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} |
| 165 | + - CLUSTER_NAME=pxc-cluster |
| 166 | + - CLUSTER_JOIN=pxc1,pxc3 |
| 167 | + - XTRABACKUP_PASSWORD=${XTRABACKUP_PASSWORD} |
| 168 | + volumes: |
| 169 | + - ./certs:/etc/mysql/certs:ro |
| 170 | + - ./conf.d:/etc/percona-xtradb-cluster.conf.d:ro |
| 171 | + networks: |
| 172 | + - pxcnet |
| 173 | + |
| 174 | + pxc3: |
| 175 | + image: percona/percona-xtradb-cluster:8.4 |
| 176 | + container_name: pxc3 |
| 177 | + environment: |
| 178 | + - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} |
| 179 | + - CLUSTER_NAME=pxc-cluster |
| 180 | + - CLUSTER_JOIN=pxc1,pxc2 |
| 181 | + - XTRABACKUP_PASSWORD=${XTRABACKUP_PASSWORD} |
| 182 | + volumes: |
| 183 | + - ./certs:/etc/mysql/certs:ro |
| 184 | + - ./conf.d:/etc/percona-xtradb-cluster.conf.d:ro |
| 185 | + networks: |
| 186 | + - pxcnet |
| 187 | + |
| 188 | + networks: |
| 189 | + pxcnet: |
| 190 | + driver: bridge |
| 191 | + ``` |
| 192 | + |
| 193 | +6. Start the Cluster |
| 194 | + |
| 195 | +Start node 1 to initialize the cluster: |
| 196 | + |
| 197 | +```{.bash data-prompt="$"} |
| 198 | +$ docker compose up -d pxc1 |
| 199 | +``` |
| 200 | + |
| 201 | +Then, start the remaining nodes: |
| 202 | + |
| 203 | +```{.bash data-prompt="$"} |
| 204 | +$ docker compose up -d pxc2 pxc3 |
| 205 | +``` |
| 206 | + |
| 207 | +7. Validate the Cluster |
| 208 | + |
| 209 | +Check the status of each node: |
| 210 | + |
| 211 | +```{.bash data-prompt="$"} |
| 212 | +docker exec -it pxc1 mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "SHOW STATUS LIKE 'wsrep_cluster_size';" |
| 213 | +docker exec -it pxc2 mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "SHOW STATUS LIKE 'wsrep_cluster_status';" |
| 214 | +``` |
| 215 | + |
| 216 | +You should see all three nodes joined and synchronized. |
| 217 | + |
| 218 | +⸻ |
| 219 | + |
| 220 | +Next Steps |
| 221 | + • Add persistent volumes for data directories |
| 222 | + • Use ProxySQL or HAProxy for load balancing |
| 223 | + • Integrate with PMM for monitoring |
| 224 | + • Enable backups using Percona XtraBackup |
| 225 | + |
0 commit comments