Saltstack之salt-ssh
salt-ssh介绍
salt-ssh可以让我们不需要在受控机上安装salt-minion客户端也能够实现管理操作。
salt-ssh的特点
远程系统需要Python支持,除非使用-r选项发送原始ssh命令 salt-ssh是一个软件包,需安装之后才能使用,命令本身也是salt-ssh salt-ssh不会取代标准的Salt通信系统,它只是提供了一个基于SSH的替代方案,不需要ZeroMQ和agent 请注意,由于所有与Salt SSH的通信都是通过SSH执行的,因此它比使用ZeroMQ的标准Salt慢得多
salt-ssh远程管理的方式 salt-ssh有两种方式实现远程管理,一种是在配置文件中记录所有客户端的信息,诸如 IP 地址、端口号、用户名、密码以及是否支持sudo等;另一种是使用密钥实现远程管理,不需要输入密码。
通过salt-ssh安装salt-minion
[root@master ~]
AppStream 1.7 MB/s | 4.4 kB 00:00
BaseOS 1.9 MB/s | 3.9 kB 00:00
MySQL Connectors Community 1.6 kB/s | 2.6 kB 00:01
MySQL Tools Community 5.4 kB/s | 2.6 kB 00:00
MySQL 5.7 Community Server 1.3 kB/s | 2.6 kB 00:02
Salt repo for RHEL/CentOS 8 PY3 1.3 kB/s | 3.0 kB 00:02
依赖关系解决。
=======================================================================================================================================
软件包 架构 版本 仓库 大小
=======================================================================================================================================
安装:
salt-ssh noarch 3004-1.el8 salt-latest-repo 25 k
事务概要
=======================================================================================================================================
安装 1 软件包
总下载:25 k
安装大小:4.1 k
下载软件包:
salt-ssh-3004-1.el8.noarch.rpm 16 kB/s | 25 kB 00:01
---------------------------------------------------------------------------------------------------------------------------------------
总计 16 kB/s | 25 kB 00:01
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : salt-ssh-3004-1.el8.noarch 1/1
运行脚本: salt-ssh-3004-1.el8.noarch 1/1
验证 : salt-ssh-3004-1.el8.noarch 1/1
Installed products updated.
已安装:
salt-ssh-3004-1.el8.noarch
完毕!
[root@master ~]
[root@master ~]
[root@master ~]
[root@master ~]
● salt-master.service - The Salt Master Server
Loaded: loaded (/usr/lib/systemd/system/salt-master.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2021-11-02 19:18:47 CST; 3 weeks 6 days ago
Docs: man:salt-master(1)
file:///usr/share/doc/salt/html/contents.html
https://docs.saltproject.io/en/latest/contents.html
Main PID: 3501947 (salt-master)
Tasks: 38 (limit: 11070)
Memory: 110.3M
CGroup: /system.slice/salt-master.service
├─ 443902 /usr/bin/python3.6 /usr/bin/salt-master
├─3501947 /usr/bin/python3.6 /usr/bin/salt-master
├─3501954 /usr/bin/python3.6 /usr/bin/salt-master
├─3501982 /usr/bin/python3.6 /usr/bin/salt-master
├─3501985 /usr/bin/python3.6 /usr/bin/salt-master
├─3501986 /usr/bin/python3.6 /usr/bin/salt-master
├─3501987 /usr/bin/python3.6 /usr/bin/salt-master
├─3501988 /usr/bin/python3.6 /usr/bin/salt-master
├─3501995 /usr/bin/python3.6 /usr/bin/salt-master
├─3501996 /usr/bin/python3.6 /usr/bin/salt-master
├─3501997 /usr/bin/python3.6 /usr/bin/salt-master
├─3501998 /usr/bin/python3.6 /usr/bin/salt-master
└─3502001 /usr/bin/python3.6 /usr/bin/salt-master
11月 02 19:18:47 master systemd[1]: Starting The Salt Master Server...
11月 02 19:18:47 master systemd[1]: Started The Salt Master Server.
11月 02 19:26:59 master salt-master[3501947]: [ERROR ] Event iteration failed with exception: 'list' object has no attribute 'items'
[5]+ 已停止 systemctl status salt-master
[root@master ~]
[root@master base]
apache.sls top.sls web
[root@master base]
[root@master base]
apache.sls minion top.sls web
[root@master base]
[root@master base]
[root@master base]
[root@master base]
[root@master base]
.
├── apache.sls
├── minion
│ ├── files
│ │ ├── minion
│ │ └── salt.repo
│ └── install.sls
├── top.sls
└── web
└── apache
├── apache.sls
└── install.sls
4 directories, 7 files
[root@master base]
[root@master base]
[root@master base]
[root@master base]
alt-repo-cp:
file.managed:
- name: /etc/yum.repos.d/salt.repo
- source: salt://minion/files/salt.repo
- user: root
- group: root
- mode: 644
salt-minion-install:
pkg.installed:
- name: salt-minion
salt-minion-conf:
file.managed:
- name: /etc/salt/minion
- source: salt://minion/files/minion
- user: root
- group: root
- mode: 644
- require:
- pkg: salt-minion-install
salt-minion-service:
service.running:
- name: salt-minion
- enable: True
- start: True
- watch:
- file: /etc/salt/minion
[root@master base]
i^H^Hminion:
ssh: connect to host 192.168.197.128 port 22: Connection timed out
[root@master base]
Accepted Keys:
master
minion
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@master base]
The key glob 'node1' does not match any unaccepted keys.
[root@master base]
The key glob 'minion' does not match any unaccepted keys.
[root@master base]
Accepted Keys:
master
minion
Denied Keys:
Unaccepted Keys:
Rejected Keys:
masterless
(无salt-master模式)
应用场景
master 与 minion 网络不通或通信有延迟,即网络不稳定 想在 minion 端直接执行状态 ? 传统的 SaltStack 是需要通过 master 来执行状态控制 minion 从而实现状态的管理,但是当网络不稳定的时候,当想在minion本地执行状态的时候,当在只有一台主机的时候,想执行状态该怎么办呢?这就需要用到 masterless 了。
有了masterless,即使你只有一台主机,也能玩saltstack,而不需要你有N台主机。
masterless配置 修改配置文件minion:
注释master行 取消注释file_client并设其值为local 设置file_roots 设置pillar_roots
[root@node1 ~]
......
16
17
......
608
609 file_client: local
......
615
616 file_roots:
617 base:
618 - /srv/salt/base
......
[root@node1 ~]
[root@node1 ~]
Removed /etc/systemd/system/multi-user.target.wants/salt-minion.service.
salt-call
masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的–local选项。
[root@node1 ~]
local:
03:35:29 up 1:28, 3 users, load average: 0.24, 0.10, 0.12
[root@node1 ~]
local:
total 4
-rw-------. 1 root root 1023 Sep 24 04:52 anaconda-ks.cfg
salt-master高可用
? 我们需要用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。salt的高可用配置非常简单,只需要改一下minion配置文件,将master用列表的形式列出即可。
? 涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:
将master上面的/etc/salt/master配置文件复制到master2上保证一致
[root@master ~]
The authenticity of host '192.168.197.131 (192.168.197.131)' can't be established.
ECDSA key fingerprint is SHA256:qGo2KBhH76C5Wuc1GcoWxBn7Gua5/QoSQQefFVgb9Yo.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.197.131' (ECDSA) to the list of known hosts.
root@192.168.197.131's password:
master 100% 52KB 24.9MB/s 00:00
[root@master ~]
root@192.168.197.129's password:
minion 100% 451 410.1KB/s 00:00
master 100% 451 783.8KB/s 00:00
node2 100% 451 395.3KB/s 00:00
node1 100% 451 210.9KB/s 00:00
master.pem 100% 1679 1.6MB/s 00:00
master.pub 100% 451 779.8KB/s 00:00
[root@master2 pki]
.
├── master
│ └── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions
│ │ ├── master
│ │ ├── minion
│ │ ├── node1
│ │ └── node2
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre
│ └── minions_rejected
└── minion
8 directories, 6 files
将master上面的/srv/下的salt和pillar目录下的所有文件复制到master2上保证一致
[root@master ~]
[root@master2 ~]
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-master.service.
[root@node2 pki]
......
16
17 master:
18 - 192.168.197.131
19 - 192.168.197.129
......
49
50 master_type: failover
......
55
56 master_alive_interval: 10
......
74 retry_dns: 0
[root@minion ~]
[root@master ~]
minion:
True
[root@master2 ~]
No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received
[root@master ~]
[root@master2 ~]
minion:
True
|