kjtyh 发表于 2014-12-31 08:15:32

saltstack使用之二(完成部署编译nginx以及部分cp模块的使用)

书接上一篇原创其中有好同事的帮忙一起测试不容易写出文档整理一下

salt.modules.cp.get_dir(path, dest, saltenv='base', template=None, gzip=None, env=None)
Used to recursively copy a directory from the salt master
1、复制目录
salt '*' cp.get_dir salt://path/to/dir/ /minion/dest

从主节点的能访问到的URL处复制文件给从节点
salt.modules.cp.get_url(path, dest, saltenv='base', env=None)
Used to get a single file from a URL.
The default behaviuor is to write the fetched file to the given destination path. To simply return the text contents instead, set destination to None.

CLI Example:

salt '*' cp.get_url salt://my/file /tmp/mine
salt '*' cp.get_url http://www.slashdot.org /tmp/index.html

2、主节点访问的URL下载到从节点
举例

1
2
3
4
5
6
7
# salt 'test1' cp.get_url http://www.05hd.com/wp-content/uploads/2014/12/Nginx-init-CentOS.sh /tmp/nginx-init-beta1.sh
test1:
    /tmp/nginx-init-beta1.sh
从节点
# ll
total 11048
-rw-r--r-- 1 root   root       2541 Dec 30 13:59 nginx-init-beta1.sh





3、把从节点的文件拷到主节点

1
2
3
4
# ll -lh | grep Python-2.7.6.tar.xz
-rw-rw-r-- 1 centos centos10M Dec 21 16:53 Python-2.7.6.tar.xz    #把这个文件拷到主节点主节点并无此tar包
2014-12-30 15:16:02,753 cp.push Failed transfer failed. Ensure master has 'file_recv' set to
'True' and that the file is not larger than the 'file_recv_size_max' setting on the maste




r

这里报了错告诉我们确保主节点配置文件file_recv为True而且最大接受文件的值。修改了就能传输此包。

果然在我的主节点这些没有被定义

1
2
3
4
5
6
7
8
9
10
# Allow minions to push files to the master. This is disabled by default, for
# security purposes.
#file_recv: False   这里为False并非是True修改需要重读配置文件甚至重启服务。
# Set a hard-limit on the size of the files that can be pushed to the master.
# It will be interpreted as megabytes.
# Default: 100
#file_recv_max_size: 100#默认为100M
修改完了后尝试重读配置文件
# service salt-master reload
can't reload configuration, you have to restart it




#看来上面推测错误了不需要重读重启服务吧。

重读取时的错误

1
2
3
4
5
6
7
in "<string>", line 212, column 1:
    file_recv:True
    ^
could not found expected ':'
in "<string>", line 213, column 1:
    # Set a hard-limit on the size o ...
    ^




报错的原因就是因为file_recv:True中间应该有一个空格
修改以后再次重启服务。主节点执行

1
2
3
4
5
# salt 'test1' cp.push /tmp/Python-2.7.6.tar.xz
test1:
    True
让我们看看这个Python的tar包被放在哪了
/var/cache/salt/master/minions/test1/files/tmp/Python-2.7.6.tar.xz




#可以看出被缓存下来了

仔细看配置文件原来
# Directory to store job and cache data
cachedir: /var/cache/salt/master   #在这里定义了
4、把从节点的目录拷贝到主节点。
官方的解释
Push a directory from the minion up to the master, the files will be saved to the salt master in the master's minion files cachedir (defaults to /var/cache/salt/master/minions/minion-id/files). It also has a glob for matching specific files using globbing.

New in version 2014.7.0.
主节点使用命令salt 'test1' cp.push_dir /tmp/ glob='*.*'   #把从节点的tmp目录下的所有文件拷贝到主节点
一会儿可以查看复制完成了。

1
2
3
4
5
6
7
8
9
10
11
# pwd
/var/cache/salt/master/minions/test1/files/tmp
# ll
总用量 10996
-rw-r--r--. 1 root root      4 12月 30 15:47 1.txt
-rw-r--r--. 1 root root      957 12月 30 15:47 epel.repo
-rw-r--r--. 1 root root   1056 12月 30 15:47 epel-testing.repo
-rw-r--r--. 1 root root   804164 12月 30 15:47 nginx-1.6.2.tar.gz
-rw-r--r--. 1 root root   2541 12月 30 15:47 nginx-init-beta1.sh
-rw-r--r--. 1 root root 10431288 12月 30 15:47 Python-2.7.6.tar.xz
-rw-r--r--. 1 root root      229 12月 30 15:47 yum_save_tx-2014-12-26-14-52MfgFoB.yumtx






下面是一次成功的定义的编译的nginx.sls文件成功测试
注意sls文件不支持tab制表符这个键使用哈希键值对儿的模式一个键可以对应一个值如果一个键对应多个值那么就称为字典dictionary空格的格式特别严格同类属于一个空格类型如B是A的子类B在A下面就要敲两个空格以此类推C是B的子类C就要空格两次那么对于A来说C就空了4个位置
如果要自定义sls的文件
首先1、要在主节点的配置文件里中file_roots位置定义比如我们要编辑安装nginx就定义个nginx

1
2
3
4
5
6
7
8
9
10
11
12
file_roots:
base:
    - /srv/salt   #默认的sls自定义文件的入口
    - /etc
    - /home
nginx:
    - /srv/salt/nginx# 这个必须定义
    - /usr/local/nginx
假设你要为从节点安装nginx了就要在/srv/salt/top.sls文件中定义如此的格式
base:#这里必须是base字符串等于
'java':# 这里为了测试先定义成java了其实如果想所有的节点全部部署可以改成'*'。
    - nginx.nginx






