一、nginx高可用集群部署
1、安裝nginx
# 安裝依賴包 yum install -y gcc pcre-devel openssl-devel zlib-devel # 下載安裝包 wget https://nginx.org/download/nginx-1.17.3.tar.gz tar zxvf nginx-1.17.3.tar.gz cd nginx-1.17.3 # 配置 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module # 編譯 make # 安裝 make install
2、配置nginx集群
# 假設有兩台機器192.168.0.1和192.168.0.2,分別安裝好nginx
# 編輯nginx.conf
http {
upstream myapp1 {
server 192.168.0.1:8080 weight=5;
server 192.168.0.2:8080;
}
server {
listen 80;
server_name myapp.com;
location / {
proxy_pass http://myapp1;
}
}
}
3、使用Keepalived實現負載均衡
# 安裝keepalived
yum install -y keepalived
# 編輯keepalived.conf
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 nginx_lvs1
}
vrrp_instance VI_1 {
state MASTER # 主機為MASTER,備份機為BACKUP
interface eth0 # 心跳檢測的網卡
virtual_router_id 51 # 必須是一樣的
priority 101 # 主機的優先級,備份機的默認是100
advert_int 1 # 每秒發送一次心跳信息
authentication {
auth_type PASS
auth_pass 1111 # 必須一樣
}
virtual_ipaddress {
192.168.10.55/24 dev eth0 label eth0:1 # VIP,必須在同一個網段,必須在同一個子網內
}
}
# 啟動keepalived
systemctl enable keepalived
systemctl start keepalived
二、nginx高可用
1、nginx異步IO模型
nginx採用了異步的事件驅動模型,與傳統的阻塞式模型相比,它能夠更高效地將請求分發至後端服務器。
nginx的進程模型也非常優秀,主進程只負責管理子進程,子進程負責處理具體業務邏輯。當一個請求到來時,nginx會將其分配至一個子進程進行處理,其他子進程仍可正常工作。
因此,即使某個子進程掛了,不會影響整個服務的正常運作。另外,nginx還支持熱部署功能,可以在不停服的情況下更新服務,大大提高了系統的可用性。
2、nginx的健康檢查
nginx支持對後端服務器進行健康檢查,以保證服務的穩定性。常見的檢查方式包括輪詢檢查和主動健康檢查。
輪詢檢查是指nginx會輪流訪問每個後端服務器,檢查它們的健康狀態。如果某個後端服務器掛了,nginx會將其從服務列表中剔除,確保請求不會再分配至該服務器。
主動健康檢查是指nginx會定時向後端服務器發送特定的檢查請求(如ping指令或HTTP請求),如果後端服務器超時或返回異常結果,nginx也會將其從服務列表中剔除。
三、linux高可用集群
1、使用pacemaker和corosync實現linux高可用集群
# 安裝軟件包 yum install -y pacemaker pcs corosync # 配置集群環境 pcs cluster auth node1 node2 -u hacluster -p password pcs cluster setup --name my_cluster node1 node2 pcs cluster start --all # 部署資源 pcs resource create nginx ocf:heartbeat:nginx \ configfile=/etc/nginx/nginx.conf \ statusurl=http://localhost:8080/nginx_status \ force_stop=true # 部署VIP pcs resource create vip ocf:heartbeat:IPaddr2 \ ip=192.168.0.100 cidr_netmask=24 \ op monitor interval=30s # 啟動 pcs constraint location nginx prefers node1=100 \ and nginx has_slave=false pcs constraint location vip prefers node1=100 \ and vip has_slave=false
2、使用heartbeat和drbd實現linux高可用集群
# 安裝heartbeat和drbd軟件包
yum install -y heartbeat drbd84-utils
# 配置heartbeat
cp /usr/share/doc/heartbeat-3.0.5/authkeys /etc/ha.d/
chmod 600 /etc/ha.d/authkeys
echo "auth 1" >> /etc/ha.d/authkeys
echo "1 sha1 password" >> /etc/ha.d/authkeys
# 配置ha.cf
echo "logfile /var/log/ha-log" >> /etc/ha.d/ha.cf
echo "logfacility local0" >> /etc/ha.d/ha.cf
echo "keepalive 2" >> /etc/ha.d/ha.cf
echo "deadtime 15" >> /etc/ha.d/ha.cf
echo "bcast eth0" >> /etc/ha.d/ha.cf
echo "ucast eth1 192.168.1.2" >> /etc/ha.d/ha.cf
echo "ucast eth1 192.168.1.3" >> /etc/ha.d/ha.cf
echo "node drbd1 drbd2" >> /etc/ha.d/ha.cf
# 配置haresources
echo "drbd1 IPaddr::192.168.1.100/24/eth0/192.168.1.255 nginx" >> /etc/ha.d/haresources
# 啟動heartbeat
systemctl enable heartbeat
systemctl start heartbeat
# 配置DRBD
echo "resource r0 {" >> /etc/drbd.d/global_common.conf
echo " protocol C;" >> /etc/drbd.d/global_common.conf
echo " startup { wfc-timeout 15; degr-wfc-timeout 60; }" >> /etc/drbd.d/global_common.conf
echo " disk {" >> /etc/drbd.d/global_common.conf
echo " on-io-error detach;" >> /etc/drbd.d/global_common.conf
echo " }" >> /etc/drbd.d/global_common.conf
echo " net {" >> /etc/drbd.d/global_common.conf
echo " cram-hmac-alg sha1;" >> /etc/drbd.d/global_common.conf
echo " shared-secret-password password;" >> /etc/drbd.d/global_common.conf
echo " after-sb-0pri discard-zero-changes;" >> /etc/drbd.d/global_common.conf
echo " after-sb-1pri discard-secondary;" >> /etc/drbd.d/global_common.conf
echo " after-sb-2pri call-pri-lost-after-sb;" >> /etc/drbd.d/global_common.conf
echo " }" >> /etc/drbd.d/global_common.conf
echo "}" >> /etc/drbd.d/global_common.conf
echo "resource r0 {" >> /etc/drbd.d/r0.res
echo " device /dev/drbd1;" >> /etc/drbd.d/r0.res
echo " disk /dev/sdb1;" >> /etc/drbd.d/r0.res
echo " meta-disk internal;" >> /etc/drbd.d/r0.res
echo " on drbd1 {" >> /etc/drbd.d/r0.res
echo " address 192.168.1.2:7788;" >> /etc/drbd.d/r0.res
echo " }" >> /etc/drbd.d/r0.res
echo " on drbd2 {" >> /etc/drbd.d/r0.res
echo " address 192.168.1.3:7788;" >> /etc/drbd.d/r0.res
echo " }" >> /etc/drbd.d/r0.res
echo "}" >> /etc/drbd.d/r0.res
# 啟動DRBD
systemctl enable drbd
systemctl start drbd
# 創建文件系統並掛載
mkfs.xfs /dev/drbd1
mkdir /mnt/drbd1
echo "/dev/drbd1 /mnt/drbd1 xfs defaults 0 0" >> /etc/fstab
mount /mnt/drbd1
四、nginx高可用session
1、使用memcached實現nginx高可用session
# 安裝memcached服務和擴展模塊
yum install -y memcached
yum install -y php-pecl-memcached
# 修改php.ini文件
session.save_handler = memcached
session.save_path = "127.0.0.1:11211"
# 修改nginx.conf文件
http {
upstream myapp1 {
server 192.168.0.1:8080 weight=5;
server 192.168.0.2:8080;
}
server {
listen 80;
server_name myapp.com;
location / {
proxy_pass http://myapp1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 將Session保存到memcached服務器上
proxy_set_header SessionID $http_cookie;
proxy_cache_bypass $http_cookie;
proxy_cache_key $scheme$proxy_host$request_uri$cookie_SessionID;
}
}
}
2、使用Redis實現nginx高可用session
# 安裝Redis服務和擴展模塊
yum install -y redis
yum install -y php-pecl-redis
# 修改php.ini文件
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
# 修改nginx.conf文件
http {
upstream myapp1 {
server 192.168.0.1:8080 weight=5;
server 192.168.0.2:8080;
}
server {
listen 80;
server_name myapp.com;
location / {
proxy_pass http://myapp1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 將Session保存到Redis服務器上
proxy_set_header SessionID $http_cookie;
proxy_cache_bypass $http_cookie;
proxy_cache_key $scheme$proxy_host$request_uri$cookie_SessionID;
}
}
}
以上的實現方式都採用了代理服務器,將session保存在分布式緩存中,以達到高可用的目的。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/185830.html
微信掃一掃
支付寶掃一掃