自動化運維之saltstack

概述

saltstack是一個服務器基礎設施管理工具,它具備配置管理、遠程執行、監控等功能。saltstack由python語言編寫,是很是簡單易用和輕量級的管理工具。node

saltstack原理

saltstack由master和minion構成,Master是服務端,表示一臺服務器;Minion是客戶服務端,表示多臺服務器。在Master上發送命令給符合條件的Minion,Minion就會執行相應的命令,Master和Minion之間經過ZeroMQ(消息隊列)進行通訊的。python

SaltStack經常使用模塊

  • pkg模塊:包管理,包括增刪更新。
  • file模塊:管理文件操做,包括同步文件、設置文件權限和所屬用戶組、刪除文件等操做。
  • cmd模塊:在Minion上執行命令或者腳本。
  • user模塊:管理系統帳號操做。
  • service模塊:管理系統服務操做。
  • cron模塊:管理cron服務操做。

SaltStack批量部署並配置Apache

部署環境

三臺服務器的部署參數如表所示:
自動化運維之saltstackweb

操做步驟

1.SaltStack安裝apache

1)設置三臺服務器的名稱和hosts文件,重啓服務器便於系統識別。vim

master:
[root@master salt]# vim /etc/hostname 
master.saltstack.com
[root@master salt]# vim /etc/hosts
192.168.126.138 master.saltstack.com
192.168.126.147 web01.saltstack.com
192.168.126.157 web02.saltstack.com

minion1:
[root@web01 ~]# vim /etc/hostname 
web01.saltstack.com
[root@web01 ~]# vim /etc/hosts
192.168.126.138 master.saltstack.com
192.168.126.147 web01.saltstack.com
192.168.126.157 web02.saltstack.com

minion2:
[root@web01 ~]# vim /etc/hostname 
web02.saltstack.com
[root@web01 ~]# vim /etc/hosts
192.168.126.138 master.saltstack.com
192.168.126.147 web01.saltstack.com
192.168.126.157 web02.saltstack.com
~

2)三臺服務上都須要安裝epel源centos

[root@master salt]# yum install epel-release -y

3)在主控端(master)上安裝saltstack軟件。api

[root@master salt]# yum install salt-master -y

4)配置主控端文件/etc/salt/master。服務器

[root@master salt]# vim /etc/salt/master 
interface: 192.168.126.138          #15行      /監聽地址本地地址

auto_accept: True    #215行      /自動認證被控端的認證

file_roots:          #416行       /saltstack文件根目錄位置,注意這個目錄默認是沒有的,須要建立。
  base:
    - /srv/salt

pillar_roots:      #529行       /修改pillar的主目錄,須要建立。
  base:
    - /srv/pillar

pillar_opts: True    #552行     /開啓pillar功能

nodegroups:          #710行       /組的分類
  group1: 'web01.saltstack.com'
  group2: 'web02.saltstack.com'

5)查看主控端修改的內容運維

[root@master ~]#  cat /etc/salt/master | grep -v ^$ | grep -v ^#
interface: 192.168.126.138
auto_accept: True
file_roots:
  base:
    - /srv/salt
pillar_roots:
  base:
    - /srv/pillar
pillar_opts: True
nodegroups:
  group1: 'web01.saltstack.com'
  group2: 'web02.saltstack.com'

6)建立salt根目錄及pillar目錄tcp

[root@master ~]# mkdir /srv/salt  
[root@master ~]# mkdir /srv/pillar

7)開啓salt-master服務並查看4505端口和4506端口是否開啓

[root@master ~]#systemctl stop firewalld.service
[root@master ~]#setenforce 0
[root@master ~]#systemctl start salt-master.service
[root@master ~]# netstat -ntap | egrep '4505|4506'
tcp        0      0 192.168.126.138:4505    0.0.0.0:*               LISTEN      5918/python         
tcp        0      0 192.168.126.138:4506    0.0.0.0:*               LISTEN      5936/python

8)在兩臺被控端(minion)上安裝saltstack軟件

[root@web01 ~]#yum install salt-minion -y

9)配置兩臺被控端配置文件/etc/salt/minion

[root@web01 ~]#vim /etc/salt/minion   
master: 192.168.126.138      #16行     /指定主控端IP

id: web01.saltstack.com      #78行     /指定被控主機名

10)分別啓動兩臺被控端服務

[root@web01 ~]#systemctl stop firewalld.service
[root@web01 ~]#setenforce 0
[root@web01 ~]#systemctl start salt-minion.service

11)測試主控端與被控端的通訊狀態

[root@master ~]# salt '*' test.ping 
web01.saltstack.com:
    True
web02.saltstack.com:
    True

2.saltstack批量部署Apache

1)修改配置文件/etc/salt/master

[root@master ~]# vim /etc/salt/master
file_roots:
   base:
       - /srv/salt/

