清风
高可用01.png

关闭防火墙

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

配置环境

[root@localhost ~]# vim /etc/hosts
192.168.116.121	 tomcat  安装tomcat
192.168.116.122	 php     安装php-fpm、nginx
192.168.116.131	 nginx1  安装nginx
192.168.116.132	 nginx2  安装nginx
192.168.116.111	 master  安装lvs、keepalived
192.168.116.156	 backup  安装lvs、keepalived``

1、安装tomcat步骤--192.168.116.121

1、安装 JDK(java)
1.上传jdk到服务器中,安装jdk
[root@localhost ~]# mkdir /application   #创建工作目录
[root@localhost ~]# tar xzf jdk-8u60-linux-x64.tar.gz -C /application/
[root@localhost ~]# mv /application/jdk1.8.0_60 /application/jdk
# 设置环境变量
[root@localhost ~]# vim /etc/profile
JAVA_HOME=/usr/local/java #指定java安装目录
PATH=$JAVA_HOME/bin:$PATH #用于指定java系统查找命令的路径
export JAVA_HOME PATH #类的路径,在编译运行java程序时,如果有调用到其他类的时候,在classpath中寻找需要的类。
[root@localhost ~]# source /etc/profile  #让环境变量生效
#测试jdk是否安装成功
[root@localhost ~]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

2、安装Tomcat
将tomcat安装包上传到服务器中:
[root@localhost ~]# tar xzf apache-tomcat-8.0.27.tar.gz -C /application/
[root@localhost ~]# mv /application/apache-tomcat-8.0.27 /application/tomcat
# 设置环境变量
[root@localhost ~]# echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile
[root@localhost ~]# source /etc/profile
启动tomcat
[root@localhost ~]# /application/tomcat/bin/startup.sh

测试

网址输入192.168.116.121:8080,会出来tomcat测试页面192.168.116.121的tomcat的测试页面

2、安装php-fpm/nginx--步骤--192.168.116.122

#下载nginx
# yum安装nginx
[root@localhost ~]#  vim /etc/yum.repo.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@localhost ~]#  yum install nginx -y
[root@localhost ~]#  systemctl start nginx
[root@localhost ~]#  systemctl enable nginx

# 下载PHP
[root@localhost ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@localhost ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@localhost ~]#  yum install php71w-xsl php71w 
[root@localhost ~]#  yum -y install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt		//php依赖包
[root@localhost ~]#  yum install -y php71w-fpm	//php主包
[root@localhost ~]#  systemctl start php-fpm
[root@localhost ~]#  systemctl enable php-fpm

# 验证nginx/php-fpm有没有起来
[root@localhost ~]#  systemctl status nginx
[root@localhost ~]#  systemctl status php-fpm

# 编辑配置文件
vim /etc/nginx/conf.d/php.conf
server {
        listen      80;
        server_name     localhost;
      location ~ \.php$ {
            root       /usr/share/nginx/html;  #指定网站目录
            fastcgi_pass   127.0.0.1:9000;    #开启fastcgi连接php地址
            fastcgi_index  index.php;		#指定默认文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
            include        fastcgi_params;  #包含fastcgi使用的常量
        		}
        }
        
[root@localhost ~]#  vim /usr/share/nginx/html/test.php
<?php
 phpinfo();
?>

# 重启nginx
[root@localhost ~]#  systemctl restart nginx

测试

输入网址192.168.116.122/test.php	//会出来php的测试页面

3、安装nginx步骤--192.168.116.131

# yum安装nginx--代理tomcat
[root@localhost ~]#  vim /etc/yum.repo.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@localhost ~]#  yum install nginx -y
[root@localhost ~]#  systemctl start nginx

# 配置负载均衡
[root@localhost ~]#  vim /etc/nginx/conf.d/upstream.conf
upstream tomcat01 {
      server 192.168.116.121:8080;
      server 192.168.116.122;
    }


