1. 配置管理:state和file

https://docs.saltstack.com/en/latest/topics/states/index.html

Full list of states

1.state状态模块

希望主机,apache ,启动状态,关闭状态,

写法1

  1. [root@linux-node1 web]# pwd
  2. /srv/salt/base/web
  3. [root@linux-node1 web]# vim apache.sls
  4. apache:
  5. pkg.installed:
  6. - name: httpd
  7. service.running:
  8. - name: httpd
  9. file.managed:
  10. - name: /etc/httpd/conf/httpd.conf
  11. - source: salt://apache/files/httpd.conf
  12. - usr: root
  13. - group: root
  14. - mode:
  1. Id声明,全局(testdevbase环境)唯一
  2. Pkg 状态模块
  3. . 引用方法
  4. Installed 模块方法
  5. Name: httpd 参数

2.file 文件管理模块

Name :管理文件的路径

在id,Apache下,每个模块只能用一次

写法2

  1. [root@linux-node1 web]# vim apache.sls
  2. apache-install:
  3. pkg.installed:
  4. - name: httpd
  5.  
  6. apache-service:
  7. service.running
  8. - name: httpd
  9.  
  10. apache-config:
  11. file.managed:
  12. - name: /etc/httpd/conf/httpd.conf
  13. - source: salt://apache/files/httpd.conf
  14. - usr: root
  15. - group: root
  16. - mode:

写法3

没有声明name,id就是name

  1. apache:
  2. pkg.installed:
  3. - name: httpd
  4. service.running:
  5. - name: httpd
  6. file.managed:
  7. - name: /etc/httpd/conf/httpd.conf
  8. - source: salt://apache/files/httpd.conf
  9. - usr: root
  10. - group: root
  11. - mode:
  12.  
  13. /etc/httpd/conf/php.conf
  14. file.managed:
  15. - source: salt://apache/files/php.conf
  16. - user: root
  17. - group: root
  18. - mode:

2.自动化安装LAMP:状态设计

1. Pkg模块

指定版本

指定仓库

需要安装的软件包

[root@linux-node1 web]# yum install -y httpd php mysql-server php-mysql php-pdo php-cli

2. jinja模板

监控本地的mac ip

用模板的实现jinja

3.file模块

File可以使用grains

4.Service模块

监控文件,文件更新,自动重载服务

3.LAMP的状态实现

学saltstack,学的是思路,三段式

前期版本:

学习状态,先把安装,配置写在一起

三段式:  安装 配置  启动

创建目录

[root@linux-node1 prod]# pwd

/srv/salt/prod

[root@linux-node1 prod]# mkdir apache

[root@linux-node1 prod]# mkdir php

[root@linux-node1 prod]# mkdir mysql

1.apache

# sls 配置文件

[root@linux-node1 prod]# cd apache/

[root@linux-node1 apache]# vim apache.sls

  1. apache-install:
  2. pkg.installed:
  3. - name: httpd
  4.  
  5. apache-config:
  6. file.managed:
  7. - name: /etc/httpd/conf/httpd.conf
  8. - source: salt://apache/files/httpd.conf
  9. - user: root
  10. - group: root
  11. - mode:
  12.  
  13. apache-service:
  14. service.running:
  15. - name: httpd
  16. - enable: True

source :对应当前目录,相对路径

- source: salt://apache/files/httpd.conf

你这个环境的根路径  salt:   /srv/salt/

# 配置文件,cp

[root@linux-node1 apache]# mkdir files

[root@linux-node1 apache]# cd files/

[root@linux-node1 files]# cp /etc/httpd/conf/httpd.conf .

# 执行命令

默认base目录

[root@linux-node1 files]# salt 'linux-node1*' state.sls apache.apache saltenv=prod

#  test

# init.sls

[root@linux-node1 apache]# pwd

/srv/salt/prod/apache

[root@linux-node1 apache]# mv apache.sls init.sls

2.php

# php目录

Php不需要启动服务,以模块的方式通信

安装多个,查看文档

[root@linux-node1 prod]# ls

apache  mysql  php

[root@linux-node1 prod]# cd php/

[root@linux-node1 php]# mkdir files

