[关闭]
@cdmonkey 2016-07-08T09:17:01.000000Z 字数 5869 阅读 1278

Saltstack(4)实战

Saltstack


一、Install Zabbix

1. Install Agent

Create SLS

首先要创建状态文件:

http://docs.saltstack.cn/ref/states/all/salt.states.file.html#salt.states.file.managed

  1. [root@test-ngx ~]# mkdir /etc/salt/states/zabbix
  2. [root@test-ngx ~]# cd /etc/salt/states/zabbix
  3. [root@test-ngx zabbix]# vim zabbix_agent.sls
  1. [root@salt-master ~]# cd /etc/salt/states/init/
  2. # Create sls:
  3. [root@salt-master init]# vim zabbix_agent.sls
  4. /etc/resolv.conf:
  5. file.managed:
  6. - source: salt://zabbix/files/resolv.conf
  7. - user: root
  8. - group: root
  9. - mode: 755
  10. zabbix_agent: # ID declaration
  11. pkg.installed:
  12. - name: zabbix22-agent # RPM Package name
  13. file.managed:
  14. - name: /etc/zabbix_agentd.conf
  15. - source: salt://zabbix/files/zabbix_agentd.conf
  16. - user: root
  17. - mode: 644
  18. - template: jinja # Define the template.
  19. - defaults: # Define the variables used in the template.
  20. Zabbix_Server: {{ pillar['zabbix-agent']['Zabbix_Server'] }}
  21. - require:
  22. - pkg: zabbix-agent
  23. service.running:
  24. - name: zabbix-agentd
  25. - enable: True # 开机自动启动。
  26. - reload: True # 如果服务进程不支持重载,那么去掉这行即可,默认就会重新启动服务。
  27. - watch: # 被监控的内容,一旦产生变动,服务就会重启或重载。
  28. - file: zabbix_agent

因为需使用“Pillar”来获取数据,那么需要将你要管理的源文件(source)变成一个模板,其操作非常的简单:

  1. [root@salt-master ~]# vim /etc/salt/states/init/files/zabbix_agentd.conf
  2. Server=127.0.0.1 --> Server={{Zabbix_Server}}
  3. # 即将需要获取的数据用双大括号括起来,中间是定义的变量名称。
  4. # 如此进行修改后,该配置文件就成为了一个模板。

Create Pillar file

  1. [root@salt-master ~]# vim /etc/salt/pillar/init/zabbix_agent.sls
  2. zabbix-agent:
  3. Zabbix_Server: 172.16.1.21
  4. #Pillar的状态文件主要是设置自定义的变量。
  5. ------------------
  6. #不要忘记修改Pillar的入口文件(指定的目标使用指定的状态文件):
  7. [root@salt-master ~]# vim /etc/salt/pillar/top.sls
  8. base:
  9. '*':
  10. - init.rsyslog
  11. - init.zabbix_agent

最后需要修改入口文件top.sls,指定目标节点及其状态文件:

  1. base:
  2. 'node(2|3).cdmonkey.com':
  3. - match: pcre
  4. - init.pkg
  5. - init.limit
  6. - init.zabbix_agent

特别需要注意:“Pliiar”与状态管理的两个入口文件top.sls在写法上完全相同,但作用范围不同。

整个过程如图所示:

此处输入图片的描述

实际的生产场景中,对于从来没有执行过的状态,一定要先测试一下:

  1. # 仅仅是进行测试,而并不真正的去同步状态:
  2. [root@salt-master ~]# salt '*' state.highstate test=True
  3. # 仅显示一个节点返回的信息以作实例:
  4. node3.cdmonkey.com:
  5. ----------
  6. ID: init.pkg
  7. Function: pkg.installed
  8. ...
  9. ----------
  10. ID: limit-conf
  11. Function: file.managed
  12. Name: /etc/security/limits.conf
  13. Result: True
  14. Comment: The file /etc/security/limits.conf is in the correct state
  15. Started: 18:47:27.315907 # 显示该文件处于正确的状态,也就是说处于与控制端一致的状态。
  16. Duration: 5.709 ms
  17. Changes:
  18. ----------
  19. ID: zabbix_agent
  20. Function: pkg.installed
  21. Name: zabbix22-agent
  22. Result: None
  23. Comment: The following packages are set to be installed/updated: zabbix22-agent
  24. Started: 18:47:27.321777 # 显示说明该软件将会被安装。
  25. Duration: 4428.429 ms
  26. Changes:
  27. ----------
  28. ID: zabbix_agent
  29. Function: file.managed
  30. Name: /etc/zabbix_agentd.conf
  31. Result: None
  32. Comment: The file /etc/zabbix_agentd.conf is set to be changed
  33. Started: 18:47:31.754157 # 显示说明该文件将会被修改。
  34. Duration: 4.279 ms
  35. Changes:
  36. ----------
  37. newfile:
  38. /etc/zabbix_agentd.conf
  39. ----------
  40. ID: zabbix_agent
  41. Function: service.running
  42. Name: zabbix_agentd
  43. Result: False
  44. Comment: The named service zabbix_agentd is not available
  45. Started: 18:47:31.761976 # 由于根本没有安装相应的服务,所以这里出现服务不存在很正常。
  46. Duration: 16.192 ms
  47. Changes:
  48. Summary
  49. ------------
  50. Succeeded: 5 (unchanged=2, changed=1)
  51. Failed: 1
  52. ------------
  53. Total states run: 6

