一、實驗環境
主機名 | ip | 服務 | 預配置服務 |
---|---|---|---|
server1 | 172.25.2.1/24 | salt-master,salt-minion | haproxy |
server2 | 172.25.2.2/24 | salt-minion | hattpd |
server3 | 172.25.2.3/24 | salt-minion | nginx |
1.實驗所用虛擬機系統均爲redhat6.5
2.實驗前提:物理機配置好rhel6的安裝包,虛擬機配置好yum源。
3.server1即充當master也充當minion,且三臺minion與master的公鑰已交換,master可以向三臺minion成功推送任務
以上具體步驟可見我的上篇博客>_< ! 博客鏈接
二、實 驗
2.1部署apache的自動化安裝及啓動
2.1 .1 部署apache自動化安裝
[root@server1 ~]# vim /etc/salt/master //開啓文件系統服務器
file_roots:
base:
- /srv/salt
[root@server1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon: [ OK ]
Starting salt-master daemon: [ OK ]
[root@server1 ~]# mkdir /srv/salt
[root@server1 ~]# cd /srv/salt
[root@server1 master]# ls
[root@server1 master]# mkdir httpd
[root@server1 master]# cd httpd/
[root@server1 httpd]# vim install.sls //部署安裝httpd與php
apache-install:
pkg.installed: //安裝包
- pkgs: // -pkgs下可寫多個安裝包
- httpd
- php
[root@server1 httpd]# salt server2 state.sls httpd.install //向server2推送
server2:
----------
ID: apache-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 11:20:27.070271
Duration: 450.606 ms
Changes:
Summary for server2
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 450.606 ms
[root@server2 minion]# rpm -q httpd //查看已安裝
httpd-2.2.15-29.el6_4.x86_64
[root@server2 minion]# rpm -q php
php-5.3.3-26.el6.x86_64
[root@server2 ~]# /etc/init.d/httpd status //但是服務併爲啓動,下面我們自動化部署啓動服務
httpd is stopped
[root@server2 ~]# chkconfig --list httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
2.1.2 部署apache自動化啓動
[root@server1 httpd]# mkdir files
[root@server1 httpd]# cd files/
[root@server1 files]# ls
httpd.conf
//這裏存放我們http的配置文件,在部署的腳本中我們會用到源文件取自這裏,而對此文件直接修改,相當於對server2上的服務配置文件修改
[root@server1 files]# ll
total 36
-rw-r--r-- 1 root root 34418 Aug 17 11:05 httpd.conf
[root@server1 httpd]# vim install.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
file.managed: //文件模塊塊
- name: /etc/httpd/conf/httpd.conf //server2配置文件地址
- source: salt://httpd/files/httpd.conf //源文件地址
- mode: 644 //權限
- user: root //用戶
service.running: //服務啓動模塊
- name: httpd //服務名稱
- enable: True //開機自啓
- reload: True //修改配置文件後刷新
- watch: //監控文件,就是上面的文件模塊中的源文件
- file: apache-install
//爲了測試文件模塊塊,我們將源文件中的配置文件端口修改爲8080
[root@server1 files]# vim httpd.conf
server1進行推送,server2端查看效果
root@server1 files]# salt server2 state.sls httpd.install
server2:
----------
ID: apache-install
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 11:16:54.397005
Duration: 450.873 ms
Changes:
----------
ID: apache-install
Function: file.managed
Name: /etc/httpd/conf/httpd.conf
Result: True
Comment: File /etc/httpd/conf/httpd.conf is in the correct state
Started: 11:16:54.849887
Duration: 50.483 ms
Changes:
----------
ID: apache-install
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 11:16:54.901259
Duration: 202.485 ms
Changes:
----------
httpd:
True
Summary for server2
------------
Succeeded: 3 (changed=1)
Failed: 0
------------
Total states run: 3
Total run time: 703.841 ms
//server2;
[root@server2 ~]# /etc/init.d/httpd status //服務已啓動
httpd (pid 3741) is running...
[root@server2 ~]# chkconfig --list httpd //加載在開機自啓項中
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@server2 ~]# netstat -antlp | grep :8080 //查看端口爲8080
tcp 0 0 :::8080 :::* LISTEN 3741/httpd
2.1.2 自動化部署sls文件優化
1. 將文件模塊與安裝模塊並列出來
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- mode: 644
- user: root
2.將安裝模塊並列出來
httpd:
pkg.installed
php:
pkg.installed
apache-install:
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: /etc/httpd/conf/httpd.conf
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd/files/httpd.conf
- mode: 644
- user: root
2.2 部署nginx的自動化安裝及啓動
2.2.1 部署nginx自動化源碼編譯安裝
[root@server1 salt]# cd /srv/salt/
[root@server1 salt]# ls //在文件系統中創建nginx目錄
httpd nginx
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls //創建用於安裝files,用於儲存安裝包
files
[root@server1 nginx]# cd files/
[root@server1 files]# ls
nginx-1.14.0.tar.gz
[root@server1 files]# cd ..
[root@server1 nginx]# vim install.sls //書寫自動化安裝腳本
nginx-install:
pkg.installed:
- pkgs: //編譯安裝依賴型軟件
- pcre-devel
- openssl-devel
- gcc
file.managed: //文件模塊
- name: /mnt/nginx-1.14.0.tar.gz //tar包存儲地址
- source: salt://nginx/files/nginx-1.14.0.tar.gz //源地址
cmd.run: //編譯安裝,這裏的命令,就是你在源碼編譯是一步步進行的動作,每個動作中間用&&來連接
- name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && 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-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &>/dev/null && make install &>/dev/null
- create: /usr/local/nginx
//文件檢測,若目的機中已存在這個目錄,則說明已安裝好,就不用再進行編譯
2.2.2 創建用戶自動化創建sls文件
//這裏我們再創建一個目錄用於存儲建立用戶的sls文件。
[root@server1 salt]# ls
httpd nginx users
[root@server1 salt]# cd users/
[root@server1 users]# vim nginx.sls
nginx-group: //用戶組
group.present:
- name: nginx
- gid: 800
nginx-user: 。。用戶設置
user.present:
- name: nginx
- uid: 800
- gid: 800
- shell: /sbin/nologin //不給用戶創建shell
- createhome: False //不自動創建家目錄
- home: /user/local/nginx //指定家目錄
2.2.3 部署服務自啓動及修改配置文件
root@server1 nginx]# cd files/
[root@server1 files]# ls
nginx nginx-1.14.0.tar.gz nginx.conf //這裏將nginx的啓動腳本及配置文件放置過來
[root@server1 files]# cd ..
[root@server1 nginx]# vim service.sls
include: //include模塊可以將前邊寫的兩個文件包括進來
- users.nginx
- nginx.install
/user/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: /user/local/nginx/conf/nginx.conf
//推送任務
[root@server1 nginx]# salt server3 state.sls nginx.service
//成功
//Server3端查看
[root@server3 salt]# ps ax|grep nginx
28983 ? Ss 0:00 nginx: master process /user/local/nginx/sbin/nginx -c /user/local/nginx/conf/nginx.conf
28986 ? S 0:00 nginx: worker process
28987 ? S 0:00 nginx: worker process
29082 pts/0 S+ 0:00 grep nginx
2.3 部署haproxy的自動化安裝啓動及實現自動化安裝
//實驗前請設置好你的yum源,將LoadBalancer模塊加入server1
2.3.1 自動化部署安裝haproxy及自啓動
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# cd haproxy/ //這裏我們創建一個目錄,用於haproxy的部署文件的存放
[root@server1 haproxy]# ls
files install.sls
[root@server1 haproxy]# cd files/ //創建files用於存儲源文件
[root@server1 files]# ls
haproxy.cfg //haproxy的配置文件
[root@server1 files]# cd ..
[root@server1 haproxy]# vim 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
2.3.2 負載均衡設置
[root@server1 /]# vim /srv/salt/haproxy/files/haproxy.cfg
//前面設置了httpd爲8080端口,這裏爲了統一,提前把端口改過來,並設置一個發佈頁面用於測試
//推送
[root@server1 files]# salt server1 state.sls haproxy.install
2.3.3 測 試
瀏覽器訪問server1
2.4 一鍵式部署安裝,實現負載均衡
[root@server1 salt]# vim top.sls //在/srv/salt直接書寫top.sls,這個文件的用於將前三個服務囊括起來
base:
'server1':
- haproxy.install
'server2':
- httpd.install
'server3':
- nginx.service
[root@server1 salt]# salt '*' state.highstate //運行