本文共 8563 字,大约阅读时间需要 28 分钟。
笔记内容:安装saltstack
笔记日期:2018-01-23随着信息时代的持续发展,IT运维已经成为IT服务内涵中重要的组成部分。面对越来越复杂的业务,面对越来越多样化的用户需求,不断扩展的IT应用需要越来越合理的模式来保障IT服务能灵活便捷、安全稳定地持续保障,这种模式中的保障因素就是IT运维(其他因素是更加优越的IT架构等)。从初期的几台服务器发展到庞大的数据中心,单靠人工已经无法满足在技术、业务、管理等方面的要求,那么标准化、自动化、架构优化、过程优化等降低IT服务成本的因素越来越被人们所重视。其中,自动化最开始作为代替人工操作为出发点的诉求被广泛研究和应用。
IT运维从诞生发展至今,自动化作为其重要属性之一已经不仅仅只是代替人工操作,更重要的是深层探知和全局分析,关注的是在当前条件下如何实现性能与服务最优化,同时保障投资收益最大化。自动化对IT运维的影响,已经不仅仅是人与设备之间的关系,已经发展到了面向客户服务驱动IT运维决策的层面,IT运维团队的构成,也从各级技术人员占大多数发展到业务人员甚至用户占大多数的局面。
因此,IT运维自动化是一组将静态的设备结构转化为根据IT服务需求动态弹性响应的策略,目的就是实现IT运维的质量,降低成本。可以说自动化一定是IT运维最高层面的重要属性之一,但不是全部。
传统运维的缺陷:
自动化运维就是要解决上面所有问题。
常见自动化运维工具:
saltstack的官方介绍文档:
saltstack可以使用salt-ssh远程执行,类似ansible,也可以使用c/s模式,下面我们将讲述该种模式的使用,因为salt-ssh很少用到,c/s模式是最常用的。
资源有限我这里只准备了两台机器,一台作为服务端一台作为客户端:
1.分别设置两台机器的主机名,并且配置hosts文件,我这里是做实验所以设置的是临时的主机名:
192.168.77.130 服务端:[root@localhost ~]# hostname server[root@server ~]# vim /etc/hosts # 增加以下两行192.168.77.130 server192.168.77.128 client
192.168.77.128 客户端:
[root@localhost ~]# hostname client[root@client ~]# vim /etc/hosts # 增加以下两行192.168.77.130 server192.168.77.128 client
2.然后两台机器上都需要安装saltstack yum源:
yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
3.服务端安装:
[root@server ~]# yum install -y salt-master salt-minion
4.客户端安装:
[root@client ~]# yum install -y salt-minion
以上我们完成了客户端与服务的安装,安装完成后需要先在服务端编辑配置文件:
[root@server ~]# vim /etc/salt/minionmaster: server # 在文件中找到master,然后配置主机名
同样的客户端也需要进行这个操作:
[root@client ~]# vim /etc/salt/minionmaster: server # 在文件中找到master,然后配置服务端的主机名
然后启动客户端,客户端不会监听端口:
[root@client ~]# systemctl start salt-minion[root@client ~]# ps aux |grep saltroot 5593 0.5 0.5 285476 19536 ? Ss 12:13 0:00 /usr/bin/python /usr/bin/salt-minionroot 5603 2.3 0.9 560680 36624 ? Sl 12:13 0:00 /usr/bin/python /usr/bin/salt-minionroot 5611 0.0 0.4 401568 19104 ? S 12:13 0:00 /usr/bin/python /usr/bin/salt-minionroot 5680 0.0 0.0 112660 964 pts/0 S+ 12:13 0:00 grep --color=auto salt[root@client ~]#
接着启动服务端,不过我这里启动时报错了,master和minion都启动不了:
[root@server ~]# systemctl start salt-masterJob for salt-master.service failed because the control process exited with error code. See "systemctl status salt-master.service" and "journalctl -xe" for details.[root@server ~]# systemctl status salt-master.service● salt-master.service - The Salt Master Server Loaded: loaded (/usr/lib/systemd/system/salt-master.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2018-01-23 20:07:41 CST; 1min 26s ago Docs: man:salt-master(1) file:///usr/share/doc/salt/html/contents.html https://docs.saltstack.com/en/latest/contents.html Process: 3903 ExecStart=/usr/bin/salt-master (code=exited, status=1/FAILURE) Main PID: 3903 (code=exited, status=1/FAILURE)Jan 23 20:07:41 server salt-master[3903]: import salt.config as configJan 23 20:07:41 server salt-master[3903]: File "/usr/lib/python2.7/site-packages/salt/config/__init__.py", line 45...odule>Jan 23 20:07:41 server salt-master[3903]: import psutilJan 23 20:07:41 server salt-master[3903]: File "/usr/lib64/python2.7/site-packages/psutil/__init__.py", line 89, i...odule>Jan 23 20:07:41 server salt-master[3903]: import psutil._pslinux as _psplatformJan 23 20:07:41 server salt-master[3903]: AttributeError: 'module' object has no attribute '_pslinux'Jan 23 20:07:41 server systemd[1]: salt-master.service: main process exited, code=exited, status=1/FAILUREJan 23 20:07:41 server systemd[1]: Failed to start The Salt Master Server.Jan 23 20:07:41 server systemd[1]: Unit salt-master.service entered failed state.Jan 23 20:07:41 server systemd[1]: salt-master.service failed.Hint: Some lines were ellipsized, use -l to show in full.[root@server ~]# systemctl start salt-minionJob for salt-minion.service failed because the control process exited with error code. See "systemctl status salt-minion.service" and "journalctl -xe" for details.[root@server ~]#
解决办法:
分析错误信息:从服务的报错信息中可以看到这主要的两句:import psutil._pslinux as _psplatformAttributeError: 'module' object has no attribute '_pslinux'
可以看到这是因为导入了psutil包中的_pslinux,但是_pslinux不存在,所以就报错了。解决的办法也很简单,重新下载安装psutil包即可,步骤如下:
[root@server ~]# wget https://pypi.python.org/packages/source/p/psutil/psutil-3.2.2.tar.gz[root@server ~]# tar zxvf psutil-3.2.2.tar.gz[root@server ~]# cd psutil-3.2.2/[root@server ~/psutil-3.2.2]# python setup.py install[root@server ~/psutil-3.2.2]# echo $?0[root@server ~/psutil-3.2.2]#
这时启动服务就正常了:
[root@server ~/psutil-3.2.2]# systemctl start salt-master[root@server ~/psutil-3.2.2]# systemctl start salt-minion[root@server ~/psutil-3.2.2]# ps aux |grep saltroot 4220 0.0 1.9 387532 37120 ? Ss 20:25 0:00 /usr/bin/python /usr/bin/salt-masterroot 4229 0.0 1.0 315048 19816 ? S 20:25 0:00 /usr/bin/python /usr/bin/salt-masterroot 4235 0.0 1.6 468824 31252 ? Sl 20:25 0:00 /usr/bin/python /usr/bin/salt-masterroot 4236 0.0 1.8 392560 35228 ? S 20:25 0:00 /usr/bin/python /usr/bin/salt-masterroot 4239 0.3 2.7 415004 51016 ? S 20:25 0:07 /usr/bin/python /usr/bin/salt-masterroot 4240 0.0 1.6 387532 31200 ? S 20:25 0:00 /usr/bin/python /usr/bin/salt-masterroot 4241 0.0 1.7 764388 31956 ? Sl 20:25 0:00 /usr/bin/python /usr/bin/salt-masterroot 4248 0.0 2.2 482512 41836 ? Sl 20:25 0:01 /usr/bin/python /usr/bin/salt-masterroot 4249 0.0 2.2 482516 41828 ? Sl 20:25 0:01 /usr/bin/python /usr/bin/salt-masterroot 4250 0.0 2.2 482248 41784 ? Sl 20:25 0:01 /usr/bin/python /usr/bin/salt-masterroot 4251 0.0 2.2 482236 41768 ? Sl 20:25 0:01 /usr/bin/python /usr/bin/salt-masterroot 4252 0.0 2.2 482256 41812 ? Sl 20:25 0:01 /usr/bin/python /usr/bin/salt-masterroot 5428 0.0 1.0 287592 19592 ? Ss 20:25 0:00 /usr/bin/python /usr/bin/salt-minionroot 5431 0.0 2.0 563292 37424 ? Sl 20:25 0:00 /usr/bin/python /usr/bin/salt-minionroot 5439 0.0 1.0 406604 19832 ? S 20:25 0:00 /usr/bin/python /usr/bin/salt-minionroot 9032 0.0 0.0 112652 956 pts/0 S+ 21:00 0:00 grep --color=auto salt[root@server ~/psutil-3.2.2]# netstat -lntp |grep pythontcp 0 0 0.0.0.0:4505 0.0.0.0:* LISTEN 4235/python tcp 0 0 0.0.0.0:4506 0.0.0.0:* LISTEN 4241/python [root@server ~/psutil-3.2.2]#
4505端口是用来发布消息的,4506端口则是与客户端通信、传输数据的。
saltstack的服务端和客户端通信需要建立一个安全通道,传输过程需要加密,所以得配置认证,也是通过密钥对来加密解密的,说白了就是配置个密钥认证。
minion在第一次启动时会在/etc/salt/pki/minion/下生成minion.pem和minion.pub,其中.pub为公钥,我们需要把公钥传输给master。
[root@client ~]# ls /etc/salt/pki/minion/minion.pem minion.pub[root@client ~]#
master第一次启动时也会在/etc/salt/pki/master下生成密钥对,当master通过salt-key工具接收到minion传过来的公钥后,就会在/etc/salt/pki/master/minions/目录里存放刚刚接受的公钥,同时客户端也会接收master传过去的公钥,把它放在/etc/salt/pki/minion目录下,并命名为minion_master.pub。
[root@server ~]# ls /etc/salt/pki/mastermaster.pem master.pub minions minions_autosign minions_denied minions_pre minions_rejected[root@server ~]#
借助salt-key工具来实现认证:
[root@server ~]# salt-key -a clientThe following keys are going to be accepted:Unaccepted Keys:clientProceed? [n/Y] yKey for minion client accepted.[root@server ~]# ls /etc/salt/pki/master/minions # 接收的公钥会存放在这个目录下client[root@server ~]#
客户端也会接收到服务端传过去的公钥:
[root@client ~]# ls /etc/salt/pki/minionminion_master.pub minion.pem minion.pub[root@client ~]#
-A可以认证所有在Unaccepted Keys下的客户端:
[root@server ~]# salt-key -AThe following keys are going to be accepted:Unaccepted Keys:serverProceed? [n/Y] yKey for minion server accepted.[root@server ~]#
这时候就都认证成功了,直接执行salt-key命令可以查看所有状态下的主机(客户端):
salt-key命令用法:
转载于:https://blog.51cto.com/zero01/2064247