saltstack的高級管理

1、saltstack的狀態管理php

狀態管理官網: https://www.unixhot.com/docs/saltstack/ref/states/all/index.htmlhtml

1)狀態分析node

[root@k8s6 ~]# cat /srv/salt/web/apache.sls 
apache-install:    
  pkg.installed:
    - names:
      - httpd
      - httpd-devel

apache-service:        # 名稱ID聲明 ID必須惟一
  service.running:    # state聲明 狀態聲明
    - name: httpd    # 選項聲明
    - enable: True

 2.1) 對於lamp 架構狀態管理分析mysql

LAMP架構
1)安裝軟件包        pkg
2)修改配置文件        file
3)啓動服務            service
pkg.installed     # 安裝
pkg.latest        # 確保最新版本
pkg.remove        # 卸載
pkg.purge         # 卸載並刪除配置文件

2.2)安裝軟件示例和配置文件定義示例git

#示例:同時安裝多個包
common_packages:
  pkg.installed:
    - pkgs:
      - unzip
      - dos2unix
      - salt-minion: 2015.8.5-1.el6
  
#示例:修改配置文件
apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://files/http.conf
    - user: root
    - group: root
    - mode: 644
View Code

2.3)對於配置文件定義的source源分析github

- source: salt://files/http.conf  指的salt環境的下的目錄
salt:// 表示 當前環境的跟目錄
salt跟目錄定義
[root@k8s6 lamp]# vim /etc/salt/master
file_roots:
  base:
    - /srv/salt

    好比 salt://lamp/files/http.conf 表示 /srv/salt/lamp/files/http.confweb

3)定義lamp架構示例sql

[root@k8s6 salt]# cat lamp/lamp.sls 
lamp-pkg:
  pkg.installed:
  - pkgs:
    - httpd
    - php
    - mariadb
    - mariadb-server
    - php-mysql
    - php-cli
    - php-mbstring

apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
      - source: salt://lamp/files/httpd.conf
      - user: root
      - group: root
      - mode: 644

php-config:
  file.managed:
    - name: /etc/php.ini
      - source: salt://lamp/files/php.ini
      - user: root
      - group: root
      - mode: 644

mysql-config:
  file.managed:
    - name: /etc/my.cnf
    - source: salt://lamp/files/my.cnf
    - user: root
    - group: root
    - mode: 644

apache-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True

mysql-service:
  service.running:
    - name: mariadb
    - enable: True
    - reload: True
cat lamp/lamp.sls

配置文件路徑apache

[root@k8s6 salt]# ls lamp/files/
httpd.conf  my.cnf  php.ini

單臺機器啓動測試vim

# 單臺機器測試 lamp.lamp ==》目錄/文件
[root@k8s6 lamp]# salt 'node01' state.sls lamp.lamp

執行的時候:須要監聽客戶端日誌,排查錯誤
tail -f /var/log/salt/minion

服務端日誌改成debug模式。排查錯誤
vim /etc/salt/master
#log_level: warning
log_level: debug

 4)對於啓動服務的另外一種寫法

[root@k8s6 salt]# cat lamp/apache.sls 
apache-server:
  pkg.installed:
    - pkgs:
      - httpd
      - php
    
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
      - source: salt://lamp/files/httpd.conf
      - user: root
      - group: root
      - mode: 644

  service.running:
    - name: httpd
    - enable: True
    - reload: True
View Code

 2、狀態關係

1)依賴關係require。服務中,只寫一個

服務服務的依賴關係
apache-server:        # 啓動服務須要有依賴關係
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - require:        # 依賴關係
      - pkg: lamp-pkg    # 須要先安裝
      - file: apache-config        # 須要有配置文件

mysql-config:    # 配置文件被啓動服務所依賴
  file.managed:
    - name: /etc/my.cnf
    - source: salt://lamp/files/my.cnf
    - user: root
    - group: root
    - mode: 644
    - require_in:    # 被依賴,被啓動服務所依賴
      - service: mysql-service
View Code

 2)監聽文件。watch

服務服務的依賴關係
apache-server:        # 啓動服務須要有依賴關係
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - require:        # 依賴關係
      - pkg: lamp-pkg    # 須要先安裝
    - watch
      - file: apache-config        # 監聽該文件
View Code

 3.1)引入實例

提早安裝的文件

[root@k8s6 lamp]# cat pkg.sls 
lamp-pkg:
  pkg.installed:
  - pkgs:
    - httpd
    - php
    - mariadb
    - mariadb-server
    - php-mysql
    - php-cli
    - php-mbstring
pkg.sls

導入pkg

[root@k8s6 lamp]# cat lamp.sls 
include:
  - lamp.pkg
  
apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
      - source: salt://lamp/files/httpd.conf
      - user: root
      - group: root
      - mode: 644