[root@linux-node1 php]# vim init.sls

# cp php配置文件

[root@linux-node1 php]# cp /etc/php.ini files/

3.mysql

安装 配置 启动

[root@linux-node1 prod]# vim mysql/init.sls

  1. mysql-install:
  2. pkg.installed:
  3. - pkgs:
  4. - mariadb
  5. - mariadb-server
  6.  
  7. mysql-config:
  8. file.managed:
  9. - name: /etc/my.cnf
  10. - source: salt://mysql/files/my.cnf
  11. - user: root
  12. - group: root
  13. - mode:
  14.  
  15. mysql-service:
  16. service.running:
  17. - name: mariadb
  18. - enable: True

# 配置文件

[root@linux-node1 mysql]# mkdir files

[root@linux-node1 mysql]# cd files/

[root@linux-node1 files]# cp /etc/my.cnf .

4.执行state

文件目录

执行

[root@linux-node1 salt]# salt -S '192.168.194.131' state.sls php.init saltenv=prod

[root@linux-node1 salt]# salt -S '192.168.194.131' state.sls mysql.init saltenv=prod

5. 高级状态.

[root@linux-node1 base]# vim top.sls

[root@linux-node1 base]# pwd

/srv/salt/base

[root@linux-node1 base]# salt 'linux-node1*' state.highstate

4.  配置管理:状态间的关系

1. Include功能

https://docs.saltstack.com/en/latest/topics/tutorials/states_pt3.html

[root@linux-node1 prod]# pwd

/srv/salt/prod

[root@linux-node1 prod]# vim lamp.sls

  1. include:
  2. - apache.init
  3. - php.init
  4. - mysql.init

[root@linux-node1 prod]# vim ../base/top.sls

  1. prod:
  2. 'linux-node1.example.com':
  3. - lamp

[root@linux-node1 prod]# salt -S '192.168.194.131' state.highstate

2.Extend扩展功能

  1. 增加其他功能,修改配置文件,到最终版本
  2. Extend 语法

需求:只能在机器1上php-mbstring 包

  1. [root@linux-node1 prod]# vim lamp.sls
  2. include:
  3. - apache.init
  4. - php.init
  5. - mysql.init
  6.  
  7. extend:
  8. php-install:
  9. pkg.installed:
  10. - name: php-mbstring
  11.  
  12. [root@linux-node1 prod]# salt -S '192.168.194.131' state.highstate

3.Require依赖

需求:if 上个操作,安装不成功或者配置不成功,下一个不执行

(1)反例子

[root@linux-node1 apache]# vim init.sls

[root@linux-node1 apache]# salt -S '192.168.194.131' state.highstate

(2)依赖于上个操作

[root@linux-node1 apache]# systemctl stop httpd

  1. apache-install:
  2. pkg.installed:
  3. - name: httpd
  4.  
  5. apache-config:
  6. file.managed:
  7. - name: /etc/httpd/conf/httpd.conf
  8. - source: salt://apache/files/httpd1.conf
  9. - user: root
  10. - group: root
  11. - mode:
  12.  
  13. apache-service:
  14. service.running:
  15. - name: httpd
  16. - enable: True
  17. - require:
  18. - file: apache-config

[root@linux-node1 apache]# salt -S '192.168.194.131' state.highstate

(3)最终版本:

启动 依赖于 安装,配置

[root@linux-node1 apache]# vim init.sls

[root@linux-node1 apache]# salt -S '192.168.194.131' state.highstate

  1. apache-install:
  2. pkg.installed:
  3. - name: httpd
  4.  
  5. apache-config:
  6. file.managed:
  7. - name: /etc/httpd/conf/httpd.conf
  8. - source: salt://apache/files/httpd.conf
  9. - user: root
  10. - group: root
  11. - mode:
  12.  
  13. apache-service:
  14. service.running:
  15. - name: httpd
  16. - enable: True
  17. - require:
  18. - pkg: apache-install
  19. - file: apache-config

(4)Require  我依赖于谁

Require_in 我被谁依赖

