ansible-playbook 安装redis 主从

ansible-playbook 安装redis 主从

 手动在测试机上安装一遍redis,最好使用utils下面的install_server.sh安装服务,然后将redis的配置文件和init需要的执行文件 拷贝备用

 

配置hosts:

[root@ansible ansible]#grep -v '^#' ./hosts |grep -v '^$'

[redis:children]

redis_master

redis_slave

[redis_master]      #命名规则使用下划线,不然后面的调用有问题

192.168.3.12

[redis_slave]

192.168.3.13

192.168.3.14 

 

 建立redis角色的文件夹:

[root@ansible redis]#mkdir /etc/ansible/roles/redis/{files,templates,tasks,handlers,vars,default,meta} -pv

roles/redis/

├── default

├── files

│ └── redis-4.0.8.tar.gz

├── handlers

├── meta

├── tasks

│ └── main.yml

├── templates

│ ├── redis.j2

│ └── redis.conf.j2

└── vars

[root@ansible redis]# cat tasks/main.yml

- name: yum gcc

  yum: name=gcc state=present

- name: copy redis

  copy: src=redis-4.0.8.tar.gz dest=/usr/local/src/

- name: tar redis

  shell: chdir=/usr/local/src/ tar -zxf redis-4.0.8.tar.gz

- name: install redis

  shell: chdir=/usr/local/src/redis-4.0.8 make PREFIX=/usr/local/redis install

- name: mkdir /usr/local/redis/conf/

  file: path=/usr/local/redis/conf state=directory

- name: copy redis_{{ port|default(6397) }}.conf

  template: src=redis.conf.j2 dest=/usr/local/redis/conf/redis_{{ port|default(6397) }}.conf

- name: copy /etc/init.d/redis_{{ port|default(6397) }}

  template: src=redis.j2 dest=/etc/init.d/redis_{{ port|default(6397) }} mode=755

- name: mkdir /var/lib/redis/{{ port|default(6397) }}

  file: path=/var/lib/redis/{{ port|default(6397) }} state=directory

- name: chkconfig redis_{{ port|default(6397) }}

  shell: chkconfig --add redis_{{ port|default(6397) }}

- name: systemctl enable redis_{{ port|default(6397) }}

  shell: systemctl enable redis_{{ port|default(6397) }}

- name: open firewalld {{ port|default(6397) }}

  shell: firewall-cmd --zone=public --add-port={{ port|default(6397) }}/tcp --permanent

- name: restart firewalld

  service: name=firewalld state=restarted

- name: start redis_{{ port|default(6397) }}

  service: name=redis_{{ port|default(6397) }} state=started

 

[root@ansible redis]#cat templates/redis.j2         #文件来源于make install之后用redis文件下面的utils下面的install_server.sh安装服务后的拷贝 主要是端口用{{ port|default(6397) }}这个代替

#!/bin/sh

#Configurations injected by install_server below....

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_{{ port|default(6397) }}.pid

CONF="/usr/local/redis/conf/redis_{{ port|default(6397) }}.conf"

REDISPORT="{{ port|default(6397) }}"

###############

# SysV Init Information

# chkconfig: - 58 74

# description: redis_{{ port|default(6397) }} is the redis daemon.

### BEGIN INIT INFO

# Provides: redis_{{ port|default(6397) }}

# Required-Start: $network $local_fs $remote_fs

# Required-Stop: $network $local_fs $remote_fs

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Should-Start: $syslog $named

# Should-Stop: $syslog $named

# Short-Description: start and stop redis_{{ port|default(6397) }}

# Description: Redis daemon

### END INIT INFO

case "$1" in

    start)

        if [ -f $PIDFILE ]

        then

            echo "$PIDFILE exists, process is already running or crashed"

        else

            echo "Starting Redis server..."

            $EXEC $CONF

        fi

        ;;

    stop)

        if [ ! -f $PIDFILE ]

        then

            echo "$PIDFILE does not exist, process is not running"

        else

            PID=$(cat $PIDFILE)

            echo "Stopping ..."

            $CLIEXEC -p $REDISPORT shutdown

            while [ -x /proc/${PID} ]

            do

                echo "Waiting for Redis to shutdown ..."

                sleep 1

            done

            echo "Redis stopped"

        fi

        ;;

    status)

        PID=$(cat $PIDFILE)

        if [ ! -x /proc/${PID} ]

        then

            echo 'Redis is not running'

        else

            echo "Redis is running ($PID)"

        fi

        ;;

    restart)

        $0 stop

        $0 start

        ;;

    *)

        echo "Please use start, stop, restart or status as first argument"

        ;;

esac

 

[root@ansible redis]#grep -v '^#' templates/redis.conf.j2 |grep -v '^$'   #文件来源于make install之后用redis文件下面的utils下面的install_server.sh安装服务后的拷贝 主要是端口用{{ port|default(6397) }}这个代替

bind 127.0.0.1 {{ inventory_hostname }}

protected-mode yes

port {{ port|default(6397) }}

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize yes

supervised no

pidfile /var/run/redis_{{ port|default(6397) }}.pid

loglevel notice

logfile /var/log/redis_{{ port|default(6397) }}.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 /var/lib/redis/{{ port|default(6397) }}

{% if inventory_hostname in groups.redis_slave %}

slaveof {{ groups.redis_master[0] }} {{ port|default(6397) }}

{% endif %}

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

lazyfree-lazy-eviction no

lazyfree-lazy-expire no

lazyfree-lazy-server-del no

slave-lazy-flush no

appendonly no

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

 

配置安装yml:

[root@ansible ansible]# cat redis.yml

- hosts: redis

  remote_user: root

  roles:

  - redis

 

[root@ansible ansible]# ansible-playbook --check redis.yml -e port=7000       # -e port=7000 不是必须的

[root@ansible ansible]# ansible-playbook  redis.yml -e port=7000                  # -e port=7000 不是必须的

[root@ansible ansible]# ansible-playbook  redis.yml -e port=7001  --start-at-task="copy redis_{{ port|default(6397) }}.conf"       #安装多个redis的时候可以从这个task开始

 

对于 sentinel 可以安装在redis服务器,也可以安装到单独的服务器上。

posted on 2018-02-24 17:13  徐应钟  阅读(874)  评论(0编辑  收藏  举报

导航