1、服务部署
2.服务部署 抽象:功能模块 redis 安装、配置、启动 mysql 安装、配置(my.cnf可以统一 目录默认配置可以统一) master: server_id 1111 slave: server_id 2222 1.redis 主从 (已经实现) 2.mysql 主从 2.1 mysql-install.sls 安装 配置 初始化 2.2 my.cnf配置不同-server id 2.3 创建主从同步用户 2.4 master上获取binlog 和 pos值 2.5 slave上,change master && start slave 2.6 检查主从状态 GRANT replication slave on 3.apache+php 4.haproxy+keepalived
2、redis(已经完成)
https://www.cnblogs.com/venicid/p/11276232.html#_label7_0
3、mysql安装,配置
1、目录结构
module模块
按类型分:
按业务分
Module 模块里面有 mysql redis 等
统一的的全部放在这里,比如安装,配置
redis 安装配置.sls
[root@# localhost /srv/salt/prod/modules]# cat redis/init.sls redis-install: pkg.installed: - name: redis redis-config: file.managed: - name: /etc/redis.conf - source: salt://redis/files/redis.conf - user: root - group: root - mode: 644 - template: jinja - defaults: PORT: 6379 IPADDR: {{ grains['fqdn_ip4'][0] }} redis-service: service.running: - name: redis - enable: True - reload: True - watch: - file: redis-config
2、mysql 安装,配置.sls
[root@# localhost /srv/salt/prod/modules/mysql]# cat install.sls mysql-install: pkg.installed: - pkgs: - mariadb - mariadb-server mysql-config: file.managed: - name: /etc/my.cnf - source: salt://modules/mysql/files/my.cnf - user: root - group: root - mode: 644
[root@linux-node1 /srv/salt/prod/modules/mysql]# salt 'linux-node1*' state.sls modules.mysql.install saltenv=prod
4、主从配置
1、目录结构
2、主从配置.sls
[root@# localhost /srv/salt/prod/modules/mysql]# cat master.sls include: - modules.mysql.install master-config: file.managed: - name: /etc/my.cnf.d/mariadb-server.cnf - source: salt://modules/mysql/files/mariadb-server-master.cnf - user: root - group: root - mode: 644 master-service: service.running: - name: mariadb - enable: True
[root@# localhost /srv/salt/prod/modules/mysql]# cat slave.sls include: - modules.mysql.install slave-config: file.managed: - name: /etc/my.cnf.d/mariadb-slave.cnf - source: salt://modules/mysql/files/mariadb-server-slave.cnf - user: root - group: root - mode: 644 slave-service: service.running: - name: mariadb - enable: True
3、file配置文件
[root@# localhost /srv/salt/prod/modules/mysql]# cp /etc/my.cnf.d/server.cnf files/mariadb-server-master.cnf [root@# localhost /srv/salt/prod/modules/mysql]# cp /etc/my.cnf.d/server.cnf files/mariadb-server-slave.cnf
[root@# localhost /srv/salt/prod/modules/mysql/files]# vim mariadb-server-master.cnf
[root@# localhost /srv/salt/prod/modules/mysql/files]# vim mariadb-server-slave.cn
[root@# localhost ~]# salt 'linux-node1*' state.sls modules.mysql.master saltenv=prod [root@# localhost ~]# salt 'linux-node2*' state.sls modules.mysql.slave saltenv=prod
5、主从同步用户,授权
赵班长:https://github.com/unixhot/salt-openstack/tree/master/states/openstack-mitaka/mysql
https://www.unixhot.com/page/ops
官方文档 http://docs.saltstack.cn/ref/states/all/salt.states.mysql_user.html
完成这个命令: grant replication slave on *.* to 'xxx'@'xxxxx.%' identified by 'xxxxx';
必须启动mysql, 从0构建mysql
1、方式1:mysql授权
master.sls
[root@# localhost /srv/salt/prod/modules/mysql]# cat master.sls include: - modules.mysql.install master-config: file.managed: - name: /etc/my.cnf.d/mariadb-server.cnf - source: salt://modules/mysql/files/mariadb-server-master.cnf - user: root - group: root - mode: 644 master-service: service.running: - name: mariadb - enable: True repl-user: ## 创建用户 mysql_user.present: - name: repl_user - host: 192.168.194.0/255.255.255.0 - password: repl_user grant-user: ##授权 mysql_grants.present: - grant: replication slave - database: '*.*' - user: repl_user - host: 192.168.194.0/255.255.255.0
slave.sls 不变
[root@# localhost /srv/salt/prod/modules/mysql]# cat slave.sls include: - modules.mysql.install slave-config: file.managed: - name: /etc/my.cnf.d/mariadb-slave.cnf - source: salt://modules/mysql/files/mariadb-server-slave.cnf - user: root - group: root - mode: 644 slave-service: service.running: - name: mariadb - enable: True [root@# localhost /srv/salt/prod/modules/mysql]#
测试下
[root@# localhost ~]# salt '*' state.sls modules.mysql.master saltenv=prod
Question:执行时出错 Comment: MySQL Error 1142: SELECT command denied to user 'salt'@'linux-node1' for table 'user'
###解决办法:断开minion连接MySQL
[root@# localhost ~]# systemctl restart salt-minion
2、方式2:cmd方式授权
[root@# localhost /srv/salt/prod/modules/mysql]# cat master.sls include: - modules.mysql.install master-config: file.managed: - name: /etc/my.cnf.d/mariadb-server.cnf - source: salt://modules/mysql/files/mariadb-server-master.cnf - user: root - group: root - mode: 644 master-service: service.running: - name: mariadb - enable: True master-grant: cmd.run: - name: mysql -e "GRANT replication slave,super on *.* to 'repl_user'@'192.168.194.0/255.255.255.0' identified by 'repl_user@pass'" - unless: mysql -h 192.168.194.131 -u repl_user -prepl_user@pass -e "exit"
[root@# localhost ~]# salt '*' state.sls modules.mysql.master saltenv=prod
3、方式3:脚本 ---按业务来分 (有时候错误,用前两方式)
基础和业务分开
资源
业务:业务使用了资源
手动执行,安装mysql
[root@# localhost ~]# salt '*' state.sls modules.mysql.master saltenv=prod
(1)目录结构
(2)master,slave.sls还原
[root@# localhost /srv/salt/prod/modules/mysql]# cat master.sls include: - modules.mysql.install master-config: file.managed: - name: /etc/my.cnf.d/mariadb-server.cnf - source: salt://modules/mysql/files/mariadb-server-master.cnf - user: root - group: root - mode: 644 master-service: service.running: - name: mariadb - enable: True [root@# localhost /srv/salt/prod/modules/mysql]# cat slave.sls include: - modules.mysql.install slave-config: file.managed: - name: /etc/my.cnf.d/mariadb-slave.cnf - source: salt://modules/mysql/files/mariadb-server-slave.cnf - user: root - group: root - mode: 644 slave-service: service.running: - name: mariadb - enable: True
(3)脚本文件
[root@# localhost /srv/salt/prod/shop-user/files]# cat start_slave.sh #!/bin/bash for i in `seq 1 10`;do mysql -h 192.168.194.131 -u repl_user -prepl_user@pass -e "exit" if [ $? -eq 0 ];then POS=$(mysql -h 192.168.194.131 -u repl_user -prepl_user@pass -e "show master status" | awk -F '|' 'NR==2 {print $1}' | awk '{print $2}') mysql -e "change master to master_host='192.168.194.131', master_user='repl_user', master_password='repl_user@pass', master_log_file='mysqlbin.000001' , master_log_pos=$POS; start slave;" touch /etc/my.cnf.d/slave.lock exit; else sleep 60; fi done
测试脚本
(4)业务与资源分开,配置文件
[root@# localhost /srv/salt/prod/shop-user]# cat mysql-master.sls include: - modules.mysql.master master-grant: cmd.run: - name: mysql -e "GRANT replication slave,super on *.* to 'repl_user'@'118.190.201.0/255.255.255.0' identified by 'repl_user@pass'" - unless: mysql -h 192.168.194.131 -ur repl_user -prepl_user@pass -e "exit"
[root@# localhost /srv/salt/prod/shop-user]# cat mysql-slave.sls include: - modules.mysql.slave slave-grant: file.managed: - name: /tmp/start_slave.sh - source: salt://shop-user/files/start_slave.sh - user: root - group: root - mode: 755 cmd.run: - name: /bin/bash /tmp/start_slave.sh - unless: test -f /etc/my.cnf.d/slave.lock
(5) top.sls
[root@# localhost /srv/salt/base]# [root@# localhost /srv/salt/base]# cat top.sls base: '*': - init.init-all prod: 'linux-node1.example.com': - shop-user.mysql-master 'linux-node2.example.com': - shop-user.mysql-slave
(6)清空数据库
[root@# localhost ~]# cd /var/lib/mysql/ [root@# localhost /var/lib/mysql]# rm -rf * [root@# localhost /var/lib/mysql]# yum remove mariadb-server
(7)执行测试
个人建议:
Mysql 不放在 topfile
每次手动执行
[root@# localhost ~]# salt '*' state.sls modules.mysql.master saltenv=prod
Salt管理: 自动化安装,自动化配置
Prod整个目录 mysql redis
执行top.file
[root@# localhost ~]# salt '*' state.highstate linux-node2.example.com: ---------- .......省略部分....... Summary ------------ Succeeded: 6 (changed=1) Failed: 0 ------------ Total states run: 6 linux-node1.example.com: ---------- .......省略部分....... Summary ------------ Succeeded: 6 (changed=1) Failed: 0 ------------ Total states run: 6
6、总结与问题
1、问题
Question1
linux下yum安装时出现Loaded plugins: fastestmirror解决办法
https://blog.51cto.com/12922638/2412602
网络问题,dhcp
Question2
Centos 7
https://www.linuxidc.com/Linux/2018-03/151403.htm
vi /etc/my.cnf.d/server.cnf
Question3
授权失败
Salt用户没有权限
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'salt'@'%' IDENTIFIED BY PASSWORD '*36F75ABC6D500DFA6E905046FD8BE5E115812DD0' WITH GRANT OPTION;
Query OK, 0 rows affected (0.04 sec)
MariaDB [(none)]> show grants for salt@'%'G;
###执行时出错
Comment: MySQL Error 1142: SELECT command denied to user 'salt'@'linux-node1' for table 'user'###解决办法:断开minion连接MySQL
[root@linux-node1 ~]# vim /etc/salt/minion
#mysql.host: '118.190.201.11'
#mysql.user: 'salt'
#mysql.pass: 'salt'
#mysql.db: 'salt'
2、心得
架构师:道法术
思路达到了运维架构的标准上