当前位置:   article > 正文

roles、通过roles实现lamp及lnmp、数据库的高可用和负载均衡的实现

roles、通过roles实现lamp及lnmp、数据库的高可用和负载均衡的实现

roles(难点)

1、介绍

roles(⻆⾊): 就是通过分别将variables, tasks及handlers等放置于单独的⽬录中,并可以便捷地调⽤它们的⼀种机制。假设我们要写⼀个playbook来安装管理lamp环境,那么这个playbook就会写很⻓。所以我们希望把这个很⼤的⽂件分成多个功能拆分, 分成apache管理,php管理,mysql管理,然后在需要使⽤的时候直接调⽤就可以了,以免重复写。就类似编程⾥的模块化的概念,以

2、创建roles的目录结构

files:⽤来存放由copy模块或script模块调用的⽂件。

tasks:⾄少有⼀个main.yml⽂件,定义各tasks。

handlers:有⼀个main.yml⽂件,定义各handlers。

templates:⽤来存放jinjia2模板。

vars:有⼀个main.yml⽂件,定义变量。

meta:有⼀个main.yml⽂件,定义此⻆⾊的特殊设定及其依赖关系。

注意: 在每个⻆⾊的目录中分别创建files,tasks,handlers,templates,vars和meta目录,用不到的目录可以创建为空目录。

3、通过roles实现lamp(需定制三个角色: httpd,mysql,php)

第1步: 创建roles目录文件,并确认目录结构

[root@m0 ~]# cd /etc/ansible/roles/

[root@m0 roles]#mkdir -p {httpd,mysql,php}/{files,tasks,handlers,templates,vars,meta}

[root@m0 roles]#touch {httpd,mysql,php}/{tasks,handlers,vars,meta}/main.yml

[root@m0 ~]#yum install tree -y

[root@m0 ~]#mkdir /etc/ansible/roles/httpd

[root@m0 ~]#tree /etc/ansible/roles/

/etc/ansible/roles/

├── httpd

├── files

├── handlers

│   └── main.yml

├── meta

    └── main.yml

├── tasks

│   └── main.yml

├── templates

└── vars

└── main.yml

第2步: 准备httpd服务器的主⻚⽂件,php测试⻚和配置文件

[root@m0 ~]#echo "test main page" > /etc/ansible/roles/httpd/files/index.html

[root@m0 ~]#echo -e "<?php\n\tphpinfo();\n?>" > /etc/ansible/roles/httpd/files/test.php

按需求修改配置⽂件后,拷⻉到httpd⻆⾊⽬录⾥的files⼦⽬录

[root@m0 ~]#vim /etc/httpd/conf/httpd.conf

[root@m0 ~]#cp /etc/httpd/conf/httpd.conf /etc/ansible/roles/httpd/files/

第3步: 编写httpd角色的main.yml文件

[root@m0 ~]#vim /etc/ansible/roles/httpd/tasks/main.yml

---

- name: 安装httpd

yum: name=httpd,httpd-devel state=present

- name: 同步httpd配置⽂件

copy: src=/etc/ansible/roles/httpd/files/httpd.conf dest=/etc/httpd/conf/httpd.conf

notify: restart httpd

- name: 同步主⻚⽂件

copy:

src=/etc/ansible/roles/httpd/files/index.html dest=/var/www/html/index.html

- name: 同步php测试⻚

copy: src=/etc/ansible/roles/httpd/files/test.php dest=/var/www/html/test.php

- name: 启动httpd并开机⾃启动

service: name=httpd state=started enabled=yes

第4步: 编写httpd角色里的handler

[root@m0 ~]#vim /etc/ansible/roles/httpd/handlers/main.yml

---

- name: restart httpd

service: name=httpd state=restarted

第5步: 编写mysql⻆⾊的main.yml文件

---

- name: 安装mysql

yum: name=mariadb,mariadb-server,mariadb-devel state=present

- name: 启动mysql并开机⾃启动

service: name=mariadb state=started enabled=yes

第6步: 编写php⻆⾊的main.yml文件

[root@m0 ~]#vim /etc/ansible/roles/php/tasks/main.yml

---

