堕落不振功业废,勤耕不辍日月新

saltstack自动安装配置redis-3.2.8

Redis hailen 328℃

一、准备redis自动化配置的文件

即安装一遍redis,然后获取相关文件和配置在salt中执行上线

1、 源码安装redis3.2.8并注册为系统服务

安装依赖

yum install -y tcl

1.1 下载安装包Redis-3.2.8.tar.gz

# cd /usr/local/src
# wget http://download.redis.io/releases/redis-3.2.8.tar.gz

1.2 解压及安装

[root@node2 src]# tar zxf redis-3.2.8.tar.gz
[root@node2 src]# cd redis-3.2.8/src/
[root@node2 redis-3.2.8]# make PREFIX=/usr/local/redis install #指定安装路径

1.3 创建配置文档,修改配置

创建配置文档路径

# mkdir /etc/redis
[root@node2 src]# cp ../redis.conf /etc/redis/redis_6350.conf

以下几个参数常用到

daemonize yes # 后台运行
bind 127.0.0.1 # 绑定ip,需要外网访问时将其注释掉
protected-mode yes # 保护模式,默认是开启的,需要其他客户端链接时,改为no关闭
requirepass redispass # 其他客户端链接时的密码
appendonly yes # 每次更新后记录日志
pidfile /var/run/redis_6350.pid # 如果不是默认的6379端口需要修改该行

1.4 注册系统服务,开机自启

创建启动脚本

# cp ../utils/redis_init_script /etc/rc.d/init.d/redis3

修改redis启动脚本,要修改的地方有

添加 chkconfig 注释

redis-server 、redis-cli 、pidfile、redis.conf路径

如果需要配置密码,还要在停止命令增加参数 -a 指定密码

[root@node2 src]# cat /etc/init.d/redis3 

#!/bin/sh
# chkconfig:   2345 90 10
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6350
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis_${REDISPORT}.conf"

usage(){
    echo "usage: $0 [start|stop|status|restart]"
}

redis_start(){
if [ -f $PIDFILE ]
    then
            echo "$PIDFILE exists, process is already running or crashed"
    else
            echo "Starting Redis server..."
            $EXEC $CONF
fi
}

redis_stop(){
    if [ ! -f $PIDFILE ]
    then
            echo "$PIDFILE does not exist, process is not running"
    else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            $CLIEXEC -p $REDISPORT -a redispass shutdown
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Redis to shutdown ..."
                sleep 1
            done
            echo "Redis stopped"
    fi
}

redis_restart(){
    redis_stop
    sleep 1
    redis_start
}
redis_status(){
    ps -ef|grep redis|grep -v grep|grep -v status
}

main(){
    case "$1" in
        start)
            redis_start;;
        stop)
            redis_stop;;
        status)
            redis_status;; 
        restart)
            redis_restart;;
        *)
            usage;
    esac
}

main $1

保存后执行注册成系统服务:

chkconfig --add redis3
chkconfig redis3 on

完成后,可以使用 service redis3 start|stop 启动关闭redis服务

1.5 添加环境变量:

vim /etc/profile

在最后添加:

PATH=$PATH:/usr/local/redis/bin
export PATH

# 使配置生效
source /etc/profile

# service redis3 start
[root@node2 src]# ps -ef|grep redis
root     20818     1  0 09:57 ?        00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6350
root     20827 14125  0 09:58 pts/0    00:00:00 grep --color=auto redis

用redis-cli 链接,set,get正常

[root@node2 src]# redis-cli -p 6350 -a redispass

127.0.0.1:6350> set name 2
OK
127.0.0.1:6350> get name
"2"
127.0.0.1:6350> set jack 18
OK
127.0.0.1:6350> get jack
"18"
127.0.0.1:6350> quit

二、salt相关的配置处理

# mkdir -p /srv/salt/prod/pkg /srv/salt/prod/redis /srv/salt/prod/redis/files 
# cd /srv/salt/prod/pkg

1、 初始化nginx相关配置文件

①下载redis-3.2.8.tar.gz上传到/srv/salt/prod/redis/files目录

②配置文件

[root@test7_chat_api_im files]# grep '^[a-Z]' redis_6350.conf 
bind 127.0.0.1
protected-mode yes
port 6350
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6350.pid
loglevel notice
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
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 redispass
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
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

③服务管理脚本

[root@test7_chat_api_im files]# cat redis3 
#!/bin/sh
# chkconfig:   2345 90 10
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6350
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis_${REDISPORT}.conf"

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 -a redispass shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

2、 编写依赖包安装

vim /srv/salt/prod/pkg/pkg-init.sls

pkg-init:
  pkg.installed:
    - names:
      - gcc
      - gcc-c++
      - glibc
      - make
      - autoconf
      - openssl
      - openssl-devel
      - pcre
      - pcre-devel
      - glib
      - glib-devel
      - tcl

3、 用户添加模块

# mkdir /srv/salt/prod/user
# vim /srv/salt/prod/user/redis.sls 
redis-user-group:
  group.present:
    - name: redis
    - gid: 6350

  user.present:
    - name: redis
    - fullname: redis
    - shell: /sbin/nologin
    - uid: 6350
    - gid: 6350

4、 编写nginx状态模块

# cd /srv/salt/prod/redis
vim /srv/salt/prod/redis/install.sls

include:
  - pkg.pkg-init
  - user.redis
redis-source-install:
  file.managed:
    - name: /usr/local/src/redis-3.2.8.tar.gz
    - source: salt://redis/files/redis-3.2.8.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src && tar zxf redis-3.2.8.tar.gz && cd cd redis-3.2.8/src/ && make PREFIX=/usr/local/redis install && chown -R redis:redis /usr/local/redis && mkdir /etc/redis
    - unless: test -d /usr/local/redis
    - require:
      - user: redis-user-group
      - file: redis-source-install
      - pkg: pkg-init

服务模块

# vim /srv/salt/prod/redis/service.sls 
include:
  - redis.install

redis-init:
  file.managed:
    - name: /etc/init.d/redis3
    - source: salt://redis/files/redis3
    - mode: 755
    - user: root
    - group: root
    - require:
      - cmd: redis-source-install
  cmd.run:
    - name: chkconfig --add redis3
    - unless: chkconfig --list | grep redis
    - require:
      - file: redis-init

/etc/redis/redis_6350.conf:
  cmd.run:
    - name: mkdir /etc/redis
  file.managed:
    - source: salt://redis/files/redis_6350.conf
    - user: redis
    - group: redis
    - mode: 644 

redis-service:
  service.running:
    - name: redis3
    - enable: True
    - restart: True
    - require:
      - cmd: redis-init
    - watch:
      - file: /etc/redis/redis_6350.conf

执行配置测试,没有问题再安装,至此salt安装redis服务已完毕:

# 先测试先处理一些简单的错误

salt 'test4_haili_dev' state.sls redis.service env=prod test=True

salt 'test4_haili_dev' state.sls redis.service env=prod

转载请注明:我是IT » saltstack自动安装配置redis-3.2.8

喜欢 (0)or分享 (0)