# 配置代理
[root@localhost ~]#  vim /etc/nginx/conf.d/tomcat_01.conf
server {
        listen       80;
        server_name localhost;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://tomcat01;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

# 重启nginx
systemctl restart nginx

测试

输入网址192.168.116.131  //会出来192.168.116.(121/122)的页面

4、安装nginx步骤--192.168.116.132

# yum安装nginx--代理php
[root@localhost ~]#  vim /etc/yum.repo.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@localhost ~]#  yum install nginx -y
[root@localhost ~]#  systemctl start nginx

# 负载均衡192.168.116.122
vim /etc/nginx/conf.d/upstream.conf
upstream php01 {
      server 192.168.116.121:8080;
      server 192.168.116.122;
    }


# 代理配置
vim /etc/nginx/conf.d/php_01.conf
server {
     listen      80;
     server_name     localhost;

     location / {
            proxy_pass http://php01;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
}

# 重启nginx
systemctl restart nginx

测试

输入网址192.168.116.132	//会出来192.168.116.(121/122)的页面

5、配置(lvs--keepalived)--192.168.116.111(MASTER)

1. 主/备调度器安装软件
[root@lvs-keepalived-master ~]# yum -y install ipvsadm keepalived 

2、配置keepalived,conf配置文件
lvs-master
[root@ha-proxy-master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lvs-keepalived-master    #辅助改为lvs-backup
}

vrrp_instance VI_1 {
    state MASTER	(修改的)
    interface ens33		(修改的)                #VIP绑定接口
    virtual_router_id 80         #VRID 同一组集群,主备一致          
    priority 100            #本节点优先级,辅助改为50
    advert_int 1            #检查间隔,默认为1s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.246.11/32	(修改的)
    }
}

virtual_server 192.168.246.11 80 {   	(修改的) #LVS配置
	delay_loop 6   #健康检查rs时间间隔
	lb_algo rr    	(修改的)	 #LVS调度算法
	lb_kind DR     	(修改的)	#LVS集群模式(路由模式)
	protocol TCP      #健康检查使用的协议
	real_server 192.168.116.131 80 {		(修改的)
		weight 1
		inhibit_on_failure   #当该节点失败时,把权重设置为0,而不是从IPVS中删除
		TCP_CHECK {          #健康检查
			connect_port 80   #检查的端口
			connect_timeout 3  #连接超时的时间
			}
		}
	real_server 192.168.116.132 80 {		(修改的)
		weight 1
		inhibit_on_failure
		TCP_CHECK {
			connect_timeout 3
			connect_port 80
			}
		}
}

3. 启动KeepAlived(主备均启动)
[root@lvs-keepalived-master ~]# systemctl start keepalived
[root@lvs-keepalived-master ~]# systemctl enable keepalived
# 查看ip
[root@lvs-keepalived-master ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.246.110:80 rr persistent 20
  -> 192.168.246.162:80           Route   1      0          0         
  -> 192.168.246.163:80           Route   0      0          0

#配置rs后端机器配置
4. 所有RS配置(nginx1,nginx2)
配置好网站服务器,测试所有RS
[root@test-nginx1 ~]# ip addr add dev lo 192.168.246.11/32
[root@test-nginx1 ~]# sysctl -p
[root@test-nginx1 ~]# systemctl start nginx

测试

输入网址192.168.116.11	//会出来192.168.116.(131/132)的页面

6、配置(lvs--keepalived)--192.168.116.156(BACKUP)

1. 主/备调度器安装软件
[root@lvs-keepalived-master ~]# yum -y install ipvsadm keepalived 

2、配置keepalived,conf配置文件
[root@lvs-keepalived-slave ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lvs-keepalived-slave		(修改的)
}

vrrp_instance VI_1 {
    state BACKUP		(修改的)
    interface ens33		(修改的)
    virtual_router_id 80
    priority 50			(修改的)
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.116.11/32		(修改的)
    }
}
virtual_server 192.168.246.11 80 {	(修改的)
	delay_loop 6
	lb_algo rr		(修改的)
	lb_kind DR		(修改的)
	protocol TCP
	real_server 192.168.116.131 80 {		(修改的)
		weight 1
		inhibit_on_failure
		TCP_CHECK {
			connect_port 80
			connect_timeout 3
			}
		}
	real_server 192.168.116.132 80 {		(修改的)
		weight 1
		inhibit_on_failure
		TCP_CHECK {
			connect_timeout 3
			connect_port 80
			}
}

3. 启动KeepAlived(主备均启动)
[root@lvs-keepalived-master ~]# systemctl start keepalived
[root@lvs-keepalived-master ~]# systemctl enable keepalived

# 查看ip
[root@lvs-keepalived-master ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.246.110:80 rr persistent 20
  -> 192.168.246.162:80           Route   1      0          0         
  -> 192.168.246.163:80           Route   0      0          0
  
#配置rs后端机器配置
4. 所有RS配置(nginx1,nginx2)
配置好网站服务器,测试所有RS
[root@test-nginx1 ~]# ip addr add dev lo 192.168.246.11/32
[root@test-nginx1 ~]# sysctl -p
[root@test-nginx1 ~]# systemctl start nginx

测试

输入网址192.168.116.11	//会出来192.168.116.(131/132)的页面