[root@linux-node1 apache]# vim init.sls

  1. apache-install:
  2. pkg.installed:
  3. - name: httpd
  4. - require_in:
  5. - service: apache-service
  6.  
  7. apache-config:
  8. file.managed:
  9. - name: /etc/httpd/conf/httpd.conf
  10. - source: salt://apache/files/httpd.conf
  11. - user: root
  12. - group: root
  13. - mode:
  14. - require-in:
  15. - service: apache-service
  16.  
  17. apache-service:
  18. service.running:
  19. - name: httpd
  20. - enable: True

4.Watch功能:同时有require功能

https://docs.saltstack.com/en/latest/ref/states/all/salt.states.service.html#salt.states.service.mod_watch

该配置文件变化,这个服务重启,重载

[root@linux-node1 apache]# vim files/httpd.conf

[root@linux-node1 apache]# salt -S '192.168.194.131' state.highstate

重载

  1. apache-install:
  2. pkg.installed:
  3. - name: httpd
  4.  
  5. apache-config:
  6. file.managed:
  7. - name: /etc/httpd/conf/httpd.conf
  8. - source: salt://apache/files/httpd.conf
  9. - user: root
  10. - group: root
  11. - mode:
  12.  
  13. apache-service:
  14. service.running:
  15. - name: httpd
  16. - enable: True
  17. - reload: True
  18. - watch:
  19. - file: apache-config

Watch_in

[root@linux-node1 apache]# cat init.sls

  1. apache-install:
  2. pkg.installed:
  3. - name: httpd
  4.  
  5. apache-config:
  6. file.managed:
  7. - name: /etc/httpd/conf/httpd.conf
  8. - source: salt://apache/files/httpd.conf
  9. - user: root
  10. - group: root
  11. - mode:
  12. - watch_in:
  13. - service: apache-service
  14.  
  15. apache-service:
  16. service.running:
  17. - name: httpd
  18. - enable: True
  19. - reload: True

5.  配置管理,状态间的条件判断

需求:Admin输入用户名,密码才能登陆

1.Apache认证登陆

https://blog.csdn.net/alexander_phper/article/details/52242474

  1. 修改配置
  2. 用户名密码文件

(1)配置admin页面

[root@linux-node1 apache]# cd /var/www/html/

[root@linux-node1 html]# mkdir admin

[root@linux-node1 html]# cd admin/

[root@linux-node1 admin]# vim index.html

This is admin

(2)配置

# 配置httpd

[root@linux-node1 files]# pwd

/srv/salt/prod/apache/files

[root@linux-node1 files]# vim httpd.conf

  1. <Directory "/var/www/html/admin">
  2. AllowOverride All
  3. Order allow,deny
  4. Allow from all
  5. AuthType Basic
  6. AuthName "hehe"
  7. AuthUserFile /etc/httpd/conf/htpasswd_file
  8. Require user admin
  9. </Directory>

[root@linux-node1 files]# whereis htpasswd

htpasswd: /usr/bin/htpasswd /usr/share/man/man1/htpasswd.1.gz

[root@linux-node1 files]# rpm -qf /usr/bin/htpasswd

httpd-tools-2.4.6-89.el7.centos.x86_64

2. Cmd认证模块

Unless

https://docs.saltstack.com/en/latest/ref/states/all/salt.states.cmd.html

3.配置init.sls

[root@linux-node1 apache]# pwd

/srv/salt/prod/apache

[root@linux-node1 apache]# vim init.sls

4 unless状态判断

If 文件存在:不执行

Else:不存在,执行

Unless

条件为假,执行

  1. apache-install:
  2. pkg.installed:
  3. - name: httpd
  4.  
  5. apache-config:
  6. file.managed:
  7. - name: /etc/httpd/conf/httpd.conf
  8. - source: salt://apache/files/httpd.conf
  9. - user: root
  10. - group: root
  11. - mode:
  12. - watch_in:
  13. - service: apache-service
  14.  
  15. apache-auth:
  16. pkg.installed:
  17. - name: httpd-tools
  18. cmd.run:
  19. - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
  20. - unless: test -f /etc/httpd/conf/htpasswd_file
  21.  
  22. apache-service:
  23. service.running:
  24. - name: httpd
  25. - enable: True
  26. - reload: True

test

6 配置管理 jinja模板