测试完成后就可以进行状态同步了:

  1. [root@salt-master ~]# salt '*' state.highstate
  2. # 以下是服务正常被启动后的输出内容:
  3. ID: zabbix_agent
  4. Function: service.running
  5. Name: zabbix-agentd
  6. Result: True
  7. Comment: Service zabbix-agentd has been enabled, and is running
  8. Started: 19:54:24.276550
  9. Duration: 345.981 ms
  10. Changes:
  11. ----------
  12. zabbix-agentd:
  13. True

下面是“Zabbix”客户端的主配文件被修改后的输出内容:

  1. ...
  2. ID: zabbix_agent
  3. Function: file.managed
  4. Name: /etc/zabbix_agentd.conf
  5. Result: True
  6. Comment: File /etc/zabbix_agentd.conf updated
  7. Started: 16:17:30.059533
  8. Duration: 19.03 ms
  9. Changes:
  10. ----------
  11. diff:
  12. ---
  13. +++
  14. @@ -78,7 +78,7 @@
  15. # Default:
  16. # Server=
  17. -Server=172.16.1.21
  18. +Server=172.16.1.1 #被控端配置文件的内容进行了相应的修改。
  19. ### Option: ListenPort
  20. ----------
  21. ID: zabbix_agent
  22. Function: service.running
  23. Name: zabbix-agentd
  24. Result: True
  25. Comment: Service restarted #由于配置文件被修改,所以服务被重启。
  26. Started: 16:17:30.170500
  27. Duration: 300.727 ms
  28. Changes:
  29. ----------
  30. zabbix-agentd:
  31. True
  32. ...

Other writing methods

上面的这种“Pillar”的写法属于有层级关系的,当然也可以直接写成键值对的形式,而没有上层的名称。

  1. - template: jinja
  2. - defaults:
  3. Zabbix_Server: {{ pillar['Zabbix_Server'] }}
  4. -----------------
  5. #相对应的,Pillar状态文件中只包含键值对:
  6. [root@salt-master ~]# cat /etc/salt/pillar/init/zabbix_agent.sls
  7. Zabbix_Server: 172.16.1.21

还有更为简单的写法,那就是不使用“Pillar”,而是将模板中用到的变量直接在pillar file中进行定义并赋值:

  1. - template: jinja
  2. - defaults:
  3. Zabbix_Server: 172.16.1.21

二、Install PHP

Create SLS

创建状态文件:

  1. [root@salt-master ~]# vim /etc/salt/states/php/php_fastcgi.sls
  2. include: #将现有的状态文件加载进来。
  3. - init.pkg
  4. php-install:
  5. file.managed:
  6. - name: /usr/local/src/php-5.5.13.tar.gz
  7. - source: salt://php/files/php-5.5.13.tar.gz
  8. - user: root
  9. - group: root
  10. - mode: 755
  11. cmd.run:
  12. - name: cd /usr/local/src && tar zxf php-5.5.13.tar.gz && cd php-5.5.13 && ./configure --prefix=/usr/local/php-fastcgi --with-mysql --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-libxml-dir --with-curl --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --with-openssl --enable-mbstring --with-gd --enable-gd-native-ttf --enable-sockets --with-xmlrpc --enable-zip --enable-soap --disable-debug --enable-opcache --enable-zip --with-config-file-path=/usr/local/php-fastcgi/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www && make && make install
  13. - unless: test -d /usr/local/php-fastcgi #条件判断:如果该目录存在(证明安装过)则不再进行安装。
  14. require: #在这里指明如果要进行该步操作,依赖的是什么。
  15. - file: php-install #依赖于指定的文件必须存在。
  16. - pkg.installed: php-install
  17. pdo-plugin:
  18. cmd.run:
  19. - name: cd /usr/local/src/php-5.5.13/ext/pdo_mysql/ && /usr/local/php-fastcgi/bin/
  20. - unless: test -f /usr/local/php-fastcgi/lib/php/extensions/no-debug-non-zts-
  21. require:
  22. - cmd: php-install

创建目录并上传源码包:

  1. [root@salt-master ~]# mkdir -p /etc/salt/states/php/files
  2. #上传源码包到该目录。
  3. [root@salt-master ~]# ll /etc/salt/states/php/files/
  4. total 16752
  5. -rw-r--r-- 1 root root 17151674 Apr 25 18:16 php-5.5.13.tar.gz
  1. [root@salt-master ~]# vim /etc/salt/states/init/pkg.sls
  2. php-pkg: #不能够直接写模块和方法,要不就是像这样声明一个名称,那不就是写上“name”。
  3. pkg.installed:
  4. - names:
  5. - gcc
  6. - gcc-c++
  7. - glibc
  8. - make
  9. - autoconf
  10. - libjpeg-turbo
  11. - libjpeg-turbo-devel
  12. - libpng
  13. - libpng-devel
  14. - freetype
  15. - freetype-devel
  16. - libxml2
  17. - libxml2-devel
  18. - zlib
  19. - zlib-devel
  20. - libcurl
  21. - libcurl-devel
  22. - openssl
  23. - openssl-devel

以上内容的第二种写法:

  1. gcc: #也可以这样写,首先是软件包名,下面是模块和方法。如果是有很多包,上面的写法更合适。
  2. pkg.installed
  3. ...

修改入口文件:

  1. [root@salt-master ~]# vim /etc/salt/states/top.sls
  2. base:
  3. 'node(2|3).cdmonkey.com':
  4. - match: pcre
  5. - init.pkg
  6. - init.limit
  7. - init.zabbix_agent
  8. - php.php_fastcgi
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注