1. 程式人生 > >corosync+pacemaker+nginx+nfs高可用

corosync+pacemaker+nginx+nfs高可用

簡單的nginx高可用

用corosync+pacemaker做一個簡單的nginx高可用,然後將nfs掛載到nginx的網頁訪問目錄。


由於nginx是源代碼安裝的,所以要寫入到systemd來管理才能夠被pacemaker識別到。接下來,寫一個nginx啟動文件:

[root@centosa system]# cat /etc/systemd/system/nginx.service 
[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

然後先停止之前的nginx,用systemctl start nginx來啟動它。

兩個節點都添加到啟動項。然後再設置為開機啟動:systemctl enable nginx


這裏還是用crm來設置nginx高可用。

先停止nginx,然後通過集群讓nginx啟動。

1、添加nginx資源:

crm(live)configure# primitive nginx systemd:nginx
crm(live)configure# verify
crm(live)configure# commit

2、添加nfs掛載資源,然後與nginx綁定並設置先後順序:

crm(live)configure# primitive nfs-server ocf:heartbeat:Filesystem params device=192.168.40.145:/nfsdata directory=/usr/local/nginx/html/ fstype=nfs op start timeout=100 op stop timeout=100
crm(live)configure# verify
crm(live)configure# colocation nginx_with_nfs-server inf: nginx nfs-server
crm(live)configure# order nginx_after_nfs-server Mandatory: nfs-server nginx
crm(live)configure# verify
crm(live)configure# commit

3、添加一個vip資源,並與nginx綁定然後設置先後順序:

crm(live)configure# primitive vip ocf:heartbeat:IPaddr params ip=192.168.40.100 op monitor interval=20 timeout=20 on-fail=restart
crm(live)configure# colocation vip_with_nginx inf: nginx vip
crm(live)configure# order vip_after_nginx Mandatory: nginx vip
crm(live)configure# verify
crm(live)configure# commit

4、查看配置信息:

crm(live)configure# show
node 1: centosa 	attributes standby=on
node 2: centosb 	attributes standby=off
primitive nfs-server Filesystem 	params device="192.168.40.145:/nfsdata" directory="/usr/local/nginx/html/" fstype=nfs 	op start timeout=100 interval=0 	op stop timeout=100 interval=0
primitive nginx systemd:nginx
primitive vip IPaddr 	params ip=192.168.40.100 	op monitor interval=20 timeout=20 on-fail=restart
order nginx_after_nfs-server Mandatory: nfs-server nginx
colocation nginx_with_nfs-server inf: nginx nfs-server
order vip_after_nginx Mandatory: nginx vip
colocation vip_with_nginx inf: nginx vip
property cib-bootstrap-options: 	have-watchdog=false 	dc-version=1.1.16-12.el7_4.4-94ff4df 	cluster-infrastructure=corosync 	cluster-name=mycluster 	stonith-enabled=false 	default-action-timeout=100s

5、查看狀態:

crm(live)# status
Stack: corosync
Current DC: centosb (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Wed Oct 25 23:12:30 2017
Last change: Wed Oct 25 23:12:25 2017 by root via cibadmin on centosa
2 nodes configured
3 resources configured
Online: [ centosa centosb ]
Full list of resources:
 nginx(systemd:nginx):Started centosa
 nfs-server(ocf::heartbeat:Filesystem):Started centosa
 vip(ocf::heartbeat:IPaddr):Started centosa

總結一下啟動的先後順序(先掛載,再啟動nginx,再啟動vip):

nfs-server > nginx >vip

查看掛載的情況:

[root@centosa ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/cl-root       18G  3.6G   15G  20% /
devtmpfs                 478M     0  478M   0% /dev
tmpfs                    489M   39M  450M   8% /dev/shm
tmpfs                    489M  6.7M  482M   2% /run
tmpfs                    489M     0  489M   0% /sys/fs/cgroup
/dev/sda1               1014M  168M  847M  17% /boot
tmpfs                     98M     0   98M   0% /run/user/0
192.168.40.145:/nfsdata   17G  3.6G   14G  21% /usr/local/nginx/html

其實就是掛載在nginx的網頁存放目錄那裏,


再去到nfs服務器,查看一下共享的文件:

[root@centos1 nfsdata]# ls
index.htm  index.html
[root@centos1 nfsdata]# cat index.html
hh


直接訪問vip:

[root@centos1 nfsdata]# curl http://192.168.40.100
hh

現在把主節點下線,然後一直刷status,會發現是按照設定順序停止,然後按照設定順序開啟到另一節點:

crm(live)# node standby
crm(live)# status
Stack: corosync
Current DC: centosb (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Wed Oct 25 23:20:50 2017
Last change: Wed Oct 25 23:20:39 2017 by root via crm_attribute on centosa
2 nodes configured
3 resources configured
Node centosa: standby
Online: [ centosb ]
Full list of resources:
 nginx(systemd:nginx):Started centosb
 nfs-server(ocf::heartbeat:Filesystem):Started centosb
 vip(ocf::heartbeat:IPaddr):Started centosb

查看另一個節點的掛載情況:

[root@centosb ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/cl-root       18G  4.4G   14G  25% /
devtmpfs                 226M     0  226M   0% /dev
tmpfs                    237M   54M  183M  23% /dev/shm
tmpfs                    237M   13M  224M   6% /run
tmpfs                    237M     0  237M   0% /sys/fs/cgroup
/dev/sda1               1014M  173M  841M  18% /boot
tmpfs                     48M     0   48M   0% /run/user/0
192.168.40.145:/nfsdata   17G  3.6G   14G  21% /usr/local/nginx/html


訪問vip:

[root@centos1 nfsdata]# curl http://192.168.40.100
hh

完。。

本文出自 “運維小記” 博客,請務必保留此出處http://lsfandlinux.blog.51cto.com/13405754/1976216

corosync+pacemaker+nginx+nfs高可用