78788909 发表于 2016-1-21 08:37:22

salt源码装nginx

其实早就知道saltstack的强大,其中的state,grain,pillar,是个很好的自动化配置工具!!由于在上一个公司像这样的脚本都已经写好,其实没有太大的动力去学salt的更强大的功能。在这家公司就是深入研究一下salt。在之前做了好多的准备。。。今天下午才做成功实验!!!!废话不多说。上干货!!思路:

    1.使用grains收集cpu信息、以及打开文件最大等信息结合jinja配置nginx.conf.
    2.使用salt推送文件。
一、做准备工作。

(1)环境介绍:

master: 192.168.100.127   web1
minion: 192.168.100.129   web2   192.168.100.90 db1   192.168.100.134db2

(2)安装saltmaster
   我们需要安装其他的yum源,这样才能顺利的安装master和minion。

master:
       yum-y install epel-release
       yum install salt-master
编辑配置文件vim /etc/salt/master

1
2
3
4
5
6
7
8
file_roots:
    base:
      - /srv/salt/
pillar_roots:
   base:
   - /srv/pillar
pillar_opts: True
order_masters: True




重启服务:service salt-master restart
主要配置是如上所示。。

(3)安装salt-minion
minion:
       yum -y install epel-release
       yuminstall salt-minion
编辑minion的配置文件vim /etc/salt/minion

#master为你所指定的master的ip或者机器名

master: web1
#id为你配置的机器minion的ip或者机器名
id: db2

重启服务: service salt-minion restart

其他的minion同上!!!!
(4)连通
master端:
salt-key-L 会显示所有minion向master发送的公钥。但是没有被master接受!!
salt-key-A可以接受那些minion发送的公钥也就是master同意了作为他们的“头”了


可以验证一下: salt ‘*’ test.ping

# salt '*' test.ping
db2:
    True
web1:
    True
web2:
    True
db1:
    True
#
此时说明master和minion之间可以互相通信了。。。。


二、目录树
(1)下面的图是这次实验的目录树

srv
├── pillar
│   ├── dbserver.sls
│   ├── top.sls
│   └── webserver.sls
└── salt
    ├── _grains
    │   └── nginx_config.py
    ├── nginx
    │   ├── conf.sls
    │   ├── files
    │   │   ├── nginx
    │   │   ├── nginx-1.8.0.tar.gz
    │   │   └── nginx.conf
    │   ├── init.sls
    │   ├── install.sls
    │   └── server.sls
    └── top.sls
(2)介绍
所有的state文件的执行都要个top.sls 该文件是入口文件。
例如salt下top.sls 要想执行 conf.sls,install.sls server.sls 必须有个top.sls
规范为主:在在某个项目都要有个独立的文件夹,其中有个init.sls 文件里面包括了这次要执行的state文件。废话不多说!!上干货!!
三、编写grain与pillar
(1)vim nginx_config.py

#该文件是用来获取打开文件最大数的方便了nginx.conf的参数的确定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#1 #!/usr/bin/python
2 import os
3 import sys
4 import commands
5 def NginxGrains():
6   grains={}
7   max_open_file=65535
8   try:
9         getulimit=commands.getstatusoutput('source /etc/profile;ulimit -n')
10   except Exception,e:
11         pass
12   if getulimit==0:
13         max_open_file=int(getulimit)
14         grains['max_open_file']=max_open_file
15         return grains


(2)测试数据
刷新grain模块:salt ‘*’ saltutil.sync_all
检验数据加载情况:salt ‘*’ grains.item max_open_file
   # salt '*' grains.item max_open_file
web1:
    ----------
    max_open_file:
      1024
web2:
    ----------
    max_open_file:
      1024
db2:
    ----------
    max_open_file:
      1024
db1:
    ----------
    max_open_file:
      1024
数据测试成功!

(3)编写pillar
vim top.sls
base:
   'web*':
   - webserver
   'db*':
   - dbserver

以上是正则匹配 ‘web*’‘db*’
在此类文件中执行state文件可以省略后缀名.sls
vim webserver.sls
nginx:
   root: /data

vim dbserver.sls
nginx:
    root: /WWW


pillar在此的作用是根据webserver和dbserver做区分,webserver的根目录为/data,而dbserver为/WWW
.简单的实现这些,其他的可以根据需求来定义。

刷新pillar:salt ‘*’saltutil.refresh_pillar

检验数据的成效:salt '*' pillar.data nginx 如下所示:

# salt '*' pillar.data nginx
web1:
    ----------
    nginx:
      ----------
      root:
            /data
web2:
    ----------
    nginx:
      ----------
      root:
            /data
db1:
    ----------
    nginx:
      ----------
      root:
            /www
db2:
    ----------
    nginx:
      ----------
      root:
            /www
四、编写salt文件下的nginx项目

vimtop.sls

   1 base:
2   '*':
3   - nginx                                                                        此处的nginx表明要执行文件夹里的sls文件。


cd nginx

vim init.sls
                                                                                             include:
