1. 程式人生 > >自動化運維saltstack 安裝部署以及一鍵部署實現負載均衡

自動化運維saltstack 安裝部署以及一鍵部署實現負載均衡

saltstack介紹

SaltStack是一個伺服器基礎架構集中化管理平臺,SaltStack基於Python語言實現,也是基於C/S架構,結合輕量級訊息佇列(ZeroMQ)與Python第三方模組(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)構建,SaltStack整合了Puppet和Chef的功能,更適合大規模批量管理伺服器,配置簡單。
SaltStack工作原理
這裡寫圖片描述
saltstack由Master和Minion構成,通過ZeroMQ進行通訊。
Saltstack的master端監聽4505與4506埠,4505為salt的訊息釋出系統,4506為salt客戶端與服務端通訊的埠,salt客戶端程式不監聽埠,SaltStack 客戶端(Minion)在啟動時,會自動生成一套金鑰,包含私鑰和公鑰。之後將公鑰傳送給伺服器端,伺服器端驗證並接受公鑰,以此來建立可靠且加密的通訊連線。然後一直保持該TCP連線,master通過這條TCP連線對客戶端控制,如果連線斷開,master對客戶端就無能為力了。當然,客戶端若檢查到斷開後會定期的一直連線master端的。

實驗環境:redhat6.5,防火牆和selinux狀態為關閉狀態;
server1 172.25.60.1 salt-master
server2 172.25.60.2 salt-minion
server3 172.25.60.3 salt-minion
server4 172.25.60.4 salt-minion

配置yum源

[[email protected] rhel6]$ pwd
/var/www/html/salt/rhel6
###安裝saltstack軟體配置yum源需要使用的包###
[[email protected] rhel6]$ ls libyaml-0.1.3-4.el6.x86_64.rpm python-babel-0.9.4-5.1.el6.noarch.rpm python-backports-1.0-5.el6.x86_64.rpm python-backports-ssl_match_hostname-3.4.0.2-2.el6.noarch.rpm python-chardet-2.2.1-1.el6.noarch.rpm python-cherrypy-3.2.2-4.el6.noarch.rpm python-crypto-2.6.1-3.el6.x
86_64.rpm python-crypto-debuginfo-2.6.1-3.el6.x86_64.rpm python-enum34-1.0-4.el6.noarch.rpm python-futures-3.0.3-1.el6.noarch.rpm python-impacket-0.9.14-1.el6.noarch.rpm python-jinja2-2.8.1-1.el6.noarch.rpm python-msgpack-0.4.6-1.el6.x86_64.rpm python-ordereddict-1.1-2.el6.noarch.rpm python-requests-2.6.0-3.el6.noarch.rpm python-setproctitle-1.1.7-2.el6.x86_64.rpm python-six-1.9.0-2.el6.noarch.rpm python-tornado-4.2.1-1.el6.x86_64.rpm python-urllib3-1.10.2-1.el6.noarch.rpm python-zmq-14.5.0-2.el6.x86_64.rpm PyYAML-3.11-1.el6.x86_64.rpm repodata salt-2016.11.3-1.el6.noarch.rpm salt-api-2016.11.3-1.el6.noarch.rpm salt-cloud-2016.11.3-1.el6.noarch.rpm salt-master-2016.11.3-1.el6.noarch.rpm salt-minion-2016.11.3-1.el6.noarch.rpm salt-ssh-2016.11.3-1.el6.noarch.rpm salt-syndic-2016.11.3-1.el6.noarch.rpm zeromq-4.0.5-4.el6.x86_64.rpm [[email protected] ~]# cat /etc/yum.repos.d/rhel-source.repo [rhel-source] name=Red Hat Enterprise Linux $releasever - $basearch - Source baseurl=http://172.25.60.250/rhel6.5 enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release [LoadBalancer] baseurl=http://172.25.60.250/rhel6.5/LoadBalancer gpgcheck=0 [HighAvailability] name=HighAvailability baseurl=http://172.25.60.250/rhel6.5/HighAvailability gpgcheck=0 [ResilientStorage] name=ResilientStorage baseurl=http://172.25.60.250/rhel6.5/ResilientStorage gpgcheck=0 [ScalableFileSystem] name=ScalableFileSystem baseurl=http://172.25.60.250/rhel6.5/ScalableFileSystem gpgcheck=0 [salt] name=salt baseurl=http://172.25.60.250/salt/rhel6 gpgcheck=0 [[email protected] ~]# yum clean all [[email protected] ~]# yum repolist ###如果yum源配置號以後會出現以下東西### [[email protected] salt]# yum list salt-* Loaded plugins: product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Installed Packages salt.noarch 2016.11.3-1.el6 @salt salt-master.noarch 2016.11.3-1.el6 @salt Available Packages salt-api.noarch 2016.11.3-1.el6 salt salt-cloud.noarch 2016.11.3-1.el6 salt salt-minion.noarch 2016.11.3-1.el6 salt salt-ssh.noarch 2016.11.3-1.el6 salt salt-syndic.noarch 2016.11.3-1.el6 salt