- name: 安装php及依赖包

yum: name=php,php-gd,php-ldap,php-odbc,phppear,php-xml,php-xmlrpc,php-mbstring,php-smp,phpsoap,curl,curl-devel,php-bcmath,php-mysql state=present

notify: restart httpd

第7步:编写lnmp的playbook文件调用前面定义好的三个角色

[root@m0 ~]#vim /etc/ansible/playbook/lamp.yaml

---

- hosts: group1

remote_user: root

roles:

- httpd

- mysql

- php

第8步: 安装mariadb,命令行形式创建数据库和表

[root@m0 ~]#vim /etc/ansible/playbook/lamp.yaml

---

- hosts: group1

  remote_user: root

  roles:

    - httpd

    - mysql

    - php

第9步: 执行lnmp的playbook⽂件

[root@m0 ~]#ansible-playbook /etc/ansible/playbook/lamp.yaml

练习:请使⽤role来实现lnmp

1、nginx端口修改为8080

[root@m0 ~]# vim /etc/ansible/playbook/nginx.yml

 ---

 - hosts: s

   remote_user: root

   tasks:

   - name: 卸载httpd

     yum: name=httpd   state=absent

   - name: 安装nginx

     yum: name=nginx   state=present

   - name: 修改资源文件

     shell: echo 'i am nginx,port is 80' > /usr/share/nginx/html/index.html

   - name: 修改端口

     command: sed -i '/listen/ s/80/8080/g' /etc/nginx/nginx.conf

     notify:

   - restart nginx

   - name: 启动服务

     service: name=nginx   state=started  enabled=yes

   handlers:

   - name: restart nginx

     service: name=nginx   state=restarted

[root@m0 ~]# ansible-playbook /etc/ansible/playbook/nginx.yml

[root@m0 ~]# yum -y remove *mariadb*

[root@m0 ~]# yum -y install mariadb

[root@m0 ~]# yum -y install mariadb-server.x86_64

[root@m0 ~]# yum -y install mariadb-devel.x86_64

[root@m0 ~]# systemctl start mariadb

[root@m0 ~]# mysql

MariaDB [(none)]> quit

Bye

2、mariadb中创建eleme数据库

[root@m0 ~]# mysql <<EOF

> create database if not exists eleme charset utf8mb4;

> use eleme;

> create table user(id int primary key auto_increment,username varchar(45) not null,password varchar(45) not null,remark varchar(45));

> quit

> EOF

[root@m0 ~]# mysql

 MariaDB [(none)]> show databases;

 +--------------------+

 | Database           |

 +--------------------+

 | information_schema |

 | eleme              |

 | mysql              |

 | performance_schema |

 | test               |

 +--------------------+

 5 rows in set (0.00 sec)

 MariaDB [test]> use eleme;

 Database changed

 MariaDB [eleme]> show tables;

 +-----------------+

 | Tables_in_eleme |

 +-----------------+

 | user            |

 +-----------------+

 1 row in set (0.00 sec)

[root@m0 ~]#cd /etc/ansible/roles/

[root@m0 roles]#ls

mysql nginx

3、将nginx下的之前生成的roles目录文件拷贝到mariabd下

[root@m0 roles]#cp -r nginx/ mariabd

[root@m0 roles]#ls

mysql nginx mariabd

[root@m0 ~]#cd mariabd

[root@m0 mariabd]#vim tasks/main.yml

---

-       name:   卸载 mariadb

        yum:    name=mariadb,mariadb-server,mariadb-devel       state=absent

-       name:   安装mariadb

        yum:    name=mariadb,mariadb-server,mariadb-devel   state=present        

-       name:           启动mariadb服务

        service:        name=mariadb   state=started  enabled=yes

-       name:   执行脚本,创建数据库和数据表

        script: /etc/ansible/script/dbtable.sh

[root@m0 ~]#cd /etc/ansible/

[root@ansible ~]#ls

[root@ansible ~]#mkdir script

[root@m0 ~]#cd script

3、编写脚本,创建创建表 t_user

[root@m0 script]#vim dbtable.sh

#!/bin/bash

mysql << EOF

create database if not exists eleme charset utf8mb4;

