diff --git a/mariadb.yml b/mariadb.yml index 2432187..bbf78bb 100644 --- a/mariadb.yml +++ b/mariadb.yml @@ -5,9 +5,9 @@ roles: - mariadb vars: - hostname_node0: "{{ hostvars[groups['mariadb'][0]]['ansible_hostname'] }}" - hostname_node1: "{{ hostvars[groups['mariadb'][1]]['ansible_hostname'] }}" - hostname_node2: "{{ hostvars[groups['mariadb'][2]]['ansible_hostname'] }}" - ip_node0: "hostvars[groups['databases'][0]]['listen_ip'] | default(hostvars[groups['databases'][0]]['ansible_default_ipv4']['address'])" - ip_node1: "hostvars[groups['databases'][1]]['listen_ip'] | default(hostvars[groups['databases'][1]]['ansible_default_ipv4']['address'])" - ip_node2: "hostvars[groups['databases'][2]]['listen_ip'] | default(hostvars[groups['databases'][2]]['ansible_default_ipv4']['address'])" + hostname_node0: "{{ hostvars[groups['databases'][0]]['ansible_hostname'] }}" + hostname_node1: "{{ hostvars[groups['databases'][1]]['ansible_hostname'] }}" + hostname_node2: "{{ hostvars[groups['databases'][2]]['ansible_hostname'] }}" + ip_node0: "{{ hostvars[groups['databases'][0]]['listen_ip'] | default(hostvars[groups['databases'][0]]['ansible_default_ipv4']['address']) }}" + ip_node1: "{{ hostvars[groups['databases'][1]]['listen_ip'] | default(hostvars[groups['databases'][1]]['ansible_default_ipv4']['address']) }}" + ip_node2: "{{ hostvars[groups['databases'][2]]['listen_ip'] | default(hostvars[groups['databases'][2]]['ansible_default_ipv4']['address']) }}" diff --git a/roles/mariadb/files/galera.cnf b/roles/mariadb/files/galera.cnf new file mode 100644 index 0000000..6b27f64 --- /dev/null +++ b/roles/mariadb/files/galera.cnf @@ -0,0 +1,20 @@ +[mysqld] +binlog_format=ROW +default-storage-engine=innodb +innodb_autoinc_lock_mode=2 +bind-address=0.0.0.0 + +# Galera Provider Configuration +wsrep_on=ON +wsrep_provider=/usr/lib/galera/libgalera_smm.so + +# Galera Cluster Configuration +wsrep_cluster_name="test_cluster" +wsrep_cluster_address="gcomm://{{ ip_node0 }},{{ ip_node1 }},{{ ip_node2 }}" + +# Galera Synchronization Configuration +wsrep_sst_method=rsync + +# Galera Node Configuration +wsrep_node_address="{{ listen_ip | default(ansible_default_ipv4.address) }}" +wsrep_node_name="{{ ansible_nodename }}" diff --git a/roles/mariadb/tasks/main.yml b/roles/mariadb/tasks/main.yml index d28b8a3..9a5c8fe 100644 --- a/roles/mariadb/tasks/main.yml +++ b/roles/mariadb/tasks/main.yml @@ -13,9 +13,6 @@ owner: root group: root -- name: start service at boot. - command: systemctl reenable mysql.service - - name: make mariadb settings volume file: path: "{{ item }}" @@ -24,30 +21,35 @@ with_items: - /srv/mariadb/lib/mysql - /srv/mariadb/etc/mysql + - /srv/mariadb/etc/mysql/conf.d - name: place settings file copy: src: files/my.cnf - dest: /srv/mariadb/etc/mysql + dest: /srv/mariadb/etc/mysql/conf.d/my.cnf mode: 660 -- name: Set galara.cnf on first node if we have at least three nodes. - copy: - src: files/galera-master.cnf +- name: Set galara.cnf on node if we have at least three nodes. + template: + src: files/galera.cnf dest: /srv/mariadb/etc/mysql/conf.d/galera.cnf mode: 660 - when: ansible_nodename == hostname_node0 and hostvars[groups['mariadb']]|length >= 3 - -- name: Set galara.cnf on other nodes if we have at least three nodes. - copy: - src: files/galera-slave.cnf - dest: /srv/mariadb/etc/mysql/conf.d/galera.cnf - mode: 660 - when: ansible_nodename != hostname_node0 and hostvars[groups['mariadb']]|length >= 3 + when: groups['databases'] | length >= 3 - command: systemctl daemon-reload +- name: start service at boot. + command: systemctl reenable mysql.service + +# This mimics galera_new_cluster.sh +- name: Initialize a new cluster. + command: systemctl set-environment _WSREP_NEW_CLUSTER='--wsrep-new-cluster' + when: groups['databases'] | length >= 3 and ansible_hostname == hostname_node0 + - name: make sure service is started systemd: name: mysql.service state: started + +- name: Prevent cluster re-initialization + command: systemctl set-environment _WSREP_NEW_CLUSTER='' diff --git a/roles/mariadb/templates/mysql.service b/roles/mariadb/templates/mysql.service index 530de12..4a5fdca 100644 --- a/roles/mariadb/templates/mysql.service +++ b/roles/mariadb/templates/mysql.service @@ -9,10 +9,12 @@ Restart=always ExecStartPre=-/usr/bin/docker kill %n || /bin/true ExecStartPre=-/usr/bin/docker rm %n ExecStartPre=/usr/bin/docker pull mariadb:10.2 -ExecStart=/usr/bin/docker run -p 3306:3306 --name %n \ +ExecStart=/usr/bin/docker run --name %n \ + -p 3306:3306 -p 4567:4567 -p 4568:4568 -p 4444:4444 \ -v /srv/mariadb/lib/mysql:/var/lib/mysql \ - -v /srv/mariadb/etc/mysql:/etc/mysql \ - -e MYSQL_ROOT_PASSWORD={{ secrets['MYSQL_ROOT_PASSWORD'] }} mariadb:10.2 + -v /srv/mariadb/etc/mysql/conf.d:/etc/mysql/conf.d \ + -e MYSQL_ROOT_PASSWORD={{ secrets['MYSQL_ROOT_PASSWORD'] }} mariadb:10.2 \ + ${MYSQLD_OPTS} ${_WSREP_NEW_CLUSTER} ${_WSREP_START_POSITION} [Install] WantedBy=multi-user.target diff --git a/secrets.yml b/secrets.yml index 91d5517..4d811f4 100644 --- a/secrets.yml +++ b/secrets.yml @@ -1,30 +1,30 @@ $ANSIBLE_VAULT;1.1;AES256 -62633134346438356462333363626164393762356139653666323461333037393536373631653565 -6631306631333538353534663738313062636232633339610a303161323131373739393735666463 -65353135626430353737373239623361306137326334333761626235353463393465383830666666 -6138616530346563310a306263316331346263356139383435316239346230313266636363313564 -36633130393062373936363765636361343939313639326237633337353665666338633338343837 -34613534333063303537323738396436333964613362636664366264313334663365336132623464 -64656131373261376466356638636338643135393139386534626132323262393064626666323462 -64323664373262356632393465653932303939313338656665336639613966626234636666373163 -35633231666338643863623737396435626364333365656536613130666435323837323136663339 -61363936336434656530313538643463663737613831646265313731363734356635356438353062 -34323063346265393737343834343065616139656234666230323131366138396265393737666236 -39353766643239323339623534393962666432656331323462656439306365613539366230643133 -36316138303361313134336431343137343433383430616137376563383233303432383664333930 -61613531313638303531643232343066376565663032326533313461363839383664366338356439 -37363233666663653736376538386536653262653633323065363830623032363063393635653762 -32636365656362323362303962306538336234626533323830656230386432666461343063663832 -62373133343933353563653762333836333862376232353339313662363865616439623635393839 -37346433346264633036343761613230396434366132653261643137386466326235613030306235 -34333065623232303939623233373762393939653639333734336336303762326662386530356563 -65303165623564303635356337353662363433626466653939323438633938386166386262623435 -64376431396631623034386434393431616631363663393835343035313639663538643565616330 -65353365303131326335646164333231306564383936396139643935646331393235326666336230 -38326165663865343966356335326438303133663239656235313935626332323332376665343132 -62336139643262333938303537313533623535333736643163373137343035393034613939663061 -36323063643734343865333138356434643266663436653435353132386330636238343637653434 -65616361333263336332643262623034343439383737366663373166643433653466313237613930 -32373162646461323266353662326134343839613264313339306430366165633838663831666565 -65333337623962313561306333616232393334353934316565666331336561633934623339353138 -62656339386530333036383831613762353234643461656436623033613930353531 +35643437313834633532373265366630663035336231306639623561613765386332663334343237 +3339363162303463353437326331656532336138373066620a623137643762383532376361353364 +37646236386466353636396535376463333133323664316634663466663164303063383830653039 +3535666361303562630a316137376531636537383138663662373865383431343035646539356137 +38323866643831353537366630363333663865383261633938346664633362343661343839383766 +66363733356333303334323136376136353738376362376231353338343763663131363731343639 +61383138626235633663666430383964616239363035663663646133636434363032626633663865 +30663732646630393163653461626435333463396463333236313930346461626364626166386365 +66323736316230376165666366363136666533376335316132343361393532616536383965363339 +30376362356665633630393561653532613139366236663961643864383738353430666562623730 +34663166393665653265663836623731386235633062306562373935633737363639383336303539 +37663763623664623038316438356138363134646230643261646262353163333430616462393866 +31666233636233356464633436626637313633623736343264613037353432386131393964386663 +36353236613662633764366437306461316138366461653731373436613039346663663536653362 +38656636303935626563303732666261373665303035333661643865393166653330646336393961 +31646539396131626464313733383638656438613530663166393035343630353764313232323432 +34386334666231323261343765623636313032373835396332623037613866613636393038653266 +36336531356534633933383432646663663364376130386239613836336263623161326563346661 +33636232313866613662353661373533383138393434396338343934326333326238336638396462 +65376133343038313437343934373265333632663133653133656130636533663237623839623634 +35363764363763363465363437623964363362616261663166633066373033633864336532633031 +32323733616562663031303230383561373637326436336462363461313532623262653866323862 +34643631333533626537373538353564306261313035303530666462326534633638363932363037 +65336230373034643966656561303164373463353638316632613431643535303930373334383134 +38323731363535313065326330653666323934636466386238616664316635303333653631396639 +39303737613361653862343964303231393164346134633366633262326230643137303331373231 +31323832363937663935333737613133323265323863623933633962633230386339636432643937 +66653763376663666637353738646565343835333937343765356539383734316231623466343634 +30663135663938393561333133663737653635393432333534306466366332333338