自动化运维, 安装saltstack, 配置认证, 远程执行命令

认识自动化运维

  • 传统运维效率低,大多工作人为完成
  • 传统运维工作繁琐,容易出错
  • 传统运维每日重复做相同的事情
  • 传统运维没有标准化流程
  • 传统运维的脚本繁多,不能方便管理
  • 自动化运维就是要解决上面所有问题

常见自动化运维工具

  • Puppet (www.puppetlabs.com)
    基于rubby开发,c/s架构,支持多平台,可管理配置文件、用户、cron任务、软件包、系统服务等。 分为社区版(免费)和企业版(收费),企业版支持图形化配置。
  • Saltstack(官网 https://saltstack.com,文档docs.saltstack.com )
    基于python开发,c/s架构,支持多平台,比puppet轻量,在远程执行命令时非常快捷,配置和使用比puppet容易,能实现puppet几乎所有的功能。
  • Ansible (www.ansible.com )
    更加简洁的自动化运维工具,不需要在客户端上安装agent,基于python开发。可以实现批量操作系统配置、批量程序的部署、批量运行命令。

saltstack安装

  • saltstack介绍
    https://docs.saltstack.com/en/latest/topics/index.html
  • 可以使用salt-ssh远程执行,类似ansible,
  • 也支持c/s模式,下面我们将讲述该种模式的使用,需要准备两台机器 - 149为服务端,150为客户端
  • 设置hostname以及hosts,linux0, linux1
  • 两台机器全部安装saltstack yum源
    yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
  • 149上执行 yum install -y salt-master salt-minion
  • 150上执行 yum install -y salt-minion
  • 服务端监听4505和4506两个端口,4505为消息发布的端口,4506为和客户端通信的端口
[root@linux0 ~]# hostnamectl set-hostname linux0  #设置主机名;
[root@linux0 ~]# hostname  #设置好hostname,管理大量机器,需要有规律;minion生成的公钥会以hostname命名,master发送命令时也是使用minion的hostname;
linux0
[root@linux0 ~]# yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
[root@linux0 ~]# yum install -y salt-master salt-minion
[root@linux0 ~]# vi /etc/salt/minion #设置配置文件,让minion能找到master;
#master: salt
master: linux0   #hostname,类似于localhost可用于本机通信;借助设置/etc/hosts使用“域名”可以与内网或外网机器通信;
[root@linux0 ~]# systemctl restart salt-master.service #先启动master端;
[root@linux0 ~]# systemctl restart salt-minion.service #再启动minion端,设置没问题的话,minion会自动把公钥发给master,成为Unaccepted Key;
[root@linux0 ~]# netstat -lntp |grep pyth
tcp        0      0 0.0.0.0:4505            0.0.0.0:*               LISTEN      29790/python        
tcp        0      0 0.0.0.0:4506            0.0.0.0:*               LISTEN      29796/python 
[root@linux0 ~]# ps aux |grep salt |grep -v auto |awk '{print  $12}' |uniq -c
     13 /usr/bin/salt-master  #13个进程;
      3 /usr/bin/salt-minion  #3个进程;

[root@second ~]# hostnamectl set-hostname linux1
[root@linux1 ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.87.149 linux0  #使用域名“linux0"与149机器通信;
[root@linux1 ~]# yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
[root@linux1 ~]# yum install -y salt-minion
[root@linux1 ~]# vi /etc/salt/minion  #修改配置文件;
#master: salt
master: linux0   #与/etc/hosts设置相对应;
[root@linux1 ~]# systemctl start salt-minion.service 

saltstack配置认证

  • master端和minion端通信需要建立一个安全通道,传输过程需要加密,所以得配置认证,也是通过密钥对来加密解密的
  • minion在第一次启动时会在/etc/salt/pki/minion/下生成minion.pem和minion.pub,其中.pub为公钥,它会把公钥传输给master
  • master第一次启动时也会在/etc/salt/pki/master下生成密钥对,当master接收到minion传过来的公钥后,通过salt-key工具接受这个公钥,一旦接受后就会在/etc/salt/pki/master/minions/目录里存放刚刚接受的公钥,同时客户端也会接受master传过去的公钥,把它放在/etc/salt/pki/minion目录下,并命名为minion_master.pub
  • 以上过程需要借助salt-key工具来实现 执行如下命令 salt-key -a linux1// -a后面跟主机名,可以认证指定主机
    salt-key -a linux0
[root@linux0 ~]# salt-key  #正常状态下master上收到两个key;
Accepted Keys:
Denied Keys:
Unaccepted Keys:
linux0
linux1
Rejected Keys:
[root@linux0 ~]# salt-key -a linux0  #添加一个key;
The following keys are going to be accepted:
Unaccepted Keys:
linux0
Proceed? [n/Y] y
Key for minion linux0 accepted.
[root@linux0 ~]# salt-key -r linux1  #reject; -R reject所有;
The following keys are going to be rejected:
Unaccepted Keys:
linux1
Proceed? [n/Y] y
Key for minion linux1 rejected.
[root@linux0 ~]# salt-key
Accepted Keys:
linux0
Denied Keys:
Unaccepted Keys:
Rejected Keys:
linux1
[root@linux0 ~]# salt-key -D -y  #删除所有;
The following keys are going to be deleted:
Accepted Keys:
linux0
Unaccepted Keys:
Rejected Keys:
linux1
Key for minion linux1 deleted.
Key for minion linux0 deleted.
[root@linux1 ~]# systemctl restart salt-minion.service  #重启会重新发送key;
[root@linux0 ~]# systemctl restart salt-minion.service   #重启会重新发送key;
[root@linux0 ~]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
linux0
linux1
Rejected Keys:
[root@linux0 ~]# salt-key -A -y  #添加所有;
The following keys are going to be accepted:
Unaccepted Keys:
linux0
linux1
Key for minion linux0 accepted.
Key for minion linux1 accepted.
[root@linux0 ~]# salt-key
Accepted Keys:
linux0
linux1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@linux0 ~]# tree /etc/salt/pki/master/  #以下目录就是存放密钥的,对应于salt-key命令的分类;
/etc/salt/pki/master/
├── master.pem
├── master.pub
├── minions
│   ├── linux0
│   └── linux1
├── minions_autosign
├── minions_denied
├── minions_pre
└── minions_rejected

5 directories, 4 files

[root@linux1 ~]# tree /etc/salt/pki/minion/  #minion端的密钥目录;
/etc/salt/pki/minion/
├── minion_master.pub
├── minion.pem
└── minion.pub

0 directories, 3 files
如果修改过hostname要把机器这些文件删除,再重启会重新生成新的hostname的密钥文件;
[root@linux1 ~]# rm -rf /etc/salt/minion_id 
[root@linux1 ~]# rm -rf /etc/salt/pki

saltstack远程执行命令

  • salt '’ test.ping //这里的表示所有已经签名的minion端,也可以指定一个
  • salt ‘aming-01’ test.ping
  • salt ‘*’ cmd.run “hostname”
  • 说明: 这里的*必须是在master上已经被接受过认证的客户端,可以通过salt-key查到,通常是我们已经设定的id值。
  • 关于这部分内容,它支持通配、列表以及正则。
    比如两台客户端aming-01,aming-02, 那我们可以写成salt ‘aming-*’, salt ‘aming-0[12]’ salt -L ‘aming-01,aming-02’ salt -E 'aming-(01|02)'等形式,使用列表,即多个机器用逗号分隔,而且需要加-L,使用正则必须要带-E选项。 它还支持grains,加-G选项,pillar 加-I选项,下面会介绍到。
[root@linux0 ~]# salt '*' test.ping  #master运行命令,两台机器会进行同样的操作;*代表accepted key的机器;test.ping测试机器是否存活;
linux1:
    True
linux0:
    True
[root@linux0 ~]# salt '*' cmd.run "hostname"
linux0:
    linux0
linux1:
    linux1
[root@linux0 ~]# salt '*' cmd.run "ip addr |grep 87"
linux1:
        inet 192.168.87.150/24 brd 192.168.87.255 scope global noprefixroute ens33
        inet 192.168.87.108/32 scope global ens33
linux0:
        inet 192.168.87.149/24 brd 192.168.87.255 scope global noprefixroute ens33
[root@linux0 ~]# salt '*' cmd.run "whoami"
linux0:
    root
linux1:
    root
[root@linux0 ~]# salt 'linux1' cmd.run "whoami"  #可指定一台机器;
linux1:
    root
[root@linux0 ~]# salt 'linux*' cmd.run "whoami"  #支持正则通配;
linux0:
    root
linux1:
    root
[root@linux0 ~]# salt 'linux[01]' cmd.run "whoami"
linux0:
    root
linux1:
    root
[root@linux0 ~]# salt -L 'linux0,linux1' cmd.run "whoami"  #列表;
linux0:
    root
linux1:
    root
[root@linux0 ~]# salt -E 'linux[0-9]+' cmd.run "hostname" #正则特殊符号加-E;
linux0:
    linux0
linux1:
    linux1
[root@linux0 ~]# salt -E 'linux(0|1)' cmd.run "hostname"
linux1:
    linux1
linux0:
    linux0
发布了125 篇原创文章 · 获赞 5 · 访问量 4608

猜你喜欢

转载自blog.csdn.net/tanyyinyu/article/details/103720199