http伺服器負載均衡安裝部署(apache+keepalived)(一)

語言: CN / TW / HK

theme: cyanosis

持續創作,加速成長!這是我參與「掘金日新計劃 · 6 月更文挑戰」的第28天,點選檢視活動詳情


環境準備

生產中心http apache使用兩臺伺服器作為masterslave節點

序號 | 伺服器節點 | IP地址 | 伺服器作業系統 | apache 版本 | | -------------- | ------------------- | ------------- | ----------------------------- | --------------------- | | 1 | http-primary-master | 192.168.4.162 | linux CentOS 6.4 64位 2核4G 50G | 2.2.4 | | 2 | http-primary-slave | 192.168.4.163 | linux CentOS 6.4 64位 2核4G 50G | 2.2.4

生產中心http keepalived使用兩臺伺服器作為master和slave節點

序號 | 伺服器節點 | IP地址 | 伺服器作業系統 | apache 版本 | | ---------- | ------------------------- | ------------- | ----------------------------- | --------------------- | | 1 | keepalived-primary-master | 192.168.4.162 | linux CentOS 6.4 64位 2核4G 50G | 2.0.6 | | 2 | keepalived-primary-slave | 192.168.4.163 | linux CentOS 6.4 64位 2核4G 50G | 2.0.6

一、生產中心http master安裝及配置

1.1、安裝http-primary-master節點

上傳httpd-2.2.34.tar.gz到/opt/tools目錄下並進行解壓 ```

cd /opt/tools/

tar -zxvf httpd-2.2.34.tar.gz

編譯安裝

cd httpd-2.2.34

./configure --prefix=/usr/local/apache2 --enable-module=so --enable-rule=SHARED_CORE --enable-so --enable-rewrite

```

image.png

如果編譯的時候出現以上錯誤,安裝GCC軟體套件,執行下面命令:

```

yum -y install gcc

重新編譯

./configure --prefix=/usr/local/apache2 --enable-module=so --enable-rule=SHARED_CORE --enable-so --enable-rewrite

make

make install

``` 至此 apache已經安裝完

啟動Apache服務: ```

/usr/local/apache2/bin/apachectl start

``` 如果啟動apache啟動報錯如下:

httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.4.162 for ServerName

需要修改apache ServerName,步驟如下 ```

vi /usr/local/apache2/conf/httpd.conf

``` 搜尋"#ServerName",修改ServerName 為 192.168.4.162:80

image.png

修改為

image.png

再重新啟動apache 即可 ```

/usr/local/apache2/bin/apachectl start

```

image.png

1.2、配置防火牆

啟動80埠 ```

vi /etc/sysconfig/iptables

新增以下內容: -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

-A INPUT -i eth0 -p 112 -j ACCEPT ``` 注意,80是HTTP埠,112是keepalived通訊埠,112埠必須要這樣寫

重啟防火牆 ```

service iptables restart

```

1.3、安裝tomcat通訊模組

```

cd /opt/tools/httpd-2.2.34/modules/proxy

/usr/local/apache2/bin/apxs -c -i mod_proxy.c proxy_util.c

```

image.png

1.4、安裝proxy_ajp模組

```

cd /opt/tools/httpd-2.2.34/modules/proxy

/usr/local/apache2/bin/apxs -c -i mod_proxy_ajp.c ajp_header.c ajp_utils.c ajp_msg.c ajp_link.c

```

image.png

```

/usr/local/apache2/bin/apxs -i -c -a mod_proxy_http.c

```

image.png

```

/usr/local/apache2/bin/apxs -i -c -a mod_proxy_connect.c

```

image.png

```

/usr/local/apache2/bin/apxs -i -c -a mod_proxy_balancer.c

```

image.png

1.5、載入模組配置

```

vi /usr/local/apache2/conf/httpd.conf

``` 搜尋"# LoadModule foo_module modules/mod_foo.so"

image.png

把以上三個LoadModule替換為以下五個LoadModule,注意順序,否則啟動會報錯:

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_http_module  modules/mod_proxy_http.so

LoadModule proxy_connect_module modules/mod_proxy_connect.so

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

image.png

apache停止命令: ```

/usr/local/apache2/bin/apachectl stop

apache啟動命令:

/usr/local/apache2/bin/apachectl start

檢視apache啟動情況:

netstat -anp | grep :80

```

image.png

