SaltStack介绍-部署和认证(一)

2020年7月27日11:43:42 发表评论 895 views

第1章 SaltStack简介

1.1 配置管理工具变化

  • 配置管理到状态管理
  • Puppet到SaltStack
  • Agent到无Agent

1.2 SaltStack介绍

SaltStack一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯。

SaltStack是使用Python语言开发的,同时提供Rest API方便二次开发以及和其他平台进行集成。

1.3 SaltStack四大功能

  • 远程执行
  • 配置管理
  • 云管理
  • 事件驱动

1.4 SaltStack的运行方式

  • Local:只有一台机器或使用SaltStack安装SaltStack环境
  • Master/Minion:类似传统Server/agent模式
  • Salt SSH:不需要Agent的运行模式

1.5 SaltStack生产实践

1.5.1 不建议使用的方式

  • 不建议使用salt的file模块进行目录管理和代码部署,因为salt对所有文件做MD5,此时性能较低
  • 不建议使用salt管理项目代码的配置文件

1.5.2 建议使用的方式

  • 类似于命令编排的状态管理,将压缩包使用managed传输到目的服务器,然后使用cmd.run执行部署
  • 建议进行分层管理,salt只管理应用服务,例如:nginx、tomcat、apache等
  • 如果有固定的文件服务器,可以使用source: salt://、http://、ftp://方式管理文件
  • sls文件版本化,可以知道谁在什么时间做了什么和输出是什么:
  1. 创建一个git项目
  2. 在测试环境中编写sls文件,测试后进行git commit && git push到版本仓库
  3. 生产环境git pull,测试成功后全部执行
  • 使用Master Job Cache保存job的输出

1.6 参考资料

中文文档:http://docs.saltstack.cn/

中国SaltStack用户组:https://www.saltstack.cn/

官方文档:https://docs.saltstack.com/en/latest/

官方网站:https://www.saltstack.com/

GitHuab:https://github.com/saltstack

第2章 SaltStack部署和认证

2.1 安装SaltStack

2.1.1 安装YUM源

[root@linux-node ~]# yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-2019.2.el7.noarch.rpm

2.1.2 安装SlatStack

2.1.2.1 安装启动Master

[root@linux-node01 ~]# yum install -y salt-master

2.1.2.2 安装Minion

[root@linux-node02 ~]# yum install -y salt-minion

2.1.3 配置SlatStack

2.1.3.1 配置minion

[root@linux-node02 ~]# vim /etc/salt/minion
16 master: 10.10.10.101

2.1.3.2 Master同意minion通信