2   - nginx.install
3   - nginx.conf
4   - nginx.server                        

   nginx.install 表示 要执行nginx目录下的install.sls文件
此处的点代表路径。省略后缀名于是就是nginx.install。其他一样。顺序不能变原因就在下面。。。




vim install.sls
#该文件是安装过程的文件

1 #nginx.tar.gz
2 nginx_source:
3   file.managed:
4   - name: /tmp/nginx-1.8.0.tar.gz
5   - unless: test -e /tmp/nginx-1.8.0.tar.gz
6   - source: salt://nginx/files/nginx-1.8.0.tar.gz
7 #extract
8 extract_nginx:
9   cmd.run:
10   - cwd: /tmp
11   - names:
12       - tar -zxvf nginx-1.8.0.tar.gz
13   - unless: test -d /tmp/nginx-1.8.0
14   - reuqire:
15       - file: nginx_source
16 #user
17 nginx_user:
18   user.present:
19   - name: nginx
20   - uid: 610
21   - createhome: False
22   - shell: /sbin/nologin
23 #nginx_pkgs
24 nginx_pkgs:
25   pkg.installed:
26   - pkgs:
27       - gcc
28       - openssl
29       - openssl-devel
30       - pcre-devel
31       - zlib-devel
32 #nginx_compile
33 nginx_compile:
34   cmd.run:
35   - cwd: /tmp/nginx-1.8.0
36   - names:
37       - ./configure --prefix=/nginx/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_sta    tus_module
38       - pwd
39       - make
40       - make install
41   - require:
42       - cmd: extract_nginx
43       - pkg: nginx_pkgs
44   - unless: test -d /nginx/nginx
45 #cache_dir
46 cache_dir:
47   cmd.run:
48   - names:
49       - mkdir -p /nginx/nginx{client,proxy,fcgi} && chown -R nginx.nginx /nginx/nginx/
50       - mkdir -p /nginx/nginx/conf/vhost && chown -R nginx.nginx /nginx/nginx/conf/vhost
51   - unless: test -d /nginx/nginx/client
52   - require:
53       - cmd: nginx_compile


vimconf.sls
#该文件是从master传送conf文件。启动过程依赖conf文件。

1 include:
2   - nginx.install
3 nginx_service:
4   file.managed:
5   - name: /nginx/nginx/conf/nginx.conf
6   - user: nginx
7   - mode: 644
8   - source: salt://nginx/files/nginx.conf
9   - template: jinja
10   service.running:
11   - name: nginx
12   - enable: True
13   - reload: True
14   - watch:
15       - file: /nginx/nginx/conf/nginx.conf
vim    server.sls
#该文件是传送启动脚本,服务启动依赖该脚本

1 include:
2   - nginx.install
3 server:
4   file.managed:
5   - name: /etc/init.d/nginx
6   - user: root
7   - mode: 755
8   - source: salt://nginx/files/nginx
9   service.running:
10   - name: nginx
11   - enable: True
12   - reload: True
13   - watch:
14       - file: /etc/init.d/nginx
15 command:
16   cmd.run:
17   - names:
18       - /sbin/chkconfig --add nginx
19       - /sbin/chkconfig --nginx on
20   - unless: /sbin/chkconfig --list nginx

