States配置管理
States是Saltstack中的配置語言,在日常進(jìn)行配置管理時需要編寫大量的States SLS文件拿诸,而編寫這些SLS文件的一般步驟也就是我們平時手動配置一臺服務(wù)器的步驟:首先安裝源碼包衙猪,然后管理一個配置文件笙什,最后再保證這個服務(wù)的開機(jī)啟動及正常運(yùn)行形导。其中使用到的states模塊功能需要我們一邊學(xué)習(xí)一邊實(shí)踐加強(qiáng)理解婉烟。
接下來鼻弧,我們通過一個簡單的例子來理解Saltstack配置管理的基本原理--安裝keepalived
1)修改master配置文件的file_roots根目錄地址
[root@saltstack-node1 ~]# vim /etc/salt/master
file_roots:
base:
- /srv/salt
[root@saltstack-node1 ~]# systemctl restart salt-master
2)創(chuàng)建states sls文件
[root@saltstack-node1 ~]# cd /srv/salt/
[root@saltstack-node1 salt]# mkdir keepalived
[root@saltstack-node1 salt]# cd keepalived/
[root@saltstack-node1 keepalived]# mkdir files #創(chuàng)建一個files文件來存放我們的源碼包和配置文件
[root@saltstack-node1 keepalived]# cd /srv/salt/keepalived/files/
[root@saltstack-node1 files]# rz #我們rz上傳一個keepalived源碼包
[root@saltstack-node1 files]# ll
total 236
-rw-r--r-- 1 root root 239438 Oct 8 2016 keepalived-1.2.1.tar.gz
[root@saltstack-node1 files]# cd ..
[root@saltstack-node1 keepalived]# vim install.sls
/application/tools: # ID聲明,在配置管理高級狀態(tài)中锦茁,這個ID必須唯一
file.directory: # State聲明攘轩,也可以叫狀態(tài)聲明(新建一個新文件夾)
- user: root # 選項(xiàng)聲明
- group: root
- mode: 755
- makedirs: True
keepalived-install: # ID聲明的第二種寫法也可以這么寫,表明以下管理功能
file.managed: # 管理一個文件
- name: /application/tools/keepalived-1.2.1.tar.gz
- source: salt://keepalived/files/keepalived-1.2.1.tar.gz
- user: root
- group: root
- mode: 755
cmd.run: # 調(diào)用系統(tǒng)命令來執(zhí)行解壓和安裝
- name: cd /application/tools/ && tar zxf keepalived-1.2.1.tar.gz && cd keepalived-1.2.1 && ./configure --prefix=/application/keepalived --disable-fwmark && make && make install
- unless: test -d /application/keepalived # unless作用是先決條件码俩,如果這么文件夾存在就不再重復(fù)執(zhí)行上面的make和make insall安裝命令度帮,節(jié)省時間
- require: # require是各ID之間的依賴,意思是只有keepalived-install下面的壓縮包存在才會繼續(xù)執(zhí)行
- file: keepalived-install
上面的install.sls就是我們需要編輯的states SLS文件格式了稿存,其中最主要的就是ID聲明和狀態(tài)聲明笨篷,ID不能唯一,狀態(tài)模塊使用可以查看幫助文檔瓣履,功能還是十分豐富和完善的
https://www.unixhot.com/docs/saltstack/ref/states/all/index.html
[root@saltstack-node1 keepalived]# salt '*' state.sls keepalived.install
...
Summary for saltstack-node2.lichengbing.com
Succeeded: 3 (changed=1)
Failed: 0
Total states run: 3
Total run time: 21.593 s
Summary for saltstack-node1.lichengbing.com
Succeeded: 3 (changed=1)
Failed: 0
Total states run: 3
Total run time: 22.882 s
到這里率翅,我們遠(yuǎn)程配置批量安裝keepalived就算完成了,接下來就是拷貝配置文件和啟動服務(wù)
4)配置文件
因?yàn)镵eepalived分為主袖迎、備節(jié)點(diǎn)冕臭,一些配置在主節(jié)點(diǎn)和備節(jié)點(diǎn)上是不同的。如果按照傳統(tǒng)的配置管理下發(fā)配置文件是行不通的燕锥,因?yàn)樗械南掳l(fā)文件都是一樣辜贵,讓我們一臺臺去修改還是比較痛苦的,所以我們需要借用Jinja模板來幫助我們完成配置文件的管理归形,文章下面有Jinja模板的介紹
[root@saltstack-node1 files]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
saltstack@example.com
}
notification_email_from keepalived@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id {{ROUTEID}}
}
vrrp_instance haproxy_ha {
state {{STATEID}}
interface eth0
virtual_router_id 36
priority {{PRIORITYID}}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.184
}
}
配置正確的服務(wù)啟動參數(shù)
[root@saltstack-node1 files]# vim keepalived.sysconfig
Options for keepalived. See `keepalived --help' output and keepalived(8) and
keepalived.conf(5) man pages for a list of all options. Here are the most
common ones :
--vrrp -P Only run with VRRP subsystem.
--check -C Only run with Health-checker subsystem.
--dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop.
--dont-release-ipvs -I Dont remove IPVS topology on daemon stop.
--dump-conf -d Dump the configuration data.
--log-detail -D Detailed log messages.
--log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON)
KEEPALIVED_OPTIONS="-D"
5)繼續(xù)編寫installer.sls文件托慨,在后面添加
[root@saltstack-node1 keepalived]# vim install.sls
/etc/sysconfig/keepalived:
file.managed:
- source: salt://keepalived/files/keepalived.sysconfig
- mode: 644
- user: root
- group: root
/etc/init.d/keepalived:
file.managed:
- source: salt://keepalived/files/keepalived.init
- mode: 755
- user: root
- group: root
keepalived-init:
cmd.run:
- name: chkconfig --add keepalived
- unless: chkconfig --list | grep keepalived
- require:
- file: /etc/init.d/keepalived
/etc/keepalived:
file.directory:
- user: root
- group: root
keepalived-server:
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/files/keepalived.conf
- mode: 644
- user: root
- group: root
- template: jinja
{% if grains['fqdn'] == 'saltstack-node1.lichengbing.com' %}
- ROUTEID: haproxy_ha
- STATEID: MASTER
- PRIORITYID: 150
{% elif grains['fqdn'] == 'saltstack-node2.lichengbing.com' %}
- ROUTEID: haproxy_ha
- STATEID: BACKUP
- PRIORITYID: 100
{% endif %}
service.running:
- name: keepalived
- enable: True
- watch:
- file: keepalived-server
6)啟動文件
[root@saltstack-node1 files]# cat keepalived.init
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
# Source function library
. /etc/rc.d/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
RETVAL=0
prog="keepalived"
start() {
echo -n $"Starting $prog: "
daemon /application/keepalived/sbin/keepalived ${KEEPALIVED_OPTIONS} #修改正確的啟動地址
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
stop() {
echo -n $"Stopping $prog: "
killproc keepalived
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}
reload() {
echo -n $"Reloading $prog: "
killproc keepalived -1
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status keepalived
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
7)執(zhí)行配置管理啟動所有服務(wù)
[root@saltstack-node1 keepalived]# salt '*' state.sls keepalived.install
Jinja
Saltstack除了使用了YAML語言以外,我們還需要學(xué)習(xí)一點(diǎn)jinja語法知識暇榴,因?yàn)樵谂渲霉芾碇薪?jīng)常會用到厚棵,這也是saltstack能真正實(shí)現(xiàn)高度自動化配置的一個重要技能
Jinja是現(xiàn)代的,設(shè)計(jì)者友好的跺撼,仿照Django模板的Python模板語言窟感,是基于pythonde 模板引擎,功能類似于PHP的smarty歉井,J2EE的Freemarker柿祈,由于速度快,被廣泛開發(fā)者接受并使用哩至。
#詳細(xì)參考資料:
http://docs.jinkan.org/docs/jinja2/
Jinja在saltstack中的作用
yaml_jinja工作流程是先用jinja2模板引擎處理SLS,然后再調(diào)用YAML解析器躏嚎。所以在開始解析YAML之前,我們可以使用jinja干一些我們想干的事情菩貌,比如:定義一個類似變量或者表達(dá)式卢佣;模板引用
1)變量
[root@saltstack-node1 keepalived]# vim install.sls #借配置管理的一個sls文件演示,有時候我們使用的源碼包可能要換不一樣的版本箭阶,一處處修改或者替換會出問題虚茶,這個定義變量的形式就比較方便了
{% set keepalived_tar = 'keeplived-1.2.17.tar.gz' %} # 用{%...%}符號定義
{% set keepalived_source = 'salt://modules/keepalived/files/keepalived-1.2.17.tar.gz' %}
keepalived-install:
file.managed:
- name: /usr/local/src/{{ keepalived_tar }} # 這里用{{...}}引用
- source: {{ keepalived_source }}
- mode: 755
- user: root
- group: root
cmd.run:
- name: cd /usr/local/src && tar zxf keepalived-1.2.17.tar.gz && cd keepalived-1.2.17 && ./configure --prefix=/usr/local/keepalived --disable-fwmark && make && make install
- unless: test -d /usr/local/keepalived
- require:
- file: keepalived-install
2)模板引用
這里我們以keepalived自定義配置文件為例(keepalived的master和backup優(yōu)先級還有routeid要單獨(dú)指定)戈鲁,演示jinja在saltstack中的作用
我們以修改在進(jìn)行配置模板引用的時候分三個步驟
a. 告訴模塊,你使用的使用的是jinja模板
b. 你出你要的參數(shù)列表
[root@linux-node2 cluster]# vim haproxy-outside-keepalived.sls
keepalived-server:
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://cluster/files/haproxy-outside-keepalived.conf
- mode: 644
- user: root
- group: root
- template: jinja # 告訴模板文件嘹叫,這是一個jinja模板
{% if grains['fqdn'] == 'saltstack-node1.lichengbing.cn' %} # 這里是借助grains自定義
如果hostname為saltstack-node1.lichengbing.cn的主機(jī)定義以下參數(shù)
- ROUTEID: haproxy_ha # 列出我們要自定義的參數(shù)
- STATEID: MASTER
- PRIORITYID: 150
{% elif grains['fqdn'] == 'saltstack-node2.lichengbing.cn' %}
- ROUTEID: haproxy_ha
- STATEID: BACKUP
- PRIORITYID: 100
{% endif %}
c. 最后再進(jìn)行模板引用
[root@linux-node2 files]# vim haproxy-outside-keepalived.conf
global_defs {
notification_email {
saltstack@example.com
}
notification_email_from keepalived@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id {{ROUTEID}} # 引用定義好的參數(shù)ROUTEID
}
vrrp_instance haproxy_ha {
state {{STATEID}} # 引用定義好的參數(shù)STATEID
interface eth0
virtual_router_id 36
priority {{PRIORITYID}} # 引用定義好的參數(shù)PRIORITYI
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.179
}
}