其次2、创建/srv/salt/nginx目录并在目录下创建并提供安装编译需要提供的文件包括nginx的tar包配置文件以及nginx的初始化脚本。

1
2
3
4
5
6
7
# tree nginx
nginx
├── nginx-1.6.2.tar.gz# 必须提供的nginx的编译tar包
├── nginx.conf            # 为编译nginx额外提供的conf文件
├── nginx-init.sh       # 为编译nginx额外提供的init脚本
├── nginx.sls    # 这个文件为提供nginx编译安装的多个步骤
└── top.sls    # 这个可以理解为nginx编译的入口文件







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
# vim nginx.sls
还是要注意格式
pcre-devel:                      #pcre-devel 我的从节点第一次安装出现了缺包的提示
    pkg.installed               #pkg.installed模块确保pcre-devel安装成功它要的是installed的状态
nginx_source:                  # nginx的源位置
file.managed:
    - name: /tmp/nginx-1.6.2.tar.gz          # 这个位置是我在主节点放置nginx的tar包的位置
    - unless: test -e /tmp/nginx-1.6.2.tar.gz   #为测试提供
    - source: salt://nginx/nginx-1.6.2.tar.gz      #这里注意相对路径是相对于你在主配置文件/srv/salt的位置源目录文件的位置
    - user: root
    - group: root
    - mode: 644
cmd.run:                                                            #cmd.run运行命令的模块为了运行下列的命令提供入口
    - cwd: /tmp       # 类似cdcd到/tmp目录下
    - name: tar zxvf nginx-1.6.2.tar.gz && cd /tmp/nginx-1.6.2 && ./configure --prefix=/usr/local/nginx --error-log-path=/home/wwwlogs/nginx/error.log --http-log-path=/home/wwwlogs/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_spdy_module --with-http_gzip_static_module --with-http_stub_status_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/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi --with-pcre && make && cd /tmp/nginx-1.6.2 && make install                      #编译安装nginx
    - unless: test -d /tmp/nginx-1.6.2.tar.gz         # 如果使用cmd时候最好加上unless这个模块。
    - require:          # require 模块
      - file: nginx_source         # 需要nginx_source
nginx-conf:         #nginx-conf这个类
file.managed:         #管理文件需要
    - name: /usr/local/nginx/conf/nginx.conf      # 主配置文件
    - source: salt://nginx/nginx.conf                     #主节点nginx的主配置文件位置相对路径还是相对于/srv/salt
    - user: root
    - group: root
    - mode: 644
nginx-init:
file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/nginx-init.sh
    - user: root
    - group: root
    - mode: 755




这时定义好了并且将对应的文件tar包 脚本都提供到了
先在主节点执行一次test看看报些什么错误之前报了无数错误了。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# salt 'test1' state.highstate -v test=True
----------
          ID: nginx-init
    Function: file.managed
      Name: /etc/init.d/nginx
      Result: True
   Comment: The file /etc/init.d/nginx is in the correct state
   Changes:   
Summary
------------
Succeeded: 3
Failed:    0
Not Run:   2
------------
Total:   5





看来测试没有问题。
第三、然后可以真正执行了
主节点# salt 'test1' state.highstate
切换到从节点
此时从节点运行状态如下

1
2
3
4
5
6
7
top - 14:34:30 up3:33,2 users,load average: 0.97, 0.39, 0.16
Tasks: 124 total,   2 running, 122 sleeping,   0 stopped,   0 zombie
Cpu(s): 44.3%us,9.3%sy,0.0%ni, 44.2%id,2.0%wa,0.0%hi,0.2%si,0.0%st
Mem:    428688k total,   418564k used,    10124k free,    28672k buffers
Swap:2097144k total,   2164k used,2094980k free,    90372k cached
PID USER      PRNIVIRTRESSHR S %CPU %MEM    TIME+COMMAND                                                                              
6907 root      20   0 5204434m 3180 R6.08.2   0:00.18 cc1




可以看出它在编译


一会儿

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
                  -    #    ssl_ciphersHIGH:!aNULL:!MD5;
                  -    #    ssl_prefer_server_cipherson;
                  -
                  -    #    location / {
                  -    #      root   html;
                  -    #      indexindex.html index.htm;
                  -    #    }
                  -    #}
                  -
                  -}
                  +}
----------
          ID: nginx-init
    Function: file.managed
      Name: /etc/init.d/nginx
      Result: True
   Comment: File /etc/init.d/nginx is in the correct state
   Changes:   
Summary
------------
Succeeded: 5
Failed:    0
------------
Total:   5





仅仅摘取一个片段显示在从节点的编译的nginx成功了
由于编译安装所以操作一定要谨慎考虑对方从节点对应的目录是否已经建立了譬如这里笔者把编译好以后的修改贴出来给大家

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1082useradd -r -s /sbin/nologin www
1083id www
1084service nginx start
1085vim /usr/local/nginx/conf/nginx.conf
1086find / -name mime.types
1087vim /usr/local/nginx/conf/nginx.conf
1088nginx -t
1089vim +153 /usr/local/nginx/conf/nginx.conf
1090nginx -t
1091vim +153 /usr/local/nginx/conf/nginx.conf
1092nginx -t
1093vim +153 /usr/local/nginx/conf/nginx.conf
1094*
1095mkdir -pv /var/log/nginx/
1096nginx -t




10多步的修改。

http://192.168.1.243:10080/

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

nginx服务已经启动了

页: [1]
查看完整版本: saltstack使用之二(完成部署编译nginx以及部分cp模块的使用)