@nalan90
2017-08-31T17:53:12.000000Z
字数 16050
阅读 883
自动化运维
基本概念与工作原理
SaltStack架构主要涉及到三个开源软件,分别是 Python 软件集,SaltStack 软件集和 ZeroMQ 消息队列软件。
SaltStack 客户端(Minion)在启动时,会自动生成一套密钥,包含私钥和公钥。之后将公钥发送给服务器端,服务器端验证并接受公钥,以此来建立可靠且加密的通信连接。同时通过消息队列 ZeroMQ 在客户端与服务端之间建立消息发布连接。具体通信原理图。
专业术语说明:
原理图说明:
架构设计
说明:
环境安装
预安装准备
## 开启防火墙
[root@master zhangshuang]# firewall-cmd --permanent --zone=public --add-port=4505-4506/tcp
success
[root@master zhangshuang]# firewall-cmd --reload
success
## 安装yum源
[root@master zhangshuang]# yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm
Master 角色的安装
## 安装salt-master
[root@master zhangshuang]# yum install -y salt-master
## 设置开机自启salt-master
[root@master zhangshuang]# systemctl enable salt-master
[root@master zhangshuang]# systemctl start salt-master
## 查看端口连接
[root@master zhangshuang]# netstat -anltp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.16.1.162:4505 172.16.1.165:58376 ESTABLISHED 22000/python
tcp 0 0 172.16.1.162:4505 172.16.1.164:42518 ESTABLISHED 22000/python
tcp 0 0 172.16.1.162:4506 172.16.1.165:41972 ESTABLISHED 22016/python
tcp 0 0 172.16.1.162:4506 172.16.1.161:52710 ESTABLISHED 22016/python
tcp 0 0 172.16.1.162:4506 172.16.1.163:39124 ESTABLISHED 22016/python
tcp 0 0 172.16.1.162:4505 172.16.1.163:37832 ESTABLISHED 22000/python
tcp 0 0 172.16.1.162:4506 172.16.1.164:60848 ESTABLISHED 22016/python
tcp 0 0 172.16.1.162:4505 172.16.1.161:50256 ESTABLISHED 22000/python
Minion 角色的安装
## 安装salt-minion
[root@slave1 zhangshuang]# yum install -y salt-minion
## 修改Master主机地址
[root@slave1 zhangshuang]# vim /etc/salt/minion
master: 172.16.1.162
## 设置开机自启salt-minion
[root@slave1 zhangshuang]# systemctl enable salt-minion
[root@slave1 zhangshuang]# systemctl start salt-minion
[root@slave1 zhangshuang]# netstat -anltp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.16.1.163:39124 172.16.1.162:4506 ESTABLISHED 20560/python
tcp 0 0 172.16.1.163:37832 172.16.1.162:4505 ESTABLISHED 20560/python
授权认证
## 接受所有minions的认证请求
[root@master zhangshuang]# salt-key -A
## 查看所有minions的认证列表
[root@master zhangshuang]# salt-key -L
Accepted Keys:
dev-161
slave2
slave3
slave1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
Grains收集系统信息
## 查看grains模块的所有functions
[root@master zhangshuang]# salt dev-161 sys.list_functions grains
dev-161:
- grains.append
- grains.delval
- grains.filter_by
- grains.get
- grains.get_or_set_hash
- grains.has_value
- grains.item
- grains.items
- grains.ls
- grains.remove
- grains.setval
- grains.setvals
----------------------------------------
## 查看grains可以收集的所有系统项
[root@master zhangshuang]# salt dev-161 grains.ls
dev-161:
- SSDs
- biosreleasedate
- biosversion
- cpu_flags
.....
- os
- os_family
- osarch
- oscodename
- osfinger
- osfullname
- osmajorrelease
- osrelease
- osrelease_info
----------------------------------------
## 查看所有项的具体内容
[root@master zhangshuang]# salt dev-161 grains.items
----------------------------------------
## 查看提定项的信息
[root@master zhangshuang]# salt dev-161 grains.item os
dev-161:
----------
os:
CentOS
----------------------------------------
## 查看minions IP
[root@master zhangshuang]# salt '*' grains.item ipv4
slave2:
----------
ipv4:
- 127.0.0.1
- 172.16.1.164
- 172.20.0.1
dev-161:
----------
ipv4:
- 127.0.0.1
- 172.16.1.161
- 172.20.0.1
slave3:
----------
ipv4:
- 127.0.0.1
- 172.16.1.165
- 172.20.0.1
slave1:
----------
ipv4:
- 127.0.0.1
- 172.16.1.163
- 172.20.0.1
minions名称匹配
## 全局匹配
root@master zhangshuang]# salt '*' test.ping
slave2:
True
slave1:
True
dev-161:
True
slave3:
True
[root@master zhangshuang]# salt dev-161 test.ping
dev-161:
True
[root@master zhangshuang]# salt '*-161' test.ping
dev-161:
True
[root@master zhangshuang]# salt 'dev-???' test.ping
dev-161:
True
[root@master zhangshuang]# salt 'slave[0-9]' test.ping
slave1:
True
slave2:
True
slave3:
True
----------------------------------------
## 正则匹配
[root@master zhangshuang]# salt -E 'dev' test.ping
dev-161:
True
[root@master zhangshuang]# salt -E 'slave' test.ping
slave1:
True
slave2:
True
slave3:
True
[root@master zhangshuang]# salt -E ".*" test.ping
slave2:
True
slave1:
True
dev-161:
True
slave3:
True
[root@master zhangshuang]# salt -E 'dev-[0-9]{3}' test.ping
dev-161:
True
----------------------------------------
## 列表匹配
[root@master zhangshuang]# salt -L 'slave1,slave2,slave3' test.ping
slave1:
True
slave2:
True
slave3:
True
## 配置/etc/salt/master nodegroups字段,暂未配置
[root@master zhangshuang]# salt -N minions test.ping
Node group minions unavailable in /etc/salt/master
常用命令
## 列出dev-161 minion的所有模块
[root@master zhangshuang]# salt dev-161 sys.list_modules
dev-161:
- acl
- aliases
- apache
- artifactory
......
- xfs
## 列出dev-161 minion test module的所有function
[root@master zhangshuang]# salt dev-161 sys.list_functions test
dev-161:
- test.arg
- test.echo
.....
- test.version
- test.versions_report
----------------------------------------
## 查看test.ping的使用文档
[root@master zhangshuang]# salt dev-161 sys.doc test.ping
'test.ping:'
Used to make sure the minion is up and responding. Not an ICMP ping.
Returns ``True``.
CLI Example:
salt '*' test.ping
----------------------------------------
## 查看所有minions的系统版本信息
[root@master zhangshuang]# salt '*' cmd.run 'uname -r'
slave1:
3.10.0-514.el7.x86_64
dev-161:
3.10.0-514.el7.x86_64
slave2:
3.10.0-514.el7.x86_64
slave3:
3.10.0-514.el7.x86_64
----------------------------------------
## 包管理
[root@master zhangshuang]# salt dev-161 pkg.install httpd
dev-161:
----------
[root@master zhangshuang]# salt dev-161 pkg.version httpd
dev-161:
2.4.6-45.el7.centos.4
[root@master zhangshuang]# salt dev-161 service.status httpd
dev-161:
True
[root@master zhangshuang]# salt dev-161 service.stop httpd
dev-161:
True
[root@master zhangshuang]# salt dev-161 service.status httpd
dev-161:
False
[root@master zhangshuang]# salt dev-161 pkg.remove httpd
dev-161:
----------
httpd:
----------
new:
old:
2.4.6-45.el7.centos.4
----------------------------------------
## 文件管理
[root@master zhangshuang]# salt dev-161 file.stats /etc/yum.conf
dev-161:
----------
atime:
1503995808.45
ctime:
1487821839.52
gid:
0
group:
root
inode:
67191933
mode:
0644
mtime:
1479223823.0
size:
970
target:
/etc/yum.conf
type:
file
uid:
0
user:
root
[root@master zhangshuang]# salt dev-161 file.chown /etc/passwd root root
dev-161:
None
----------------------------------------
## 用户管理
[root@master zhangshuang]# salt dev-161 user.add mysql
dev-161:
True
[root@master zhangshuang]# salt dev-161 cmd.run 'ls -l /home'
dev-161:
total 0
drwx------. 3 gitlab-runner gitlab-runner 74 Mar 8 08:27 gitlab-runner
drwx------ 5 hadoop hadoop 137 Aug 25 09:40 hadoop
drwx------. 3 1000 1000 74 Mar 8 08:27 jenkins
drwx------ 2 mysql mysql 62 Aug 30 13:47 mysql
drwx------. 4 shensi shensi 112 Mar 27 13:00 shensi
drwx------. 4 2004 2004 112 Mar 27 13:00 shuran
drwx------ 2 mysql mysql 62 Aug 29 17:57 test
drwx------. 5 zhangshuang zhangshuang 147 Mar 27 13:09 zhangshuang
drwx------. 4 2007 2007 112 Mar 27 13:00 zhanxin
[root@master zhangshuang]# salt dev-161 user.info mysql
dev-161:
----------
fullname:
gid:
2010
groups:
- mysql
home:
/home/mysql
homephone:
name:
mysql
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
2010
workphone:
[root@master zhangshuang]# salt dev-161 user.delete mysql
dev-161:
True
模块管理
## 编写自定义的模块
[root@master zhangshuang]# mkdir -p /srv/salt/_modules
[root@master zhangshuang]# vim /srv/salt/_modules/hello.py
[root@master zhangshuang]# cat /srv/salt/_modules/hello.py
def world():
"""
This is my first function.
CLI Example::
salt '*' hello.world
"""
return 'Hello, world!'
## 推送模块至minions
[root@master zhangshuang]# salt '*' saltutil.sync_modules
slave1:
- modules.hello
salve2:
- modules.hello
dev-161:
- modules.hello
salve3:
- modules.hello
[root@master zhangshuang]# salt '*' hello.world
salve3:
Hello, world!
dev-161:
Hello, world!
salve2:
Hello, world!
slave1:
Hello, world!
state 模块使用
[root@master salt]# salt dev-161 sys.list_functions state
dev-161:
- state.apply
- state.check_request
- state.clear_cache
- state.clear_request
- state.disable
- state.enable
- state.high
- state.highstate
- state.list_disabled
- state.low
- state.pkg
- state.request
- state.run_request
- state.running
- state.show_highstate
- state.show_low_sls
- state.show_lowstate
- state.show_sls
- state.show_top
- state.single
- state.sls
- state.sls_id
- state.template
- state.template_str
- state.top
----------------------------------------
## 编写SLS文件
[root@master salt]# pwd
/srv/salt
[root@master salt]# vim apache.sls
[root@master salt]# cat apache.sls
install_httpd:
pkg.installed:
- name: httpd
## 安装httpd
[root@master salt]# salt '*' state.sls apache
salve2:
----------
ID: install_httpd
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 14:43:44.602919
Duration: 49646.909 ms
Changes:
----------
apr:
----------
new:
1.4.8-3.el7
old:
apr-util:
----------
new:
1.5.2-6.el7
old:
httpd:
----------
new:
2.4.6-45.el7.centos.4
old:
httpd-tools:
----------
new:
2.4.6-45.el7.centos.4
old:
mailcap:
----------
new:
2.1.41-2.el7
old:
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
----------------------------------------
## 查看所有state模块
[root@master salt]# salt dev-161 sys.list_state_modules
dev-161:
- acl
- alias
- alternatives
- apache
......
- winrepo
[root@master salt]# salt dev-161 sys.list_state_functions pkg
dev-161:
- pkg.installed
- pkg.latest
- pkg.mod_aggregate
- pkg.mod_init
- pkg.purged
- pkg.removed
- pkg.uptodate
----------------------------------------
[root@master ~]# tree /srv/salt/
/srv/salt/
├── apache.sls
├── httpd.conf
└── _modules
├── hello.py
└── prank.py
1 directory, 4 files
[root@master ~]# tree /srv/salt/
/srv/salt/
├── apache.sls
├── httpd.conf
└── _modules
├── hello.py
└── prank.py
1 directory, 4 files
## apache.sls内容
[root@master ~]# cat /srv/salt/apache.sls
install_httpd:
pkg.installed:
- name: httpd
httpd_running:
service.running:
- name: httpd
- enable: True
- require:
- pkg: install_httpd
- watch:
- file: httpd_conf
httpd_conf:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://httpd.conf
- user: root
- group: root
- mode: 600
----------------------------------------
## 执行SLS文件命令
[root@master ~]# salt dev-161 state.sls apache
dev-161:
----------
ID: install_httpd
Function: pkg.installed
Name: httpd
Result: True
Comment: Package httpd is already installed.
Started: 15:03:07.779195
Duration: 547.207 ms
Changes:
----------
ID: httpd_conf
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf updated
Started: 15:03:08.328355
Duration: 3.764 ms
Changes:
----------
mode:
0600
----------
ID: httpd_running
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 15:03:08.332237
Duration: 527.233 ms
Changes:
----------
httpd:
True
Summary
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3
state 实例
## 安装Nginx并启动
## SLS文件
[root@master ~]# cat /srv/salt/nginx/init.sls
[root@master ~]# cat /srv/salt/nginx/init.sls
nginx:
pkg:
- installed
service:
- running
- enable: True
- user: nginx
- require:
- user: nginx
- watch:
- file: /etc/nginx/nginx.conf
- file: /etc/nginx/sites-enabled/default
user.present:
- home: /home/nginx
- shell: /bin/bash
- gid: nginx
- require:
- group: nginx
group.present:
- require:
- pkg: nginx
configure_nginx:
file.managed:
- name: /etc/nginx/nginx.conf
- source: salt://nginx/files/nginx.conf
- user: nginx
- group: nginx
/etc/nginx/sites-enabled/default:
file.absent
----------------------------------------
## 文件结构
[root@master ~]# tree /srv/salt/nginx/
/srv/salt/nginx/
├── files
│ └── nginx.conf
└── init.sls
1 directory, 2 files
----------------------------------------
## 执行结果
[root@master ~]# salt dev-161 state.sls nginx
dev-161:
## 1、安装nginx
----------
ID: nginx
Function: pkg.installed
Result: True
Comment: Package nginx is already installed.
Started: 17:02:02.257036
Duration: 669.776 ms
Changes:
## 2、创建nginx组
----------
ID: nginx
Function: group.present
Result: True
Comment: Group nginx is present and up to date
Started: 17:02:02.928639
Duration: 0.36 ms
Changes:
## 3、创建nginx用户
----------
ID: nginx
Function: user.present
Result: True
Comment: User nginx is present and up to date
Started: 17:02:02.929107
Duration: 0.741 ms
Changes:
## 4、复制本地nginx.conf到指定服务器
----------
ID: configure_nginx
Function: file.managed
Name: /etc/nginx/nginx.conf
Result: True
Comment: File /etc/nginx/nginx.conf is in the correct state
Started: 17:02:02.931000
Duration: 3.591 ms
Changes:
## 5、删除/etc/nginx/sites-enabled/default文件
----------
ID: /etc/nginx/sites-enabled/default
Function: file.absent
Result: True
Comment: File /etc/nginx/sites-enabled/default is not present
Started: 17:02:02.934678
Duration: 0.205 ms
Changes:
## 6、启动nginx服务
----------
ID: nginx
Function: service.running
Result: True
Comment: Service nginx is already enabled, and is running
Started: 17:02:02.935036
Duration: 336.938 ms
Changes:
----------
nginx:
True
Summary
------------
Succeeded: 6 (changed=1)
Failed: 0
------------
Total states run: 6
## 创建wilson用户并拷贝公钥
## SLS文件
[root@master ~]# cat /srv/salt/useradd/init.sls
wilson:
user.present:
- home: /home/wilson
- shell: /bin/bash
- gid: wilson
- require:
- group: wilson
group.present:
- name: wilson
/home/wilson/.ssh/authorized_keys:
file.managed:
- source: salt://useradd/authorized_keys
- user: wilson
- group: wilson
- mode: 600
- require:
- user: wilson
- file: /home/wilson/.ssh
/home/wilson/.ssh:
file.directory:
- user: wilson
- group: wilson
- mode: 700
- require:
- user: wilson
----------------------------------------
## 文件结构
[root@master ~]# tree /srv/salt/useradd/
/srv/salt/useradd/
├── authorized_keys
└── init.sls
0 directories, 2 files
----------------------------------------
## 执行结果
[root@master ~]# salt dev-161 state.sls useradd
dev-161:
----------
ID: wilson
Function: group.present
Result: True
Comment: Group wilson is present and up to date
Started: 17:37:47.683562
Duration: 1.205 ms
Changes:
----------
ID: wilson
Function: user.present
Result: True
Comment: User wilson is present and up to date
Started: 17:37:47.684946
Duration: 37.578 ms
Changes:
----------
ID: /home/wilson/.ssh
Function: file.directory
Result: True
Comment: Directory /home/wilson/.ssh updated
Started: 17:37:47.726595
Duration: 1.553 ms
Changes:
----------
/home/wilson/.ssh:
New Dir
----------
ID: /home/wilson/.ssh/authorized_keys
Function: file.managed
Result: True
Comment: File /home/wilson/.ssh/authorized_keys updated
Started: 17:37:47.728318
Duration: 8.479 ms
Changes:
----------
diff:
New file
group:
wilson
user:
wilson
Summary
------------
Succeeded: 4 (changed=2)
Failed: 0
------------
Total states run: 4
----------------------------------------
## SLS使用变量
[root@master useradd]# cat init.sls
useradd:
user.present:
{% set name = pillar['name'] %}
- name: {{ name }}
- home: /home/{{ name }}
- shell: /bin/bash
- gid: {{ name }}
- groups:
- docker
- {{ name }}
- require:
- group: {{ name }}
group.present:
- name: {{ name }}
/home/{{ name }}/.ssh/authorized_keys:
file.managed:
- source: salt://useradd/authorized_keys
- user: {{ name }}
- group: {{ name }}
- mode: 600
- require:
- user: {{ name }}
- file: /home/{{ name }}/.ssh
/home/{{ name }}/.ssh:
file.directory:
- user: {{ name }}
- group: {{ name }}
- mode: 700
- require:
- user: {{ name }}
## 执行结果
[root@master useradd]# salt dev-161 state.sls useradd pillar='{"name":"jack"}'
dev-161:
----------
ID: useradd
Function: group.present
Name: jack
Result: True
Comment: New group jack created
Started: 18:02:01.274312
Duration: 60.186 ms
Changes:
----------
gid:
2011
members:
name:
jack
passwd:
x
----------
ID: useradd
Function: user.present
Name: jack
Result: True
Comment: New user jack created
Started: 18:02:01.334737
Duration: 46.215 ms
Changes:
----------
fullname:
gid:
2011
groups:
- jack
home:
/home/jack
homephone:
name:
jack
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
2011
workphone:
----------
ID: /home/jack/.ssh
Function: file.directory
Result: True
Comment: Directory /home/jack/.ssh updated
Started: 18:02:01.384423
Duration: 8.189 ms
Changes:
----------
/home/jack/.ssh:
New Dir
----------
ID: /home/jack/.ssh/authorized_keys
Function: file.managed
Result: True
Comment: File /home/jack/.ssh/authorized_keys updated
Started: 18:02:01.392797
Duration: 7.8 ms
Changes:
----------
diff:
New file
group:
jack
user:
jack
Summary
------------
Succeeded: 4 (changed=4)
Failed: 0
------------
Total states run: 4