博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于saltstack实现的配置集中化管理
阅读量:2434 次
发布时间:2019-05-10

本文共 4413 字,大约阅读时间需要 14 分钟。

  是一个具备puppet与func功能为一身的集中化管理平台,saltstack基于python实现,功能十分强大,各模块融合度及复用性极高,官方极力推荐作为云计算平台的基础架构。轻松维护成千上万台服务器不是问题,现分享作者基于saltstack实现一个集中化的配置管理平台,以Nginx配置例子展开,涉及salt的grains、grains_module、pillar、States、jinja(template)等,本文适合有salt基础的同学阅读。
一、设备环境说明
    有两组web业务服务器,组名分别为web1group与web2group,设备硬件配置、web根目录存在异常,见下图:
    
二、master配置说明
    1、关键配置定义:
  1. nodegroups:  
  2.    web1group: 'L@SN2012-07-010,SN2012-07-011,SN2012-07-012'  
  3.    web2group: 'L@SN2013-08-021,SN2013-08-022'  
  4.   
  5. file_roots:  
  6.   base:  
  7.     - /srv/salt  
  8.   
  9. pillar_roots:  
  10.   base:  
  11.     - /srv/pillar  
    2、定义的文件树结构(具体文件后续说明)
三、自定义grains_module
1)#vi /srv/salt/_grains/nginx_config.py
  1. import os,sys,commands  
  2.   
  3. def NginxGrains():  
  4.     ''' 
  5.         return Nginx config grains value 
  6.     '''  
  7.     grains = {}  
  8.     max_open_file=65536  
  9.     #Worker_info={'cpus2':'01 10','cpus4':'1000 0100 0010 0001','cpus8':'10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001'}  
  10.     try:  
  11.         getulimit=commands.getstatusoutput('source /etc/profile;ulimit -n')  
  12.     except Exception,e:  
  13.         pass  
  14.     if getulimit[0]==0:  
  15.         max_open_file=int(getulimit[1])  
  16.     grains['max_open_file'] = max_open_file  
  17.     return grains  
2)同步grains模块
salt '*' saltutil.sync_all
3)刷新模块(让minion编译模块)
salt '*' sys.reload_modules
4)验证max_open_file key的value
[root@SN2013-08-020 _grains]# salt '*' grains.item max_open_file              
SN2013-08-022:
  max_open_file: 1024
SN2013-08-021:
  max_open_file: 1024
SN2012-07-011:
  max_open_file: 1024
SN2012-07-012:
  max_open_file: 1024
SN2012-07-010:
  max_open_file: 1024
四、配置pillar
    本例使用分组规则定义pillar,即不同分组引用各自的sls属性
1)定义入口top.sls
#vi /srv/pillar/top.sls
  1. base:  
  2.   web1group:  
  3.     - match: nodegroup  
  4.     - web1server  
  5.   web2group:  
  6.     - match: nodegroup  
  7.     - web2server  
2)定义私有配置,本例只配置web_root的数据,当然可以根据不同需求进行定制,格式为python的字典形式,即"key:value"。
#vi /srv/pillar/web1server.sls
  1. nginx:  
  2.     root: /www  
#vi /srv/pillar/web2server.sls
  1. nginx:  
  2.     root: /data  
3)验证配置结果:
#salt 'SN2013-08-021' pillar.data nginx
SN2013-08-021:
    ----------
    root:
        /data
#salt 'SN2012-07-010' pillar.data nginx
SN2012-07-010:
    ----------
    root:
        /www
五、配置States
1)定义入口top.sls
#vi /srv/salt/top.sls
  1. base:  
  2.   '*':  
  3.     - nginx   
2)定义nginx配置及重启服务SLS,其中salt://nginx/nginx.conf为配置模板文件位置。
#vi /srv/salt/nginx.sls
  1. nginx:  
  2.   pkg:  
  3.    - installed  
  4.   file.managed:  
  5.    - source: salt://nginx/nginx.conf  
  6.    - name: /etc/nginx/nginx.conf  
  7.    - user: root  
  8.    - group: root  
  9.    - mode: 644  
  10.    - template: jinja  
  11.   
  12.   service.running:  
  13.    - enable: True  
  14.    - reloadTrue  
  15.    - watch:  
  16.      - file: /etc/nginx/nginx.conf  
  17.      - pkg: nginx  