需求:配置文件,监听minion自己本地的ip地址

1.学习方法

1 官方文档

https://docs.saltstack.com/en/latest/contents.html

2 配置管理

https://docs.saltstack.com/en/latest/topics/states/index.html

3 file模块

https://docs.saltstack.com/en/latest/ref/states/all/index.html#all-salt-states

4 搜索jinja

https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#module-salt.states.file

2.jinja

Salt默认模板 jinja2

Jinja2 是一个现代的,设计者友好的,仿照 Django 模板的 Python 模板语言。

http://docs.jinkan.org/docs/jinja2/templates.html

两种分隔符: {% ... %} 和 {{ ... }} 。

前者用于执行诸如 for 循环 或赋值的语句,

后者把表达式的结果打印到模板上

如何区分这是一个模板

3. 如何配置jinja?

1. 修改模板配置文件

2 修改sls增加

conf配置

  1. [root@linux-node1 apache]# pwd
  2. /srv/salt/prod/apache
  3.  
  4. [root@linux-node1 apache]# vim files/httpd.conf
  5. Listen {{ IPADDR }}:{{ PORT }}

sls

3.验证

[root@linux-node1 apache]# salt -S '192.168.194.131' state.highstate

[root@linux-node1 apache]# vim /etc/httpd/conf/httpd.conf

另一个方法:(不推荐)

7. job管理

执行1次highstate,会产生1个任务

最近干了什么事

装某个东西,太慢了给我,停止

1.查看job

  1. [root@linux-node1 ~]# cd /var/cache/salt/master/jobs/
  2. [root@linux-node1 jobs]# ls
  3. 0d 5a 7e 8d 9a a5 b4 c0 cd df ea f6
  4. 0e 1a 5b 8e 9b a7 b6 c2 cf e0 eb f8
  5. 0f 1b 5c 8f 9d a8 b8 c4 d2 e1 ec f9
  6. 1c 2a 5d 9e a9 b9 c5 d3 e2 ee fa
  7. 1e 2b 5e 9f aa ba c6 d4 e3 f0 fb
  8. 1f 2c 5f 6a a0 ab bb c7 d5 e4 f1 fc
  9. 2e 3a 4a 6b a1 ac bc c8 d7 e5 f2 fe
  10. 0a 2f 3b 4c 6c 7a a2 ad bd c9 d9 e7 f3 ff
  11. 0b 3d 4d 6e 7b 8a a3 ae be ca da e8 f4
  12. 0c 3f 4e 7c 8c a4 b3 bf cc dd e9 f5
  13.  
  14. [root@linux-node1 jobs]# cd 6c/
  15. [root@linux-node1 6c]# ls
  16. 210bdfecd6c424d9d7e1c5bbe2f171 53117bf95a2bea7fbf2d81c8c471ce
  17. [root@linux-node1 6c]# ll 210bdfecd6c424d9d7e1c5bbe2f171/
  18. total
  19. -rw-r--r--. root root Jul : jid
  20. drwxr-xr-x. root root Jul : linux-node1.example.com
  21. [root@linux-node1 6c]# cd 210bdfecd6c424d9d7e1c5bbe2f171/
  22. [root@linux-node1 210bdfecd6c424d9d7e1c5bbe2f171]# cat jid
  23. [root@linux-node1 210bdfecd6c424d9d7e1c5bbe2f171]# tree
  24. .
  25. ├── jid
  26. └── linux-node1.example.com
  27. ├── out.p
  28. └── return.p
  29.  
  30. directory, files

缓存时间默认24h

[root@linux-node1 ~]# vim /etc/salt/master

2.saltutil模块.job

远程执行

https://docs.saltstack.com/en/latest/topics/execution/index.html

执行模块

https://docs.saltstack.com/en/latest/ref/modules/all/index.html#all-salt-modules

Saltutil

https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.saltutil.html#module-salt.modules.saltutil

Test

[root@linux-node1 ~]# salt 'linux-node2*' cmd.run 'sleep 160'

[root@linux-node1 ~]# salt \* saltutil.running

[root@linux-node1 ~]# salt 'linux-node2*' saltutil.kill_job 20190731050029734898

