Ansible自动部署->redis(主从+哨兵)+zabbix

使用Ansible部署redis集群,要求:

1. 三台redis使用主从架构,主服务器高可用;

2. 监控每台服务器的服务运行情况;

3. 当有服务宕机的情况下先让zabbix自动重启,如果重启成功发送恢复邮件给管理员,如果失败则发送报警邮件给管理员。

计划:

hostname

ip

server

master.test

192.168.10.12

ansible+zabbix-server

master.redis

192.168.10.120

redis(master)(sentinel)+zabbix-agent

slave1.redis

192.168.10.130

redis(slave)(sentinel)+zabbix-agent

slave2.redis

192.168.10.140

redis(slave)(sentinel)+zabbix-agent

zabbix-server快速安装脚本

#!/bin/bash
#Zabbix-Server  5.0

#安装zabbix源、aliyun YUM源
cd /etc/yum.repos.d/
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i.bak 's#repo.zabbix.com#mirrors.aliyun.com/zabbix#' zabbix.repo
sed -i 's#enabled=0#enabled=1#' zabbix.repo

#安装zabbix
yum -y install zabbix-server-mysql zabbix-agent
yum -y install centos-release-scl
yum -y install zabbix-web-mysql-scl zabbix-apache-conf-scl

#安装启动 mariadb数据库
yum -y install mariadb mariadb-server httpd
systemctl start mariadb.service

#创建数据库
mysql -e 'create database zabbix character set utf8 collate utf8_bin;'
mysql -e 'grant all privileges on zabbix.* to zabbix@localhost identified by "zabbix";'
mysql -e 'flush privileges;'

#导入数据
zcat /usr/share/doc/zabbix-server-mysql-5.0.3/create.sql.gz|mysql -uzabbix -pzabbix -Dzabbix

#配置zabbixserver连接mysql
sed -i.bak '/^# DBPassword=*/i DBPassword=zabbix' /etc/zabbix/zabbix_server.conf

#添加时区
sed -i.bak '/^; php_value[date.timezone]*/i php_value[date.timezone] = Asia/Shanghai' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

#解决中文乱码
yum -y install wqy-microhei-fonts
\cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf

#启动服务
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm

#开机自启动
systemctl enable mariadb.service
systemctl enable httpd
systemctl enable zabbix-server

#输出信息
echo "浏览器访问 http://`hostname -I|awk '{print $1}'`/zabbix"

zabbix-agent快速部署脚本

#!/bin/bash
#Zabbix-Agent  5.0

Zabbix_Service=192.168.10.13

#安装zabbix源、aliyun YUM源
cd /etc/yum.repos.d/
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i.bak 's#repo.zabbix.com#mirrors.aliyun.com/zabbix#' zabbix.repo
sed -i 's#enabled=0#enabled=1#' zabbix.repo

#安装zabbix
yum -y install zabbix-agent

#修改配置文件
sed -i.bak "s/^Server=127.0.0.1/Server=$Zabbix_Service/" /etc/zabbix/zabbix_agentd.conf
sed -i "s/^ServerActive=127.0.0.1/ServerActive=$Zabbix_Service/" /etc/zabbix/zabbix_agentd.conf
sed -i "/^Hostname=/c Hostname=$(echo `hostname`)" /etc/zabbix/zabbix_agentd.conf

#开启zabbix-agent服务 10050 端口
systemctl start zabbix-agent

#设置开机自启
systemctl enable zabbix-agent

#输出提示
echo -e "Now you can use \033[32mnetstat -tnlp\033[0m check \033[33mport:10050\033[0m"

zabbix-proxy快速安装脚本

#!/bin/bash
#Zabbix-Proxy  5.0

Zabbix_Service=192.168.10.13

#安装zabbix源、aliyun YUM源
cd /etc/yum.repos.d/
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i.bak 's#repo.zabbix.com#mirrors.aliyun.com/zabbix#' zabbix.repo
sed -i 's#enabled=0#enabled=1#' zabbix.repo