[root@linux-node01 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
linux-node01
linux-node02
Proceed  [n/Y] Y
Key for minion linux-node01 accepted.
Key for minion linux-node02 accepted.

  • 命令解析:
  1. -A:同意所有的Key
  2. -D:删除所有的Key

2.1.4 启动服务

2.1.4.1 启动Master

[root@linux-node01 ~]# systemctl start salt-master

2.1.4.2 启动Minion

[root@linux-node02 ~]# systemctl start salt-minion

2.2 Master高可用

备注:官方文档:https://docs.saltstack.com/en/latest/topics/tutorials/multimaster.html

2.2.1 安装master服务

参见2.1章中的2.1.1和2.1.2节。

2.2.2 修改双机minion配置文件

[root@linux-node ~]# vim /etc/salt/minion
master:
  - 10.10.10.101
  - 10.10.10.102

2.2.3 使两端master配置文件和数据同步

  • 需要保证:
  1. master配置文件一样
  2. master file_roots一样
  3. master 公钥和私钥一样

2.2.3.1 同步配置文件

[root@linux-node01 ~]# scp /etc/salt/master 10.10.10.102:/etc/salt/

2.2.3.2 同步数据文件

  • 使用rsync实时同步数据目录
  • 使用分布式文件系统共享数据目录
  • 使用NFS做共享数据目录

示例:使用NFS共享数据目录

[root@linux-node ~]# yum install -y nfs-utils rpcbind

# 是数据目录为共享目录
[root@linux-node01 ~]# vim /etc/exports
/etc/salt/pki/master 10.10.10.102(rw,sync,no_root_squash,no_all_squash)
/srv/salt 10.10.10.102(rw,sync,no_root_squash,no_all_squash)

[root@linux-node01 ~]# systemctl start nfs
[root@linux-node02 ~]# showmount -e 10.10.10.101
Export list for 10.10.10.101:
/srv/salt            10.10.10.102
/etc/salt/pki/master 10.10.10.102

[root@linux-node02 ~]# mkdir -p /srv/salt
[root@linux-node02 ~]# mount -t nfs 10.10.10.101:/etc/salt/pki/master /etc/salt/pki/master
[root@linux-node02 ~]# mount -t nfs 10.10.10.101:/srv/salt /srv/salt

2.2.4 启动两端master和minion

[root@linux-node ~]# systemctl restart salt-master
[root@linux-node ~]# systemctl start salt-minion

2.2.5 两端master都同意key

[root@linux-node ~]# salt-key -A

2.2.6 测试批量管理

[root@linux-node01 ~]# salt '*' test.ping
linux-node02:
    True
linux-node01:
True

[root@linux-node02 ~]# salt '*' test.ping
linux-node01:
    True
linux-node02:
    True

2.3 SaltStack认证

2.3.1 id相关解析

2.3.1.1 id简介

在SaltStack计算集群中同一台机器上可以运行多个minion,用此id区分不同的minion可以防止minion互相影响。若不配置id,则默认id为主机名。

2.3.1.2 id相关文件位置

# Minion服务器配置id的位置
[root@linux-node02 ~]# vim /etc/salt/minion
112 #id:

# 启动服务后id在minion服务器中保存的位置
[root@linux-node02 ~]# cat /etc/salt/minion_id
linux-node02

# 在minion服务器中key保存的位置
[root@linux-node02 ~]# tree /etc/salt/
/etc/salt/
├── cloud
├── cloud.conf.d
├── cloud.deploy.d
├── cloud.maps.d
├── cloud.profiles.d
├── cloud.providers.d
├── master
├── master.d
├── minion
├── minion.d
├── minion_id
├── pki
│   ├── master
│   └── minion
│       ├── minion.pem
│       └── minion.pub
├── proxy
├── proxy.d
└── roster

# 在Master服务器中的Key保存的位置
[root@linux-node01 ~]# tree /etc/salt/
/etc/salt/
├── cloud
├── cloud.conf.d
├── cloud.deploy.d
├── cloud.maps.d
├── cloud.profiles.d
├── cloud.providers.d
├── master
├── master.d
├── minion
├── minion.d
├── minion_id
├── pki
│   ├── master
│   │   ├── master.pem
│   │   ├── master.pub
│   │   ├── minions
│   │   ├── minions_autosign
│   │   ├── minions_denied
│   │   ├── minions_pre
│   │   │   ├── linux-node02    # Minion服务器key(以id命名)保存在此处
│   │   │   └── linux-node1
│   │   └── minions_rejected
│   └── minion
│       ├── minion.pem
│       └── minion.pub
├── proxy
├── proxy.d
└── roster

2.3.2 Mater和Minion认证解析

2.3.2.1 salt-key接受之前

# Master端key在minions_pre目录下
[root@linux-node01 ~]# tree /etc/salt/
/etc/salt/
├── cloud
├── cloud.conf.d
├── cloud.deploy.d
├── cloud.maps.d
├── cloud.profiles.d
├── cloud.providers.d
├── master
├── master.d
├── minion
├── minion.d
├── minion_id
├── pki
│   ├── master
│   │   ├── master.pem
│   │   ├── master.pub
│   │   ├── minions
│   │   ├── minions_autosign
│   │   ├── minions_denied
│   │   ├── minions_pre
│   │   │   ├── linux-node02
│   │   │   └── linux-node1
│   │   └── minions_rejected
│   └── minion
│       ├── minion.pem
│       └── minion.pub
├── proxy
├── proxy.d
└── roster

# minion端minion目录下仅有minion自己的key
[root@linux-node02 ~]# tree /etc/salt/
/etc/salt/
├── cloud
├── cloud.conf.d
├── cloud.deploy.d
├── cloud.maps.d
├── cloud.profiles.d
├── cloud.providers.d
├── master
├── master.d
├── minion
├── minion.d
├── minion_id
├── pki
│   ├── master
│   └── minion
│       ├── minion.pem
│       └── minion.pub
├── proxy
├── proxy.d
└── roster

2.3.2.2 salt-key接受之后

# Master端key转换到minions目录下
[root@linux-node01 ~]# tree /etc/salt/
/etc/salt/
├── cloud
├── cloud.conf.d
├── cloud.deploy.d
├── cloud.maps.d
├── cloud.profiles.d
├── cloud.providers.d
├── master
├── master.d
├── minion
├── minion.d
│   └── _schedule.conf
├── minion_id
├── pki
│   ├── master
│   │   ├── master.pem
│   │   ├── master.pub
│   │   ├── minions
│   │   │   ├── linux-node01
│   │   │   └── linux-node02
│   │   ├── minions_autosign
│   │   ├── minions_denied
│   │   ├── minions_pre
│   │   └── minions_rejected
│   └── minion
│       ├── minion_master.pub
│       ├── minion.pem
│       └── minion.pub
├── proxy
├── proxy.d
└── roster

# minion端minion目录下出现了master的key
[root@linux-node02 ~]# tree /etc/salt/
/etc/salt/
├── cloud
├── cloud.conf.d
├── cloud.deploy.d
├── cloud.maps.d
├── cloud.profiles.d
├── cloud.providers.d
├── master
├── master.d
├── minion
├── minion.d
│   └── _schedule.conf
├── minion_id
├── pki
│   ├── master
│   └── minion
│       ├── minion_master.pub
│       ├── minion.pem
│       └── minion.pub
├── proxy
├── proxy.d
└── roster

2.3.2.3 总结

综上所述,Master和Minion之间的通信是通过交换双方的key.pub文件来完成互相认证的。

2.3.3 Mater和Minion通信解析

2.3.3.1 SaltStack与ZeroMQ

SaltStack介绍-部署和认证(一)

2.3.3.2 查看通信端口

[root@linux-node01 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1219/sshd          
tcp        0      0 0.0.0.0:4505            0.0.0.0:*               LISTEN      1564/python        
tcp        0      0 0.0.0.0:4506            0.0.0.0:*               LISTEN      1570/python        
tcp6       0      0 :::22                   :::*                    LISTEN      1219/sshd          
udp        0      0 127.0.0.1:323           0.0.0.0:*                           953/chronyd        
udp6       0      0 ::1:323                 :::*                                953/chronyd

[root@linux-node01 ~]# lsof -n -i:4505
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
salt-mast  1564 root   16u  IPv4  23012      0t0  TCP *:4505 (LISTEN)
salt-mast  1564 root   18u  IPv4  50977      0t0  <strong>TCP 10.10.10.101:4505->10.10.10.102:60386 (ESTABLISHED)</strong>
salt-mast  1564 root   19u  IPv4  51005      0t0  <strong>TCP 10.10.10.101:4505->10.10.10.101:50834 (ESTABLISHED)</strong>
salt-mini 19875 root   21u  IPv4  51004      0t0  TCP 10.10.10.101:50834->10.10.10.101:4505 (ESTABLISHED)

2.3.3.3 总结

Minion服务器本身不开启端口,所有Minion都使用TCP长连接的方式连接到Master的4505端口,发送命令的时候所有的minion都会收到命令,但是只有在salt执行时指定的minion才会响应命令。

weinxin
我的微信
如果有技术上的问题可以扫一扫我的微信
版权声明
1. 本网站名称:Leon的博客
2. 本站永久网址:https://blog.leonshadow.cn
3. 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长QQ632113590进行删除处理。
4. 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5. 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6. 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
liyang