这次主要介绍saltstack的配置管理,这一部分在企业应用中比较重要,也经常用到。
states是saltstack系统中的配置语言,在日常运维中需要编写大量的states文件,例如:创建用户、安装软件、配置软件、服务运行等。需要编写一些"states sls"文件。该文件主要使用YAML语言,也可以支持使用python语言编写。
配置管理中常用模块
pkg模块
列出所有状态模块
salt ‘‘ sys.list_modules
查看状态模块中的函数:如pkg模块
[[email protected] _grains]# salt ‘‘ sys.list_state_functions pkg
salt-minion01:
- pkg.downloaded
- pkg.group_installed
- pkg.installed
- pkg.latest
- pkg.mod_aggregate
- pkg.mod_init
- pkg.mod_watch
- pkg.patch_downloaded
- pkg.patch_installed
- pkg.purged
- pkg.removed
- pkg.uptodate
salt-minion02: - pkg.downloaded
- pkg.group_installed
- pkg.installed
- pkg.latest
- pkg.mod_aggregate
- pkg.mod_init
- pkg.mod_watch
- pkg.patch_downloaded
- pkg.patch_installed
- pkg.purged
- pkg.removed
- pkg.uptodate
file模块
[email protected] app]# mkdir files
[[email protected] app]# cp /etc/resolv.conf files/
[[email protected] app]# cat dns.sls
dns-config:
file.managed: - name: /etc/resolv.conf #file of
- source: salt://app/files/resolv.conf salt代表file根目录
- user: root
- group: root
- mode: 644
- backup: minion ------备份到minion端
修改/srv/salt/app/files下的resolv.conf文件
[[email protected] files]# cat resolv.conf
- backup: minion ------备份到minion端
nameserver 101.206.4.6
nameserver 114.114.114.114
###########saltstack configure ----------》添加一行注释
salt ‘salt-minion01‘ state.sls app.dns test=True 测试
salt ‘salt-minion01‘ state.sls app.dns
查看
[[email protected] salt]# cat /etc/resolv.conf
nameserver 101.206.4.6
nameserver 114.114.114.114
###########saltstack configure
file.directory ------创建目录
[[email protected] app]# cat dir.sls
mkdir_tmp_dns:
file.directory:
- name: /tmp/dns
- user: root
- group: root
- mode: 755
- makedirs: True
目录下发 file.recurse
[[email protected] app]# cat xf.sls
httpd-config:
file.recurse:
- name: /etc/httpd/conf.d/
- source: salt://app/files/httpd_conf.d
- file_mode: 644
- dir_mode: 755
- include_empty: True
- clean: True #####远程强制删除
mkdir files/httpd_conf.d
[[email protected] app]# cat files/httpd_conf.d/www.conf
server {www
}
[[email protected] app]# cat files/httpd_conf.d/blog.conf
erver {
blog
}
[[email protected] app]# salt ‘salt-minion01‘ state.sls app.xf
minion01
[[email protected] tmp]# ls /etc/httpd/conf.d/
autoindex.conf fcgid.conf README userdir.conf www.conf
blog.conf manual.conf ssl.conf welcome.conf
可以直接采用以下方法:
[[email protected] app]# cat ln.sls
Link:
cmd.run:
- name: ln -s /tmp /mnt
service模块
httpd_service:
service.running:
- name: httpd
- enable: True
- reload: True #######允许重载,不写表示restart
- watch: #####监控谁
-pkg.httpd-filecron模块
[[email protected] app]# cat cron.sls
crontab_scripts:
cron.present: - name: /usr/bin/uptime >/dev/null 2>&1
- user: root
- minute: ‘/5‘
salt ‘salt-minion01‘ state.sls app.cron
结果:
[[email protected] tmp]# crontab -l
/30 /usr/sbin/ntpdate time.windows.com
/5 * /usr/bin/uptime >/dev/null 2>&1高级状态模块,高级状态文件需要在base环境下编写
base:
‘*‘: 匹配所有minion - app.httpd #base模块下httpd目录下的安装sls文件
#webserver: #定义的分组名称进行匹配,需要定义nodegroups‘os:centos‘: #通过grains模块匹配
- match: grains
- app.cron
top.sls需要写在/srv/salt下
执行是salt ‘*‘ state.highstate
LAMP架构部署
此案例在prod生产环境下部署
修改master端配置文件
file_roots:
base:
- /srv/salt/base
prod: - /srv/salt/prod
systemctl restart salt-master
mkdir /srv/salt/{base,prod} -p
把之前的基础环境下写的放在base下
1 先手动安装lamp环境
yum install httpd mariadb mariadb-server php php-mysql php-gd gd -y
[[email protected] init]# pwd
/srv/salt/prod/init
mkdir files -------相关配置写在该目录下
2 准备相应目录与对应配置文件
cp /etc/httpd/conf/httpd.conf files/
cp /etc/my.cnf files/
cp /etc/php.ini files/
在init目录下写lamp安装,配置 启动
3 部署状态文件lamp.sls
[[email protected] init]# cat lamp.sls
#yum install httpd mariadb-server php php-mysql php-gd gd -y
httpd-install:
pkg.installed:
- names:
- httpd
- httpd-tools
httpd-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://init/files/httpd.conf
- user: root
- group: root
- mode: 644
- backup: minion
httpd-service:
service.running: - name: httpd
- enable: True
- reload: True
php-install:
pkg.installed: - names:
- php
- php-mysql
- php-gd
- gd
php-config:
file.managed:
- name: /etc/php.ini
- source: salt://init/files/php.ini
- user: root
- group: root
- mode: 644
- backup: minion
mysql-install:
pkg.installed: - names:
- mariadb
- mariadb-server
mysql-config:
file.managed:
- name: /etc/my.cnf
- source: salt://init/files/my.cnf
- user: root
- group: root
- mode: 644
- backup: minion
mysql-service:
service.running: - name: mariadb
- enable: True
salt ‘salt-minion01‘ state.sls init.lamp saltenv=prod 指定为prod环境,因为默认为base环境
通过高级状态进行管理
通过高级状态来管理更方便,需要在base下写top.sls
[[email protected] base]# cat top.sls
prod:
‘salt-minion01‘:
- init.lamp
然后执行salt ‘salt-minion01‘ state.highstate
查看目录结构
[[email protected] base]# tree /srv/salt/
/srv/salt/
├── base
│ ├── app
│ │ ├── cron.sls
│ │ ├── dir.sls
│ │ ├── dns.sls
│ │ ├── files
│ │ │ ├── httpd_conf.d
│ │ │ │ ├── blog.conf
│ │ │ │ └── www.conf
│ │ │ └── resolv.conf
│ │ ├── httpd.sls
│ │ ├── ln.sls
│ │ ├── service.sls
│ │ ├── vsftpd.sls
│ │ └── xf.sls
│ ├── _grains
│ │ └── my_grains.py
│ ├── top.sls
│ └── top.sls20181121
└── prod
└── init
├── files
│ ├── httpd.conf
│ ├── my.cnf
│ └── php.ini
└── lamp.sls
states状态依赖管理
可以用state模块来定义minion的状态,但是如果一个主机涉及多个状态,并且状态之间有相互关联,需要在执行顺序上有先后之分,那么必须引入requisites 来进行控制。
- require 我依赖某个状态,我依赖谁
- require_in 我被某个状态依赖,谁依赖我
- watch 我监控某个状态,如当状态发生变化时,就进行restart或reload操作
- watch_in 我被某个状态关注
- include 我引用谁
require写法
httpd-install:
pkg.installed:- names:
- httpd
- httpd-tools
httpd-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://init/files/httpd.conf
- user: root
- group: root
- mode: 644
- backup: minion
- require:
- pkg: httpd-install 因为要修改配置需要先安装软件
- names:
watch写法
httpd-service:
service.running:
- name: httpd
- enable: True
- reload: True
- require:
- file: httpd-config
- watch:
- file: httpd-config
watch_in写法
php-config:
file.managed:
- file: httpd-config
- name: /etc/php.ini
- source: salt://init/files/php.ini
- user: root
- group: root
- mode: 644
- backup: minion
- watch_in:
- service: httpd-service
利用include将lamp拆分,如将mysql拆分出来
- service: httpd-service
[[email protected] init]# cat mysql.sls
mysql-install:
pkg.installed:
- names:
- mariadb
- mariadb-server
mysql-config:
file.managed:
- name: /etc/my.cnf
- source: salt://init/files/my.cnf
- user: root
- group: root
- mode: 644
- backup: minion
- require:
- pkg: mysql-install
mysql-service:
service.running:
- pkg: mysql-install
- name: mariadb
- enable: True
- require:
- file: mysql-config
[[email protected] init]# mv lamp.sls apache.sls
在prod目录建立lamp.sls
[[email protected] prod]# cat lamp.sls
include:
- init.apache
- init.mysql
执行salt ‘salt-minion01‘ state.sls lamp saltenv=prod
通过高级状态管理:修改topfile文件如下:
prod:
‘salt-minion01‘:- lamp
Jinja模板使用
配置文件一般都是固定无法灵活多变,实际生产过程中,需要配置文件灵活多变,不能写死,有时还需要进行流程控制,这就需要用到jinjia,可以很灵活地进行配置管理及简化配置过程。
Jinja2模板包含变量和表达式
变量用{{...}}包围,表达式用{%...%}包围
看一个简单的例子
[[email protected] prod]# cat var.sls
{% set var= ‘hello world!‘ %}
test_var:
cmd.run:
- name: echo "{{ var }}"
字符串类型
{% set var= ‘hello world!‘ %}
列表类型
{% set list = [‘one‘,‘two‘ ] %}
{{ list[1]}}
字典类型
{% set dict = {‘first‘:‘value‘,‘second‘:‘value1‘} %}
{{ dict[‘first‘] }}
Jinja模板使用步骤
- 告诉file状态模块,需要使用jinja
- template: jinja
2列出参数列表 - defaults:
PORT: 88
3.配置文件引用jinja模板
{{ PORT }}
配置实例,修改apache端口
httpd-config:
file.managed: - name: /etc/httpd/conf/httpd.conf
- source: salt://init/files/httpd.conf
- user: root
- group: root
- mode: 644
- backup: minion
- require:
- pkg: httpd-install
- template: jinja
- defaults:
PORT: 8000
files目录下http配置文件中将Listen 80改为Listen {{ PORT }}
然后执行salt ‘salt-minion01‘ state.highstate
查看minion端端口已改动
[[email protected] ~]# netstat -tnlp|grep 8000
tcp6 0 0 :::8000 :::* LISTEN 3915/httpd
- template: jinja
模板支持grains pillar进行赋值
grains方式
通过grains获取minion端IP
[[email protected] prod]# salt ‘salt-minion01‘ grains.item fqdn_ip4
salt-minion01:
fqdn_ip4:
- 192.168.132.31
如果想在http配置文件中使用IP:PORT
Listen {{ IPADDR}}:{{ PORT }}
apache文件中修改
- defaults:
PORT: 8000
IPADDR: {{ grains[‘fqdn_ip4‘][0]}} ---取列表中的第一个值即IP地址pillar方式:
apache:
IP: {{ grains[‘fqdn_ip4‘][0] }}
PORT: 8008
引用
{{ pillar[‘apache‘][IP] }}
{{ pillar[‘apache‘][port] }}
实际场景中,如系统有centos和ubuntu,都要安装apache,由于安装的包不一样,则可以根据grains静态数据,使用jinja2流程控制
httpd_install:
pkg.installed:
- template: jinja
{% if grains[‘os_family‘] == ‘Debian‘ %}- name: apache2
{% elif grains[‘os_family‘] == ‘RedHat‘ %} - name: http
{% endif %}
saltstack Job管理
Job概述
salt每次运行任务都会将任务发布到pub-sub总线,minion会对任务作出响应,为区分不同的任务,saltmaster每次发布一个任务都会为该任务创建一个jobid。
master默认情况下会缓存24小时内的所有job的详细操作
master缓存目录:/var/cache/salt/master/jobs/
minion端每次执行任务都会缓存在/var/cache/salt/minion/proc/目录下,任务执行完成后文件会被删除。
在master端执行一个长时间的任务
salt ‘salt-minion01‘ cmd.run "sleep 100"
在minion01上用strings查看文件内容
[[email protected] proc]# strings 20181125111411231106
tgt_type
glob
20181125111411231106
salt-minion01
user
root
sleep 100
cmd.run
Job管理
通过salt-run 命令来管理job也可以通过salt util模块
在master中执行一个长时间的命令
salt ‘salt-minion01‘ cmd.run "sleep 1000;echo hello"
然后ctrl+c结束,获取jobid后登陆查看
salt ‘salt-minion01‘ saltutil.find_job-id
salt ‘salt-minion01‘ saltutil.kill_job jobid 停掉任务
查看master上cache的所有job
salt ‘salt-minion‘ saltutil.runner jobs.list_jobs - name: apache2
原文地址:http://blog.51cto.com/tuwei/2326119