案例:每5分钟给所有机器跑一下状态

8.总结

1. 作业:saltstack部署redis主从配置

init.sls

  1. [root@linux-node1 redis]# pwd
  2. /srv/salt/prod/redis
  3. [root@linux-node1 redis]# tree
  4. .
  5. ├── files
  6.    └── redis.conf
  7. ├── init.sls
  8. ├── master.sls
  9. └── slave.sls
  10.  
  11. 1 directory, 4 files
  12. [root@linux-node1 redis]# vim init.sls
  13. redis-install:
  14. pkg.installed:
  15. - name: redis
  16.  
  17. redis-config:
  18. file.managed:
  19. - name: /etc/redis.conf
  20. - source: salt://redis/files/redis.conf
  21. - user: root
  22. - group: root
  23. - mode: 644
  24. - template: jinja
  25. - defaults:
  26. PORT: 6379
  27. IPADDR: {{ grains['fqdn_ip4'][0] }}
  28.  
  29. redis-service:
  30. service.running:
  31. - name: redis
  32. - enable: True
  33. - reload: True
  34. - watch:
  35. - file: redis-config

conf

  1. [root@linux-node1 redis]# cp /etc/redis.conf files/
  2. bind {{ IPADDR }}
  3. port {{ PORT }}
  4. daemonize yes

master

slave

  1. [root@linux-node1 redis]# vim master.sls
  2. include:
  3. - redis.init
  4.  
  5. [root@linux-node1 redis]# vim slave.sls
  6. include:
  7. - redis.init
  8.  
  9. slave-config:
  10. cmd.run:
  11. - name: redis-cli -h 192.168.194.131 slaveof 192.168.194.132 6379
    - unless: redis-cli -h 192.168.194.132 info |grep role:slave
  12. - require:
  13. - service: redis-service

