Rumah >Operasi dan penyelenggaraan >Nginx >Bagaimana untuk mencapai ketersediaan tinggi keepalived+nginx
1.Pengenalan Keeplived
keepalived pada asalnya direka untuk perisian pengimbangan beban lvs untuk mengurus dan memantau status setiap nod perkhidmatan dalam sistem kluster lvs Ia kemudiannya ditambah Implement fungsi vrrp yang sangat tersedia. Selain mengurus perisian LVS, keepalived juga boleh menyokong penyelesaian ketersediaan tinggi untuk perkhidmatan lain.
keepalived melaksanakan fungsi ketersediaan tinggi melalui protokol vrrp. vrrp (protokol redundansi penghala maya) protokol redundansi penghala maya. Tujuan vrrp adalah untuk menyelesaikan satu titik masalah kegagalan penghalaan statik Ia boleh memastikan bahawa apabila nod individu turun, keseluruhan rangkaian boleh berjalan tanpa gangguan.
2. Prinsip failover ketersediaan tinggi Keepalived
Kegagalan antara perkhidmatan ketersediaan tinggi keepalived dilaksanakan melalui vrrp. Apabila perkhidmatan keepalived berfungsi, nod induk akan menghantar mesej degupan jantung (berbilang hantar) secara berterusan ke nod sandaran untuk memberitahu nod sandaran bahawa ia masih hidup.
Apabila nod induk gagal, mesej degupan jantung tidak boleh dihantar ke nod sandaran jika nod sandaran tidak dapat terus mengesan degupan jantung daripada nod induk. Ia akan memanggil program pengambilalihan sendiri untuk mengambil alih sumber IP dan perkhidmatan nod induk. Apabila nod utama pulih, nod siap sedia akan mengeluarkan sumber IP dan perkhidmatan yang diambil alih apabila nod utama gagal, dan kembali kepada peranan siap sedia asalnya
3 Pasang nginx
3.1. Nod induk (192.168.80.22)
3.1.1 Pasang alatan kompilasi dan fail perpustakaan
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
3.1.2 >
#进入目录 cd /usr/local/develop/anginx #上传安装文件并解压 tar -zxvf pcre-8.38.tar.gz #进入安装目录 cd pcre-8.38 #检查配置 ./configure #编译、安装 make && make install #查看pcre版本 pcre-config --version3.1.3. Pasang nginx
#进入目录 cd /usr/local/develop/anginx #上传安装文件,并解压 tar -zxvf nginx-1.8.1.tar.gz #进入安装目录 cd nginx-1.8.1 #检查配置 ./configure --prefix=/usr/local/develop/anginx/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/develop/anginx/pcre-8.38 #编译安装 make && make install #查看nginx版本 /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v -------------------------------------------------------- [root@hadoop02 webserver]# /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v nginx version: nginx/1.8.1 #配置nginx(检查) /usr/local/develop/anginx/webserver/nginx/sbin/nginx -t #nginx管理命令 /usr/local/develop/anginx/webserver/nginx/sbin/nginx # 启动 nginx /usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop # 停止 nginx /usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件 /usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reopen # 重启 nginx3.1.4.nginx konfigurasi asas
vi nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #添加tomcat列表,真实应用服务器都放在这 upstream tomcat_pool{ #server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大; server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s; server 192.168.80.22:8081 weight=4 max_fails=2 fail_timeout=30s; } server { listen 80; server_name tomcat_pool; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; proxy_pass http://tomcat_pool; #转向tomcat处理 proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
3.2. Arahan : Kaedah pemasangan adalah sama seperti nod induk nginx.
4. Pasang keepalived
4.1. Nod induk (192.168.80.22)
rreeeMengenai arahan konfigurasi:
[router_id] ialah pengecam penghalaan, yang sepatutnya unik dalam LAN[auth_type] ialah kaedah Pengesahan
#安装keepalived yum install keepalived -y #启动keepalived服务 /etc/init.d/keepalived start ------------------------------------------- [root@hadoop02 anginx]# /etc/init.d/keepalived start 正在启动 keepalived: [确定] [root@hadoop02 anginx]# ps -ef |grep keepalived root 15723 1 0 00:59 ? 00:00:00 /usr/sbin/keepalived -d root 15724 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -d root 15725 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -d root 15731 15622 0 00:59 pts/1 00:00:00 grep keepalived [root@hadoop02 anginx]# #设置开机自启动 echo "/etc/init.d/keepalived start" >>/etc/rc.local #关闭keepalived服务 /etc/init.d/keepalived stop #编辑keepalived配置文件 vi /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 lb01 } vrrp_instance vi_1 { state master interface eth1 virtual_router_id 55 priority 150 advert_int 1 authentication { auth_type pass auth_pass server123 } virtual_ipaddress { 192.168.80.100 dev eth1 label eth1:1 } } ...........................................................5.1
#安装keepalived yum install keepalived -y #启动keepalived服务 /etc/init.d/keepalived start ------------------------------------------- [root@hadoop02 anginx]# /etc/init.d/keepalived start 正在启动 keepalived: [确定] [root@hadoop02 anginx]# ps -ef |grep keepalived root 15723 1 0 00:59 ? 00:00:00 /usr/sbin/keepalived -d root 15724 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -d root 15725 15723 0 00:59 ? 00:00:00 /usr/sbin/keepalived -d root 15731 15622 0 00:59 pts/1 00:00:00 grep keepalived [root@hadoop02 anginx]# #设置开机自启动 echo "/etc/init.d/keepalived start" >>/etc/rc.local #关闭keepalived服务 /etc/init.d/keepalived stop #编辑keepalived配置文件 vi /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 lb02 } vrrp_instance vi_1 { state backup interface eth1 virtual_router_id 55 priority 100 advert_int 1 authentication { auth_type pass auth_pass server123 } virtual_ipaddress { 192.168.80.100 dev eth1 label eth1:1 } } .............................................................5.2 Akses perkhidmatan melalui IP maya
http://192.168.80.100/session-redis-demo/
5.3. Hentikan perkhidmatan Node keepalived hos
#在节点一执行(192.168.80.22) /etc/init.d/keepalived start ------------------------------------- [root@hadoop02 anginx]# ps -ef |grep keepalived root 15788 1 0 01:09 ? 00:00:00 /usr/sbin/keepalived -d root 15790 15788 0 01:09 ? 00:00:00 /usr/sbin/keepalived -d root 15791 15788 0 01:09 ? 00:00:00 /usr/sbin/keepalived -d root 15807 15622 0 01:33 pts/1 00:00:00 grep keepalived [root@hadoop02 anginx]# #在节点二执行(192.168.80.21) /etc/init.d/keepalived start --------------------------------------- [root@hadoop01 ~]# ps -ef |grep keepalived root 11542 1 0 01:30 ? 00:00:00 /usr/sbin/keepalived -d root 11544 11542 0 01:30 ? 00:00:00 /usr/sbin/keepalived -d root 11545 11542 0 01:30 ? 00:00:00 /usr/sbin/keepalived -d root 11550 11512 0 01:33 pts/1 00:00:00 grep keepalived [root@hadoop01 ~]#5.4 Teruskan mengakses perkhidmatan melalui ip mayahttp://. 192.168.80.100/session-redis-demo/
6.keepalived+nginx integration
Arahan: Tulis skrip nginx daemon . Jika perkhidmatan nginx gagal, hentikan perkhidmatan kekal nod semasa. Beralih ke nod sandaran secara automatik.
6.1. Tulis skrip daemon nginx
#在节点一执行(192.168.80.22) /etc/init.d/keepalived stop #观察备节点变化 ip addr ------------------------------------------- [root@hadoop01 ~]# ip addr 1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth1: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000 link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff inet 192.168.80.21/24 brd 192.168.80.255 scope global eth1 inet 192.168.80.100/32 scope global eth1:1 inet6 fe80::250:56ff:fe38:e546/64 scope link valid_lft forever preferred_lft forever [root@hadoop01 ~]#
6.2. Hentikan perkhidmatan nginx nod induk
rreee
Atas ialah kandungan terperinci Bagaimana untuk mencapai ketersediaan tinggi keepalived+nginx. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!