Ansible部署galera集群
# Ansible部署galera集群
节点 | IP | 主机名 |
---|---|---|
ansible | 172.25.253.137 | ansible |
galera | 172.25.253.138 | node1 |
mariadb | 172.25.253.139 | node2 |
mariadb | 172.25.253.140 | node3 |
# 查看文件树
[root@ansible tasks]# tree galera/
galera/
├── galera.yaml
├── hosts
├── local.repo
└── templates
└── server.cnf.j2
# Ansible部署galera集群
- 由于部署galera集群比较简单所以使用单个playbook
- 配置好galera的hosts文件
- 配置好主机变量以及主机清单
- 配置好yum源安装高可用数据库
- 配置好j2的server.cnf的配置文件
# 基础环境部署
# 1.配置主机清单
[root@ansible tasks]# cat /etc/ansible/hosts
[server]
172.25.253.138 hostname=node1
172.25.253.140 hostname=node2
172.25.253.139 hostname=node3
# 2.配置hosts的映射
[root@ansible galera]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.253.138 node1
172.25.253.139 node2
172.25.253.140 node3
# 3.配置yum源安装mariadb galera
[root@ansible galera]# cat local.repo
[centos]
name=centos
baseurl=http://172.25.253.144/file/centos/
gpgcheck=0
[mariadb-repoo]
name=mariadb-repo
baseurl=http://172.25
# 4.编写playbook的
- 使用playbook部署多个主机名
- 复制hosts和local.repo
- 安装mariadb 和 galera以及所需要的安装包
- 配置环境 配置配置文件 启动galera的环境
- 使用meta触发handlers在完成清单立马触发handlers效果
[root@ansible galera]# cat galera.yaml
- hosts: server
remote_user: root
tasks:
- name: change name
raw: "echo {{hostname|quote}} > /etc/hostname"
- name:
shell: hostname {{hostname|quote}}
- name: copy hosts
copy: src=hosts dest=/etc/hosts
- name: delete repo
shell: rm -rf /etc/yum.repos.d/*
- name: copy repo
copy: src=local.repo dest=/etc/yum.repos.d/local.repo
- name: Install Packages
yum:
name:
- mariadb
- mariadb-server
- galera
- name: start mariadb
service: name=mariadb state=started
- name: mysql_admin
shell: mysqladmin -uroot password 000000
- name: grant all
shell: mysql -uroot -p000000 -e "grant all privileges on *.* to root@'%' identified by '000000';"
- name: change config
template: src=server.cnf.j2 dest=/etc/my.cnf.d/server.cnf
notify: stop service
- meta: flush_handlers
- name: start galera
shell: galera_new_cluster
when: ansible_fqdn == "node1"
- name: start mariadb1
service: name=mariadb state=started
when: ansible_fqdn == {{ "item" }}
with_items:
- node2
- node3
handlers:
- name: stop service
service: name=mariadb state=stopped
# 5.修改template的j2文件
- 创建templates的目录
- 添加server.cnf命名j2结尾的配置文件,更改配置
[root@ansible galera]# cat templates/server.cnf.j2
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://node1,node2,node3
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
# this is only for embedded server
[embedded]
# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
# This group is only read by MariaDB-10.3 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.3]
# 6.执行脚本
[root@ansible tasks]# ansible-playbook galera.yaml
PLAY [server] ***************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [172.25.253.139]
ok: [172.25.253.140]
ok: [172.25.253.138]
TASK [change name] **********************************************************************************************************
changed: [172.25.253.138]
changed: [172.25.253.140]
changed: [172.25.253.139]
·
·
·
PLAY RECAP ******************************************************************************************************************
172.25.253.138 : ok=12 changed=10 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.253.139 : ok=12 changed=10 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.253.140 : ok=12 changed=10 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
# 7.验证galera集群效果
- 查看端口号
- 查看集群状
- 若看到3306和4567端口,则MariaDB Galera Cluster集群配置成功态
[root@ansible galera]# ansible server -m shell -a "netstat -ntpl"
172.25.253.140 | CHANGED | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 9369/mysqld
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 522/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1022/sshd
tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 9369/mysqld
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 958/master
tcp6 0 0 :::111 :::* LISTEN 522/rpcbind
tcp6 0 0 :::22 :::* LISTEN 1022/sshd
tcp6 0 0 ::1:25 :::* LISTEN 958/master
[root@node1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.23-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready | ON |
+---------------+-------+
1 row in set (0.001 sec)
MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.000 sec)
MariaDB [(none)]> exit
Bye
上次更新: 2023/11/28, 22:03:59