SaltStack 使用 server-agent 通信模型,这里采用俩台云主机;一台充当 Salt master ,另一台充当 Salt minion 进行使用实验。
VM-0-7-centos (Salt master)
VM-0-8-centos (Salt minion)
注意:Salt master
主要负责向Salt minions
发送命令,Salt master
与Salt minion
通信的连接由Salt minion
发起,Salt server
使用端口4505
和4506
.
1.安装
# VM-0-7-centos (Salt master)
[root@VM-0-7-centos dev]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@VM-0-7-centos dev]# yum install -y https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest.el7.noarch.rpm
[root@VM-0-7-centos dev]# yum clean expire-cache
[root@VM-0-7-centos dev]# yum install -y salt-master
# VM-0-8-centos (Salt minion)
[root@VM-0-8-centos ~]# yum install -y https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest.el7.noarch.rpm
[root@VM-0-8-centos ~]# yum clean expire-cache
[root@VM-0-8-centos ~]# yum install -y salt-minion
2.配置
# VM-0-7-centos (Salt master)
[root@VM-0-7-centos dev]# vim /etc/salt/master
interface: 10.206.0.7
hash_type: sha256
[root@VM-0-7-centos dev]# service salt-master restart
# VM-0-8-centos (Salt minion)
[root@VM-0-8-centos ~]# vim /etc/salt/minion
master: 10.206.0.7
hash_type: sha256
[root@VM-0-8-centos ~]# service salt-minion restart
3.认证
# VM-0-7-centos (Salt master)
[root@VM-0-7-centos dev]# salt-key --accept=VM-0-8-centos
[root@VM-0-7-centos dev]# sudo salt-key -L
Accepted Keys:
VM-0-8-centos
Denied Keys:
Unaccepted Keys:
Rejected Keys:
4.远程执行命令
[root@VM-0-7-centos dev]# salt VM-0-8-centos cmd.run pwd
VM-0-8-centos:
/root
[root@VM-0-7-centos dev]# salt VM-0-8-centos cmd.run ls
VM-0-8-centos:
1.txt
[root@VM-0-7-centos dev]# salt VM-0-8-centos cmd.run uptime
VM-0-8-centos:
18:13:52 up 1 day, 6:18, 2 users, load average: 0.00, 0.01, 0.05
5.配置管理
1.配置sals
[root@VM-0-7-centos dev]# vim /etc/salt/master
file_roots:
base:
- /srv/salt/base
dev:
- /srv/salt/dev/
prod:
- /srv/salt/prod/
[root@VM-0-7-centos dev]# mkdir -p /srv/salt/{base,dev,prod}
[root@VM-0-7-centos dev]# service salt-master restart
2.编写sls
[root@VM-0-7-centos salt]# pwd
/srv/salt
[root@VM-0-7-centos salt]# cat base/apache.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
[root@VM-0-7-centos salt]# cat dev/vsftpd.sls
vsftpd-install:
pkg.installed:
- name: vsftpd
vsftpd-service:
service.running:
- name: vsftpd
- enable: True
3.使用salt命令的state状态模块让minion应用配置
[root@VM-0-7-centos salt]# salt '*' state.sls apache
VM-0-8-centos:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 18:22:11.733659
Duration: 886.431 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 18:22:12.621848
Duration: 24.923 ms
Changes:
Summary for VM-0-8-centos
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 911.354 ms
[root@VM-0-7-centos salt]# salt '*' state.sls vsftpd saltenv=dev
VM-0-8-centos:
----------
ID: vsftpd-install
Function: pkg.installed
Name: vsftpd
Result: True
Comment: All specified packages are already installed
Started: 18:23:32.281894
Duration: 887.518 ms
Changes:
----------
ID: vsftpd-service
Function: service.running
Name: vsftpd
Result: True
Comment: The service vsftpd is already running
Started: 18:23:33.171101
Duration: 22.149 ms
Changes:
Summary for VM-0-8-centos
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 909.667 ms
网友评论