- | - | - |
---|---|---|
主机名 | IP地址 | 安装服务(角色) |
porxy | 192.168.0.10 | 客户端测试访问 |
web1 | 192.168.0.20 | nginx |
web2 | 192.168.0.21 | nginx |
haproxy-0001 | 192.168.0.22 | haproxy,keepalived |
haproxy-0002 | 192.168.0.23 | haproxy,keepalived |
vip | 192.168.0.100 | keepalived提供的vip |
haproxy
keepalived
[root@web1 ~]# yum -y install gcc make
[root@web1 ~]# yum -y install pcre-devel
[root@web1 ~]# yum -y install openssl openssl-devel
[root@web1 ~]# tar xf nginx-1.17.6.tar.gz
[root@web1 ~]# cd nginx-1.17.6
[root@web1 nginx-1.17.6]# ./configure --with-http_ssl_module
[root@web1 nginx-1.17.6]# make && make install
[root@web1 nginx-1.17.6]# ls /usr/local/nginx/
conf html logs sbin
[root@web1 nginx-1.17.6]# cd /usr/local/nginx/
#nginx默认的配置,打开解析php文件,实现动静分离,注意此处修改为include fastcgi.conf;
[root@web1 nginx]# vim conf/nginx.conf
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi.conf;
71 }
#准备静态页面
[root@web1 nginx]# vim html/index.html
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
#准备动态页面
[root@web1 nginx]# vim html/test.php
<pre>
<?PHP
//phpinfo();
//$arr = array("id" => random_int(0000,9999));
foreach (array("REMOTE_ADDR", "REQUEST_METHOD", "HTTP_USER_AGENT", "REQUEST_URI") as $i) {
$arr[$i] = $_SERVER[$i];
}
if($_SERVER['REQUEST_METHOD']=="POST"){
$arr += $_POST;
}else{
$arr += $_GET;
}
print_R($arr);
print_R("php_host: \t".gethostname()."\n");
$n = 0;
$start = 1;
$end = isset($_GET["id"])? $_GET["id"] : 10000 ;
for($num = $start; $num <= $end; $num++) {
if ( $num == 1 ) continue;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) continue 2;
}
$n++;
}
print_R($n."\n");
?>
yum -y install php
yum -y install php-fpm
[root@web1 nginx]# systemctl enable php-fpm --now
[root@web1 nginx]# sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web1 nginx]# sbin/nginx
[root@web1 nginx]# ss -utnlp | grep 80
tcp LISTEN 0 511 *:80 *:* users:(("nginx",pid=16056,fd=6),("nginx",pid=16055,fd=6))
[root@web1 nginx]# ss -utnlp | grep 9000
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=16052,fd=0),("php-fpm",pid=16051,fd=0),("php-fpm",pid=16050,fd=0),("php-fpm",pid=16049,fd=0),("php-fpm",pid=16048,fd=0),("php-fpm",pid=16047,fd=6))
#关闭nginx服务
[root@web1 ~]# /usr/local/nginx/sbin/nginx -s sotp
#编写Unit文件,使systemctl命令控制nginx
[root@web1 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
#nginx是多进程类型程序,要设置为forking
Type=forking
#当执行了systemctl start nginx之后执行的命令
ExecStart=/usr/local/nginx/sbin/nginx
#当执行了systemctl reload nginx之后执行的命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#当执行了systemctl stop nginx之后执行的命令,这里是用kill命令发送退出信号给nginx的进程号,相当于停止nginx服务,-s QUIT是发送退出信号,$MAINPID是变量,里面存了nginx的进程号
ExecStop=/bin/kill -s QUIT ${MAINPID}
[Install]
#支持开机自启
WantedBy=multi-user.target
#激活刚才的test.service文件,但有时可能不好使,可以重启系统然后重启服务之后可以用systemctl等命令控制nginx
[root@web1 ~]# systemctl daemon-reload
#启动服务并设置开机自启
[root@web1 ~]# systemctl enable nginx --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@web1 ~]# ss -utnlp | grep nginx
tcp LISTEN 0 511 *:80 *:* users:(("nginx",pid=1748,fd=6),("nginx",pid=1747,fd=6))
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
[root@web1 nginx]# curl localhost
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~
</marquee>
</html>
[root@web1 nginx]# curl localhost/test.php
<pre>
Array
(
[REMOTE_ADDR] => 127.0.0.1
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web1
1229
[root@web2 ~]# yum -y install gcc make
[root@web2 ~]# yum -y install pcre-devel
[root@web2 ~]# yum -y install openssl openssl-devel
[root@web2 ~]# tar xf nginx-1.17.6.tar.gz
[root@web2 ~]# cd nginx-1.17.6/
[root@web2 nginx-1.17.6]# ./configure --with-http_ssl_module
[root@web2 nginx-1.17.6]# make && make install
[root@web2 nginx-1.17.6]# cd /usr/local/nginx/
[root@web2 nginx]# vim conf/nginx.conf
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi.conf;
71 }
[root@web2 nginx]# vim html/index.html
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web2~~~~
</marquee>
</html>
#准备动态页面
[root@web2 nginx]# vim html/test.php
<pre>
<?PHP
//phpinfo();
//$arr = array("id" => random_int(0000,9999));
foreach (array("REMOTE_ADDR", "REQUEST_METHOD", "HTTP_USER_AGENT", "REQUEST_URI") as $i) {
$arr[$i] = $_SERVER[$i];
}
if($_SERVER['REQUEST_METHOD']=="POST"){
$arr += $_POST;
}else{
$arr += $_GET;
}
print_R($arr);
print_R("php_host: \t".gethostname()."\n");
$n = 0;
$start = 1;
$end = isset($_GET["id"])? $_GET["id"] : 10000 ;
for($num = $start; $num <= $end; $num++) {
if ( $num == 1 ) continue;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) continue 2;
}
$n++;
}
print_R($n."\n");
?>
yum -y install php
yum -y install php-fpm
[root@web2 nginx]# systemctl enable php-fpm --now
[root@web2 nginx]# sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web2 nginx]# sbin/nginx
[root@web2 nginx]# ss -utnlp | grep 80
udp UNCONN 0 0 *:68 *:* users:(("dhclient",pid=580,fd=6))
tcp LISTEN 0 511 *:80 *:* users:(("nginx",pid=13691,fd=6),("nginx",pid=13690,fd=6))
[root@web2 nginx]# ss -utnlp | grep 9000
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=13687,fd=0),("php-fpm",pid=13686,fd=0),("php-fpm",pid=13685,fd=0),("php-fpm",pid=13684,fd=0),("php-fpm",pid=13683,fd=0),("php-fpm",pid=13682,fd=6))
#关闭nginx服务
[root@web2 ~]# /usr/local/nginx/sbin/nginx -s sotp
#编写Unit文件,使systemctl命令控制nginx
[root@web2 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
#nginx是多进程类型程序,要设置为forking
Type=forking
#当执行了systemctl start nginx之后执行的命令
ExecStart=/usr/local/nginx/sbin/nginx
#当执行了systemctl reload nginx之后执行的命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#当执行了systemctl stop nginx之后执行的命令,这里是用kill命令发送退出信号给nginx的进程号,相当于停止nginx服务,-s QUIT是发送退出信号,$MAINPID是变量,里面存了nginx的进程号
ExecStop=/bin/kill -s QUIT ${MAINPID}
[Install]
#支持开机自启
WantedBy=multi-user.target
#激活刚才的test.service文件,但有时可能不好使,可以重启系统然后重启服务之后可以用systemctl等命令控制nginx
[root@web2 ~]# systemctl daemon-reload
#启动服务并设置开机自启
[root@web2 ~]# systemctl enable nginx --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@webw ~]# ss -utnlp | grep nginx
tcp LISTEN 0 511 *:80 *:* users:(("nginx",pid=1748,fd=6),("nginx",pid=1747,fd=6))
[root@web2 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
[root@web2 nginx]# curl localhost
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web2~~~~
</marquee>
</html>
[root@web2 nginx]# curl localhost/test.php
<pre>
Array
(
[REMOTE_ADDR] => 127.0.0.1
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web2
1229
[root@haproxy-0001 ~]# yum -y install haproxy
vim /etc/haproxy/haproxy.cfg
# 配置文件中,global是全局配置;default是缺省配置,如果后续有和default相同的配置,default配置将会被覆盖。
# 配置文件中,frontend描述haproxy怎么和用户交互;backend描述haproxy怎么和后台应用服务器交互。这两个选项,一般不单独使用,而是合并到一起,名为listen。
# 将61行之后全部删除,写入以下内容
61 listen myweb 0.0.0.0:80 # 定义本机监听地址
62 balance roundrobin # 调度算法为轮询
# 对web服务器做健康检查,2秒检查一次,如果连续2次检查成功,认为服务器是健康的,如果连续5次检查失败,认为服务器坏了
63 server web1 192.168.0.20 check inter 2000 rise 2 fall 5
64 server web2 192.168.0.21 check inter 2000 rise 2 fall 5
systemctl enable haproxy.service --now
ss -utnlp | grep 80
tcp LISTEN 0 1024 *:80 *:* users:(("haproxy",pid=10946,fd=5))
#访问静态页面
[root@haproxy-0001 ~]# for i in {1..6}; do curl http://192.168.0.22/; done
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web2~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web2~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web2~~~~
</marquee>
</html>
#访问动态页面
[root@haproxy-0001 ~]# for i in {1..6}; do curl http://192.168.0.22/test.php; done
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.22
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web1
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.22
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web2
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.22
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web1
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.22
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web2
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.22
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web1
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.22
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web2
1229
[root@haproxy-0002 ~]# curl http://192.168.0.23
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
[root@haproxy-0002 ~]# for i in {1..6}; do curl http://192.168.0.23/; done
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web2~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web2~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web2~~~~
</marquee>
</html>
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
[root@haproxy-0002 ~]# for i in {1..6}; do curl http://192.168.0.23/test.php; done
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.23
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web2
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.23
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web1
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.23
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web2
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.23
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web1
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.23
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web2
1229
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.23
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web1
1229
[root@haproxy-0001 ~]# yum -y install keepalived.x86_64
[root@haproxy-0001 ~]# vim /etc/keepalived/keepalived.conf
12 router_id haproxy-0001 # 设置本机在集群中的唯一识别符
13 vrrp_iptables ## 自动配置iptables放行规则
14 vrrp_skip_check_adv_addr
15 vrrp_strict
16 vrrp_garp_interval 0
17 vrrp_gna_interval 0
18 }
19
20 vrrp_instance VI_1 {
21 state MASTER # 状态,主为MASTER,备为BACKUP
22 interface eth0 # haproxy服务使用的网卡
23 virtual_router_id 51 # 虚拟路由器地址
24 priority 100 # 优先级,数字越大越优先
25 advert_int 1 # 发送心跳消息的间隔
26 authentication {
27 auth_type PASS # 认证类型为共享密码
28 auth_pass 1111 # 集群中的机器密码相同,才能成为集群
29 }
30 virtual_ipaddress {
31 192.168.0.100/24 # VIP地址,可配置多个
32 }
33 }
# 删除下面所有行
#启动keepalived,并设置开机自启
[root@haproxy-0001 ~]# systemctl enable keepalived.service --now
#能看到vip 192.168.0.100/24
[root@haproxy-0001 ~]# ip a s
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,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fa:16:3e:e0:38:4e brd ff:ff:ff:ff:ff:ff
inet 192.168.0.22/24 brd 192.168.0.255 scope global noprefixroute dynamic eth0
valid_lft 84683sec preferred_lft 84683sec
inet 192.168.0.100/24 scope global secondary eth0
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fee0:384e/64 scope link
valid_lft forever preferred_lft forever
[root@haproxy-0002 ~]# yum -y install keepalived.x86_64
[root@haproxy-0002 ~]# vim /etc/keepalived/keepalived.conf
12 router_id haproxy-0002 # 设本机在集群中的唯一识别符
13 vrrp_iptables ## 自动配置iptables放行规则
14 vrrp_skip_check_adv_addr
15 vrrp_strict
16 vrrp_garp_interval 0
17 vrrp_gna_interval 0
18 }
19
20 vrrp_instance VI_1 {
21 state BACKUP # 状态,主为MASTER,备为BACKUP
22 interface eth0 # haproxy服务使用的网卡
23 virtual_router_id 51 # 虚拟路由器地址
24 priority 80 # 优先级,数字越大越优先
25 advert_int 1 # 发送心跳消息的间隔
26 authentication {
27 auth_type PASS # 认证类型为共享密码
28 auth_pass 1111 # 集群中的机器密码相同,才能成为集群
29 }
30 virtual_ipaddress {
31 192.168.0.100/24 # VIP地址,可配置多个
32 }
33 }
# 删除下面所有行
#启动keepalived,并设置开机自启
[root@haproxy-0002 ~]# systemctl enable keepalived.service --now
云主机需要关闭服务器的源目的地址检查,haproxy-0001,haproxy-0002都需要关闭
[root@proxy ~]# curl 192.168.0.100
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World web1~~~~
</marquee>
</html>
[root@proxy ~]# curl 192.168.0.100/test.php
<pre>
Array
(
[REMOTE_ADDR] => 192.168.0.22
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /test.php
)
php_host: web2
1229
详细步骤请参考https://support.huaweicloud.com/usermanual-vpc/zh-cn_topic_0067802474.html
在管理控制台左上角单击,选择区域和项目。
在系统首页,选择“网络>虚拟私有云”。
进入“我的VPC”页面。
笔者这里申请的为keepalievd设置的虚拟IP192.168.0.100,把虚拟IP绑定给实例haproxy-0001和proxy-0002
选择需要绑定的弹性公网IP地址或弹性云服务器(及网卡)。
笔者把新购买的弹性公网IP绑定到虚拟IP192.168.0.100
说明:
弹性云服务器多网卡时,建议绑定主网卡。
一个弹性云服务器的网卡可以绑定多个虚拟IP。
IPv6的虚拟IP仅支持绑定一个网卡(双栈网卡),如需进行服务器的主备切换,请通过调用API方式。具体请参考配置云服务器高可用的IPv6虚拟IP功能。
为已绑定虚拟IP的弹性云服务器手工配置虚拟IP地址。