經過以上步驟安裝部署,可以通過瀏覽器正常訪問到伺服器80埠,http伺服器安裝完成。

image.png

1.6、引數調優及應用配置

1.6.1、主配置檔案配置

```

vi /usr/local/apache2/conf/httpd.conf

``` 將虛擬主機配置檔案路徑與效能配置模組路徑路徑前面#號註釋去掉,生效配置檔案,配置如圖所示:

image.png

1.6.2、高效能配置

配置MPM

在apace安裝目錄/conf/extra目錄編輯個名為httpd-mpm.conf的配置檔案,執行指令碼如下: ```

vi /usr/local/apache2/conf/extra/httpd-mpm.conf

``` 在預設情況下,apache的MPM模組配置檔案並沒有啟用

image.png

此時,我們就需要根據當前Apache伺服器所使用的MPM模組,來修改對應<IfModule>節點下的引數配置,配置情況如下:

<IfModule mpm_prefork_module>     StartServers          30     MinSpareServers       30 MaxSpareServers      90 ServerLimit          5500     MaxClients          5000     MaxRequestsPerChild   50000 </IfModule>

image.png

對應的配置引數作用如下:

startServers:啟動Apache時建立的子程序數。

MinSpareServers:處於空閒狀態的最小子程序數。

MaxSpareServers:處於空閒狀態的最大子程序數。

MinSpareThreads:處於空閒狀態的最小執行緒數。

MaxClients:允許同時連線的最大請求數量

MaxRequestsPerChild:每個子程序允許處理的請求總數

1.6.3、站點應用配置

```

vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

把兩個example的配置註釋掉,在檔案末尾新增以下內容:     DocumentRoot "/usr/local/apache2/htdocs"     ServerName 192.168.4.162     ErrorLog "logs/http-primary-master-error_log"     CustomLog "logs/http-primary-master-access_log" common ``` 如圖:

image.png

重啟apache令配置生效: ```

/usr/local/apache2/bin/apachectl restart

修改預設頁

vi /usr/local/apache2/htdocs/index.html

修改為以下內容:

This is http-primary-master!

``` 瀏覽訪問檢視效果:

image.png

1.7、apache命令

重啟apache: ```

/usr/local/apache2/bin/apachectl restart

啟動apache:

/usr/local/apache2/bin/apachectl start

停止apache:

/usr/local/apache2/bin/apachectl stop

```

1.8、安裝keepalived

上傳keepalived-2.0.6.tar.gz到/opt/tools目錄下並進行解壓 ```

cd /opt/tools/

tar -zxvf keepalived-2.0.6.tar.gz

cd keepalived-2.0.6

./configure --prefix=/usr/local/keepalived

```

image.png

如果出現以上錯誤,執行以下命令安裝openssl ```

yum -y install openssl-devel

安裝完opendssl後,重新安裝keepalived

./configure --prefix=/usr/local/keepalived

make

make install

```

1.9、將 keepalived 安裝成 Linux 系統服務

因為沒有使用 keepalived 的預設路徑安裝(預設是/usr/local),安裝完成之後,需要做一些工作複製預設配置檔案到預設路徑 ```

mkdir /etc/keepalived

複製 keepalived 服務指令碼到預設的地址

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

cp /opt/tools/keepalived-2.0.6/keepalived/etc/init.d/keepalived /etc/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

ln -s /usr/local/sbin/keepalived /usr/sbin/

ln -s /usr/local/keepalived/sbin/keepalived /sbin/

設定 keepalived 服務開機啟動

chkconfig keepalived on

```

1.10、修改 Keepalived 配置檔案

MASTER 節點配置檔案(192.168.4.162)

首先先備份keepalived配置檔案 ```

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

建立新的配置檔案

vi /etc/keepalived/keepalived.conf

``` 新增以下內容:

``` global_defs {    router_id http-primary-master }

vrrp_instance VI_1 {   state MASTER   interface eth0   virtual_router_id 166   priority 100   advert_int 1   authentication {     auth_type PASS     auth_pass 1111   }

virtual_ipaddress {     192.168.4.166   } }

virtual_server 192.168.4.166 80 {   delay_loop 6   lb_algo wrr   lb_kind DR   persistence_timeout 0   protocol TCP   real_server 192.168.4.162 80 {     weight 1     TCP_CHECK {       connect_timeout 10       delay_before_retry 3       connect_port 80     }   }

real_server 192.168.4.163 80 {     weight 1     TCP_CHECK {       connect_timeout 10       delay_before_retry 3       connect_port 80     }   } } ``` 以下是配置檔案的說明

