keepalived+nginx 负载均衡+高可用
准备三台机器
192.168.198.133 nginx-1 centos7
192.168.198.136 nginx-2 centos7
192.168.198.135 nginx-3 centos7
VIP 192.168.198.111
使用的是yum 安装
首先下载ali的centos_bash 源和epel源,因为国内的比较快,习惯用阿里也可以使用163或者是别的
在nginx-1
ssh-****** -t rsa 一路回车
ssh-copy-id nginx-2
ssh-copy-id nginx-3
成功推了公钥就可以免密登录了
cd /etc/yum.repos.d
mkdir bak
mv * bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache fast
下载epel
yum install epel-release -y
然后可以安装nginx和keepalived
在ngin-1安装nginx
yum list | grep nginx
r然后yum install nginx
配置nginx的配置文件
vim /etc/nginx/nginx.conf
加上你要跳转的的地址如图
如图所示:这是nginx的配置文件
[[email protected] nginx]# cat nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
upstream webs{
server 192.168.198.135:80 weight=1;
server 192.168.198.136:80 weight=1;
}
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name webs;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://webs;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
[[email protected] nginx]# ^C
以上是我的配置文件,然后安装keepalived
yum install keepalived
我安装的版本就是 1.3.5的版本
编辑keepalive的配置文件
keepalived一主两从的关系,然后需要注意的state是master还是backup还有就是优先级注意
首先咱们需要做的是将 keepalived.conf 备份
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
删除里边的全部
dG
然后
[[email protected] nginx]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs{
script_user root
enable_script_security
router_id director2
}
vrrp_script nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
track_script {
nginx
}
vrrp_instance VI_1 {
state MASTER
interface ens33
#nopreempt
virtual_router_id 80
priority 30 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.198.111
}
}
然后将其他两台安装上nginx和keepalived,nginx的配置不变,keepalived的backup只需将backup的优先级改成比master小 state改成 backup即可,两个backup依次递减
然后访问
就可成功