五、files文件夹
在目录树中有files这里面是放了在安装过程中需要的文件。例如配置文件、启动脚本、安装包、
vim nginx.conf
1 # For more information on configuration, see:
2 user            nginx;
3 worker_processes{{ grains['num_cpus'] }};
4 {% if grains['num_cpus'] == 1 %}
5 worker_cpu_affinity 1 ;
6 {% elif grains['num_cpus'] == 2 %}
7 worker_cpu_affinity 01 10;
8 {% elif grains['num_cpus'] == 4 %}
9 worker_cpu_affinity 1000 0100 0010 0001;
10 {% elif grains['num_cpus'] >= 8 %}
11 worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
12 {% else %}
13 worker_cpu_affinity 1000 0100 0010 0001;
14 {% endif %}
15 worker_rlimit_nofile {{ grains['max_open_file'] }};
16
17 error_log/var/log/nginx/error.log;
18 #error_log/var/log/nginx/error.lognotice;
19 #error_log/var/log/nginx/error.loginfo;
20
21 pid      /var/run/nginx.pid;
22
23 events {
24         worker_connections{{ grains['max_open_file'] }};
25}
26
27 http
28      {
29               include       mime.types;
30               default_typeapplication/octet-stream;
31               charsetutf-8;
32               server_names_hash_bucket_size 128;
33               client_header_buffer_size 32k;
34               large_client_header_buffers 4 32k;
35               client_max_body_size 128m;
36               sendfile on;
37               tcp_nopush   on;
38               keepalive_timeout 60;
39               tcp_nodelay on;
40               server_tokens off;
41               client_body_buffer_size512k;
42               gzip on;
43               gzip_min_length1k;
44               gzip_buffers   4 16k;
45               gzip_http_version 1.1;
46               gzip_comp_level 2;
47               gzip_types      text/plain application/x-javascript text/css application/xml;
48               gzip_vary on;
49               log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
50                                 '$status $body_bytes_sent "$http_referer" '
51                                 '"$http_user_agent" "$http_x_forwarded_for" "$host"' ;
52
53
54         server {
55                        listen            80;
56                        root            {{ pillar['nginx']['root'] }};
57                        index             index.html;
58
59               }
60
61               include vhost/*.conf;
62      }
关键注意加黑的字体的参数的数值。。。这就是pillar和grain的威力!!!!!
vim nginx
#启动脚本没什么可说的
1 #!/bin/sh
2 #
3 # nginx - this script starts and stops the nginx daemon
4 #
5 # chkconfig:   - 85 15
6 # description:Nginx is an HTTP(S) server, HTTP(S) reverse \
7 #               proxy and IMAP/POP3 proxy server
8 # processname: nginx
9 # config:      /nginx/nginx/conf/nginx.conf
10 # pidfile:   /nginx/nginx/logs/nginx.pid
11
12 # Source function library.
13 . /etc/rc.d/init.d/functions
14
15 # Source networking configuration.
16 . /etc/sysconfig/network
17
18 # Check that networking is up.
19 [ "$NETWORKING" = "no" ] && exit 0
20
21 nginx="/nginx/nginx/sbin/nginx"
22 prog=$(basename $nginx)
23
24 NGINX_CONF_FILE="/nginx/nginx/conf/nginx.conf"
25
26
27 lockfile=/var/lock/subsys/nginx
28
29 make_dirs() {
30    # make required directories
31    user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
32    if [ -z "`grep $user /etc/passwd`" ]; then
33      useradd -M -s /bin/nologin $user
34    fi
35    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
36    for opt in $options; do
37      if [ `echo $opt | grep '.*-temp-path'` ]; then
38            value=`echo $opt | cut -d "=" -f 2`
39            if [ ! -d "$value" ]; then
40                # echo "creating" $value
41                mkdir -p $value && chown -R $user $value
42            fi
43      fi
44    done

45 }
46
47 start() {
48   [ -x $nginx ] || exit 5
49   [ -f $NGINX_CONF_FILE ] || exit 6
50   make_dirs
51   echo -n $"Starting $prog: "
52   daemon $nginx -c $NGINX_CONF_FILE
53   retval=$?
54   echo
55   [ $retval -eq 0 ] && touch $lockfile
56   return $retval
57 }
58
59 stop() {
60   echo -n $"Stopping $prog: "
61   killproc $prog -QUIT
62   retval=$?
63   echo
64   [ $retval -eq 0 ] && rm -f $lockfile
65   return $retval
66 }
67
68 restart() {
69   configtest || return $?
70   stop
71   sleep 1
72   start
73 }
74
75 reload() {
76   configtest || return $?
77   echo -n $"Reloading $prog: "
78   killproc $nginx -HUP
79   RETVAL=$?
80   echo
81 }
82
83 force_reload() {
84   restart
85 }
86
87 configtest() {
88   $nginx -t -c $NGINX_CONF_FILE
89 }
90
91 rh_status() {
92   status $prog
93 }
94
95 rh_status_q() {
96   rh_status >/dev/null 2>&1
97 }
98
99 case "$1" in
100   start)
101         rh_status_q && exit 0
102         $1
103         ;;
104   stop)
105         rh_status_q || exit 0
106         $1
107         ;;
108   restart|configtest)
109         $1
110         ;;
111   reload)
112         rh_status_q || exit 7
113         $1
114         ;;
115   force-reload)
116         force_reload
117         ;;
118   status)
119         rh_status
120         ;;
121   condrestart|try-restart)
122         rh_status_q || exit 0
123             ;;
124   *)
125         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
126         exit 2
127 esac

到此主要步骤结束。

六、安装
master端:salt ‘*’ state.highstate
如果你运气好的话,一遍就成功了。这基本上是不可能的!!!!!
假设是成功的:
会有如下:
nginx就能用了呗。。
跟手动编译的一模一样!!!
最重要的是pillar和grain起的作用!!


七、问题总结
(1)、就是在编译安装时,./configure 通过了,可是在make,make install 相继报错make: *** No targets specified and no makefile found.Stop
这意思就是该文件夹下没有Makefile。
解决办法:我认为是不是路径变了。于是在make和make install前加了条pwd 看看是不是该路径。没想到加上这条命令这个问题就解决了。
(2)就是在推送启动脚本和nginx配置文件时,明明存在就是不能解释,报类似的错误:
# service nginx start
env: /etc/init.d/nginx: No such file or directory

突然想起来原来我是在windows上编写的。于是用dos2unix 这个命令解决了

(3)就是在安装过程中,saltstack有个不好的地方就是明明上一次还行,下一次同样的系统就是不可以了。这样的解决办法就是多推送几次。前提是之前的问题都解决了。还出现同样的问题。


好了,就到这吧。学得所有关于saltstack的知识都是为了能按照需求源码搭建应用!!



页: [1]
查看完整版本: salt源码装nginx