设为首页 收藏本站
查看: 1696|回复: 0

[经验分享] 基于saltstack实现的配置集中化管理

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-26 11:15:14 | 显示全部楼层 |阅读模式
Saltstack是一个具备puppet与func功能为一身的集中化管理平台,saltstack基于python实现,功能十分强大,各模块融合度及复用性极高,官方极力推荐作为云计算平台的基础架构。轻松维护成千上万台服务器不是问题,现分享作者基于saltstack实现一个集中化的配置管理平台,以Nginx配置例子展开,涉及salt的grains、grains_module、pillar、States、jinja(template)等,本文适合有salt基础的同学阅读。
一、设备环境说明
    有两组web业务服务器,组名分别为web1group与web2group,设备硬件配置、web根目录存在异常,见下图:
    DSC0000.jpg

二、master配置说明
    1、关键配置定义:

  • nodegroups:  
  •    web1group: 'L@SN2012-07-010,SN2012-07-011,SN2012-07-012'  
  •    web2group: 'L@SN2013-08-021,SN2013-08-022'  
  •   
  • file_roots:  
  •   base:  
  •     - /srv/salt  
  •   
  • pillar_roots:  
  •   base:  
  •     - /srv/pillar  

    2、定义的文件树结构(具体文件后续说明)
DSC0001.jpg

三、自定义grains_module
1)#vi /srv/salt/_grains/nginx_config.py
view plainprint?

  • import os,sys,commands  
  •   
  • def NginxGrains():  
  •     '''
  •         return Nginx config grains value
  •     '''  
  •     grains = {}  
  •     max_open_file=65536  
  •     #Worker_info={'cpus2':'01 10','cpus4':'1000 0100 0010 0001','cpus8':'10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001'}  
  •     try:  
  •         getulimit=commands.getstatusoutput('source /etc/profile;ulimit -n')  
  •     except Exception,e:  
  •         pass  
  •     if getulimit[0]==0:  
  •         max_open_file=int(getulimit[1])  
  •     grains['max_open_file'] = max_open_file  
  •     return grains  

2)同步grains模块
salt '*' saltutil.sync_all

3)刷新模块(让minion编译模块)
salt '*' sys.reload_modules

4)验证max_open_file key的value
[iyunv@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

  • base:  
  •   web1group:  
  •     - match: nodegroup  
  •     - web1server  
  •   web2group:  
  •     - match: nodegroup  
  •     - web2server  

2)定义私有配置,本例只配置web_root的数据,当然可以根据不同需求进行定制,格式为python的字典形式,即"key:value"。
#vi /srv/pillar/web1server.sls

  • nginx:  
  •     root: /www  

#vi /srv/pillar/web2server.sls

  • nginx:  
  •     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

  • base:  
  •   '*':  
  •     - nginx   

2)定义nginx配置及重启服务SLS,其中salt://nginx/nginx.conf为配置模板文件位置。
#vi /srv/salt/nginx.sls
view plainprint?

  • nginx:  
  •   pkg:  
  •    - installed  
  •   file.managed:  
  •    - source: salt://nginx/nginx.conf  
  •    - name: /etc/nginx/nginx.conf  
  •    - user: root  
  •    - group: root  
  •    - mode: 644  
  •    - template: jinja  
  •   
  •   service.running:  
  •    - enable: True  
  •    - reload: True  
  •    - watch:  
  •      - file: /etc/nginx/nginx.conf  
  •      - 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
view plainprint?

  • # For more information on configuration, see:  
  • user              nginx;  
  • worker_processes  {{ grains['num_cpus'] }};  
  • {% if grains['num_cpus'] == 2 %}  
  • worker_cpu_affinity 01 10;  
  • {% elif grains['num_cpus'] == 4 %}  
  • worker_cpu_affinity 1000 0100 0010 0001;  
  • {% elif grains['num_cpus'] >= 8 %}  
  • worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;  
  • {% else %}  
  • worker_cpu_affinity 1000 0100 0010 0001;  
  • {% endif %}  
  • worker_rlimit_nofile {{ grains['max_open_file'] }};  
  •   
  • error_log  /var/log/nginx/error.log;  
  • #error_log  /var/log/nginx/error.log  notice;  
  • #error_log  /var/log/nginx/error.log  info;  
  •   
  • pid        /var/run/nginx.pid;  
  •   
  • events {  
  •     worker_connections  {{ grains['max_open_file'] }};  
  • }  
  •   
  •   
  • http {  
  •     include       /etc/nginx/mime.types;  
  •     default_type  application/octet-stream;  
  •   
  •     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  •                       '$status $body_bytes_sent "$http_referer" '  
  •                       '"$http_user_agent" "$http_x_forwarded_for"';  
  •   
  •     access_log  /var/log/nginx/access.log  main;  
  •   
  •     sendfile        on;  
  •     #tcp_nopush     on;  
  •   
  •     #keepalive_timeout  0;  
  •     keepalive_timeout  65;  
  •   
  •     #gzip  on;  
  •       
  •     # Load config files from the /etc/nginx/conf.d directory  
  •     # The default server is in conf.d/default.conf  
  •     #include /etc/nginx/conf.d/*.conf;  
  •     server {  
  •         listen       80 default_server;  
  •         server_name  _;  
  •   
  •         #charset koi8-r;  
  •   
  •         #access_log  logs/host.access.log  main;  
  •   
  •         location / {  
  •             root   {{ pillar['nginx']['root'] }};  
  •             index  index.html index.htm;  
  •         }  
  •   
  •         error_page  404              /404.html;  
  •         location = /404.html {  
  •             root   /usr/share/nginx/html;  
  •         }  
  •   
  •         # redirect server error pages to the static page /50x.html  
  •         #  
  •         error_page   500 502 503 504  /50x.html;  
  •         location = /50x.html {  
  •             root   /usr/share/nginx/html;  
  •         }  
  •   
  •     }  
  •   
  • }  

4)同步配置
#salt '*' state.highstate
DSC0002.jpg
(由于非第一次运行,看不到配置文件比对的信息)

5)验证结果:
1、登录root@SN2013-08-021
#vi /etc/nginx/nginx.conf
DSC0003.jpg


DSC0004.jpg

2、登录root@SN2012-07-010
#vi /etc/nginx/nginx.conf
DSC0005.jpg


DSC0006.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143831-1-1.html 上篇帖子: 实战使用saltstack源码安装配置mysql 下篇帖子: SaltStack Pillar攻略
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表