#安装zabbix-proxy-mysql zabbix-get zabbix-agent zabbix-sender
yum -y install zabbix-proxy-mysql zabbix-get zabbix-agent zabbix-sender

#安装启动 mariadb数据库
yum -y install mariadb mariadb-server
systemctl start mariadb.service

#初始化数据库
#mysql_secure_installation #设置密码,删除匿名用户,禁止root远程登录,删除test库

#创建数据库
mysql -e 'create database zabbix_proxy character set utf8 collate utf8_bin;'
mysql -e 'grant all privileges on zabbix_proxy.* to zabbix_proxy@localhost identified by "zabbix_proxy";'
mysql -e 'flush privileges;'

#导入数据
zcat /usr/share/doc/zabbix-proxy-mysql-5.0.3/schema.sql.gz |mysql -uzabbix_proxy -pzabbix_proxy -D zabbix_proxy

#修改配置文件
cat >>/etc/my.cnf.d/server.cnf<> /etc/zabbix/zabbix_proxy.conf
echo "LogRemoteCommands=1" >> /etc/zabbix/zabbix_proxy.conf

#启动服务并开机自启
systemctl start zabbix-proxy
systemctl enable zabbix-proxy

#安装netsats等网络工具
yum -y install net-tools

#输出信息
echo -e "Now you can use \033[32mnetstat -tnlp\033[0m check \033[33mport:10051\033[0m"

批量上传公钥脚本

