keepalived高可用nginx

實驗拓撲:

wKiom1gl3jziZXdyAACuXCAga-U585.png


1.在兩臺realserver上配置web服務

wKioL1gl3prBYIqvAAAh06bMISc370.png


2.keepalived配置

master的配置

global_defs {
   notification_email {
       root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
}

vrrp_script chk_down {    #keepalived控制腳本       
	script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"      #腳本內容
	interval 2
	weight -6
}

vrrp_script chk_nginx {            #檢測nginx服務腳本
	script "killall -0 nginx && exit 0 || exit 1"   #腳本內容     
	interval 2
	weight -6
}

vrrp_instance VI_1 {    #配置實例
    state MASTER
    interface eno16777736
    virtual_router_id 88
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
		10.1.88.88/16 dev eno16777736 label eno16777736:0
    }

	track_script {    #調用腳本
		chk_down
		chk_nginx
	}

	notify_master "/etc/keepalived/notify.sh master"    #定義通告腳本
	notify_backup "/etc/keepalived/notify.sh backup"    
	notify_fault  "/etc/keepalived/notify.sh fault"
}

backup的配置:

global_defs {
   notification_email {
       root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
}

vrrp_script chk_down {
	script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
	interval 2
	weight -6
}

vrrp_script chk_nginx {
	script "killall -0 nginx && exit 0 || exit 1"
	interval 2
	weight -6
}

vrrp_instance VI_1 {
    state BACKUP    
    interface eno16777736
    virtual_router_id 88
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
		10.1.88.88/16 dev eno16777736 label eno16777736:0
    }

	track_script {
		chk_down
		chk_nginx
	}

	notify_master "/etc/keepalived/notify.sh master"
	notify_backup "/etc/keepalived/notify.sh backup"
	notify_fault  "/etc/keepalived/notify.sh fault"
}

3.nginx調度器配置

兩臺nginx調度器的配置文件

#http段中的配置
http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

	upstream webserver {
		server 10.1.68.5 weight=1 max_fails=2;
		server 10.1.68.6 weight=1 max_fails=2;
	}

	server {
		listen 80;
		server_name localhost;

		location / {
			index index.html index.htm;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_pass http://webserver;
		}
	}

}

在兩臺調度器上同時啓動nginx和keepalived,此時VIP在nginx1主機上

wKiom1gmwG6QBUnMAAAvmDWMZuQ466.png


4.測試

通過VIP訪問,可正常調度

wKioL1gmwRmjfTgQAABFoeUeRcM238.png


停止調度器1上面的nginx服務,VIP漂移至nginx2主機上

wKioL1gmweGxEKsCAABzZhKdZnk786.png

wKioL1gmwgSy8qdoAAAsDxDaZPU964.png


依然可以正常調度

wKiom1gmwkng4IsdAAA1jZz97gs945.png





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章