Ansible搭建redis主从+哨兵模式

Ansible安装主从哨兵redis

一、环境准备:三台服务器

112.124.35.48

120.26.50.173

121.41.13.43

目录结构:

Ansible搭建redis主从+哨兵模式_第1张图片

二、主机内容

vim host_passwd.txt

112.124.35.48 523320

120.26.50.173 523320

121.41.13.43 523320

vim key.sh

#!/bin/bash

Public_Key() {

[ ! -f /usr/bin/expect ] && yum install expect -y &>/dev/null

/usr/bin/expect <

set timeout 300

spawn ssh-keygen

expect "(/root/.ssh/id_rsa):"

send "\n"

expect  {

                "Overwrite" { send "n\n" }

                "Enter passphrase (empty for no passphrase):" {

                                send "\n"

                                expect "Enter same passphrase again:"

                                send "\n" }

}

expect eof

EOF

while read line;do

host=$(echo $line | awk '{print $1}')

passwd=$(echo $line | awk '{print $2}')

/usr/bin/expect <

spawn ssh-copy-id $host

expect {

                "yes/no" { send "yes\n"; exp_continue }

                "password:" { send "$passwd\n"}

}

expect eof

EOF

done < ./host_passwd.txt

}

Public_Key

vim redis-hosts  #redis安装服务器

[redis]

112.124.35.48 master=1

120.26.50.173 master=0

121.41.13.43 master=0

[redis:vars]

master_ip=112.124.35.48

redis_port=6379

redis_pass="Ab123456"

redis_log="/var/log/redis.log"

vim redis.yml #这个脚本相当于快捷方式的存在

---

- hosts: all

  roles:

  - redis

vim roles/redis/defaults/main.yml #ansible的执行会默认先寻找执行main.yml问价,并且只能同时执行五个任务

basedir: /usr/local/redis-5.0.12

redis_name: redis-5.0.12.tar.gz

redis_conf: redis.conf.j2

sentinel_conf: sentinel.conf.j2

redis_service: redis.service.j2

sentinel_service: sentinel.service.j2

vim 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: configure redis.service

    template:

          src: "{{ redis_service }}"

          dest: /usr/lib/systemd/system/redis.service

          backup: True

  - name: configure sentinel.service

    template:

          src: "{{ sentinel_service }}"

          dest: /usr/lib/systemd/system/sentinel.service

          backup: True

  - name: enable && start redis.service

    systemd:

      daemon_reload: yes

      name: redis

      state: started

      enabled: yes

  - name: enable && start redis.sentinel

    systemd:

      daemon_reload: yes

      name: sentinel

      state: started

      enabled: yes

三、模块文价调用内容

vim roles/redis/templates/redis.conf.j2 # redis配置文件内容

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 }}

replica-serve-stale-data yes

replica-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5

requirepass "{{ redis_pass }}"

appendfilename "appendonly.aof"

{% if master!=1 %}

slaveof {{ master_ip }} {{ redis_port }}

{% endif %}

vim roles/redis/templates/redis.service.j2

[Unit]

Description=Redis

After=network.target

[Service]

Type=forking

PIDFile=/var/run/redis_6379.pid

ExecStart=/usr/local/redis-5.0.12/src/redis-server /usr/local/redis-5.0.12/redis.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/usr/local/redis-5.0.12/src/redis-cli -p 6379 shutdown

PrivateTmp=true

[Install]

WantedBy=multi-user.target

vim roles/redis/templates/sentinel.conf.j2

bind 0.0.0.0

protected-mode no

port 26379

daemonize yes

pidfile /var/run/redis-sentinel.pid

logfile "./sentinel.log"

dir /tmp

sentinel monitor mymaster {{ master_ip }} {{ redis_port }} 2

sentinel auth-pass mymaster {{ redis_pass }}

sentinel down-after-milliseconds mymaster 10000

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 60000

sentinel deny-scripts-reconfig yes

vim roles/redis/templates/sentinel.service.j2

[Unit]

Description=Redis

After=network.target

[Service]

Type=forking

PIDFile=/var/run/redis-sentinel.pid

ExecStart=/usr/local/redis-5.0.12/src/redis-sentinel /usr/local/redis-5.0.12/sentinel.conf

ExecStop=/usr/local/redis-5.0.12/src/redis-cli -p 26379 shutdown

ExecReload=/bin/kill -s HUP $MAINPID

#ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

[Install]

WantedBy=multi-user.target

最后执行ansible-playbook redis.yml

你可能感兴趣的:(redis,ansible,数据库)