王艳玲 发表于 2018-7-31 09:48:15

saltstack参数解析及代码分享

  altstack搭建haproxy代理的nginx服务
  关于saltstack的搭建,请参见我的博客。
  测试salt正常:
  # salt '*' cmd.run 'free -m'
  server2.example.com:
  total       used       free   shared    buffers   cached
  Mem:         996      185      811          0          9         74
  -/+ buffers/cache:      101      895
  Swap:          991          0      991
  server3.example.com:
  total       used       free   shared    buffers   cached
  Mem:         490      371      119          0         12         92
  -/+ buffers/cache:      265      224
  Swap:          991          0      991
  haproxy是一个开源的高性能反向代理,支持四层和七层的负载均衡,多种负载均衡算法和健康检查。
  keepalived是一个高可用集群,通过vrrp协议实现,可以通过keepalived管理haproxy,当haproxy出现故障时,通过高可用机制使其继续提供服务。
  nginx作为后端
  使用saltstack一键部署
  所用主机为rhel6.5
  salt-master :server1.example.com :172.25.33.1
  salt-minion: server2.example.com:172.25.33.2 haproxy keepalived
  salt-minion: server3.example.com:172.25.33.3 haproxy keepalived
  salt-minion: server4.example.com:172.25.33.4 nginx
  salt-minion: server5.example.com:172.25.33.5 nginx
  # salt-key -L
  Accepted Keys:
  server2.example.com
  server3.example.com
  server4.example.com
  server5.example.com
  Denied Keys:
  Unaccepted Keys:
  Rejected Keys:
  **saltstack的组件Pillar**
  Pillar是salt非常重要的组件,他用于给特定的minion定义任何你需要的数据,这些数据可以被salt的其他组件使用,Pillar在解析完成后,是一个嵌套的dict结构,最上层的key是minionID ,其value是该minion所拥有的Pillar数据,每个value也都是一个键值对。
  Pillar的数据是与特定的minion关联的,每个Minion只能看到自己的数据,所以Pillar用于传递敏感数据
  saltstack布局:
  # treesalt
  salt
  |-- haproxy
  |   |-- conf
  |   |   `-- haproxy.cfg
  |   |-- init.d
  |   |   `-- haproxy
  |   |-- init.sls
  |   |-- package
  |   |   `-- haproxy-1.4.24.tar.gz
  |   `-- sbin
  |       `-- haproxy
  |-- keepalived-master
  |   |-- conf
  |   |   |-- check_haproxy.sh
  |   |   |-- keepalived
  |   |   `-- keepalived.conf
  |   |-- init.d
  |   |   `-- keepalived
  |   |-- init.sls
  |   |-- package
  |   |   `-- keepalived-1.2.24.tar.gz
  |   `-- sbin
  |       `-- keepalived
  |-- keepalived-slave
  |   |-- conf
  |   |   |-- check_haproxy.sh
  |   |   |-- keepalived
  |   |   `-- keepalived.conf
  |   |-- init.d
  |   |   `-- keepalived
  |   |-- init.sls
  |   |-- package
  |   |   `-- keepalived-1.2.24.tar.gz
  |   `-- sbin
  |       `-- keepalived
  |-- memcache-node1
  |   |-- init.sls
  |   `-- package
  |       |-- context.xml
  |       |-- libevent-1.4.13-4.el6.x86_64.rpm
  |       `-- memcached-1.4.22-4.el6.art.x86_64.rpm
  |-- memcache-node2
  |   |-- init.sls
  |   `-- package
  |       |-- context.xml
  |       |-- libevent-1.4.13-4.el6.x86_64.rpm
  |       `-- memcached-1.4.22-4.el6.art.x86_64.rpm
  |-- mysql-master
  |   |-- conf
  |   |   |-- my.cnf
  |   |   `-- mysqld
  |   |-- init.sls
  |   |-- mysql_login.sh
  |   `-- package
  |-- mysql-slave
  |   |-- conf
  |   |   `-- my.cnf
  |   |-- init.sls
  |   `-- package
  |-- nginx
  |   |-- conf
  |   |   `-- nginx.conf
  |   |-- init.sls
  |   |-- package
  |   |   `-- nginx-1.10.1.tar.gz
  |   `-- sbin
  |       `-- nginx
  |-- php
  |   |-- init.sls
  |   `-- package
  |       |-- libmcrypt-2.5.8-9.el6.x86_64.rpm
  |       |-- libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
  |       |-- php-5.6.19.tar.bz2
  |       |-- php-fpm
  |       |-- php-fpm.conf
  |       |-- php-fpm.conf.default
  |       |-- php.ini
  |       `-- php.ini-production
  |-- README
  |-- tomcat
  |   |-- init.sls
  |   `-- package
  |       |-- apache-tomcat-7.0.8.tar.gz
  |       |-- jdk-8u45-linux-x64.tar.gz
  |       |-- profile
  |       `-- test.jsp
  |-- top.sls
  `-- user
  `-- init.sls
  34 directories, 54 files
  **saltstack常见的模块:**
  cmdcronfilemountntppkgserviceuser group
  **cmd 模块:**
  name: 要执行的命令,该命令会在salt-minion的路径和权限下执行
  onlyif:用于检查命令,仅当onlyif选项指向的命令返回值为True时才执行name后命令
  unless: 用于检查命令,仅当unless 选项指向的命令返回为False时才执行name指向的命令
  cwd: 执行命令时的当前工作目录,默认时/root
  user/group:指定用户身份运行命令
  shell: 用于执行命令的shell,默认时shell grain
  run:运行name后的命令
  nginx_source:
  file.managed:
  - name: /tmp/nginx-1.4.5.tar.gz
  - unless: test -f /tmp/nginx-1.4.5.tar.gz       ##若minion端不存在/tmp/nginx-1.4.5.tar.gz这个文件,才会执行这个file模块
  - source: salt://nginx/files/nginx-1.4.5.tar.gz
  tar_nginx:
  cmd.run:
  - cwd: /usr/local/src                              ##当前工作目录
  - name: tar zxvf nginx-1.4.5.tar.gz
  - unless: test -d /usr/local/src/nginx-1.4.5
  - require:
  - file: nginx_source
  **cron模块:**
  minute:分
  hour:时
  daymonth:日
  month:月
  dayweek:周
  user:用户名
  present:创建计划任务
  absent:删除计划任务
  name:计划任务内容
  cat cron.sls
  mysql_cron:
  cron:
  - present
  - name: cd /tmp/scripts;./mysql_backup.sh
  - user: root
  - minute: 0
  - hour: 1
  #- daymonth:
  #- month:
  #- dayweek:
  #- minute: "*/5"
  mysql_cron:
  cron:
  - absent
  - name: cd /tmp/scripts;./mysql_backup.sh          ##只要保证name后命令不变,能匹配到
  - user: root
  - minute: 0
  - hour: 2
  #- daymonth:
  #- month:
  #- dayweek:
  #- minute: "*/5"
  **file模块:**
  /tmp/salt/:
  file:
  - directory               #目录不存在,则创建目录
  - name: /tmp/salt/
  - user: nobody
  - group: nobody
  - file_mode: 644            #与recurse递归函数一起用
  - dir_mode: 755             #文件权限644,目录权限755
  - makedirs: True
  - recurse:               #递归目录权限,包括用户和权限
  - user                   #若本来此目录就存在,且此目录下有很多文件,则recurse函数会把此目录和目录下的所有文件的权限都修改(nobody 644 755)
  - group
  - mode                   #文件644,目录755,就是上面的file_mode和dir_mode
  #- ignore_files         #忽略文件
  #- ignore_dirs
  /tmp/salt/file:
  file:
  - managed                   #创建文件
  - name: /tmp/salt/file
  - source: salt://web/files/httpd.conf
  - user: nobody
  - group: nobody               #若此文件本来就存在,则会修改此文件的权限(nobody 644)
  - mode: 644
  #- backup: minion
  #- template: jinja          #调用pillar变量和grains变量
  - require:
  - file: /tmp/salt/
  **file.managed:**
  常规文件可以使用file.managed函数,这个状态函数从salt-master下载文件到目标地址
  Manage a given file, this function allows for a file to be downloaded from the salt master and potentially run through a templating system.
  *- name: 要管理文件的位置。可以有多个*:
  ==- name:/mnt/haproxy-1.4.24.tar.gz
  - names:
  - /mnt/memcached-1.4.22-4.el6.art.x86_64.rpm
  - /mnt/libevent-1.4.13-4.el6.x86_64.rpm==
  *- source: 资源位置:*
  The source file to download to the minion, this source file can be hosted on either the salt master server (salt://), the salt minion local file system (/), or on an HTTP or FTP server (http(s)://, ftp://).即可以是从主上下载,也可以是从自己的,同时可以是网络资源。
  If the file is hosted on a HTTP or FTP server then the source_hash argument is also required.需要资源加密
  ==- source: salt://haproxy/package/haproxy-1.4.24.tar.gz
  file.managed:
  - source:
  - salt://file_that_does_not_exist
  - salt://file_that_exists
  tomdroid-src-0.7.3.tar.gz:
  file.managed:
  - name: /tmp/tomdroid-src-0.7.3.tar.gz
  - source: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.tar.gz
  - source_hash: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.hash
  The source_hash can be specified as a simple checksum, like so
  file.managed:
  - name: /tmp/tomdroid-src-0.7.3.tar.gz
  - source: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.tar.gz
  - source_hash: 79eef25f9b0b2c642c62b7f737d4f53f
  或:- source_hash: md5=79eef25f9b0b2c642c62b7f737d4f53f==
  user,group,mode挺简单,就不赘述了
  **template**
  If this setting is applied, the named templating engine will be used to render the downloaded file
  如果使用了template,那么会使用模版命名的引擎来渲染下载文件。
  支持的模版:
  cheetah
  genshi
  jinja
  mako
  py
  wempy
  (不太懂)
  **service**
  Services are defined as system daemons typically started with system init or rc scripts
  service只能定义以init.d启动的或rc.d启动的服务。
  *service.running*
  - name
  The name of the init or rc script used to manage the service使用init或rc脚本来管理的服务名称/etc/rc.d/rc
  - enable
  Set the service to be enabled at boot time
  - init_delay 等待多长时间后启动
  **directory**
  This function can create and enforce the permissions on a directory
  /srv/stuff/substuf:
  file.directory:
  - user: fred
  - group: users
  - mode: 755
  - makedirs: True
  区分目录权限和文件权限:
  - file_mode: 744
  - dir_mode: 755
  **pkg模块:**
  mypkgs:
  pkg.installed:
  - pkgs:            #安装多个软件包,用pkgs函数,一行一个
  - gcc
  - cmake
  - make
  #- name: httpd       #安装的软件包名,与pkgs一起用时,此函数将被忽略
  ##installed:安装软件包
  ##latest:将软件更新到最新
  ##removed:卸载软件包
  ##pkgs:安装多个软件包时,一次全部安装(推荐)
  ##names:安装多个软件包时,一次安装一个,需安装多次
  **service模块:**
  httpd:
  service:
  - running               #使服务处于运行状态
  - enable: True          #设置开机自动启动

  ->  - watch:
  - file: /etc/httpd/conf/httpd.conf
  - require:
  - pkg: httpd
  ##dead:使服务处于stop状态
  **user模块:**
  user1:
  user:
  - present
  #- name: user1
  #- home: /home/user1
  #- shell: /bin/bash
  - uid: 600
  - gid: 600
  - system: True               #设置为系统用户
  - groups:                  #设置用户的所属组
  - nobody                   #把user1用户加入到nobody组中,且仅属于nobody组;如果用户原本在jeff组中,执行后用户只在nobody组中,不再属于jeff组了
  #- group1
  ##absent:删除用户
  ##force:强制删除用户(即便用户正在登陆)
  **group模块:**
  absent:删除用户组
  name:与absent一起用,则是删除的用户组名
  present:管理用户组,不存在则创建;存在则管理其属性
  name:与present一起用,则是被管理的用户组名
  ##下面的属性全都是与present一起用的##
  gid:组ID分配给命名组;如果留空,那么下一个可用的组ID将被分配
  system:命名组是否是一个系统组,这本质上是groupadd“-r”选项
  addusers:添加更多的用户列表作为组成员
  delusers:确保这些用户从组成员中删除
  members:用新成员的名单替换现有组成员。
  ##注:选项'members'和'adduser/delusers'是相互排斥的,不能一起使用
  cheese:
  group.present:            #用户组不存在,则创建;若存在,则管理,包括将用户添加到此组中或将用户从组中移除
  - gid: 7648
  - system: True
  - addusers:
  - user1               #将user1添加到组中
  - users2
  - delusers:
  - foo
  cheese:
  group.present:
  - gid: 7648
  - system: True
  - members:            #只有这4个用户属于这个组
  - foo
  - bar
  - user1
  - user2
  **lvm模块:**
  /dev/sda:
  lvm:
  - pv_present         #创建pv
  #-name: /dev/sda
  my_vg:
  lvm.vg_present:          #创建vg
  - devices: /dev/sda    #指定pv
  lvroot:
  lvm.lv_present:
  - vgname: my_vg      #指定vg
  - name: lvroot

  ->  #- stripes: 5
  #- stripesize: 8K
  **mount模块:**
  /mnt/sdb:                        #标识,没有name函数则表示挂载点
  mount.mounted:
  - device: /dev/sdb1          #挂载的设备名
  #- name: /mnt/sdb            #挂载点
  - fstype: ext4
  - mkmnt: True                #挂载点不存在则自动创建,最好将其设置为True
  - opts:
  - defaults
  /srv/bigdata:
  mount:
  - mounted:
  - device: UUID=066e0200-2867-4ebe-b9e6-f30026ca2314
  - fstype: xfs
  - opts: nobootwait,noatime,nodiratime,nobarrier,logbufs=8
  #- dump: 0               #默认是0
  #- pass_num: 2         #默认是0
  #- persist: True         #是不是将其写入/etc/fstab文件里,默认是True,写入
  - mkmnt: True
  ##dump: The dump value to be passed into the fstab, Default is 0
  ##pass_num: The pass value to be passed into the fstab, Default is 0
  本博客部分转载自:http://732233048.blog.51cto.com/9323668/1677843
页: [1]
查看完整版本: saltstack参数解析及代码分享