saltstack安裝部署

server1安裝salit-master:
[[email protected] ~]# yum install -y salt-master
server2和server3安裝salt-minion:
[[email protected] yum.repos.d]# yum install -y salt-minion
[[email protected] yum.repos.d]# yum install -y salt-minion

server1修改配置檔案
[[email protected] ~]# cd /etc/salt/
[[email protected] salt]# ls
cloud           cloud.maps.d       master    minion.d  proxy.d
cloud.conf.d    cloud.profiles.d   master.d  pki       roster
cloud.deploy.d  cloud.providers.d  minion    proxy
[[email protected] salt]# vim master
15 interface: 172.25.60.1
server2和server3修改配置檔案:
[[email protected] yum.repos.d]# cd /etc/salt/
[[email protected] salt]# ls
cloud           cloud.maps.d       master    minion.d   proxy
cloud.conf.d    cloud.profiles.d   master.d  minion_id  proxy.d
cloud.deploy.d  cloud.providers.d  minion    pki        roster
[[email protected] salt]# vim minion
16 master: 172.25.60.1

每個虛擬機器啟動服務:
[[email protected] ~]# /etc/init.d/salt-master start
Starting salt-master daemon:                               [  OK  ]
[[email protected] ~]# /etc/init.d/salt-minion start
Starting salt-minion:root:server2 daemon: OK
[[email protected] ~]# /etc/init.d/salt-minion start
Starting salt-minion:root:server2 daemon: OK

檢視服務是否啟動:
[[email protected] ~]# netstat -antplue
tcp        0      0 172.25.60.1:4505            0.0.0.0:*                   LISTEN      0          8559       996/python2.6 
tcp        0      0 172.25.60.1:4506            0.0.0.0:*                   LISTEN      0          14279      1549/python2.6      

將server2和server3新增到server1的認證salt-key裡面:
[[email protected] salt]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
server2
server3
Rejected Keys:
[[email protected] salt]# salt-key -a server2
The following keys are going to be accepted:
Unaccepted Keys:
server2
Proceed? [n/Y] y
Key for minion server2 accepted.
[[email protected] salt]# salt-key -a server3
The following keys are going to be accepted:
Unaccepted Keys:
server3
Proceed? [n/Y] y
Key for minion server3 accepted.
[[email protected] salt]# salt-key -L
Accepted Keys:
server2
server3
Denied Keys:
Unaccepted Keys:
Rejected Keys:

檢視server1的salt-minion情況:
[[email protected] ~]# yum install -y tree
[[email protected] pki]# pwd
/etc/salt/pki
[[email protected] pki]# tree .
.
|-- master
|   |-- master.pem
|   |-- master.pub
|   |-- minions
|   |   |-- server2
|   |   `-- server3
|   |-- minions_autosign
|   |-- minions_denied
|   |-- minions_pre
|   `-- minions_rejected
`-- minion
7 directories, 4 files

檢視server2和server3的salt-minion情況:
[[email protected] pki]# pwd
/etc/salt/pki
[[email protected] pki]# tree .
.
|-- master
`-- minion
    |-- minion_master.pub
    |-- minion.pem
    `-- minion.pub

2 directories, 3 files

saltstack簡單模組命令測試

[root@server1 ~]# salt  '*'  test.ping
server3:
    True
server2:
    True
[root@server1 ~]# salt '*' cmd.run hostname
server3:
    server3
server2:
    server2
[root@server1 ~]# salt '*' cmd.run 'df -Th'
server3:
    Filesystem                   Type   Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root ext4    19G  967M   17G   6% /
    tmpfs                        tmpfs  499M   16K  499M   1% /dev/shm
    /dev/vda1                    ext4   485M   33M  427M   8% /boot
server2:
    Filesystem                   Type   Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root ext4    19G  967M   17G   6% /
    tmpfs                        tmpfs  499M   16K  499M   1% /dev/shm
    /dev/vda1                    ext4   485M   33M  427M   8% /boot

建立salt-master的工作目錄

修改配置檔案:
[root@server1 ~]# vim /etc/salt/master
 524 file_roots:
 525    base:
 526      - /srv/salt/
重新啟動服務:
[root@server1 salt]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
建立工作主目錄:
[root@server1 ~]# mkdir /srv/salt

server1為server2自動化部署http服務

[[email protected] ~]# cd /srv/salt/
[[email protected] salt]# mkdir httpd
[[email protected] salt]# cd httpd/
###編寫http安裝服務sls檔案###
[[email protected] httpd]# vim apache.sls
apache-install:
  pkg.installed:
    - pkgs:
      - httpd
      - php

###將該檔案推送給server2執行安裝檔案###
(httpd.apache中httpd表示httpd目錄,apache表示我們編輯的檔案)
[[email protected] httpd]# salt server2 state.sls httpd.apache test=True 
###上面命令表示執行測試,不進行安裝###
[[email protected] httpd]# salt server2 state.sls httpd.apache
server2:
----------
          ID: apache-install
    Function: pkg.installed
      Result: True
     Comment: The following packages were installed/updated: httpd, php
     Started: 13:58:44.762661
    Duration: 20169.999 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.3.9-5.el6_2
                  old:
              apr-util:
                  ----------
                  new:
                      1.3.9-3.el6_0.1
                  old:
              apr-util-ldap:
                  ----------
                  new:
                      1.3.9-3.el6_0.1
                  old:
              httpd:
                  ----------
                  new:
                      2.2.15-29.el6_4
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.2.15-29.el6_4
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.31-2.el6
                  old:
              php:
                  ----------
                  new:
                      5.3.3-26.el6
                  old:
              php-cli:
                  ----------
                  new:
                      5.3.3-26.el6
                  old:
              php-common:
                  ----------
                  new:
                      5.3.3-26.el6
                  old:

Summary for server2
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  20.170 s
###上面的配置檔案也可以修改為###
httpd:
  pkg.installed 

php:
  pkg.installed

###再次執行該命令也可以測試apache是否安裝成功###
[[email protected] httpd]# salt server2 state.sls httpd.apache
server2:
----------
          ID: apache-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed   ###測試的安裝包已經安裝
     Started: 11:06:23.404093
    Duration: 278.786 ms
     Changes:   

Summary for server2
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 278.786 ms

修改server2的httpd服務為8080埠:

[[email protected] ~]# mkdir /srv/salt/httpd/file 
###將server2的配置檔案傳送到server1的新建目錄###
[[email protected] conf.d]# scp /etc/httpd/conf/httpd.conf 172.25.60.1:/srv/salt/httpd/file
###修改httpd.conf檔案的80埠為8080埠###
[[email protected] ~]# vim /srv/salt/httpd/file/httpd.conf
 136 Listen 8080
 ###修改server1的apache的安裝檔案###
[[email protected] file]# vim /srv/salt/httpd/apache.sls 
[[email protected] file]# cat /srv/salt/httpd/apache.sls 
apache-install:
  pkg.installed:
    - pkgs:
      - httpd
      - php

apapche-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf    #####傳送到server2的配置檔案,檔名和路徑需要保持一直,
    - source: salt://httpd/file/httpd.conf  #####該路徑是載入(更改8080埠操作)檔案的路徑
指定httpd.conf檔案路徑,推送時該路徑下檔案將被髮送到server2的httpd配置檔案下,httpd啟用該檔案修改埠為8080  

apache-service:   ###設定httpd服務開機自啟
  service.running:
    - name:  httpd
    - enable: True   
[[email protected] file]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
測試:
[[email protected] httpd]# salt server2  state.sls httpd.apache
server2:
----------
          ID: apache-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 14:16:47.766538
    Duration: 271.519 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf updated
     Started: 14:16:48.039457
    Duration: 44.548 ms
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -133,7 +133,7 @@
                   # prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
                   #
                   #Listen 12.34.56.78:80
                  -Listen 80
                  +Listen 8080

                   #
                   # Dynamic Shared Object (DSO) Support
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 14:16:48.084378
    Duration: 109.853 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for server2
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3
Total run time: 425.920 ms

進入server2檢視是否修改成功:
[[email protected] ~]# netstat -antplue
tcp        0      0 :::8080                     :::*                        LISTEN      0          16662      1996/httpd                    

給server2的httpd服務設定相關的使用者和組

修改配置檔案:
[[email protected] file]# vim /srv/salt/httpd/apache.sls 
apache-install:
  pkg.installed:
    - pkgs:
      - httpd
      - php

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

apache-service:
  service.running:
    - name:  httpd
    - enable: True
    - reload: True  ###檔案更改後進行重新載入reload
    - watch:  ####監控檔案,修改更改的檔案
      - file: /etc/httpd/conf/httpd.conf
給server2新增使用者和使用者組

saltstack一鍵部署(server3原始碼編譯安裝 nginx)

1.原始碼編譯安裝nginx

[[email protected] nginx]# pwd
/srv/salt/nginx
[[email protected] nginx]# cat install.sls 
nginx-install:
  pkg.installed:
    - pkgs:
      - gcc
      - openssl-devel
      - pcre-devel

  file.managed:
    - name: /mnt/nginx-1.10.1.tar.gz     ###指定tar包放置
    - source: salt://nginx/nginx-1.10.1.tar.gz    ###指定tar包

  cmd.run:
    - name: cd /mnt && tar zxf nginx-1.10.1.tar.gz && cd nginx-1.10.1 && sed -i.bak 's/define NGINX_VER          "nginx\/" NGINX_VERSION/define NGINX_VER          "nginx"'/g src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-threads --with-http_ssl_module --with-file-aio --with-http_stub_status_module &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx
###先切入/mnt目錄中,解包,然後修改src/core/nginx.h使其不顯示版本號,然後修改gcc,註釋掉debug,然後進行./configure編譯三部曲,(&> /dev/null使其不顯示編譯的過程),creates:檢測目錄下有沒有nginx目錄,防止重複編譯。###
[[email protected] nginx]# salt server3 state.sls nginx.install 
##推送時只要不報錯就表示完成
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 15:02:21.163881
    Duration: 306.046 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.10.1.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.10.1.tar.gz is in the correct state
     Started: 15:02:21.472147
    Duration: 45.622 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.10.1.tar.gz && cd nginx-1.10.1 && sed -i.bak 's/define NGINX_VER          "nginx\/" NGINX_VERSION/define NGINX_VER          "nginx"'/g src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-threads --with-http_ssl_module --with-file-aio --with-http_stub_status_module &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: /usr/local/nginx exists
     Started: 15:02:21.518365
    Duration: 0.316 ms
     Changes:   

Summary for server3
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time: 351.984 ms

2.nginx啟動指令碼:

server3的nginx安裝好了以後沒有啟動指令碼,需要自己配置:
將server2的httpd服務啟動指令碼/etc/init.d/httpd傳送給server3的/srv/salt/nginx/files/目錄下,修改配置檔案如下:
#!/bin/bash
#
# nginx        Startup script for the Apache HTTP Server
#
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
#

# Source function library.
. /etc/rc.d/init.d/functions

nginx=${HTTPD-/usr/local/nginx/sbin/nginx}
prog=nginx
pidfile=${PIDFILE-/usr/local/nginx/logs/nginx.pid}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
RETVAL=0
start() {
        echo -n $"Starting $prog: "
        $nginx
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} $nginx
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: " 
    $nginx -s reload
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"nginx shutdown"
        fi
    echo
}
# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} $nginx
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  force-reload|reload)
        reload
        ;;
  graceful|help|configtest|fullstatus)
        $apachectl [email protected]
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|reload|status}"
        RETVAL=2
esac

exit $RETVAL

3.啟動nginx,建立使用者以及使用者組

1.在server1的/srv/salt/nginx/files目錄下建立目錄pkgs和users,分別為安裝nginx所需要的依賴包,建立Nginx的使用者!
[[email protected] salt]# ls
_grains  haproxy  httpd  keepalive  nginx  pkgs  top.sls  users
[[email protected] salt]# cd pkgs/
[[email protected] pkgs]# ls
make.sls
[[email protected] pkgs]# cat make.sls 
make:
  pkg.installed:
    - pkgs:
      - gcc
      - pcre-devel
      - openssl-devel
[[email protected] salt]# cd users/
[[email protected] users]# ls
nginx.sls
[[email protected] users]# cat nginx.sls 
nginx-group:
  group.present:
    - name: nginx
    - gid: 800

nginx-user:
  user.present:
    - name: nginx
    - shell: /sbin/nologin
    - home: /usr/local/nginx
    - createhome: false
    - uid: 800
    - gid: 800
2.修改install.sls配置檔案包含新建的兩個模組:
[[email protected] salt]# cd nginx/
[[email protected] nginx]# ls
files  install.sls  service.sls
[[email protected] nginx]# cat install.sls 
include:
  - pkgs.make
  - users.nginx
nginx-install:
#  pkg.installed:
#    - pkgs:
#      - gcc
#      - openssl-devel
#      - pcre-devel

  file.managed:
    - name: /mnt/nginx-1.10.1.tar.gz
    - source: salt://nginx/files/nginx-1.10.1.tar.gz

  cmd.run:
    - name: cd /mnt && tar zxf nginx-1.10.1.tar.gz && cd nginx-1.10.1 && sed -i.bak 's/define NGINX_VER          "nginx\/" NGINX_VERSION/define NGINX_VER          "nginx"'/g src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-threads --with-http_ssl_module --with-file-aio --with-http_stub_status_module &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx
3.編輯service.sls檔案:
[[email protected] nginx]# cat service.sls 
include:
  - nginx.install 

/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/files/nginx.conf

nginx-service:
  file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx
    - mode: 755   ###給配置檔案新增執行許可權

  service.running:
    - name: nginx
    - reload: True
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf
4.將serve3的/usr/local/nginx/conf/nginx.conf檔案傳送到server1的/srv/salt/nginx/files/目錄下,修改配置檔案:
[[email protected] files]# pwd
/srv/salt/nginx/files
[[email protected] files]# ls
nginx  nginx-1.10.1.tar.gz  nginx.conf
推送測試:
[[email protected] nginx]# salt server3 state.sls nginx.service
server3:
----------
          ID: nginx-service
    Function: file.managed
        Name: /usr/local/nginx/conf/nginx.conf
      Result: True
     Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
     Started: 15:58:58.219243
    Duration: 47.734 ms
     Changes:   
----------
          ID: nginx-service
    Function: cmd.run
        Name: /usr/local/nginx/sbin/nginx
      Result: True
     Comment: Command "/usr/local/nginx/sbin/nginx" run
     Started: 15:58:58.267515
    Duration: 8.309 ms
     Changes:   
              ----------
              pid:
                  4724
              retcode:
                  0
              stderr:
              stdout:

Summary for server3
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:  56.043 ms
檢視server3是否啟動:ps ax
4725 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx 

server1安裝haproxy

1.將server1新增salt-key的認證

[root@server1 nginx]# salt server1 state.sls haproxy.install
No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received
server1安裝salt-minion,修改配置檔案/etc/salt/minion檔案:
[root@server1 salt]# vim minion
16 master: 172.25.60.1
[root@server1 salt]# salt-key -a server1
The following keys are going to be accepted:
Unaccepted Keys:
server1
Proceed? [n/Y] y
Key for minion server1 accepted.
[root@server1 ~]# salt-key -L
Accepted Keys:
server1
server2
server3
server4
Denied Keys:
Unaccepted Keys:
Rejected Keys:

2.server1安裝haproxy:

在server1的/srv/salt/目錄下新建目錄haproxy.進入haproxy目錄編輯install.sls檔案:
[[email protected] haproxy]# cat install.sls 
haproxy-install:
  pkg.installed:
    - pkgs:
       - haproxy
[[email protected] haproxy]# salt server1 state.sls haproxy.install
server1:
----------
          ID: haproxy-install
    Function: pkg.installed
      Result: True
     Comment: The following packages were installed/updated: haproxy
     Started: 16:07:09.310949
    Duration: 6465.456 ms
     Changes:   
              ----------
              haproxy:
                  ----------
                  new:
                      1.4.24-2.el6
                  old:

Summary for server1
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   6.465 s

3.haproxy負載均衡

[[email protected] haproxy]# cat install.sls 
haproxy-install:
  pkg.installed:
    - pkgs:
       - haproxy

  file.managed:
    - name: /etc/haproxy/haproxy.cfg
    - source: salt://haproxy/files/haproxy.cfg

  service.running:
    - name: haproxy
    - reload: True
    - watch:
      - file: haproxy-install
將/etc/haproxy/haproxy.cfg檔案複製到當前目錄下,修改配置檔案:
[[email protected] haproxy]# cp  /etc/haproxy/haproxy.cfg  files/
[[email protected] haproxy]# ls files/
haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #   
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #   
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #   
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
frontend  main *:80
#    acl url_static       path_beg       -i /static /images /javascript /stylesheets
#    acl url_static       path_end       -i .jpg .gif .png .css .js

#    use_backend static          if url_static
    default_backend             app
backend app
    balance     roundrobin
    server      app1 172.25
            
           

相關推薦

自動化saltstack 安裝部署以及部署實現負載均衡

saltstack介紹 SaltStack是一個伺服器基礎架構集中化管理平臺,SaltStack基於Python語言實現,也是基於C/S架構,結合輕量級訊息佇列(ZeroMQ)與Python第三方模組(Pyzmq、PyCrypto、Pyjinjia2、pyth

Saltstack自動化工具 ---- Salt部署部署原始碼安裝ngixn

Salt,,一種全新的基礎設施管理方式,部署輕鬆,在幾分鐘內可執行起來,擴充套件性好,很容易管理上萬臺伺服器,速度夠快,伺服器之間秒級通訊。 salt底層採用動態的連線匯流排, 使其可以用於編配, 遠端執行, 配置管理等等. 1、安裝配置 [[email&

自動化工具安裝部署 chef () - 測試環境配置

系統/運維 Linux 最近一直在做tensorflow的研究,看書看視頻群內討論什麽的,有些頭大,抽空刷朋友圈看到有朋友一直關註devops,市面上也有相關人員需求,索性抽空玩一個自動化運維的產品chef,加上之前也有開發過相關運維產品,對比下rundeck,看看這個有什麽好玩。官網https://

自動化工具安裝部署 chef (四) - node的安裝

系統/運維 Linux 原文在此https://docs.chef.io/nodes.html節點分好幾種又加了一臺機器[root@chefnode ~]# cat /etc/hosts先確保hosts裏面都有解析[root@chefserver chef]# scp chefdk-2.4.17-1.

自動化工具安裝部署 chef (二) - server的安裝

系統/運維 Linux 經過一個晚上的下載,終於下載好了安裝前先抓個虛擬機鏡像原始文檔https://docs.chef.io/install_server.html開始安裝安裝完成初始化下[root@chef_server chef]# chef-server-ctl reconfigure初始化失

自動化工具安裝部署 chef (三) - workstation的安裝

系統/運維 Linux workstation原文https://docs.chef.io/workstation.html[root@chef_server etc]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost

自動化工具安裝部署 chef (八)- 分發配置策略

系統/運維 Linux 如果沒有生成chef-repo目錄的話到workstation 先clone一個git 初始目錄git clone git://github.com/opscode/chef-repo.git這裏我已經生成過了knife cookbook site install userwo

自動化工具Ansible實戰()簡介和部署

Ansible 自動化運維 一、Ansible的介紹 Ansible是新出現的自動化運維工具,基於Python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點。實現了批量系統配置、批量程序部署、批量運行命令等功能。Ansible是基於模塊工作的,本身沒

老男孩14期自動化day15隨筆和作業()

1.JavaScript簡介 獨立的語言,瀏覽器本身就是一個JavaScript的直譯器 js要加分號 是因為網頁上要儘量把js程式碼壓縮成一行,所以必須加分號來識別哪個是一行 2.JavaScript程式碼存在形式: head中 (一進入網頁就觸發)

老男孩14期自動化day14隨筆和作業()

HTML 瀏覽器本質相當於socket客戶端,業務伺服器相當於socket服務端,建立sokcet傳輸資料, 伺服器端收到訊息進行處理,傳送資料並關閉,然後瀏覽器收到訊息展示頁面, 1.其實就是一套規則,瀏覽器認識的規則 2.開發者: (1)學習html規則 (2)開發後

自動化saltstack

一、簡介      SaltStack是一個伺服器基礎架構集中化管理平臺,具備配置管理、遠端執行、監控等功能,一般可以理解為簡化版的puppet和加強版的func。SaltStack基於Python語言實現,結合輕量級訊息佇列(ZeroMQ)與 Pytho

網上最新的devstack安裝教程【部署openstack測試環境】

這段時間由於測試需要搭建openstack環境,搜遍谷歌百度進行不下30次嘗試...看了陳沙克等前輩的安裝教程,很多配置資訊都已經過期了。不過大致的步驟基本不變,由於最近devstack在github

自動化工具---SaltStack安裝部署及簡單案例

com 常用模塊 分類 fire thead 批量部署 配置 出現 nio SaltStack原理 SaltStack由Master(服務端)和Minion(客戶端)組成,Master和Minion之間通過ZeroMQ(消息隊列)進行通訊,Master和Minion分別監

部署 SaltStack 自動化工具,並簡易批量安裝 httpd 服務

SaltStack 介紹  SaltStack 是一個基於 Python 開發的一套架構配置管理工具,採用 C/S模式,server端就是salt的master,client端就是minion,minion與master之間通過ZeroMQ訊息佇列通訊。master監聽4505和4506埠,4505為mas

Saltstack自動化工具(部署高可用負載均衡叢集)

在上篇部落格中我們利用salt推送了一臺主機上的haproxy為了實現高可用和負載均衡,我們再使用一臺虛擬機器server4搭建叢集 server1和server4組成高可用和負載均衡叢集 在server4 做好底層配置: 在server4上安裝minion,更改配置檔

Saltstack自動化環境部署

管理工具 虛擬機 service master 服務端 Saltstack介紹Saltstack是一個新的基礎設施管理工具。目前處於快速發展階段,可以看做是強化的Func+弱化的Puppet的組合。間接的反映出了saltstack的兩大功能:遠程執行和配置管理。Saltstack使用Pyt

自動化工具SaltStack詳細部署【轉】

lib64 time 兩個 功能 .so odi 組件 部分 srv ==========================================================================================一、基礎介紹======

saltstack自動化平臺的介紹、部署、基本使用

使用介紹 saltstack saltstack:服務器基礎架構集中化管理平臺,有配置管理、遠程執行、監控等功能,基於Python語言開發,結合輕量級消息隊列(ZeroMQ)與Python第三方模塊(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)構

自動化工具之SaltStack-1、SaltStack介紹及安裝

自動生成 模塊 模糊匹配 mini 說明 pac roc redhat 通信 1、SaltStack簡介 官方網址:http://www.saltstack.com官方文檔:http://docs.saltstack.comGitHub:https:github.com/s

Saltstack自動化工具 實戰與部署

caf 主動推送 n-k 分鐘 啟動服務 ade 個人觀點 ots 處理 自動化工具比較Puppet也許是四款工具中最深入人心的。就可用操作、模塊和用戶界面而言,它是最全面的。Puppet呈現了數據中心協調的全貌,幾乎涵蓋每一個運行系統,為各大操作系統提供了深入的工具。初始