Keepalived+Nginx+Memcached構架高可用

假設場景:css

web應用服務器1 : 192.168.21.21:8001java

web應用服務器2 : 192.168.21.22:8001nginx

須要虛擬出IP:192.168.21.201:8805web

 

解決方案:vim

在web1和web2上部署一樣的兩套應用程序,端口號都爲8001.tomcat

在web1和web2上部署nginx,nginx使用集羣方式,將請求到的8805端口分發到集羣服務器上。服務器

在web1和web2上部署keepalived,keepalived配置虛擬ip:192.168.21.201session

在web1和web2上部署Memcached,進行session複製。併發

 

實際路由:tcp

外部訪問192.168.21.201:8805,keepalived監聽到後,確認分發到哪一臺nginx。nginx經過箭筒8805端口,使用集羣策略,再分發到具體服務器。

 

1.安裝nginx

nginx依賴openssl、pcre、zlib。

nginx具體安裝方案查看:  Linux系統 - 源碼編譯安裝Nginx

 

2.安裝keepalived

# yum install -y keepalived
# keepalived -v

或者經過源碼編譯方式:

wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

tar xzf keepalived-1.2.15.tar.gz

cd keepalived-1.2.15

./configure

make && make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

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

cp /usr/local/sbin/keepalived /usr/sbin/

 

mkdir /etc/keepalived

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

chkconfig keepalived on

 

3.配置keepalived

假設web1爲主機,web2爲備份機。

編輯web1的keepalived的配置文件:

vim /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   #notification_email_from Alexandre.Cassen@firewall.loc
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 

vrrp_script chk_http_port {

    script "</dev/tcp/127.0.0.1/8801"   #監聽nginx狀態

    interval 1
    weight -2

}

vrrp_instance VI_1 {

    state MASTER     #主機設置爲MASTER

    interface eth0     #使用ifconfig,查看當前網卡名稱

    virtual_router_id 51   #主機ID
    priority 100         #主機優先級
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.21.201    #監聽的虛擬IP
    }
    track_script {
        chk_http_port
    }

}

 

編輯web2的keepalived的配置文件:

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   #notification_email_from Alexandre.Cassen@firewall.loc
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_http_port {
    script "</dev/tcp/127.0.0.1/8801"
    interval 1
    weight -2

}

vrrp_instance VI_1 {
    state BACKUP     #備份機
    interface eth0     #備份機ifconfig的網卡
    virtual_router_id 51   #必須與主機相同
    priority 99           #必須小於主機
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.21.201
    }
    track_script {
        chk_http_port
    }

}

 

4. 配置nginx

兩臺服務器的nginx配置同樣

     vim /usr/local/nginx/conf/nginx.conf

 

5.檢驗keepalived和nginx

     啓動keepalived和nginx:

          service keepalived start

          /usr/loca/nginx/sbin/nginx

 

     /var/log/messages

 

     在web1主機上輸入:ip a,查看ip綁定狀態:

   

    說明虛擬IP配置成功

     

     在web2備份機上輸入:ip a,查看IP綁定狀態:

     

    沒有虛擬IP地址,說明主機良好的狀態下,備份機沒有接管。

 

     ① 模擬服務器宕機情況(服務器關機、keepalived宕機)

          手動關掉主機keepalived:

               killall keepalived

          查看主機的ip a,發現虛擬主機已沒有;查看備份機的ip a,發現已經綁定虛擬主機。

          手動啓動主機的keepalived:

               service keepalived start

          再次查看主機的ip a,發現虛擬主機又一次綁定。

          

          說明keepalived能夠瞬時切換,運行正常。

 

     ②模擬nginx掛掉

         手動關掉主機的nginx:

               ps -ef|grep nginx

               kill -9 pid

          繼續①中的驗證,keepalived將虛擬主機轉到備份機。

 

     以上驗證說明keepalived能夠保障nginx的高可用。

 

6.配置Memcached

     在兩臺服務器上都裝上Memcached。

     memcached須要依賴libevent:

tar xf libevent-2.0.22-stable.tar.gz

cd libevent-2.0.22-stable

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

make && make install

echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf

ldconfig

     安裝memcached:

tar xf memcached-1.4.24.tar.tar
cd memcached-1.4.24
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

make && make install

 

     啓動memcached:

/usr/local/bin/memcached -d -m 1024 -u root  -p 11211 -c 1024

     啓動參數說明:

  • -d 選項是啓動一個守護進程,
  • -m 是分配給Memcache使用的內存數量,單位是MB,默認64MB 
  • -M return error on memory exhausted (rather than removing items)
  • -u 是運行Memcache的用戶,若是當前爲root 的話,須要使用此參數指定用戶。
  • -l 是監聽的服務器IP地址,默認爲全部網卡。
  • -p 是設置Memcache的TCP監聽的端口,最好是1024以上的端口
  • -c 選項是最大運行的併發鏈接數,默認是1024
  • -P 是設置保存Memcache的pid文件 

 

     配置tomcat下的conf/context.xml

     

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

                memcachedNodes="n1:192.168.21.21:11211,n2:192.168.21.22:11211"

                sticky="false"

                sessionBackupAsync="false"

                lockingMode="auto"

                requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"                  transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"

        />

 

 

     其中n1和n2 即須要負載的機器上所裝memcached的ip和端口。如家政的iFly-Housekeep項目須要

     192.168.21.21和192.168.21.22兩臺機器,這兩臺上都須要安裝memcached,端口都爲11211

 

7.拷貝memcached的jar包

     將jar包拷入tomcat/lib/目錄下

     

相關文章
相關標籤/搜索