nginx服务器做apache服务器的负载均衡
在nginx上设置keepalived实现集群的高可用
NFS/DNS服务器实现共享存储,DNS解析虚拟ip
环境:VMvare+KVM
step1:创建虚拟机
以下是各个服务器的IP地址
192.168.122.151 keepalived MASTER knb1
192.168.122.152 keepalived SLAVE knb2
192.168.122.153 apache1 apache1
192.168.122.154 apache2 apache2
192.168.122.155 nfs/dns nd
192.168.122.160 apache虚拟IP地址
192.168.122.1 网关地址,宿主机地址
step2:安装nginx
①安装nginx确认包
yum install pcre-devel openssl-devel -y
安装nginx依赖包
wget http://nginx.org/download/nginx-1.26.2.tar.gz
获取nginx源码安装包
tar -xvf nginx-1.26.2.tar.gz -C /usr/local/src
解压缩
useradd -s /sbin/nologin -M nginx
添加nginx用户
./configure --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
检查nginx安装环境是否正确
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
出现上述信息,说明检查成功
make && make install
编译安装
make[1]: 离开目录“/usr/local/src/nginx-1.26.2”
/usr/local/nginx/sbin/nginx
出现上述信息,安装完成
[root@knb1 nginx-1.26.2]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
出现上述信息,说明nginx启动成功
step3:安装keepalived服务
yum install keepalived -y
step4:编辑配置文件
①nginx
vi /usr/local/nginx/conf/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 {
upstream apache {
server 192.168.122.153:80;
server 192.168.122.154:80;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://apache;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
②修改keepalived配置文件
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.122.160/24
}
}
step5:配置双网卡
①添加网卡
[root@knb1 ~]# virsh attach-interface knb1 --type bridge --source virbr0 --persistent
[root@knb2 ~]# virsh attach-interface knb2 --type bridge --source virbr0 --persistent
[root@knb1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
link/ether 52:54:00:81:73:63 brd ff:ff:ff:ff:ff:ff
3: ens9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
link/ether 52:54:00:4b:ba:6c brd ff:ff:ff:ff:ff:ff
5: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52:54:00:4b:ba:6c brd ff:ff:ff:ff:ff:ff
inet 192.168.122.151/24 brd 192.168.122.255 scope global noprefixroute bond0
valid_lft forever preferred_lft forever
inet 192.168.122.160/24 scope global secondary bond0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fe4b:ba6c/64 scope link
valid_lft forever preferred_lft forever
②编辑网卡文件
[root@knb1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
USERCTL=no
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
[root@knb1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-en9
DEVICE=ens9
USERCTL=no
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
[root@knb1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
NAME=bond0
IPADDR=192.168.122.151
NETMASK=255.255.255.0
GATEWAY=192.168.122.1
DNS1=114.114.114.114
ONBOOT=yes
BOOTPROTO=none
BONDING_OPTS="miimon=50 mode=1 fail_over_mac=1"
[root@knb1 ~]# systemctl restart network
[root@knb1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
link/ether 52:54:00:81:73:63 brd ff:ff:ff:ff:ff:ff
3: ens9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
link/ether 52:54:00:4b:ba:6c brd ff:ff:ff:ff:ff:ff
5: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52:54:00:4b:ba:6c brd ff:ff:ff:ff:ff:ff
inet 192.168.122.151/24 brd 192.168.122.255 scope global noprefixroute bond0
valid_lft forever preferred_lft forever
inet 192.168.122.160/24 scope global secondary bond0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fe4b:ba6c/64 scope link
valid_lft forever preferred_lft forever
配置成功
step6:配置dns
[root@nd ~]# yum install bind
[root@nd ~]# vi /etc/named.conf
//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//
// See the BIND Administrator's Reference Manual (ARM) for details about the
// configuration located in /usr/share/doc/bind-{version}/Bv9ARM.html
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
[root@nd ~]# vi /etc/named.rfc1912.zones
zone "XMJ.com" IN {
type master;
file "XMJ.com.zone";
};
[root@nd ~]# cd /var/named/
[root@nd named]# cp -a named.localhost XMJ.com.zone
[root@nd named]# vi XMJ.com.zone
$TTL 1D
@ IN SOA dns.XMJ.com. root. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
XMJ.com. IN NS dns.XMJ.com.
dns.XMJ.com. IN A 192.168.122.155
www.XMJ.com. IN A 192.168.122.160
[root@nd named]# vi /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.122.155
[root@nd named]# systemctl restart named
[root@nd named]# host www.XMJ.com
www.XMJ.com has address 192.168.122.160
step7:配置nfs
[root@nd ~]# yum install nfs-utils
[root@nd ~]# mkdir /nfsa1
[root@nd ~]# mkdir /nfsa2
[root@nd ~]# vim /etc/exports
/nfsa1 *(rw,sync)
/nfsa2 *(rw,sync)
[root@nd ~]# systemctl restart nfs
step8:挂载
[root@apache1 ~]# mount -t nfs 192.168.122.155:/nfsa1 /var/www/html/
[root@apache2 ~]# mount -t nfs 192.168.122.155:/nfsa2 /var/www/html/
[root@apache1 ~]# echo apache111111111 > /nfsa1/index.html
[root@apache2 ~]# echo apache222222222 > /nfsa2/index.html
step9:测试
在宿主机上测试
[root@lvs ~]# curl 192.168.122.160
apache222222222
[root@lvs ~]# curl 192.168.122.160
apache111111111
[root@lvs ~]# curl www.XMJ.com
apache222222222
[root@lvs ~]# curl www.XMJ.com
apache111111111