SaltStack安装Redis-第十篇
实验环境
node1 192.168.56.11 角色 salt-master
node2 192.168.56.12 角色 salt-minon
完成内容
Salt远程安装Redis服务
步骤
在前面的文章中已经搭建好了salt-master和saltminion环境
一,在prod目录下创建redis相关的目录,存放状态文件
[root@linux-node1 ~]# cd /srv/salt/prod/ [root@linux-node1 prod]# mkdir modules/redis -p [root@linux-node1 prod]# tree . └── modules └── redis
二,进入redis目录创建redis基础状态文件,这里我们用简单的rpm包按照为例
[root@linux-node1 redis]# cat redis-install.sls
redis-install:
pkg.installed:
- name: redis
三,有时候我们修改redis的配置文件或创建集群
[root@linux-node1 prod]# pwd
/srv/salt/prod
[root@linux-node1 prod]# mkdir redis-cluster
[root@linux-node1 prod]# cd redis-cluster/
[root@linux-node1 redis-cluster]# vi redis-master.sls
[root@linux-node1 redis-cluster]# cat redis-master.sls
include:
- modules.redis.redis-install
redis-master-config:
file.managed:
- name: /etc/redis.conf
- source: salt://redis-cluster/files/redis-master.conf
- user: root
- group: root
- mode: 644
- template: jinja
- defaults:
REDIS_MEM: 1G
redis-master-service:
service.running:
- name: redis
- enable: True
- watch:
- file: redis-master-config
四,按照redis取配置文件作为salt模板
[root@linux-node1 redis-cluster]# yum install redis
[root@linux-node1 redis-cluster]# cp /etc/redis.conf /srv/salt/prod/redis-cluster/files/
[root@linux-node1 redis-cluster]# tree
.
├── files
│ └── redis.conf
└── redis-master.sls
五,重命名redis模板文件名
[root@linux-node1 files]# mv redis.conf redis-master.conf [root@linux-node1 files]# pwd /srv/salt/prod/redis-cluster/files
六,更改redis配置文件模板,bind也可以只监听内网端口
[root@linux-node1 files]# grep -E 'bind|daemonize|maxmemory' redis-master.conf |grep -v ^# bind 0.0.0.0 daemonize yes maxmemory {{ REDIS_MEM }}
七,测试,因为是在prod目录下 需要添加 saltenv=prod环境变量
[root@linux-node1 redis-cluster]# salt 'linux-node2*' state.sls redis-cluster.redis-master test=True saltenv=prod linux-node2.example.com: ---------- ID: redis-install Function: pkg.installed Name: redis Result: None Comment: The following packages are set to be installed/updated: redis Started: 22:45:55.034779 Duration: 2514.889 ms Changes: ---------- ID: redis-master-config Function: file.managed Name: /etc/redis.conf Result: None Comment: The file /etc/redis.conf is set to be changed Started: 22:45:57.551713 Duration: 27.659 ms Changes: ---------- newfile: /etc/redis.conf ---------- ID: redis-master-service Function: service.running Name: redis Result: None Comment: Service is set to be started Started: 22:45:57.637546 Duration: 71.324 ms Changes: Summary ------------ Succeeded: 3 (unchanged=3, changed=1) Failed: 0 ------------ Total states run: 3
八,执行redis状态模块
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@linux-node1 redis-cluster]# salt 'linux-node2*' state.sls redis-cluster.redis-master saltenv=prod linux-node2.example.com: ---------- ID: redis-install Function: pkg.installed Name: redis Result: True Comment: The following packages were installed/updated: redis Started: 22:48:16.616612 Duration: 15732.74 ms Changes: ---------- jemalloc: ---------- new: 3.6.0-1.el7 old: redis: ---------- new: 3.2.10-2.el7 old: ---------- ID: redis-master-config Function: file.managed Name: /etc/redis.conf Result: True Comment: File /etc/redis.conf updated Started: 22:48:32.351877 Duration: 45.19 ms Changes: ---------- diff: --- +++ @@ -44,7 +44,7 @@ # # Examples: # -# bind 192.168.1.100 10.0.0.1 +#bind 0.0.0.0 # bind 127.0.0.1 ::1 # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the @@ -58,7 +58,7 @@ # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -bind 127.0.0.1 +bind 0.0.0.0 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. @@ -125,7 +125,7 @@ # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. -daemonize no +daemonize yes # If you run Redis from upstart or systemd, Redis can interact with your # supervision tree. Options: @@ -534,7 +534,7 @@ # limit for maxmemory so that there is some free RAM on the system for slave # output buffers (but this is not needed if the policy is 'noeviction'). # -# maxmemory <bytes> +maxmemory 1G # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached. You can select among five behaviors: mode: 0644 user: root ---------- ID: redis-master-service Function: service.running Name: redis Result: True Comment: Service redis has been enabled, and is running Started: 22:48:32.412154 Duration: 453.972 ms Changes: ---------- redis: True Summary ------------ Succeeded: 3 (changed=3) Failed: 0 ------------ Total states run: 3
九,登陆node2节点查看redis服务已经成功启动
[root@linux-node2 ~]# ps aux |grep redis redis 5147 0.3 0.3 142904 5808 ? Ssl 22:48 0:00 /usr/bin/redis-server 0.0.0.0:6379 root 5192 0.0 0.0 112648 976 pts/0 S+ 22:49 0:00 grep --color=auto redis [root@linux-node2 ~]#
总结
1.生产环境我们的状态模块可以在prod下面,在执行的时候需要设置环境saltenv=prod(使用top.sls不需要设置环境变量)
2.记得使用test=True先测试
3.提前查清楚软件包和相关配置文件
4.当使用jinja模板管理时,可以不用登陆redis服务器就可以查看redis设置的最大内存
附 赵班长的 GitHub saltbook-code网址
https://github.com/unixhot/saltbook-code/tree/master
志不强者智不达