注意:環境:base、dev(開發環境)、test(測試環境)、prod(生成環境)

2)建立top.sls文件並寫入如下內容

[root@master ~]# vim /srv/salt/top.sls

base:
  '*':        #表示在全部的客戶端執行apache模塊
   - apache
~

3)建立apache.sls文件並寫入如下內容

[root@master ~]# vim /srv/salt/apache.sls

apache-service:
 pkg.installed:
   - names:
     - httpd
     - httpd-devel
 service.running:
   - name: httpd
   - enable: True

4)重啓salt-master服務

[root@master ~]#systemctl restart salt-master

5)刷新state配置命令,讓兩臺被控端去執行安裝apache並配置。

[root@master salt]# salt '*' state.highstate
web02.saltstack.com:
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 15:41:54.228461
    Duration: 157517.576 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.4.8-3.el7_4.1
                  old:
              apr-util:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              httpd:
                  ----------
                  new:
                      2.4.6-80.el7.centos.1
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.6-80.el7.centos.1
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.41-2.el7
                  old:
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd-devel
      Result: True
     Comment: The following packages were installed/updated: httpd-devel
     Started: 15:44:31.928768
    Duration: 61210.573 ms
     Changes:   
              ----------
              apr-devel:
                  ----------
                  new:
                      1.4.8-3.el7_4.1
                  old:
              apr-util-devel:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              cyrus-sasl:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-devel:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
              cyrus-sasl-gssapi:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-lib:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-md5:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-plain:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-scram:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              expat-devel:
                  ----------
                  new:
                      2.1.0-10.el7_3
                  old:
              httpd-devel:
                  ----------
                  new:
                      2.4.6-80.el7.centos.1
                  old:
              libdb:
                  ----------
                  new:
                      5.3.21-24.el7
                  old:
                      5.3.21-20.el7
              libdb-devel:
                  ----------
                  new:
                      5.3.21-24.el7
                  old:
              libdb-utils:
                  ----------
                  new:
                      5.3.21-24.el7
                  old:
                      5.3.21-20.el7
              openldap:
                  ----------
                  new:
                      2.4.44-15.el7_5
                  old:
                      2.4.44-5.el7
              openldap-devel:
                  ----------
                  new:
                      2.4.44-15.el7_5
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 15:45:33.717897
    Duration: 4787.005 ms
     Changes:   
              ----------
              httpd:
                  True

Summary
------------
Succeeded: 3 (changed=3)
Failed:    0
------------
Total states run:     3
web01.saltstack.com:
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 15:41:54.383424
    Duration: 157125.605 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.4.8-3.el7_4.1
                  old:
              apr-util:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              httpd:
                  ----------
                  new:
                      2.4.6-80.el7.centos.1
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.6-80.el7.centos.1
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.41-2.el7
                  old:
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd-devel
      Result: True
     Comment: The following packages were installed/updated: httpd-devel
     Started: 15:44:31.684541
    Duration: 63010.684 ms
     Changes:   
              ----------
              apr-devel:
                  ----------
                  new:
                      1.4.8-3.el7_4.1
                  old:
              apr-util-devel:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              cyrus-sasl:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-devel:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
              cyrus-sasl-gssapi:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-lib:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-md5:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-plain:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              cyrus-sasl-scram:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
                      2.1.26-21.el7
              expat-devel:
                  ----------
                  new:
                      2.1.0-10.el7_3
                  old:
              httpd-devel:
                  ----------
                  new:
                      2.4.6-80.el7.centos.1
                  old:
              libdb:
                  ----------
                  new:
                      5.3.21-24.el7
                  old:
                      5.3.21-20.el7
              libdb-devel:
                  ----------
                  new:
                      5.3.21-24.el7
                  old:
              libdb-utils:
                  ----------
                  new:
                      5.3.21-24.el7
                  old:
                      5.3.21-20.el7
              openldap:
                  ----------
                  new:
                      2.4.44-15.el7_5
                  old:
                      2.4.44-5.el7
              openldap-devel:
                  ----------
                  new:
                      2.4.44-15.el7_5
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 15:45:35.659786
    Duration: 4142.607 ms
     Changes:   
              ----------
              httpd:
                  True

Summary
------------
Succeeded: 3 (changed=3)
Failed:    0
------------
Total states run:     3
經過執行結果看到了三個ID,它們至關於三個任務,第一個安裝,第二個配置,第三個啓動。並且顯示三個都成功了,失敗爲零。

6)查看被控端httpd是否安裝80端口是否開啓

[root@web02 ~]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64
[root@web02 ~]# netstat -ntap | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      4550/httpd 

[root@web01 ~]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64
[root@web01 ~]# netstat -ntap | grep 80
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1280/dnsmasq        
tcp6       0      0 :::80                   :::*                    LISTEN      4688/httpd

部署成功

相關文章
相關標籤/搜索