use eleme;

create table t_user(

id int primary key auto_increment,

username varchar(45) not null,

password varchar(45) not null,

remark varchar(45)

);

quit

EOF

5、运行剧本

 [root@m0 script]# vim /etc/ansible/playbook/test003.yml

 ---

 - hosts: s

   remote_user: root

   roles:

   - mariadb

 [root@1 script]# cd /etc/ansible/playbook/

 [root@1 playbook]# ansible-playbook test003.yml

饿了么后端的登录模块

1、数据库服务器:

1)基于gtids事务性的主从复制实现数据库的高可用

# 主服务器(192.168.2.206)的基础配置

[root@s1 ~]# ls

anaconda-ks.cfg

mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz

mysql_master.sh

soft

[root@s1 ~]# source mysql_master.sh

tar name:mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz

anaconda-ks.cfg

mysql-5.7.44-linux-glibc2.12-x86_64

mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz

mysql_master.sh

soft

directory:mysql-5.7.44-linux-glibc2.12-x86_64

2024-08-20T07:23:58.298348Z 1 [Note] A temporary password is generated for root@localhost: /wfjS!NrR1)Z

Starting MySQL.Logging to '/usr/local/mysql/data/db01-master.err'.

SUCCESS!

sign initialize password:/wfjS!NrR1)Z

[root@s1 ~]#cat /usr/local/mysql/my.cnf  //查看配置文件(基于gtids模式)

[mysqld]

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

port=3306

log-error=/usr/local/mysql/data/db01-master.err

log-bin=/usr/local/mysql/data/binlog

server-id=10

character_set_server=utf8mb4

gtid-mode=on

log-slave-updates=1

enforce-gtid-consistency

[root@s1 ~]# mysql -proot  //启动主的mysql服务

mysql> create user 'slave0'@'%' identified by 'slave0';  //创建一个slave0用户,%表示在每个主机上都可以使用slave登录到该主机上

Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'slave0'@'%';  //将所有的数据库的所有表的replication slave操作权限给到slave0用户

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;  //重新加载权限表

Query OK, 0 rows affected (0.01 sec)

mysql> create user 'slave1'@'%' identified by 'slave1';  //创建一个slave1用户,%表示在每个主机上都可以使用slave登录到该主机上

Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'slave1'@''%';  //将所有的数据库的所有表的replication slave操作权限给到slave0用户

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql> set @@global.read_only=ON;  //配置只读模式(因为是基于gtids模式的主从复制)

Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

[root@s1 ~]# service mysql57 stop

Shutting down MySQL.. SUCCESS!

做同步:

[root@s1 ~]# rm -rf /usr/local/mysql/data/auto.cnf

[root@s1 ~]# yum -y install rsync   //将主服务器的data文件同步到从服务器上

[root@s1 ~]# yum list installed|grep rsync

rsync.x86_64                          3.1.2-12.el7_9                   @updates

[root@s1 ~]# rsync -av /usr/local/mysql/data root@192.168.2.176:/usr/local/mysql/

# 从服务器(192.168.2.207)的配置

[root@m0 ~]# ls

mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz

mysql57.tar.gz

[root@m0 ~]# tar -xvf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz

[root@m0 ~]# cp -r mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql/

[root@m0 ~]# ls /usr/local/mysql/

bin   include  LICENSE  README  support-files

docs  lib      man      share

[root@m0 ~]# mkdir /usr/local/mysql/mysql-files

[root@m0 ~]# useradd -r -s /sbin/nologin mysql

[root@m0 ~]# chown mysql:mysql /usr/local/mysql/mysql-files/

[root@m0 ~]# chmod 750 /usr/local/mysql/mysql-files/

