官网安装:http://repo.saltstack.com/
模块-官网文档:docs.saltstack.cn
特点
服务架构
功能
运行方式
配置文件
master端:
server1:192.168.17.1
minion端:
server2:192.168.17.2
server3:192.168.17.3
vim /etc/yum.repos.d/salt-3000.repo
[salt-3000]
name=SaltStack 3000 Release Channel for Python 2 RHEL/Centos $releasever
baseurl=https://mirrors.aliyun.com/saltstack/yum/redhat/7/$basearch/3000
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/saltstack-signing-key, file:///etc/pki/rpm-gpg/centos7-signing-key
yum install -y salt-minion
vim /etc/salt/minion
16 master: 192.168.17.1
systemctl enable --now salt-minion
yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-3000.el7.noarch.rpm
sed -i "s/repo.saltstack.com/mirrors.aliyun.com\/saltstack/g" /etc/yum.repos.d/salt-latest.repo
cat /etc/yum.repos.d/salt-3000.repo
[salt-3000]
name=SaltStack 3000 Release Channel for Python 2 RHEL/Centos $releasever
baseurl=https://mirrors.aliyun.com/saltstack/yum/redhat/7/$basearch/3000
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/saltstack-signing-key, file:///etc/pki/rpm-gpg/centos7-signing-key
yum install -y salt-master
systemctl enable --now salt-master
salt-key -A
:接受所有minion等待认证的key
salt-key -L
:列出所有公钥信息
-a minion#接受指定minion等待认证的key
-r minion#拒绝指定minion等待认证的key
-R#拒绝所有minion等待认证的key
yum install -y lsof#可以显示监听端口详细信息
yum install -y python-setproctitle#可以显示进程详细信息
systemctl restart salt-master.service
salt '*' test.ping
salt '*' cmd.run hostname
salt '*' cmd.run 'uname -a'
salt 'server2' sys.doc pkg
:查看模块文档
#安装httpd
salt server2 pkg.install httpd
#列出安装包
salt server2 cmd.run 'rpm -q httpd'
##开启服务
salt server2 service.start httpd
echo server2 > index.html
salt-cp server2 index.html /var/www/html/
- 有apache.sls和apache目录,先匹配apache.sls
- 调用时不加.sls后缀
mkdir -p /srv/salt/apache/files
执行 salt minion主机 state.sls apache.install
运行
#apache/install.sls
httpd:
pkg.installed
#apache/install.sls
apache:
pkg.installed:
- pkgs:
- httpd
- php
#apache/install.sls
apache:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- source: salt://apache/files/index.html#此文件内容一修改,运行时就会显示有改变,因为有md5sum码
- name: /var/www/html/index.html
#apache/install.sls
apache:
pkg.installed:
- pkgs:
- httpd
- php
/var/www/html/index.html:
file.managed:
- source: salt://apache/files/index.html
httpd:
service.running
#apache/install.sls
apache:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- source: salt://apache/files/index.html
- name: /var/www/html/index.html
service.running:
- name: httpd
cp /etc/httpd/conf/httpd.conf apache/files/
:复制模板文件
vim apache/files/httpd.conf
:修改端口检测
salt server2 state.sls apache.install
#apache/install.sls
apache:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- source: salt://apache/files/index.html
- name: /var/www/html/index.html
service.running:
- name: httpd
- enable: true
- reload: true
- watch:
- file: /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://apache/files/httpd.conf
#apache/install.sls
apache:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- source: salt://apache/files/httpd.conf
- name: /etc/httpd/conf/httpd.conf
service.running:
- name: httpd
- enable: true
- watch:
- file: apache
mkdir /srv/salt/_modules
:创建模块目录
vim _modules/mydisk.py
:编写模块
def df():
return __salt__['cmd.run']('df -h')
salt server2 cmd.run df
salt server2 saltutil.sync_modules
:同步模块Grains是SaltStack的一个组件,存放在SaltStack的minion端。
当salt-minion启动时会把收集到的数据静态存放在Grains当中,只有当minion重启时才会进行数据的更新。
由于grains是静态数据,因此不推荐经常去修改它。
应用场景:
master端查看信息
#列出每一项
salt server2 grains.ls
#查看每一项的具体值
salt server2 grains.items
#查看单项的值
salt server2 grains.item ipv4
salt server2 grains.item fqdn
server2
[root@server2 ~]# vim /etc/salt/minion
129 grains:
130 roles:
131 - apache
[root@server2 ~]# systemctl restart salt-minion
server3
[root@server3 ~]# vim /etc/salt/minion
129 grains:
130 roles:
131 - nginx
[root@server3 ~]# systemctl restart salt-minion.service
在master端查看
salt server2 grains.item roles
salt server3 grains.item roles
mkdir /srv/salt/_grains
vim /srv/salt/_grains/mygrains.py
def my_grains():
grains = {
}
grains['salt'] = 'stack'
grains['hello'] = 'world'
return grains
salt '*' saltutil.sync_grains
可以在minion端查看到 /var/cache/salt/minion/files/base/_grains/mygrains.py
salt '*' grains.item salt
salt '*' grains.item hello
salt -G roles:apache cmd.run hostname
salt -G roles:nginx test.ping
salt -G salt:stack test.ping
salt -G hello:world test.ping
mkdir -p /srv/salt/nginx/files/
[root@server1 salt]# ls nginx/files/
nginx-1.18.0.tar.gz
vim /srv/salt/top.sls
base:
'roles:apache':
- match: grain
- apache
'roles:nginx':
- match: grain
- nginx
vim /srv/salt/nginx/init.sls
nginx:
file.managed:
- source: salt://nginx/files/nginx-1.18.0.tar.gz
- name: /mnt/nginx-1.18.0.tar.gz
salt '*' state.highstate
:读取所有环境的top.sls文件cd /src/salt/
vim nginx/files/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
vim nginx/install.sls
nginx-install:
pkg.installed:
- pkgs:
- gcc
- pcre-devel
- openssl-devel
file.managed:
- source: salt://nginx/files/nginx-1.18.0.tar.gz
- name: /mnt/nginx-1.18.0.tar.gz
cmd.run:
- name: cd /mnt && tar zxf nginx-1.18.0.tar.gz && cd nginx-1.18.0 && ./configure --prefix=/usr/local/nginx --with-http_ssl_module &> /dev/null && make &> /dev/null && make install &> /dev/null
- creates: /usr/local/nginx
vim nginx/init.sls
include:
- nginx.install
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://nginx/files/nginx.conf
nginx-service:
user.present:
- name: nginx
- shell: /sbin/nologin
- home: /usr/local/nginx
- createhome: false
file.managed:
- source: salt://nginx/files/nginx.service
- name: /usr/lib/systemd/system/nginx.service
service.running:
- name: nginx
- enable: true
- reload: true
- watch:
- file: /usr/local/nginx/conf/nginx.conf
salt server3 state.sls nginx.install
scp server3:/usr/local/nginx/conf/nginx.conf nginx/files/
vim nginx/files/nginx.conf
user nginx;
salt server3 state.sls nginx
{% XXX %}
{ { XXX }}
vim test.sls
/mnt/testfile:
file.append:
{
% if grains['fqdn'] == 'server2' %}
- text: server2
{
% elif grains['fqdn'] == 'server3' %}
- text: server3
{
% endif %}
salt '*' state.sls test
cd /src/salt/
vim apache/init.sls
apache:
pkg.installed:
- pkgs:
- httpd
file.managed:
- source: salt://apache/files/httpd.conf
- name: /etc/httpd/conf/httpd.conf
- template: jinja
- context:
port: 80
bind: {
{
grains['ipv4'][-1] }}
service.running:
- name: httpd
- enable: true
- watch:
- file: apache
/var/www/html/index.html:
file.managed:
- source: salt://apache/files/index.html
- template: jinja
- context:
NAME: {
{
grains['ipv4'][-1] }}
vim apache/files/index.html
{
{
grains['os'] }} - {
{
grains['fqdn'] }}
{
{
NAME }}
vim apache/files/httpd.conf
Listen {
{
bind }}:{
{
port }}
salt server2 state.sls apache