一、部署Redis主从
需求:
- 192.168.56.11是主,192.168.56.12是从
- redis监听自己的ip地址,而不是0.0.0.0
分析:
linux-node1 安装 配置 启动
linux-node2 安装 配置 启动 设置主从
[[email protected] ~]# yum install redis -y [[email protected]-node1 prod]# mkdir redis/files -p [[email protected]-node1 redis]# cp /etc/redis.conf /srv/salt/prod/redis/files/ [[email protected]-node1 redis]# tree . ├── files │ └── redis.conf └── init.sls 1 directory, 2 files [[email protected]-node1 redis]# vim init.sls redis-install: pkg.installed: - name: redis redis-config: file.managed: - name: /etc/redis.conf - source: salt://redis/files/redis.conf - user: root - group: root - mode: 644 - template: jinja defaults: PORT: 6379 IPADDR: {{ grains[‘fqdn_ip4‘][0] }} redis-service: service.running: - name: redis - enable: True - reload: True [[email protected]-node1 redis]# salt ‘*‘ state.sls redis.init saltenv=prod #测试单一执行sls是否成功 [[email protected]-node1 redis]# netstat -tulnp|grep redis-server tcp 0 0 192.168.56.11:6379 0.0.0.0:* LISTEN 10186/redis-server [[email protected]-node2 ~]# netstat -tulnp |grep redis-server tcp 0 0 192.168.56.12:6379 0.0.0.0:* LISTEN 17973/redis-server 主从配置: [[email protected]-node1 redis]# vim master.sls include: - redis.init [[email protected]-node1 redis]# vim slave.sls include: - redis.init slave_config: cmd.run: - name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379--->设置主从 - unless: redis-cli -h 192.168.56.12 info |grep role:slave-->判断node2是否为从,如果是就不执行设置主从 - require: - service: redis-service [[email protected]-node1 redis]# vim /srv/salt/base/top.sls #配置top file prod: ‘linux-node1.example.com‘: - lamp - redis.master ‘linux-node2.example.com‘: - lamp - redis.slave [[email protected]-node1 redis]# salt ‘*‘ state.highstate ...... ---------- ID: slave_config Function: cmd.run Name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379 Result: True Comment: Command "redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379" run Started: 12:08:46.428924 Duration: 31.328 ms Changes: ---------- pid: 18132 retcode: 0 stderr: stdout: OK Summary for linux-node2.example.com ------------- Succeeded: 14 (changed=1) Failed: 0 ------------- Total states run: 14 Total run time: 1.527 s ...... [[email protected]-node1 redis]# tree . ├── files │ └── redis.conf ├── init.sls ├── master.sls └── slave.sls 1 directory, 4 files [[email protected]-node1 redis]# cat slave.sls include: - redis.init slave_config: cmd.run: - name: redis-cli -h 192.168.56.12 slaveof 192.168.56.11 6379 - unless: redis-cli -h 192.168.56.12 info |grep role:slave - require: - service: redis-service
TIPS:生产环境中,务必使用test=True进行与测试,并且目标选择一个节点进行,避免错误,影响业务的运行。
二、SaltStack–Job管理
官方文档:https://docs.saltstack.com/en/2016.11/ref/modules/all/salt.modules.saltutil.html
在SaltStack里面执行任何一个操作都会在Master上产生一个jid号。Minion端会在cache目录下的proc目录创建一个以jid为名称的文件,这个文件里面的内容就是记录此次操作的记录,当操作处理完成后改文件会自动删除。而master端会记录每次操作的详细信息,这个记录都是存到在Master端cache目录下的jobs下。
[[email protected] ~]# cd /var/cache/salt/master/jobs/----->任务管理目录 [[email protected]-node1 jobs]# pwd /var/cache/salt/master/jobs [[email protected]-node1 jobs]# ls 07 0e 2f 3a 44 4c 53 5c 72 92 ac b2 bf e6 f4 0c 0f 34 3f 45 4e 5a 63 8b 93 ad b9 c1 e9 fb 0d 13 37 43 49 52 5b 64 8c a5 af be c4 f1 fe [[email protected]-node1 linux-node1.example.com]# pwd /var/cache/salt/master/jobs/07/f8d6ec1380412c95718d931cfb300e793f6b7316d58ad3f34dd57052ca178f/linux-node1.example.com [[email protected]-node1 linux-node1.example.com]# ll total 8 -rw------- 1 root root 10 Jan 20 09:39 out.p -rw------- 1 root root 1748 Jan 20 09:39 return.p---->结果返回 [[email protected]-node1 ~]# grep "#keep_jobs: 24" /etc/salt/master #keep_jobs: 24 默认的缓存是24小时,可以进行修改。管理job是模块进行管理,由执行模块进行管理:SALT.MODULES.SALTUTIL salt ‘*‘ saltutil.clear_cache 清除缓存 salt ‘*‘ saltutil.find_job <job id> 查找当前在运行的job,并返回它的id salt ‘*‘ saltutil.is_running 查看当前在运行的job salt ‘*‘ saltutil.kill_job <job id> 杀死job
原文地址:https://www.cnblogs.com/linuxk/p/9273499.html
时间: 2024-09-30 19:43:43