jjzjj

saltstack 之源码部署管理nginx

小罗ge11 2023-03-28 原文
      saltstack接触也有一段时间了,感觉saltstack强大之处在于state文件部署,通过他可以给我们大批量部署节省很多时间,今天就用部署我前端的转发服务器为例进行源码部署nginx;水平有限希望大家多多指导。

      思路:

             1、用grains收集cpu、打开文件数等信息结合jinja配置nginx.conf文件

             2、使用pillar保存我们要使用的变量结合jinja配置vhost.conf文件

             3、state安装推送文件

部署步骤:

     1、编写grains,根据系统打开文件数配置合理的nginx打开文件数量:

[root@mail nginx]# cd /srv/salt/_grains/ [root@mail _grains]# cat nginx_config.py  import os,sys,commands         def NginxGrains():       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  if __name__ == '__main__':     print NginxGrains()    推送文件到客户端并启动文件重启客户端生效:   salt '*' saltutil.sync_all   salt '*' sys.reload_modules     2、编写变量之pillar,这里我定义了域名和后端转发主机:

[root@mail pillar]# cat top.sls  base:   '*':        - vhost [root@mail pillar]# cat vhost.sls  hostname: www.huasuan.com pass: 192.168.10.100     3、编写state所有文件,先查看目录选项:

[root@mail salt]# tree nginx nginx ├── conf.sls ├── files │?? ├── nginx │?? ├── nginx-1.6.0.tar.gz │?? ├── nginx.conf │?? └── huasuan.conf ├── init.sls ├── install.sls ├── server.sls └── vhost.sls 注释:init.sls指定启用哪个入口选项,install.sls指定安装步骤,server.sls表示管理服务脚本, conf.sls指定管理配置文件nginx.conf,vhost.sls 指定管理vhost.sls目录下的虚拟主机。     4、查看top文件和init文件:

[root@mail nginx]# cat install.sls [root@mail salt]# cat top.sls  base:   '*':     - nginx [root@mail salt]# cat nginx/init.sls  include:   - nginx.install   - nginx.conf   - nginx.server   - nginx.vhost     5、安装install,sls文件:

#nginx.tar.gz nginx_source:   file.managed:     - name: /tmp/nginx-1.6.0.tar.gz     - unless: test -e /tmp/nginx-1.6.0.tar.gz     - source: salt://nginx/files/nginx-1.6.0.tar.gz #extract extract_nginx:   cmd.run:     - cwd: /tmp     - names:       - tar zxvf nginx-1.6.0.tar.gz     - unless: test -d /tmp/nginx-1.6.0     - require:       - file: nginx_source #user nginx_user:   user.present:     - name: nginx     - uid: 1501     - createhome: False     - gid_from_name: True     - shell: /sbin/nologin #nginx_pkgs nginx_pkg:   pkg.installed:     - pkgs:       - gcc       - openssl-devel       - pcre-devel       - zlib-devel #nginx_compile nginx_compile:   cmd.run:     - cwd: /tmp/nginx-1.6.0     - names:       - ./configure --prefix=/usr/local/nginx  --user=nginx  --group=nginx  --with-http_ssl_module  --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/   --http-fastcgi-temp-path=/usr/local/nginx/fcgi/   --with-poll_module  --with-file-aio  --with-http_realip_module  --with-http_addition_module --with-http_random_index_module   --with-pcre   --with-http_stub_status_module       - make       - make install     - require:       - cmd: extract_nginx       - pkg:  nginx_pkg     - unless: test -d /usr/local/nginx #cache_dir cache_dir:   cmd.run:     - names:       - mkdir -p /usr/local/nginx/{client,proxy,fcgi} && chown -R nginx.nginx /usr/local/nginx/       - mkdir -p /usr/local/nginx/conf/vhost && chown -R nginx.nginx /usr/local/nginx/conf/vhost      - unless: test -d /usr/local/nginx/client/     - require:       - cmd: nginx_compile        注释:nginx使用源码编译安装的方式,包括了文件包推送,解压、安装管理,主要核心是cmd的使用    6、管理配置文件conf.sls:

[root@mail nginx]# cat conf.sls  include:   - nginx.install       nginx_service:     file.managed:     - name: /usr/local/nginx/conf/nginx.conf     - user: nginx     - mode: 644     - source: salt://nginx/files/nginx.conf     - template: jinja   service.running:        - name: nginx     - enable: True     - reload: True      - watch:       - file: /usr/local/nginx/conf/nginx.conf     7、服务脚本启动文件管理server.sls:

[root@mail nginx]# cat server.sls  include:   - nginx.install server:      file.managed:     - name: /etc/init.d/nginx     - user: root     - mode: 755     - source: salt://nginx/files/nginx   service.running:         - name: nginx     - enable: True     - reload: True     - watch:       - file: /etc/init.d/nginx command:   cmd.run:     - names:       - /sbin/chkconfig --add nginx       - /sbin/chkconfig  nginx on      - unless: /sbin/chkconfig --list nginx     8、虚拟主机管理配置文件:vhost.sls

[root@mail nginx]# cat vhost.sls  include:   - nginx.install       vhostconfig:     file.managed:     - name: /usr/local/nginx/conf/vhost/huasuan.conf     - user: root     - mode: 644     - source: salt://nginx/files/huasuan.conf     - template: jinja   service.running:        - name: nginx     - enable: True     - reload: True     - watch:       - file: /usr/local/nginx/conf/vhost/huasuan.conf上面几个分别是把已经保存在files目录下的配置文件推送到客户端,都是使用jinja模板为了使用系统的grains和pillar变量:

     9、分别查看以下几个配置文件nginx.conf:

# 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       mime.types;               default_type  application/octet-stream;               charset  utf-8;               server_names_hash_bucket_size 128;               client_header_buffer_size 32k;               large_client_header_buffers 4 32k;               client_max_body_size 128m;               sendfile on;               tcp_nopush     on;               keepalive_timeout 60;               tcp_nodelay on;               server_tokens off;               client_body_buffer_size  512k;               gzip on;               gzip_min_length  1k;               gzip_buffers     4 16k;               gzip_http_version 1.1;               gzip_comp_level 2;               gzip_types      text/plain application/x-javascript text/css application/xml;               gzip_vary on;               log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                                 '$status $body_bytes_sent "$http_referer" '                                 '"$http_user_agent" "$http_x_forwarded_for" "$host"' ;               include vhost/*.conf;        }         注释:grains['max_open_file']这个变量由我们第一个创建的自定义grains收集到服务端,基于jinja 来返回客户端     10、虚拟主机配置文件vhost:

[root@mail files]# cat huasuan.conf  server {         listen 80;         server_name {{ pillar['hostname'] }};         location / {                 proxy_pass http://{{ pillar['pass'] }};                 proxy_set_header Host $host;                 proxy_set_header X-Real-IP $remote_addr;                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;         }         location ~ /\.git {            deny all;         }  } 注释:pillar['hostname']和pillar['pass']由上面我们定义的pillar基于jinja获得,这里用反向代 理服务器为例     10、服务启动脚本,没什么特别;就是放上去服务器端同步到客户端启动目录下:

[root@mail files]# cat nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig:   - 85 15  # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \ #               proxy and IMAP/POP3 proxy server # processname: nginx # config:      /usr/local/nginx/conf/nginx.conf # pidfile:     /usr/local/nginx/logs/nginx.pid   # Source function library. . /etc/rc.d/init.d/functions   # Source networking configuration. . /etc/sysconfig/network   # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0   nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx)   NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"     lockfile=/var/lock/subsys/nginx   make_dirs() {    # make required directories    user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`    if [ -z "`grep $user /etc/passwd`" ]; then        useradd -M -s /bin/nologin $user    fi    options=`$nginx -V 2>&1 | grep 'configure arguments:'`    for opt in $options; do        if [ `echo $opt | grep '.*-temp-path'` ]; then            value=`echo $opt | cut -d "=" -f 2`            if [ ! -d "$value" ]; then                # echo "creating" $value                mkdir -p $value && chown -R $user $value            fi        fi    done }   start() {     [ -x $nginx ] || exit 5     [ -f $NGINX_CONF_FILE ] || exit 6     make_dirs     echo -n $"Starting $prog: "     daemon $nginx -c $NGINX_CONF_FILE     retval=$?     echo     [ $retval -eq 0 ] && touch $lockfile     return $retval }   stop() {     echo -n $"Stopping $prog: "     killproc $prog -QUIT     retval=$?     echo     [ $retval -eq 0 ] && rm -f $lockfile     return $retval }   restart() {     configtest || return $?     stop     sleep 1     start }   reload() {     configtest || return $?     echo -n $"Reloading $prog: "     killproc $nginx -HUP     RETVAL=$?     echo }   force_reload() {     restart }   configtest() {   $nginx -t -c $NGINX_CONF_FILE }   rh_status() {     status $prog }   rh_status_q() {     rh_status >/dev/null 2>&1 }   case "$1" in     start)         rh_status_q && exit 0         $1         ;;     stop)         rh_status_q || exit 0         $1         ;;     restart|configtest)         $1         ;;     reload)         rh_status_q || exit 7         $1         ;;     force-reload)         force_reload         ;;     status)         rh_status         ;;     condrestart|try-restart)         rh_status_q || exit 0             ;;     *)         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"         exit 2 esac     11、配置完成:启动服务器开始安装操作:

  启动操作: [root@mail salt]# salt 'monitor' state.highstate     12、查看结果:

 

    查看客户端文件配置文件看到已经生效,我客户端是4核所以给的worker_processer是4:

     并且已经启动了nginx服务:


     到此全部的安装部署流程已经走完,用saltstack我们发现有再多的机器很快也能按照我们需求对系统来快速部署。

有关saltstack 之源码部署管理nginx的更多相关文章

  1. ruby - i18n Assets 管理/翻译 UI - 2

    我正在使用i18n从头开始​​构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在ruby​​onrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi

  2. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  3. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  4. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  5. ruby-on-rails - 事件管理员日期过滤器日期格式自定义 - 2

    是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s

  6. ruby-on-rails - Ruby on Rails 可以部署在 Azure 网站上吗? - 2

    我可以在Azure网站上部署RubyonRails吗? 最佳答案 还没有。目前仅支持.NET和PHP。 关于ruby-on-rails-RubyonRails可以部署在Azure网站上吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/12964010/

  7. jenkins部署1--jenkins+gitee持续集成 - 2

    前置步骤我们都操作完了,这篇开始介绍jenkins的集成。话不多说,看操作1、登录进入jenkins后会让你选择安装插件,选择第一个默认的就行。安装完成后设置账号密码,重新登录。2、配置JDK和Git都需要执行路径,所以需要先把执行路径找到,先进入服务器的docker容器,2.1JDK的路径root@69eef9ee86cf:/usr/bin#echo$JAVA_HOME/usr/local/openjdk-82.2Git的路径root@69eef9ee86cf:/#whichgit/usr/bin/git3、先配置JDK和Git。点击:ManageJenkins>>GlobalToolCon

  8. 深度学习部署:Windows安装pycocotools报错解决方法 - 2

    深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal

  9. ruby - (Ruby || Python) 窗口管理器 - 2

    我想用这两种语言中的任何一种(最好是ruby​​)制作一个窗口管理器。老实说,除了我需要加载某种X模块外,我不知道从哪里开始。因此,如果有人有线索,如果您能指出正确的方向,那就太好了。谢谢 最佳答案 XCB,X的下一代API使用XML格式定义X协议(protocol),并使用脚本生成特定语言绑定(bind)。它在概念上与SWIG类似,只是它描述的不是CAPI,而是X协议(protocol)。目前,C和Python存在绑定(bind)。理论上,Ruby端口只是编写一个从XML协议(protocol)定义语言到Ruby的翻译器的问题。生

  10. Ruby,使用包含 TK GUI 的 ocra 部署一个 exe - 2

    Ocra无法处理需要“tk”的应用程序require'tk'puts'nope'用奥克拉http://github.com/larsch/ocra不起作用(如链接中的一个问题所述)问题:https://github.com/larsch/ocra/issues/29(Ocra是1.9的"new"rubyscript2exe,本质上它用于将rb脚本部署为可执行文件)唯一的问题似乎是缺少tcl的DLL文件我不认为这是一个问题据我所知,问题是缺少tk的DLL文件如果它们是已知的,则可以在执行ocra时将它们包括在内有没有办法知道tk工作所需的DLL依赖项? 最佳答

随机推荐