saltstack Detailed + apache service deployment

saltstack Introduction

1, saltstack using python language development;
2, lightweight management tool, batch execution command;
3, commonly used modules: pkg (package), file (file), cmd (execute commands or scripts), user, service, the cron
. 4, saltstack data system
Grains (static data)
Pillar (dynamic data)

saltstack三大功能,远程执行,配置管理,云管理

SaltStack是一个服务器基础架构集中化管理平台,具备配置管理、远程执行、监控等功能,基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)构建。

通过部署SaltStack,我们可以在成千万台服务器上做到批量执行命令,根据不同业务进行配置集中化管理、分发文件、采集服务器数据、操作系统基础及软件包管理等,SaltStack是运维人员提高工作效率、规范业务配置与操作的利器。

The basic principle of salt

SaltStack 采用 C/S模式,server端就是salt的master,client端就是minion,minion与master之间通过ZeroMQ消息队列通信

minion上线后先与master端联系,把自己的pub key发过去,这时master端通过salt-key -L命令就会看到minion的key,接受该minion-key后,也就是master与minion已经互信

master可以发送任何指令让minion执行了,salt有很多可执行模块,比如说cmd模块,在安装minion的时候已经自带了,它们通常位于你的python库中,locate salt | grep /usr/可以看到salt自带的所有东西。

这些模块是python写成的文件,里面会有好多函数,如cmd.run,当我们执行salt '*' cmd.run 'uptime'的时候,master下发任务匹配到的minion上去,minion执行模块函数,并返回结果。master监听4505和4506端口,4505对应的是ZMQ的PUB system,用来发送消息,4506对应的是REP system是来接受消息的。

Specific steps are as follows

Salt stack的Master与Minion之间通过ZeroMq进行消息传递,使用了ZeroMq的发布-订阅模式,连接方式包括tcp,ipc
salt命令,将cmd.run ls命令从salt.client.LocalClient.cmd_cli发布到master,获取一个Jodid,根据jobid获取命令执行结果。
master接收到命令后,将要执行的命令发送给客户端minion。
minion从消息总线上接收到要处理的命令,交给minion._handle_aes处理
minion._handle_aes发起一个本地线程调用cmdmod执行ls命令。线程执行完ls后,调用minion._return_pub方法,将执行结果通过消息总线返回给master
master接收到客户端返回的结果,调用master._handle_aes方法,将结果写的文件中
salt.client.LocalClient.cmd_cli通过轮询获取Job执行结果,将结果输出到终端。

saltstack Detailed + apache service deployment

advantage:

First, he is fast, queue-based messaging + thread finish multiple devices, all of milliseconds
Secondly, very flexible, the source code is python, easy to understand and custom module (python language compared to other perl, ruby, etc., or well understood)
command is simple, powerful

Cons: deploy minion end more inconvenience

saltstack several important components of
grains? grains is to collect some of the information in the minion (the client) to start, such as the type of operating system, network card ip and other static information.
Information grains is not dynamic and does not always change, it is only collected when the minion start

pillar? pillar and grains are not the same, is defined on the master, and against some of the information minion definition. Like some of the more important data (passwords) can exist pillar where you can also define variables.

State
he is the core function of saltstack, manage controlled by pre-specified hosts good sls Files: Package / File / Network Configuration / System Services / System users.

Saltstack volume deployments apache

Experimental environment:
Master: 192.168.136.167
web01: 192.168.136.168
web02: 192.168.136.185

#添加主机名,三台机器都要添加,并且主机要改成相应的名称
[root@master ~]# vim /etc/hosts
192.168.136.167 master.saltstack.com
192.168.136.168 web01.saltstack.com
192.168.136.185 web02.saltstack.com

#每台都需要关闭防火墙
[root@master ~]# vim /etc/hostname 
master.saltstack.com

[root@web01 ~]# vim /etc/hostname 
web01.saltstack.com

[root@web02 ~]# vim /etc/hostname 
web02.saltstack.com

#安装epel源(三台都要装)
[root@master ~]# yum install -y epel-release
[root@master ~]# yum -y install salt-master

[root@master ~]# vim /etc/salt/master 
15行 interface: 192.168.175.132   //监听地址
215行 auto_accept: True        //避免要运行salt-key来确认证书认证
416行 file_roots:
base:
- /srv/salt           //saltstack文件根目录位置,目录需要创建
710行组分类:
nodegroups:
group1: 'web01.saltstack.com'
group2: 'web02.saltstack.com'

552行 pillar_opts: True        //开启pillar功能,同步文件功能
529行 
pillar_roots:
base:
- /srv/pillar         //pillar的主目录,需要创建

创建salt与pillar文件根目录:
mkdir /srv/salt
mkdir /srv/pillar

启动服务器:
systemctl start salt-master
systemctl enable salt-master

netstat -anpt | egrep '4505|4506'

创建salt与pillar文件根目录:
mkdir /srv/salt
mkdir /srv/pillar

The following operation on the host -------------------- -------------------

在两台上分别配置:
yum -y install salt-minion

vi /etc/salt/minion
修改配置如下:
16行 master: 192.168.175.132     //指定主控端IP
78行 id: web01.saltstack.com     //指定被控端主机名

启动被控端服务
systemctl start salt-minion

在主控端测试与被控端的通信状态!
salt '*' test.ping

web01.saltstack.com:
True
web02.saltstack.com:
True

salt '*' cmd.run 'df -h'   //远程执行命令

salt-key   //查看在 master 上已经被接受过的客户端

查看被控主机上grains所有值:(每次minion在启动是都会获取客户端信息)
salt 'web01.saltstack.com' grains.items (静态数据)
salt 'web01.saltstack.com' pillar.items  (动态数据)

配置管理安装Apache
下面进行的演示是远程通过 yum 方式安装 Apache。步骤如下:
修改配置文件
vi /etc/salt/master     // 打开如下内容的注释
file_roots:
base:
- /srv/salt/
注意:环境: base、dev(开发环境)、test(测试环境)、prod(生产环境)。

mkdir /srv/salt
vi /srv/salt/top.sls
base:
'*':
- apache
注意:'*',则表示在所有的客户端执行 apache 模块。

vi /srv/salt/apache.sls
apache-service:
pkg.installed:
- names:                // 如果只有一个服务,那么就可以写成 –name: httpd 不用再换一行
- httpd
- httpd-devel
service.running:
- name: httpd
- enable: True
注意:apache-service 是自定义的 id 名。pkg.installed 为包安装函数,下面是要安装的包的名字。service.running 也是一个函数,来保证指定的服务启动,enable 表示开机启动。

重启服务
#systemctl restart salt-master
执行命令
#salt '*' state.highstate 

Guess you like

Origin blog.51cto.com/14449524/2469961