3)Nginx配置文件(引用jinja模板)
功能点:
1、worker_processes参数采用grains['num_cpus'] 上报值(与设备CPU核数一致);
2、worker_cpu_affinity分配多核CPU根据当前设备核数进行匹配,分别为2\4\8\其它核;
3、worker_rlimit_nofile参数与grains['max_open_file'] 获取的系统ulimit -n一致;
4、worker_connections 参数理论上为grains['max_open_file'];
5、 root参数为定制的pillar['nginx']['root']值。
#vi /srv/salt/nginx/nginx.conf
  1. # For more information on configuration, see:  
  2. user              nginx;  
  3. worker_processes  {
    { grains['num_cpus'] }};  
  4. {% if grains['num_cpus'] == 2 %}  
  5. worker_cpu_affinity 01 10;  
  6. {% elif grains['num_cpus'] == 4 %}  
  7. worker_cpu_affinity 1000 0100 0010 0001;  
  8. {% elif grains['num_cpus'] >= 8 %}  
  9. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;  
  10. {% else %}  
  11. worker_cpu_affinity 1000 0100 0010 0001;  
  12. {% endif %}  
  13. worker_rlimit_nofile {
    { grains['max_open_file'] }};  
  14.   
  15. error_log  /var/log/nginx/error.log;  
  16. #error_log  /var/log/nginx/error.log  notice;  
  17. #error_log  /var/log/nginx/error.log  info;  
  18.   
  19. pid        /var/run/nginx.pid;  
  20.   
  21. events {  
  22.     worker_connections  {
    { grains['max_open_file'] }};  
  23. }  
  24.   
  25.   
  26. http {  
  27.     include       /etc/nginx/mime.types;  
  28.     default_type  application/octet-stream;  
  29.   
  30.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  31.                       '$status $body_bytes_sent "$http_referer" '  
  32.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  33.   
  34.     access_log  /var/log/nginx/access.log  main;  
  35.   
  36.     sendfile        on;  
  37.     #tcp_nopush     on;  
  38.   
  39.     #keepalive_timeout  0;  
  40.     keepalive_timeout  65;  
  41.   
  42.     #gzip  on;  
  43.       
  44.     # Load config files from the /etc/nginx/conf.d directory  
  45.     # The default server is in conf.d/default.conf  
  46.     #include /etc/nginx/conf.d/*.conf;  
  47.     server {  
  48.         listen       80 default_server;  
  49.         server_name  _;  
  50.   
  51.         #charset koi8-r;  
  52.   
  53.         #access_log  logs/host.access.log  main;  
  54.   
  55.         location / {  
  56.             root   {
    { pillar['nginx']['root'] }};  
  57.             index  index.html index.htm;  
  58.         }  
  59.   
  60.         error_page  404              /404.html;  
  61.         location = /404.html {  
  62.             root   /usr/share/nginx/html;  
  63.         }  
  64.   
  65.         # redirect server error pages to the static page /50x.html  
  66.         #  
  67.         error_page   500 502 503 504  /50x.html;  
  68.         location = /50x.html {  
  69.             root   /usr/share/nginx/html;  
  70.         }  
  71.   
  72.     }  
  73.   
  74. }  
4)同步配置
#salt '*' state.highstate
(由于非第一次运行,看不到配置文件比对的信息)
5)验证结果:
1、登录root@SN2013-08-021
#vi /etc/nginx/nginx.conf
2、登录root@SN2012-07-010
#vi /etc/nginx/nginx.conf

转载地址:http://hrlmb.baihongyu.com/

你可能感兴趣的文章
脱机备份与恢复实战(转)
查看>>
WLINUX下的DNS服务器设置(转)
查看>>
游戏引擎剖析(二)(转)
查看>>
sms发mms C语言源码(转)
查看>>
窝CDMA网络中移动IP接入Internet(转)
查看>>
为什么选择百度?-- 巧用百度专题(转)
查看>>
WinXP PRO平台下VS.NET+Series60开发环境配置指南(转)
查看>>
保护你的网络,完全解读网络防火墙(转)
查看>>
实现MMS增值业务的关键技术(转)
查看>>
Vista被破解 一个小程序可成功激活(转)
查看>>
[组图]网络游戏设计(转)
查看>>
SEO作弊常见方法和形式(转)
查看>>
蓝芽技术的原理和应用(2)(转)
查看>>
ACCESS默认保存路径的修改方法(转)
查看>>
解决接通电源后自动开机问题(转)
查看>>
Linux操作系统的使用技巧集锦(转)
查看>>
安全防护:入侵检测实战之全面问答(转)
查看>>
助手的反叛——全面分析浏览器劫持的情况(转)
查看>>
搭建WAP应用JAVA开发环境(转)
查看>>
自启动程序之十大藏身之所(转)
查看>>