#!/bin/bash
Public_Key() {
[ ! -f /usr/bin/expect ] && yum install expect -y &>/dev/null
    /usr/bin/expect <

注意脚本遍历的host_passwd.txt文件与脚本在同一目录下,主要内容是:主机IP和passwd

vim host_passwd.txt

192.168.10.120 123456
192.168.10.130 123456
192.168.10.140 123456

ansible安装新机redis(主从+哨兵)

Ansible自动部署->redis(主从+哨兵)+zabbix_第1张图片

vim redis.yml

---
- hosts: all
  roles:
  - redis

vim redis-hosts              #编辑清单   后续用-i redis-host 就不用修改源清单了

[redis]
192.168.10.120 master=1
192.168.10.130 master=0
192.168.10.140 master=0
[redis:vars]
master_ip=192.168.10.120
redis_port=6380   
redis_pass="sunli.com"
redis_log="/var/log/redis.log"

vim redis.conf.j2

bind 0.0.0.0
protected-mode no
port {{ redis_port }}
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "{{ redis_log }}"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir ./
masterauth {{ redis_pass }}
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "{{ redis_pass }}"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
{% if master!=1 %}
slaveof {{ master_ip }} {{ redis_port }}
{% endif %}

egrep -v "^ *#|^$" sentinel.conf.j2
bind 0.0.0.0
protected-mode no
port 26379
dir "/tmp"
logfile "./sentinel.log"
sentinel monitor mymaster {{ master_ip }} {{ redis_port }} 2
sentinel auth-pass mymaster {{ redis_pass }}
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1

2、./roles/redis/tasks/main.yml文件内容


---
  - name: Close firewalld and forbid enabled
    systemd:
      name: firewalld
      enabled: False
      state: stopped
  - name: Close SELINUX
    shell: setenforce 0 || true
  - name: Forbid SELINUX enabled
    lineinfile:
     path: /etc/selinux/config
     regexp: '^SELINUX='
     line: SELINUX=disabled
  - name: unarchive redis.tar.gz to remote server
    unarchive:
          src: "{{ redis_name }}"
          dest: "/usr/local"
  - name: Install base packages
    yum:
      name: ['gcc','gcc-c++']
      state: present
  - name: make redis
    shell: cd {{ basedir }} && make MALLOC=libc
  - name: configure redis.conf
    template:
          src: "{{ redis_conf }}"
          dest: "{{ basedir }}/redis.conf"
          backup: True
  - name: configure sentinel.conf
    template:
          src: "{{ sentinel_conf }}"
          dest: "{{ basedir }}/sentinel.conf"
          backup: True
  - name: start redis
    shell: "{{ basedir }}/src/redis-server {{ basedir }}/redis.conf"
  - name: start redis.sentinel
    shell: "{{ basedir }}/src/redis-sentinel {{ basedir }}/sentinel.conf"

3、./roles/redis/defaults/main.yml文件内容


basedir: /usr/local/redis-4.0.10
redis_name: redis-4.0.10.tar.gz
redis_conf: redis.conf.j2
sentinel_conf: sentinel.conf.j2
 

4、运行


[root@master ansible-roles]# ls
host_passwd.txt  redis.yml  key.sh  redis-hosts  roles
[root@master ansible-roles]# ansible-playbook -i redis-hosts redis.yml -C
[root@master ansible-roles]# ansible-playbook -i redis-hosts redis.yml
 
 
 
撤回以上操作:
 
[root@master ansible-roles]# ansible redis -i redis-hosts -m shell -a 'pkill redis'
[root@master ansible-roles]# ansible redis -i redis-hosts -m shell -a 'rm -rf /usr/local/redis-4.0.10/'

ansible安装新机zabbix-agent

 

方法一:

当前目录下编写脚本 
vim zabbix-agent_install.sh
#!/bin/bash
#Zabbix-Agent  5.0

Zabbix_Service=192.168.10.13

#安装zabbix源、aliyun YUM源
cd /etc/yum.repos.d/
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i.bak 's#repo.zabbix.com#mirrors.aliyun.com/zabbix#' zabbix.repo
sed -i 's#enabled=0#enabled=1#' zabbix.repo

#安装zabbix
yum -y install zabbix-agent

#修改配置文件
sed -i.bak "s/^Server=127.0.0.1/Server=$Zabbix_Service/" /etc/zabbix/zabbix_agentd.conf
sed -i "s/^ServerActive=127.0.0.1/ServerActive=$Zabbix_Service/" /etc/zabbix/zabbix_agentd.conf
sed -i "/^Hostname=/c Hostname=$(echo `hostname`)" /etc/zabbix/zabbix_agentd.conf

#开启zabbix-agent服务 10050 端口
systemctl start zabbix-agent

#设置开机自启
systemctl enable zabbix-agent

#输出提示
echo -e "Now you can use \033[32mnetstat -tnlp\033[0m check \033[33mport:10050\033[0m"
 
执行
[root@master ansible-roles]# ansible redis -i redis-hosts -m script -a './zabbix-agent_install.sh'

撤回操作:


ansible redis -i redis-hosts -m shell -a 'rpm -e zabbix-release-5.0-1.el7.noarch'
ansible redis -i redis-hosts -m shell -a 'rpm -e zabbix-agent-5.0.3-1.el7.x86_64'
ansible redis -i redis-hosts -m shell -a 'rm -rf /etc/yum.repos.d/zabbix*'

 

方法二:

[root@master ansible-roles]# cd roles/
[root@master roles]# ls
nginx_install  redis_install
[root@master roles]# mkdir zabbix-agent_install/{defaults,files,handlers,tasks,templates} -p

2、tasks/main.yml文件内容


---
  - name: copy zabbix.repo
    copy:
      src: zabbix.repo
      dest: /etc/yum.repos.d/
      force: no
  - name: Install zabbix-agent
    yum:
      name: ['zabbix-agent']
      state: present
  - name: configure zabbix-agent.conf
    template:
          src: /app/ansible-roles/roles/zabbix-agent_install/templates/zabbix_agentd.conf.j2
          dest: /etc/zabbix/zabbix-agentd.conf
          backup: True
  - name: start && enable zabbix-agent
    systemd:
      name: zabbix-agent
      state: started
      enabled: yes
 

ansible-playbook -i redis-hosts install.yml
 

你可能感兴趣的:(ansible,zabbix,redis,linux)