1. 程式人生 > >saltstack SLS 實例分析學習

saltstack SLS 實例分析學習

resolv grep style libevent epel manage nginx comm 4.2

文件下載:https://github.com/unixhot/saltbook-code

首先看一下目錄結構(沒用的已經刪掉)

[root@k8s_master saltbook-code-master]# tree saltstack-haproxy/
saltstack-haproxy/
├── pillar
│   └── base
│       ├── top.sls
│       └── zabbix
│           └── agent.sls
└── salt
    ├── base
    │   ├── init
    │   │   ├── audit.sls
    │   │   ├── dns.sls
    │   │   ├── env_init.sls
    │   │   ├── epel.sls
    │   │   ├── files
    │   │   │   ├── resolv.conf
    │   │   │   └── zabbix_agentd.conf
    │   │   ├── history.sls
    │   │   ├── sysctl.sls
    │   │   └── zabbix_agent.sls
    │   └── top.sls
    └── prod
        ├── cluster
        │   ├── files
        │   │   ├── haproxy-outside.cfg
        │   │   └── haproxy-outside-keepalived.conf
        │   ├── haproxy-outside-keepalived.sls
        │   └── haproxy-outside.sls
        ├── haproxy
        │   ├── files
        │   │   ├── haproxy-1.5.3.tar.gz
        │   │   └── haproxy.init
        │   └── install.sls
        ├── keepalived
        │   ├── files
        │   │   ├── keepalived-1.2.17.tar.gz
        │   │   ├── keepalived.init
        │   │   └── keepalived.sysconfig
        │   └── install.sls
        ├── libevent
        │   ├── files
        │   │   └── libevent-2.0.22-stable.tar.gz
        │   └── install.sls
        ├── memcached
        │   ├── files
        │   │   └── memcached-1.4.24.tar.gz
        │   ├── install.sls
        │   └── service.sls
        ├── nginx
        │   ├── files
        │   │   ├── nginx-1.9.1.tar.gz
        │   │   ├── nginx.conf
        │   │   └── nginx-init
        │   ├── install.sls
        │   └── service.sls
        ├── pcre
        │   ├── files
        │   │   └── pcre-8.37.tar.gz
        │   └── install.sls
        ├── php
        │   ├── files
        │   │   ├── init.d.php-fpm
        │   │   ├── memcache-2.2.7.tgz
        │   │   ├── php-5.6.9.tar.gz
        │   │   ├── php-fpm.conf.default
        │   │   ├── php.ini-production
        │   │   └── redis-2.2.7.tgz
        │   ├── install.sls
        │   ├── php-memcache.sls
        │   └── php-redis.sls
        ├── pkg
        │   └── pkg-init.sls
        ├── user
        │   └── www.sls
        └── web
            ├── bbs.sls
            └── files
                └── bbs.conf

1)、pillar

[root@k8s_master saltstack-haproxy]# tree pillar/
pillar/
└── base
    ├── top.sls
    └── zabbix
        └── agent.sls

2 directories, 2 files

[root@k8s_master base]# cat base/top.sls 
base:
  ‘*‘:
    - zabbix.agent
[root@k8s_master base]# cat base/zabbix/agent.sls     #配置zabbix server的變量及值
zabbix-agent: Zabbix_Server: 192.168.56.21

2)、salt(目錄結構看上面)

[root@k8s_master base]# cat top.sls 
base:
  ‘*‘:
    - init.env_init                  ##首先跳到init目錄下的環境初始化sls
prod:                           #然後跳到對所有主機安裝haproxy 目錄下的sls
  ‘*‘:
    - cluster.haproxy-outside
    - cluster.haproxy-outside-keepalived
    - web.bbs
  ‘saltstack-node2.example.com‘:          #最後對特定主機安裝memcache服務
- memcached.service

2.1)init目錄(入口)

#目錄結構:
[root@k8s_master base]# tree init/ init/ ├── audit.sls ├── dns.sls ├── env_init.sls ├── epel.sls ├── files │ ├── resolv.conf │ └── zabbix_agentd.conf ├── history.sls ├── sysctl.sls └── zabbix_agent.sls 1 directory, 9 files

入口sls文件為env_init.sls

[root@k8s_master init]# cat env_init.sls 
include:
  - init.dns    
  - init.history
  - init.audit
  - init.sysctl
  - init.epel
  - init.zabbix_agent

對初始化環境sls文件進行分析(根據eve_init.sls中的位置依次查看)

[root@k8s_master init]# cat dns.sls 
/etc/resolv.conf:
  file.managed:
    - source: salt://init/files/resolv.conf      #表示將source所指目錄下的文件覆蓋到/etc/resolv.conf
    - user: root
    - gourp: root
    - mode: 644

#其他用法
[root@k8s_master salt] cat test.sls
test:
file.managed:
- name: /tmp/aaa.txt        
- source: salt://top.sls              #表示將source目錄下文件覆蓋到/tmp/目錄下並命名為aaa.txt(此處不能直接寫目錄名稱)
[root@k8s_master init]# cat history.sls 
/etc/profile:
  file.append:
    - text:
      - export HISTTIMEFORMAT="%F %T `whoami` "

[root@k8s_master init]# cat audit.sls 
/etc/bashrc:
  file.append:
    - text:
      - export PROMPT_COMMAND=‘{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }‘

[root@k8s_master init]# cat sysctl.sls 
net.ipv4.ip_local_port_range:
  sysctl.present:
    - value: 10000 65000
fs.file-max:
  sysctl.present:
    - value: 2000000
net.ipv4.ip_forward:
  sysctl.present:
    - value: 1
vm.swappiness:
  sysctl.present:
    - value: 0

[root@k8s_master init]# cat epel.sls 
yum_repo_release:
  pkg.installed:
    - sources:
      - epel-release: http://mirrors.aliyun.com/epel/6/x86_64/epel-release-6-8.noarch.rpm
    - unless: rpm -qa | grep epel-release-6-8

[root@k8s_master init]# cat zabbix_agent.sls 
zabbix-agent:
  pkg.installed:
    - name: zabbix22-agent
  file.managed:
    - name: /etc/zabbix_agentd.conf
    - source: salt://init/files/zabbix_agentd.conf
    - template: jinja
    - defaults:
      Server: {{ pillar[‘zabbix-agent‘][‘Zabbix_Server‘] }}
    - require:
      - pkg: zabbix-agent
  service.running:
    - enable: True
    - watch:
      - pkg: zabbix-agent
      - file: zabbix-agent
zabbix_agentd.conf.d:
  file.directory:
    - name: /etc/zabbix_agentd.conf.d
    - watch_in:
      - service: zabbix-agent
    - require:
      - pkg: zabbix-agent
      - file: zabbix-agent

  

  

  

saltstack SLS 實例分析學習