[root@m0 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql57

[root@m0 ~]# chkconfig --add mysql57

[root@m0 ~]# chkconfig mysql57 on

[root@m0 ~]# vim /usr/local/mysql/my.cnf   //查看配置文件(基于gtids模式)

[mysqld]

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

port=3310

log-error=/usr/local/mysql/data/db01-slave.err

relay-log=/usr/local/mysql/data/relaylog

server-id=11

character_set_server=utf8mb4

log-bin=/usr/local/mysql/data/binlog

gtid-mode=on

log-slave-updates=1

enforce-gtid-consistency

skip-slave-start

[root@m0 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql8

[root@m0 ~]# sed -i '$aexport PATH=/usr/local/mysql/bin:$PATH' /etc/profile

[root@m0 ~]# source /etc/profile

[root@m0 ~]# service mysql57 start

Starting MySQL.Logging to '/usr/local/mysql/data/m0.err'.

ERROR! The server quit without updating PID file (/usr/local/mysql/data/m0.pid).  //此时启动失败,无data文件

[root@m0 ~]# yum -y install rsync

[root@m0 ~]# ls /usr/local/mysql/data

auto.cnf         ib_logfile0

binlog.000001    ib_logfile1

binlog.000002    ibtmp1

binlog.index     mysql

ca-key.pem       performance_schema

ca.pem           private_key.pem

client-cert.pem  public_key.pem

client-key.pem   s1.pid

db01-master.err  server-cert.pem

ib_buffer_pool   server-key.pem

ibdata1          sys

[root@m0 ~]# service mysql57 start  //启动从服务器,密码跟主服务器一致

Starting MySQL.Logging to '/usr/local/mysql/data/db01-slave.err'.

 SUCCESS!

[root@m0 ~]# mysql -P3310 -proot

# 主从复制的实现

主服务器:

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

mysql> create database if not exists abc charset utf8;

ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock

mysql> show master status;  //显示主服务器当前二进制日志的状态信息,

+---------------+----------+--------------+------------------+-------------------------------------------+

| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |

+---------------+----------+--------------+------------------+-------------------------------------------+

| binlog.000003 |      194 |              |                  | 29bcc498-5ec5-11ef-a93f-000c2927b393:1-11 |

+---------------+----------+--------------+------------------+-------------------------------------------+

1 row in set (0.00 sec)

 File:当前正在写入的二进制日志文件的名称。

 Position:当前写入的位置(偏移量)在二进制日志文件中的位置。

从服务器:

[root@mysql-slave ~]# mysql -P3310 -proot   //登录到从的数据库中

mysql> set @@global.read_only=ON;  //配置只读模式(因为是基于gtids模式的主从复制)

Query OK, 0 rows affected (0.00 sec)

mysql> change master to

    -> master_host='192.168.2.206',  //指定主服务器的主机地址

    -> master_user='slave0',  //指定用于连接主服务器进行复制的用户名

    -> master_password='slave0',   //指定对应的密码

    -> master_port=3306,  //指定主服务器的端口

    -> master_log_file='binlog.000003',  //指定从哪个二进制日志文件开始复制

    -> master_log_pos=194;  //指定在指定的二进制日志文件中的起始位置

    -> master_auto_position=1;  //基于gtids模式的主从复制

Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected, 1 warning (0.03 sec)

 mysql> show slave status\G   //查看主从是否同步

  *************************** 1. row ***************************

                 Slave_IO_State: Waiting for source to send event

                    Master_Host: 10.0.0.57

                    Master_User: slave

                    Master_Port: 3306

                  Connect_Retry: 60

                Master_Log_File: binlog.000003

            Read_Master_Log_Pos: 1178

                 Relay_Log_File: relaylog.000002

                  Relay_Log_Pos: 323

          Relay_Master_Log_File: binlog.000003

               Slave_IO_Running: Yes

              Slave_SQL_Running: Yes

# 在主mysql中添加eleme数据库设置为utf8mb4、添加表t_user、添加2行记录

 [root@s1~]# mysql -p'root'

 mysql> create database if not exists eleme charset utf8mb4;

 Query OK, 1 row affected (0.00 sec)

 mysql> show databases;

 +--------------------+

 | Database           |

 +--------------------+

 | information_schema |

 | eleme              |

 | mysql              |

 | performance_schema |

 | sys                |

 +--------------------+

 6 rows in set (0.00 sec)

 mysql> create table eleme.t_user(

     -> id int not null primary key,

     -> name varchar(32) not null,

     -> username varchar(32) not null,

     -> password varchar(32) not null,

     -> remark varchar(32) not null

     -> );

 Query OK, 0 rows affected (0.01 sec)

mysql> desc eleme.t_user;

 +----------+--------------+------+-----+---------+-------+

 | Field    | Type         | Null | Key | Default | Extra |

 +----------+--------------+------+-----+---------+-------+

 | id       | int(11)      | NO   | PRI | NULL    |       |

 | name     | varchar(32) | NO   |     | NULL    |       |

 | username | varchar(32) | NO   |     | NULL    |       |

 | password | varchar(32) | NO   |     | NULL    |       |

 | remark   | varchar(32) | NO   |     | NULL    |       |

 +----------+--------------+------+-----+---------+-------+

5 rows in set (0.02 sec)

mysql> insert into eleme.t_user values(1,'超级管理员','admin','admin','超级管理员'),(2,'普通用户','guest','guest','普通用户');

 Query OK, 2 rows affected (0.00 sec)

 Records: 2  Duplicates: 0  Warnings: 0

 mysql> select * from eleme.t_user;

 +----+-----------------+----------+----------+-----------------+

 | id | name            | username | password | remark          |

 +----+-----------------+----------+----------+-----------------+

 |  1 | 超级管理员      | admin    | admin    | 超级管理员      |

 |  2 | 普通用户        | guest    | guest    | 普通用户        |

 +----+-----------------+----------+----------+-----------------+

 2 rows in set (0.00 sec)

2)基于mycat读写分离(需要jdk环境的支持)实现数据库的负载均衡

# 设置防火墙规则

 [root@mycat ~]# systemctl stop firewalld

 [root@mycat ~]# systemctl disable firewalld

 [root@mycat ~]# ls

 anaconda-ks.cfg             Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

 jdk-8u192-linux-x64.tar.gz  

# 下载并安装jdk、mycat

 [root@mycat ~]# tar -xf jdk-8u192-linux-x64.tar.gz

 [root@mycat ~]# tar -xf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

 [root@mycat ~]# cp -r jdk1.8.0_192/ /usr/local/jdk

 [root@mycat ~]# cp -r mycat/ /usr/local/

 [root@mycat ~]# ls /usr/local/jdk/

 bin             lib          src.zip

 COPYRIGHT       LICENSE      THIRDPARTYLICENSEREADME-JAVAFX.txt

 include         man          THIRDPARTYLICENSEREADME.txt

 javafx-src.zip  README.html

 jre             release

# 添加环境变量

 [root@mycat ~]# sed -i '$aexport JAVA_HOME=/usr/local/jdk' /etc/profile

 [root@mycat ~]# source /etc/profile

 [root@mycat ~]# $JAVA_HOME

 -bash: /usr/local/jdk: 是一个目录

 [root@mycat ~]# sed -i '$aexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile

 [root@mycat ~]# source /etc/profile

 [root@mycat ~]# $PATH

 -bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/bin: 没有那个文件或目录

 [root@mycat ~]# javac -version

 javac 1.8.0_192

 [root@mycat ~]# ls /usr/local/mycat/

 bin  catlet  conf  lib  logs  version.txt

 [root@mycat ~]# ll /usr/local/mycat/

 总用量 12

 drwxr-xr-x. 2 root root  190 8月  15 15:19 bin

 drwxr-xr-x. 2 root root    6 8月  15 15:19 catlet

 drwxr-xr-x. 4 root root 4096 8月  15 15:19 conf

 drwxr-xr-x. 2 root root 4096 8月  15 15:19 lib

 drwxr-xr-x. 2 root root    6 8月  15 15:19 logs

 -rwxr-xr-x. 1 root root  219 8月  15 15:19 version.txt

# 修改mycat的两个配置文件

 [root@mycat ~]# ls /usr/local/mycat/bin/mycat      

 [root@mycat ~]# vim /usr/local/mycat/conf/server.xml

# user标签设置对外的账号和密码,以及数据库名称, 如果只是代理⼀个主从,就注释其他的user标签

         <user name="hj" defaultAccount="true">

                 <property name="password">hj</property>

                 <property name="schemas">eleme</property>

                 <!-- 表级 DML 权限设置 -->

                 <!--            

                 <privileges check="false">

                         <schema name="TESTDB" dml="0110" >

                                 <table name="tb01" dml="0000"></table>

                                 <table name="tb02" dml="1111"></table>

                         </schema>

                 </privileges>           

                  -->

         </user>

 <!--

         <user name="user">

                 <property name="password">user</property>

                 <property name="schemas">TESTDB</property>

                 <property name="readOnly">true</property>

         </user>

 -->

[root@mycat ~]# vim /usr/local/mycat/conf/schema.xml

<?xml version="1.0"?>

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">

<mycat:schema xmlns:mycat="http://io.mycat/">

<schema name="eleme" dataNode="dn1"  checkSQLschema="false" sqlMaxLimit="100"></schema>

<dataNode name="dn1" dataHost="localhost1" database="eleme" />

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"

  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">

<heartbeat>select user()</heartbeat>

<writeHost host="hostM1" url="192.168.2.206:3306" user="hj" password="hj">

     <readHost host="hostS2" url="192.168.2.207:3310" user="hj" password="hj" />

     <readHost host="hostS3" url="192.168.2.207:3310" user="hj" password="hj" />

</writeHost>

</dataHost>

</mycat:schema>

# 启动mycat服务

[root@mycat ~]# /usr/local/mycat/bin/mycat start

Starting Mycat-server...

[root@mycat ~]# netstat -lnput | grep 8066

tcp6       0      0 :::8066                 :::*                    LISTEN      1336/java

注意!!!:

(1)如果出现启动异常,8806端口没有打开,可以检查两个日志文件

vim /usr/local/mycat/logs/wrapper.log

vim /usr/local/mycat/logs/mycat.log

(2)nginx、lvs和haproxy 都是负载均衡软件,但它们分不清什么数据库能读、什么数据库能写,master可读可写,而slave只能读,因此选择mycat实现数据库负载均衡。

2、后端服务器配置

1)配置两台java后端服务器    java01(192.168.2.11)  java02(192.168.2.12)

