redis使用playbook批量安装

时间:2022-07-22
本文章向大家介绍redis使用playbook批量安装,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

我们想使用playbook来安装redis服务,redis不能使用默认端口,要加入systemd系统服务里,且启动时以端口号来区分并启动

目录结构

把这些文件贴一下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

$ cat redis/tasks/main.yml - name: apt depend pkg   apt: name={{ item }} state=latest   with_items:     - build-essential     - tcl - name: copy redis   copy: src=redis-4.0.11.tar.gz dest=/usr/local/src - name: tar redis   shell: chdir=/usr/local/src/ tar -zxf redis-4.0.11.tar.gz - name: mkdir /usr/lcoal/redis   file: path=/usr/local/redis state=directory - name: install redis   shell: chdir=/usr/local/src/redis-4.0.11 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: add PATH   shell: ln -s /usr/local/redis/bin/* /usr/local/sbin/ - name: add systemd service   template: src=redis.service.j2 dest=/lib/systemd/system/redis@.service - name: reload systemd   shell: systemctl daemon-reload - name: auto start redis-{{ port|default(6397) }}   shell: systemctl enable redis@{{ port|default(6397) }} - name: start redis-{{ port|default(6397) }}   shell: systemctl start redis@{{ port|default(6397) }}

redis的主配置文件

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

$ cat redis/templates/redis.conf.j2 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) }} 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

redis的自定义服务模版

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

$ cat redis/templates/redis.service.j2 [Unit] Description=redis%I After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/redis-%I.pid ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-%I.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target

添加到/etc/init.d/目录下

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

$ cat redis/templates/redis.j2          #!/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 $ cat redis/redis.yml --- - hosts: redis   remote_user: tcmedia   roles:   - redis

这里需要注意的是如何根据不同的端口来启动对应的redis,我们这里使用了systemd服务的模版写法,具体写法可参考:https://blog.mallux.me/2017/02/13/systemd/

后面会对systemd做下研究

执行playbook

1 2

ansible-playbook -C redis.yml -e port=9998 ansible-playbook redis.yml -e port=9998

安装完之后,我们看下被控端redis的启动方式

看下我们的redis自定义服务模版