使用ansible-playbook自动化安装redis哨兵
【使用自动化安装redis哨兵架构】
说明:使用ansible-playbook 自动化安装redis一主两从3哨兵 + redis-exporter的采集数据
【剧本说明】
以下文件在roles目录下
tree roles/redis_sentinel/
roles/redis_sentinel/
├── tasks
│ ├── install_exporter.yml 数据采集剧本
│ └── main.yml 主剧本信息
├── templates
│ ├── readme.md
│ ├── redis.conf 配置文件
│ ├── redis-exporter.service 数据采集的服务信息
│ ├── redis.service 数据库的服务信息
│ ├── sentinel.conf 哨兵的配置文件
│ ├── sentinel.service 哨兵的服务信息
└── vars
└── main.yml
【对应目录创建脚本】
tasks目录
vim main.yml
---
- name: add redis user
user:
name: redis
system: yes
- name: create data directory
file:
path: '{{install_dir}}/redis_{{item.0}}/{{item.1}}'
state: directory
owner: redis
group: redis
recurse: yes
with_nested:
- ['{{instance_port}}','{{sentinel_port}}']
- ['conf', 'log', 'run', 'data']
- name: download binary
copy: src={{download_target}}/{{item.name}} dest=/usr/local/bin/{{item.name}} mode=0755
with_items:
- { name: 'redis-benchmark' }
- { name: 'redis-check-aof' }
- { name: 'redis-check-rdb' }
- { name: 'redis-cli' }
- { name: 'redis-sentinel' }
- { name: 'redis-server' }
- name: disable hugepage
shell: echo never > /sys/kernel/mm/transparent_hugepage/enabled
- name: disable hugepage permanent
lineinfile:
path: /etc/rc.local
line: echo never > /sys/kernel/mm/transparent_hugepage/enabled
- name: config redis instance
template:
src: redis.conf
dest: '{{install_dir}}/redis_{{instance_port}}/conf/'
owner: redis
group: redis
register: configure_instance_result
- name: make redis serivce
template:
src: redis.service
dest: '/etc/systemd/system/redis-{{instance_port}}.service'
- name: start redis
systemd:
name: 'redis-{{instance_port}}'
daemon_reload: yes
state: restarted
when: configure_instance_result is changed
- name: config sentinel
template:
src: sentinel.conf
dest: '{{install_dir}}/redis_{{sentinel_port}}/conf/redis.conf'
owner: redis
group: redis
register: configure_sentinel_result
- name: make sentinel service
template:
src: sentinel.service
dest: '/etc/systemd/system/redis-{{sentinel_port}}.service'
- name: start sentinel
systemd:
name: 'redis-{{sentinel_port}}'
daemon_reload: yes
state: restarted
when: configure_sentinel_result is changed
- name: get master
run_once: yes
set_fact:
master_list_str: '{% for host in groups["redis_sentinel"] %}{% if "master" in hostvars[host] and hostvars[host].master %}{{host}},{% endif %}{% endfor %}'
- name: set fact
run_once: yes
set_fact:
master_list: '{{master_list_str[0:-1].split(",")}}'
- name: config slave node
shell: /usr/local/bin/redis-cli -h {{ansible_host}} -p {{instance_port}} -a '{{password}}' slaveof {{master_list[0]}} {{instance_port}}
when: not ansible_host in master_list
- name: config redis service enable
systemd:
name: 'redis-{{ instance_port }}'
enabled: yes
- name: config sentinel service enable
systemd:
name: 'redis-{{ sentinel_port }}'
enabled: yes
- name: install Exporter
include: install_exporter.yml
tags:
- redis_exporter
vim install_exporter.yml
---
- name: Download redis Exporter
copy: src={{download_target}}/{{exporter_binary}} dest=/usr/local/bin/{{exporter_binary}} mode=0755
- name: Add redis exporter system server
template:
dest: /etc/systemd/system/redis-exporter.service
src: redis-exporter.service
- name: Ensure redis exporter is enabled
systemd:
daemon_reload: yes
name: redis-exporter
enabled: yes
- name: Start redis exporter
service:
name: redis-exporter
state: restarted
enabled: yes
templates目录
vim redis-exporter.service
[Unit]
Description=redis_exporter
Documentation=https://github.com/oliver006/redis_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/redis-exporter -redis.addr=127.0.0.1:6379 -redis.password=Cqhhyun!8090
Restart=on-failure
[Install]
WantedBy=multi-user.target
vim redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server {{install_dir}}/redis_{{instance_port}}/conf/redis.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
vim redis.conf
daemonize yes
port {{instance_port}}
pidfile {{install_dir}}/redis_{{instance_port}}/run/redis.pid
logfile {{install_dir}}/redis_{{instance_port}}/log/redis.log
save 900 1
save 300 10
save 60 10000
dbfilename redis.rdb
maxmemory 2048MB
dir {{install_dir}}/redis_{{instance_port}}/data/
requirepass {{password}}
masterauth {{ password }}
rename-command KEYS ""
rename-command SHUTDOWN ""
# rename-command CONFIG ""
# rename-command FLUSHALL ""
# appendonly yes
# appendfilename "redis.aof"
# appendfsync everysec
vim sentinel.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-sentinel {{install_dir}}/redis_{{sentinel_port}}/conf/redis.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli -p {{sentinel_port}} shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
vim sentinel.conf
port {{sentinel_port}}
daemonize yes
logfile {{install_dir}}/redis_{{sentinel_port}}/log/redis.log
dir {{install_dir}}/redis_{{sentinel_port}}/data/
sentinel monitor mymaster {% for host in groups["redis_sentinel"] %}{% if "master" in hostvars[host] and hostvars[host].master %}{{host}} {{instance_port}} {{(groups["redis_sentinel"] | length / 2) | round | int}}{% endif %}{% endfor %}
sentinel auth-pass mymaster {{password}}
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 18000
var目录
main.yml
---
install_dir: /data/redis
instance_port: 6379
sentinel_port: 26379
password: Redis_1234
download_target: /tmp/soft
exporter_binary: redis-exporter
【安装包及配置】
环境变量说明,这里默认数据目录为/data,注意redis端口跟sentinel端口
main.yml
---
install_dir: /data/redis
instance_port: 6379
sentinel_port: 26379
password: Redis_1234
download_target: /tmp/soft
exporter_binary: redis-exporter
将编译好的redis文件放到download_target目录下
redis-benchmark,redis-check-aof,redis-check-rdb,redis-cli,redis-sentinel,redis-server,redis-exporter
创建ansible的hosts文件,前面换成你的IP,root用户root密码
vim /etc/ansible/hosts
[redis_sentinel]
IPXXX1 ansible_user=root ansible_ssh_pass=XXX master=true
IPXXX2 ansible_user=root ansible_ssh_pass=XXX
IPXXX3 ansible_user=root ansible_ssh_pass=XXX
创建playbook文件
vim redis_sentinel.yaml
---
- hosts: redis_sentinel
gather_facts: yes
roles:
- redis_sentinel
然后执行自动化安装,等待安装完成即可
ansible-playbook mysql_ms.yaml
【检查】
查看redis服务信息:systemctl status redis-6379.service
查看redis sentinel服务信息:systemctl status redis-26379.service
查看监控信息:systemctl status redis-exporter