``` global_defs {     router_id http-primary-master  # 設定lvs的id,在一個網路內應該是唯一的,標識本節點的字條串,通常為 hostname }  

vrrp_instance VI_1 {     state MASTER   #指定Keepalived的角色,MASTER為主,BACKUP為備             interface eth0  #指定Keepalived的角色,MASTER為主,BACKUP為備   virtual_router_id 166  #虛擬路由編號,主備要一致   priority 100  #定義優先順序,數字越大,優先順序越高,主DR必須大於備用DR       advert_int 1  #檢查間隔,預設為1s   # 設定驗證資訊,兩個節點必須一致   authentication {       auth_type PASS       auth_pass 1111   }  

virtual_ipaddress {       192.168.4.166  #定義虛擬IP(VIP),可多設,每行一個   }   }  

定義對外提供服務的LVS的VIP以及port

virtual_server 192.168.4.166 80 {     delay_loop 6 # 設定健康檢查時間,單位是秒                       lb_algo wrr # 設定負載排程的演算法為wlc                      lb_kind DR # 設定LVS實現負載的機制,有NAT、TUN、DR三個模式      nat_mask 255.255.255.0                   persistence_timeout 0             protocol TCP                     real_server 192.168.4.162 80 {  # 指定real server1的IP地址     weight 1   # 配置節點權值,數字越大權重越高                   TCP_CHECK {         connect_timeout 10                nb_get_retry 3         delay_before_retry 3         connect_port 80       }     }  

real_server 192.168.4.163 80 {  # 指定real server2的IP地址     weight 1  # 配置節點權值,數字越大權重越高       TCP_CHECK {         connect_timeout 10         nb_get_retry 3         delay_before_retry 3         connect_port 80        }     }   } ```

完成以上步驟,master節點的keepalived已經安裝配置完成,可以開啟keepalieved服務

1.11、編輯realserver指令碼檔案

```

vi /etc/init.d/realserver

新增以下內容: SNS_VIP=192.168.4.166 /etc/rc.d/init.d/functions case "$1" in start)        ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP        /sbin/route add -host $SNS_VIP dev lo:0        echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore        echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce        echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore        echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce        sysctl -p >/dev/null 2>&1        echo "RealServer Start OK"        ;; stop)

ifconfig lo:0 down        route del $SNS_VIP >/dev/null 2>&1        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce        echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore        echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce        echo "RealServer Stoped"        ;; *)        echo "Usage: $0 {start|stop}"        exit 1 esac

exit 0 儲存指令碼檔案後更改該檔案許可權:

chmod 755 /etc/init.d/realserver

開啟realserver服務:

service realserver start

設定開機自動啟動realserver服務:

vi /etc/rc.local

在末尾新增以下內容: service realserver start ``` (注意:這個realserver步驟一定要做,否則keepalived無法實現負載均衡)

1.12、啟動keepalived

```

service keepalived start

```

image.png

檢視是否成功建立VIP ```

ip a

``` image.png

可以看到VIP已經建立成功

1.13、 Keepalived日誌

預設日誌存放在系統日誌:/var/log/messages下,把日誌單獨存放

修改/etc/sysconfig/keepalived ```

vi /etc/sysconfig/keepalived

``` 把KEEPALIVED_OPTIONS="-D" 修改為:KEEPALIVED_OPTIONS="-D -d -S 0"

image.png

然後修改rsyslog.conf ```

vi /etc/rsyslog.conf

``` 檔案末尾新增:

local0.* /var/log/keepalived.log

重啟日誌記錄服務 ```

/etc/init.d/rsyslog restart

重啟keepalived

/etc/init.d/keepalived restart

檢視日誌

tail -f /var/log/keepalived.log

```


好了,以上就是http master主節點安裝apache和keepalived的過程了。

篇幅有點長,大傢伙耐心點看!!!

http slave從節點,安裝大致差不多,可能就是一些配置檔案的不一樣,那就留著下次分享了。

好了,今天就先到這裡了!!!^_^

個人理解,可能也不夠全面,班門弄斧了。

如果覺得有收穫的,幫忙點贊、評論、收藏一下唄!!!

image.png