1. 程式人生 > >使用SaltStack部署服務

使用SaltStack部署服務

tar red htm 服務 fun tac php.ini 方法 nod

實驗目標

使用SaltStack部署apache和php

修改master的配置文件,指定base環境路徑,base環境是必須指定的

[[email protected] base]# grep  -9  ^file_roots /etc/salt/master  |grep -v ^#
file_roots:
  base:
    - /srv/salt/base
  dev:
    - /srv/salt/dev
  test:
    - /srv/salt/test
  prod:
    - /srv/salt/prod

創建目錄

[[email protected] base]# mkdir
-p /srv/salt/{base,dev,test,prod} [[email protected]-node1 base]# tree /srv/salt/ /srv/salt/ ├── base ├── dev ├── prod └── test

重啟master

[[email protected] base]# systemctl restart salt-master

在base目錄下面創建一個web目錄用於存放web相關的sls文件

[[email protected] base]# mkdir -p web

cd到bash/web目錄裏面創建apache.sls文件

[[email protected] base]# cd web/
[[email protected]-node1 web]# cat apache.sls 
apache-install:   #id 名字自己取 需要形象一點, 一個id下面一個狀態只能出現一次
  pkg.installed:  #pkg 是狀態模塊,installed 是模塊裏面的方法
    - name: httpd #方法裏面的參數
apache-service:
  service.running:
    - name: httpd
    - enable: True #設置開機自動啟動
#yaml裏面格式有嚴格的要求,註釋用#號,不能有table,- 兩邊需要空格,縮進用2個空格層級關系後面要加分號

執行狀態模塊部署服務

[[email protected] base]# salt "linux-node2*" state.sls apache
linux-node2.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 14:58:09.228934
    Duration: 633.681 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is running
     Started: 14:58:09.863302
    Duration: 310.567 ms
     Changes:   
              ----------
              httpd:
                  True

Summary
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
#此時node2 上面已經部署好了apache

高級狀態的使用 需要在master配置文件裏面打開 state_top: top.sls並重啟master

[[email protected] web]# grep -n ^state_top /etc/salt/master
329:state_top: top.sls
[[email protected] web]# systemctl restart salt-master

在bese環境目錄下面添加top.sls

[[email protected] base]# more top.sls 
base:
  linux-node2.example.com:
    - web.apache
  linux-node1.example.com:
    - web.apache
[[email protected]-node1 base]# pwd
/srv/salt/base

執行高級模塊方法,高級方法到 base下面找top.sls 文件編排告訴每個minion需要幹什麽

一般生產環境用高級狀態多些
[[email protected] base]#   salt "*" state.highstate 
linux-node1.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 15:23:08.597951
    Duration: 709.521 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 15:23:09.308417
    Duration: 233.623 ms
     Changes:   

Summary
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
linux-node2.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 15:23:09.171596
    Duration: 721.901 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 15:23:09.894209
    Duration: 221.615 ms
     Changes:   

Summary
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2

上面我們使用了2個狀態模塊pkg和service,下面我們使用file文件配置模塊

模塊使用參考文檔

https://www.unixhot.com/docs/saltstack/ref/states/all/salt.states.file.html#module-salt.states.file 

在base/web目錄下面添加一個lamp.sls,

一般在添加裏面的內容之前需要在外面找一臺服務器進行測試拿到準確的包信息後再進行配置

[[email protected] web]# cat lamp.sls
lamp-install:
pkg.installed:
- pkgs:
- httpd
- php
- php-pdo
- php-mysql

apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf #服務實際使用的文件路徑
- source: salt://web/files/httpd.conf #salt的源文件用於分發到minion上面 路徑是base目錄下面的web 這裏也支持http和ftp方式
- user: root
- group: root
- mode: 644

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

lamp-service:
service.running:
- name: httpd
- enable: True

拷貝源文件到base/web目錄下,這個根據自己的實際情況找源文件拷貝過來

[[email protected] web]# cp /etc/httpd/conf/httpd.conf /srv/salt/base/web/files/
[[email protected] web]# cp /etc/php.ini /srv/salt/base/web/files/

執行狀態模塊部署服務

[[email protected] web]# salt "*" state.sls web.lamp
linux-node1.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 15:43:56.883540
    Duration: 633.814 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 15:43:57.520199
    Duration: 4.242 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 15:43:57.524589
    Duration: 4.149 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 15:43:57.529404
    Duration: 258.952 ms
     Changes:   

Summary
------------
Succeeded: 4
Failed:    0
------------
Total states run:     4
linux-node2.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 15:43:58.566172
    Duration: 611.409 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 15:43:59.180091
    Duration: 4.063 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 15:43:59.184248
    Duration: 3.803 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 15:43:59.188496
    Duration: 208.1 ms
     Changes:   

Summary
------------
Succeeded: 4
Failed:    0
------------
Total states run:     4

使用SaltStack部署服務