node2上实验命令

  1. [root@linux-node2 modules]# redis-cli -h 192.168.194.132 info
  2.  
  3. [root@linux-node2 modules]# redis-cli -h 192.168.194.132 slaveof 192.168.194.131 6379
  4.  
  5. [root@linux-node2 modules]# redis-cli -h 192.168.194.132
  6. 192.168.194.132:6379> info [sectio

不需要unless

执行top

  1. [root@linux-node1 base]# vim top.sls
  2. prod:
  3. 'linux-node1.example.com':
  4. - lamp
  5. - redis.master
  6. 'linux-node2.example.com':
  7. - lamp
  8. - redis.slave
  9. [root@linux-node1 base]# pwd
  10. /srv/salt/base
  11.  
  12. [root@linux-node1 redis]# salt \* state.highstate

设置node2为主

  1. [root@linux-node2 modules]# redis-cli -h 192.168.194.132
  2. 192.168.194.132:6379>
  3. 192.168.194.132:6379> slaveof no one
  4. OK

unless应用

  1. [root@linux-node2 modules]# redis-cli -h 192.168.194.132 info |grep role:slave
  2. role:slave
  3. [root@linux-node2 modules]# echo $?
  4. 0

Watch

生产不要watch

生产 test=True

不用* 用1个节点

1.学习saltstack思路:三段式

2.学习模块方法

1 官方文档

https://docs.saltstack.com/en/latest/contents.html

2 配置管理

https://docs.saltstack.com/en/latest/topics/states/index.html

3 file模块

https://docs.saltstack.com/en/latest/ref/states/all/index.html#all-salt-states

4 搜索jinja

https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#module-salt.states.file

5 LAMP配置管理:模块(state、file、pkg、service)、jinja模板、job管理、redis主从的更多相关文章

  1. 架构师成长之路5.3-Saltstack配置管理(State状态模块)

    点击架构师成长之路 架构师成长之路5.3-Saltstack配置管理(State状态模块) 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需要 ...

  2. [ionic开源项目教程] - 第12讲 医疗模块的实现以及Service层loadMore和doRefresh的提取封装

    关注微信订阅号:TongeBlog,可查看[ionic开源项目]全套教程. 这一讲主要实现tab2[医疗]模块,[医疗]模块跟tab1[健康]模块类似. [ionic开源项目教程] - 第12讲 医疗 ...

  3. Ansible笔记(7)---常用模块之系统类模块(cron、service)

    一.cron模块 1.1作用: cron 模块可以帮助我们管理远程主机中的计划任务,功能相当于 crontab 命令. 在了解cron模块的参数之前,先写出一些计划任务的示例: # 示例1,每天的1点 ...

  4. Saltstack_使用指南12_配置管理-jinja模板

    1. 说明 下文的案例是根据上一篇文章进行的修改.因此请优先读取上一章博文内容<Saltstack_使用指南11_配置管理-状态之间依赖关系> 2. 主机规划 salt 版本 [root@ ...

  5. 【重要】Nginx模块Lua-Nginx-Module学习笔记(三)Nginx + Lua + Redis 已安装成功(非openresty 方式安装)

    源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一. 目标 使用Redis做分布式缓存:使用lua API来访问redis缓存:使用nginx向客户端提 ...

  6. 架构师成长之路5.6-Saltstack配置管理(jinja模板)

    点击架构师成长之路 架构师成长之路5.6-Saltstack配置管理(jinja模板) 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需要FU ...

  7. 把编译安装的httpd 实现服务脚本,通过service和chkconfig 进行管理

    把编译安装的httpd 实现服务脚本,通过service和chkconfig 进行管理 1 编译安装httpd 把httpd编译安装在/app/httpd/目录下. 2 在/etc/rc.d/init ...

  8. SaltStack配置管理-jinja模板

    上一篇:SaltStack配置管理-状态间关系 需求:需要apache配置文件里面监听的端口是每个minion本地的地址 saltstack默认的模板是jinjia模板 参考文档:http://doc ...

  9. 为windows开启winrm service, 以便进行远程管理

    为windows开启winrm service, 以便进行远程管理   是windows 一种方便远程管理的服务:开启winrm service,便于在日常工作中,远程管理服务器,或通过脚本,同时管理 ...

随机推荐

  1. spring-boot war包部署(二)

    环境 jdk 8 tomcat 8.5 sts 4.4.2 maven 3.6.1 背景 有时候,服务器已经有了,我们必须要使用 war 包进行部署,所以需要 spring boot 支持打包和部署成 ...

  2. redis 工具包

    java通过jedis操作redis(从JedisPool到JedisCluster) redis作为一个缓存数据库,在绝大多数java项目开发中是必须使用的,在web项目中,直接配合spring-r ...

  3. oracle建表详细信息

    一张用户表 -- Create table create table OA_DM.DM_GY_USER ( ), username ) not null, loginname ) not null, ...

  4. WEBAPI 最近更新项目时 服务器总是提示:An error has occurred.

    解决办法: 在webconfig中设置 <system.web><customErrors mode="Off"/></system.web> ...

  5. luogu题解P4198楼房重建--线段树神操作

    题目链接 https://www.luogu.org/problemnew/show/P4198 分析 一句话题意,一条数轴上有若干楼房,坐标为\(xi\)的楼房有高度\(hi\),那么它的斜率为\( ...

  6. TCP/IP协议栈各个层次及分别的功能

    网络接口层:这是协议栈的最低层,对应OSI的物理层和数据链路层,主要完成数据帧的实际发送和接收.网络层:处理分组在网络中的活动,例如路由选择和转发等,这一层主要包括IP协议.ARP.ICMP协议等.传 ...

  7. vue打包后css背景图片地址找不到

    背景图片变成了这样:static/css/static/imgs/xxx.jpg 解决方法,修改build/utils,添加   publicPath: '../../'    就行 对比了下,com ...

  8. PHP之配置

    1) 错误日志 一.相关配置 需要将php.ini中的配置指令做如下修改: . error_reporting = E_ALL ;将会向PHP报告发生的每个错误 . display_errors = ...

  9. 高德地图API-设置考勤范围

    <template> <div class="page-setting-setgps"> <!--head--> <div class=& ...

  10. 【Spring Cloud】 总结

    一.Spring Cloud简介 简单来说,Spring Cloud 就是致力于分布式系统.微服务等的一套在目前非常火热的框架.但它的本身也是一系列框架的有序集合(由多个模块组成). 相比较于Dubb ...