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

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

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-31 08:15:32 | 显示全部楼层 |阅读模式
书接上一篇原创其中有好同事的帮忙一起测试不容易写出文档整理一下

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
[iyunv@master nginx]# salt 'test1' cp.get_url http://www.05hd.com/wp-content/u ... ginx-init-CentOS.sh /tmp/nginx-init-beta1.sh
test1:
    /tmp/nginx-init-beta1.sh
从节点
[iyunv@localhost tmp]# ll
total 11048
-rw-r--r-- 1 root   root       2541 Dec 30 13:59 nginx-init-beta1.sh




3、把从节点的文件拷到主节点
1
2
3
4
[iyunv@localhost tmp]# ll -lh | grep Python-2.7.6.tar.xz
-rw-rw-r-- 1 centos centos  10M Dec 21 16:53 Python-2.7.6.tar.xz    #把这个文件拷到主节点主节点并无此tar包
2014-12-30 15:16:02,753 [salt.loaded.int.module.cp                   ][ERROR   ] 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
修改完了后尝试重读配置文件
[iyunv@master salt]# 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
[iyunv@master salt]# 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
[iyunv@master tmp]# pwd
/var/cache/salt/master/minions/test1/files/tmp
[iyunv@master 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
[iyunv@master salt]# 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
[iyunv@master nginx]# 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




看来测试没有问题。
第三、然后可以真正执行了
主节点[iyunv@master nginx]# salt 'test1' state.highstate
切换到从节点
此时从节点运行状态如下
1
2
3
4
5
6
7
top - 14:34:30 up  3: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      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                
6907 root      20   0 52044  34m 3180 R  6.0  8.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_ciphers  HIGH:!aNULL:!MD5;
                  -    #    ssl_prefer_server_ciphers  on;
                  -
                  -    #    location / {
                  -    #        root   html;
                  -    #        index  index.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
1082  useradd -r -s /sbin/nologin www
1083  id www
1084  service nginx start
1085  vim /usr/local/nginx/conf/nginx.conf
1086  find / -name mime.types
1087  vim /usr/local/nginx/conf/nginx.conf
1088  nginx -t
1089  vim +153 /usr/local/nginx/conf/nginx.conf
1090  nginx -t
1091  vim +153 /usr/local/nginx/conf/nginx.conf
1092  nginx -t
1093  vim +153 /usr/local/nginx/conf/nginx.conf
1094*
1095  mkdir -pv /var/log/nginx/
1096  nginx -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、欢迎大家加入本站运维交流群:群②: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-39970-1-1.html 上篇帖子: saltstack基本应用之一 下篇帖子: saltstack安装配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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