# 添加环境变量

[root@java01 ~]#vim /etc/profile

export JAVA_HOME

export PATH=$PATH:$JAVA_HOME

# 使用jar包启动后端java服务器

[root@java01 ~]#vim application.yml

# 修改application.yml文件,修改yml中的端口或数据源(port默认8080)

url:jdbc:mysql://192.168.2.206:8066/eleme

username: hj

password: hj

[root@java01 ~]#java -jar eleme_server-0.0.1-SNAPSHOT.jar

[root@java01 ~]#nohup java -jar element_server_server-0.0.1-SNAPSHOT.jar&  //后台运行

2)基于lvs的DR模式代理java服务器

# ds主机    内网(dip为192.168.2.13)   外网(vip为192.168.2.14)

ifconfig ens33:0 192.168.2.14 broadcast 192.168.2.14 netmask 255.255.255.255 up  //挂载对外的vip192.168.2.14

route add vip dev ens33:0  //为vip添加路由

yum -y install ipvsadm  //安装ipvsadm

ipvsadm -Ln  //查看规则

ipvsadm -C  //清空之前的规则

ipvsadm -A -t 192.168.2.14:8080 -s rr  

ipvsadm -a -t 192.168.2.14:8080 -r 192.168.2.11 -g    # -g:网关路由

ipvvsadm -a -t 192.168.2.14:8080 -r 192.168.2.12 -g

# rs主机,不需要指定端口

ifconfig lo:0 192.168.2.14 broadcast 192.168.2.14 netmask 255.255.255.255 up  //挂载vip

route add 192.168.2.14 dev lo:0  //为vip 添加路由

echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore                                                                                 

echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

echo 2 > /proc/sys/net/ipv4/conf/all/arp_announc

扩展:部署nginx代理java服务器,负载均衡策略为轮询问,端⼝为80

4、测试:

# 打开java服务器的端口,端⼝8080,不关防⽕墙,在远程浏览器上测试,看其是否是动态的,数据库里存在账号密码即可登录

firewall-cmd --add-port=8080/tcp --permanent

firewall-cmd --reload

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/1021010
推荐阅读
相关标签
  

闽ICP备14008679号