php-config:
  file.managed:
    - name: /etc/php.ini
      - source: salt://lamp/files/php.ini
      - user: root
      - group: root
      - mode: 644

mysql-config:
  file.managed:
    - name: /etc/my.cnf
    - source: salt://lamp/files/my.cnf
    - user: root
    - group: root
    - mode: 644

apache-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True

mysql-service:
  service.running:
    - name: mariadb
    - enable: True
    - reload: True
include

3.2)引入多個文件

 安裝模塊

[root@k8s6 lamp]# cat pkg.sls 
lamp-pkg:
  pkg.installed:
  - pkgs:
    - httpd
    - php
    - mariadb
    - mariadb-server
    - php-mysql
    - php-cli
    - php-mbstring
pkg.sls

配置文件模塊

[root@k8s6 lamp]# cat config.sls 
apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
      - source: salt://lamp/files/httpd.conf
      - user: root
      - group: root
      - mode: 644

php-config:
  file.managed:
    - name: /etc/php.ini
      - source: salt://lamp/files/php.ini
      - user: root
      - group: root
      - mode: 644

mysql-config:
  file.managed:
    - name: /etc/my.cnf
    - source: salt://lamp/files/my.cnf
    - user: root
    - group: root
    - mode: 644
config.sls

啓動服務模塊

[root@k8s6 lamp]# cat service.sls 
apache-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True

mysql-service:
  service.running:
    - name: mariadb
    - enable: True
    - reload: Tru
service.sls

導入模塊

[root@k8s6 lamp]# cat init.sls 
include:
  - lamp.pkg
  - lamp.config
  - lamp.service

文件目錄關係圖

[root@k8s6 salt]# tree /srv/salt/lamp/
/srv/salt/lamp/
├── config.sls
├── files
│   ├── httpd.conf
│   ├── my.c
│   └── php.ini
├── init.sls
├── pkg.sls
└── service.sls

啓動服務

salt 'node1' state.sls lamp.init

 四、jinja模板的使用

4.1)先在sls文件中定於使用jinja模板。並定於變量

[root@k8s6 lamp]# cat config.sls 
apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
      - source: salt://lamp/files/httpd.conf
      - user: root
      - group: root
      - mode: 644
      - template: jinja
      - defaults:
        PORT: 88

...............................
View Code

4.2) 在引用的配置文件中寫入jinja模板

[root@k8s6 lamp]# cat files/httpd.conf 
......
Listen {{ PORT }}
.....
Listen {{ PORT }}

salt 'node1' state.sls lamp.init  測試

 5)擴展

5.1)引用salt默認的模塊。{{ grains['fgdn_ip4'][0] }}  引入salt默認執行的結果

[root@k8s6 lamp]# cat files/httpd.conf 
Listen {{ grains['fgdn_ip4'][0] }}:{{ PORT }}

5.2)grains 也可寫在sls的配置文件中

[root@k8s6 lamp]# cat config.sls 
apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
      - source: salt://lamp/files/httpd.conf
      ..........
      - template: jinja
      - defaults:
        IPADDR: {{ {{ grains['fgdn_ip4'][0] }} }}
        PORT: 88

httpd.conf文件引用

[root@k8s6 lamp]# cat files/httpd.conf 
Listen {{ IPADDR }}:{{ PORT }

5.3)salt遠程執行模塊

{{ salt['network.hw_addr']('ens33') }}   # 寫入配置文件模板
[root@k8s6 lamp]# salt 'node01' network.hw_addr ens33
node01:
    00:0c:29:f7:16:c5

 5.4)pillar 值獲取

[root@k8s6 web]# salt '*' pillar.items
k8s6:
    ----------
    apache:
        httpd
node01:
    ----------
可寫jinja模板
{{ pillar['apache'] }}

 3、企業用法

案例:https://github.com/unixhot/saltbook-code/
base 基礎環境
[root@k8s6 lamp]# vim /etc/salt/master
file_roots:
  base:
    - /srv/salt/base
  prod:
    - /srv/salt/prod

pillar_roots:
  base:
    - /srv/pillar/base
  prod:
    - /srv/pillar/prod
    
[root@k8s6 salt]# mkdir /srv/salt/base
[root@k8s6 salt]# mkdir /srv/salt/prod
[root@k8s6 salt]# mkdir /srv/pillar/base
[root@k8s6 salt]# mkdir /srv/pillar/prod
[root@k8s6 lamp]# systemctl restart salt-master

1)base基礎環境
init目錄,環境初始化:
    1、dns配置
    2、history記錄時間
    3、記錄命令操做
    4、內核參數優化
    5、安裝yum倉庫
    6、安裝zabbix-agent
View Code
相關文章
相關標籤/搜索