linux集群高可用

什么是高可用集群

       高可用集群(High Availability Cluster,简称HA Cluster),是指以减少服务中断时间为目的的服务器集群技术。它通过保护用户的业务程序对外不间断提供的服务,把因为软件、硬件、人为造成的故障对业务的影响降低到最小程度。总而言之就是保证公司业务7x24小时不宕机

 集群中必须满足一下基本要求

1:集群中的节点必须时间同步

2:能够通过/etc/hosts进行主机名解析

3:基于ssh免密码通信

文件配置/etc/hosts 

192.168.43.9   node0
192.168.43.10 node1
192.168.43.11 node2
 
ssh-keygen -t rsa -P ""          生成密钥对
cd .ssh                                   进入文件
mv id_rsa.pub authorized_keys     对公钥改名                        ssh-copy-id  -i  )
rm -f known_hosts                          删除
cd ../
scp -rp .ssh/ node1:/root/                传给其他的节点
scp -rp .ssh/ node2:/root/
 
 
 
时间同步
 
 
1:在node0上安装配置chrony服务,同步国内互联网时间服务器的时间
yum install -y chrony

 

2:编辑配置文件/etc/chrony.conf
修改默认的centos时间同步服务器地址为国内时间同步服务器地址,且允许192.168.43.0/24网段的主机同步时间
server    node1    iburst
server     node2   iburst
server     node3   iburst
server      node4  iburst
allow 172.16.19.0/24
 
3:启动chrony
systemctl enable chronyd.service
systemctl start chronyd.service
 
4:在node1和node2上分别安装chrony,且修改配置文件/etc/chrony.conf,将时间同步指向node0节点
yum install -y chrony
server node0 iburst(配置文件里面修改)
systemctl enable chronyd.service
systemctl start chronyd.service
 
 
5:node1,node2验证时间同步
[root@node1 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* node0
 
 
高可用集群的配置
 
安装pacemaker( corosync将会被作为依赖安装)
yum install -y pacemaker
 
node1上面操作
 
cd /etc/corosync/
cp corosync.conf.example corosync.conf
 
 
vim /etc/corosync/corosync.conf
 
totem {
version: 2 # 指定版本号
cluster_name: uplookingcluster # 指定集群名称
crypto_cipher: aes256 # 指定对称加密算法
crypto_hash: sha1 # 指定单向加密算法
interface {
ringnumber: 0 # 指定心跳信息传递的环路id
bindnetaddr: 192.168.43.0 # 指定环路中的网络地址
mcastaddr: 239.255.1.1 # 指定心跳信息通过多播的方式传递的IP通道
mcastport: 5405 # 指定多播的端口
ttl: 1 # 指定多播报文信息的生命周期
}
}
 
logging {
fileline: off
to_stderr: no
to_logfile: yes # 指定开启日志文件
logfile: /var/log/cluster/corosync.log # 指定日志文件保存的位置
to_syslog: no # 指定不使用系统日志保存日志信息
debug: off
timestamp: on
logger_subsys { # 指定启用仲裁机制
subsys: QUORUM
debug: off
}
}
 
quorum { # 指定仲裁机制是corosync投票系统
provider: corosync_votequorum
}
 
nodelist { # 指定节点列表
node {
ring0_addr: 192.168.43.10
nodeid: 1
}
node {
ring0_addr: 192.168.43.11
nodeid: 2
}
}
 
注意:   要求删除中文,改掉心跳信息通过多播的方式传递的IP通道
 
 
corosync-keygen(由于在message layer  层传递心跳信息需要加密,因此先必须生成加密文件)
开另一个窗口安装卸载东西,要不能时间好长
 
  要求cuthkey的权限是400才能够启动
scp authkey corosync.conf node2:/etc/corosync/   复制 到另一个节点   
 
systemctl enable corosync.service  开机自启
systemctl start corosync.service
两个节点都要启动
 
 
systemctl enable pacemaker.service
systemctl start pacemaker.service
 
yum install -y   crmsh-3.0.0-6.2.noarch.rpm   

crmsh-scripts-3.0.0-6.2.noarch.rpm

python-parallax-1.0.1-6.2.noarch.rpm

安装三个包 可能是下面的没有用吧  

 
wget -O /etc/yum.repos.d/crmsh.repo http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-7/network:ha-clustering:Stable.repo
 
yum repolist
yum install -y crmsh
 
 
高可用集群  
 
crm(live)# help
Help overview for crmsh(各种一级命令还有二级命令) 
分析所需的资源:
1:vip
2:httpd
3:NFS(因为不同的httpd节点需要有相同的静态页面)
4:mysql
5;php php-mysql(驱动)
 
 由于无法模拟生成环境中的stonith设备,因此需要先禁用stonith设备,且配置完毕之后必须要提交(温馨提示:使用vsphpere也可以虚拟出stonish设备)
[root@node1 ~]# crm
crm(live)# configure property stonith-enabled=false
crm(live)# commit
cd 
status
 
 
1:配置vip资源
 
 
在configure二级命令接口处配置vip资源
 
# 定义vip主资源,vip为资源的名称,ocf为资源类型,IPaddr为ocf资源类型下的IPaddr资源,ip、nic、cidr_netmask为IPaddr资源的参数。如果需要查看某个资源的定义,可以使用命令 ra info IPaddr 查看
primitive vip ocf:IPaddr params ip=192.168.43.100 nic=enp0s3 cidr_netmask=24
 
# 查看配置信息
show
 
# 提交配置,让配置生效,
commit
 
cd 
status(查看在哪个节点,然后在那台虚拟机上面ip addr  看到效果)
 
 
# 如果让node1节点成为备用节点,那么vip将会漂移到其他节点上
[root@node1 ~]# crm node standy  node1
# 重新让node1上线,在node1节点上,发现vip并没有重新漂移至node1节点,因为,vip资源没有设置倾向性
[root@node1 ~]# crm  (一级)node(二级) online  node1

 

# 将 vip资源迁移到node1上 ,再使用ip addr 发现vip 资源已经在node1上了
[root@node1 ~]# crm resource move vip node1
INFO: Move constraint created for vip to node1

 2:配置httpd资源

 
1:在configure二级命令接口处配置httpd资源,在配置之前需要在各个节点安装上httpd,且开机自启,且为了测试,给node1节点和node2节点使用两个不同的测试首页
yum install -y httpd
systemctl enable httpd.service
 
2:在node1 和 node2 分别查看,是否有systemd类型的httpd资源,如果有说明可以使用httpd资源代理
[root@node1 ~]# crm ra list systemd | grep httpd
httpd hypervkvpd hypervvssd ip6tables iptables
 
3:可以查看一下httpd资源代理的使用帮助
crm ra info systemd:httpd
 
4:定义主资源httpd
[root@node2 ~]# crm configure primitive httpd systemd:httpd
[root@node2 ~]# crm configure commit
 
5:查看资源配置情况
crm verify
 
6:查看下资源分布情况,发现vip运行在node1上, 而httpd运行在node2上,因此需要手动迁移资源的分布
[root@node2 ~]# crm status
 
crm(live)resource# move httpd node1
 
7:再次查看一下,资源的分布情况,此时vip、httpd资源都运行在node1之上了,使用浏览器可以访问到httpd提供的服务
[root@node2 ~]# crm verify
 
8:为了避免手动迁移资源,因此可以将必须运行在一起的primitive资源绑定到一个group资源里面
crm(live)# configure group webservice vip httpd
INFO: modified location:cli-prefer-vip from vip to webservice
INFO: modified location:cli-prefer-httpd from httpd to webservice
 
9:查看集群配置的cib文件
crm(live)# configure show
 
10:让node1成为备用节点,此时资源已经全部迁移到node2节点
crm(live)# node standy
 
11:再让node1节点上线,迁移group资源到node1节点
crm(live)resource# move webservice node1
 
12:可以直接编辑cib文件,配置集群资源信息
crm configure edit
 
3.配置NFS资源(重新启动一个虚拟机,节点主机名为nfs 在NFS节点上面配置)
 
1:在NFS节点安装nfs-utils
yum install -y nfs-utils
 
2:创建共享目录
mkdir /data/
 
3:编辑配置文件/etc/exports(这里使用了选择no_root_squash不压缩root权限,但是NFS一定不要添加这个选项,如果客户端要实现读写访问,必须给/data目录设置文件系统级别的访问权限)
/data 192.168.0.0/16(rw, no_root_squash)
o_root_squash:登入 NFS 主机使用分享目录的使用者,如果是 root 的话,那么对于这个分享的目录来说,他就具有 root 的权限!这个项目『极不安全』,不建议使用!
root_squash:在登入 NFS 主机使用分享之目录的使用者如果是 root 时,那么这个使用者的权限将被压缩成为匿名使用者,通常他的 UID 与 GID 都会变成 nobody 那个系统账号的身份;
 
4:启动nfs
systemctl start nfs.service
exportfs -r
 
5.在node2节点上面
查看NFS节点的挂载情况
showmount -e 192.168.43.13
mount  -t nfs node1:/gongll   /var/www/html/
vi index.html
 
6.
直接在node1的crm接口中配置NFS资源
crm(live)configure# primitive nfs ocf:Filesystem parameters device="192.168.43.13:/data" directory="/var/www/html" fstype=nfs

 7.

检查且提交
crm(live)configure# verify
crm(live)configure# commit
8.
:查看集群中的资源分布
crm(live)# status

 

4.配置资源集群监控
 
# 如果此时的高可用集群的环境是 vip nfs httpd 都运行在node1,如果在node1停止httpd
[root@node1 ~]# systemctl stop httpd
 
# 再使用netstat -lntup查看的时候,httpd是不会自动启动的,因此在定义资源的同时需要定义监控,先停止httpd资源
crm(live)# resource stop httpd
 
# 编辑cib文件,删除所有的关于httpd资源的信息,因为httpd资源需要重新定义
crm(live)# cofigure edit
 
# 重新定义httpd资源,超时时长建议为100s
crm(live)configure# primitive httpd systemd:httpd op start timeout=100s op stop timeout=100s op monitor interval=15s timeout=100s
 
# 校验
crm(live)configure# verify
 
# 在编辑cib文件
crm(live)configure# edit
 
# 将新创建的资源添加至webservice资源组中,修改cib文件即可
 
# 提交
crm(live)configure# commit
 
# 查看资源在节点的发布情况,发现httpd资源运行在node1节点上
[root@node2 ~]# crm status
 
# 在node1上停止httpd资源,看看是否httpd资源会自动重启,查看发现确实自动重启
systemctl stop httpd
 
 
5 配置mysql的资源
 
yum install -y   php php-mysql   mariadb*
 
 
vi /etc/hosts   两个节点都要安装启动配置

skip_name_resolve=on
innodb_file_per_table=on

mysql

create database wp;      

grant all on wp.* to wp@'%'  identified by '123456';     

flush privileges;       刷新权限

crm(live)configure

primitive mysql systemd:mariadb op start timeout=100s op stop timeout=100s

verify

commit

 

配置组使他们在一个节点上面 

crm(live)configure# group webservice vip nfs httpd
crm(live)configure# location webservice_node1 webservice 100: node1
 
configure    edit  (新的服务可以在里面加入到组里面去)
 
 

 vi  index.php

<?php

$link=mysql_connect("172.16.19.239","wp","123456");

if(!$link)

echo "FAILD!连接错误,用户名密码不对";

else
echo "OK!可以连接";
?>

 

 网站上面进行连接 

 

 三个节点   ( )

在环境部署完之后,我们要搭建lanp在什么上实现高可用,共有node1、node2、node3
三个节点。还有一个node0,搭载时间同步服务器和nfs服务器。

此时在node1上
-----------创建虚拟vip1 2 3
crm(live)configure# primitive vip1 ocf:IPaddr params ip=172.16.19.175 nic=enp0s3 cidr_netmask=24
crm(live)configure# commit
crm(live)configure# primitive vip2 ocf:IPaddr params ip=172.16.19.176 nic=enp0s3 cidr_netmask=24
crm(live)configure# commit
crm(live)configure# primitive vip3 ocf:IPaddr params ip=172.16.19.177 nic=enp0s3 cidr_netmask=24
crm(live)configure# commit

---------查看下
crm(live)# status
Stack: corosync
Current DC: node3 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Thu Oct 26 13:17:34 2017
Last change: Thu Oct 26 13:13:28 2017 by root via cibadmin on node1

3 nodes configured
3 resources configured

Online: [ node1 node2 node3 ]

Full list of resources:

vip1 (ocf::heartbeat:IPaddr): Started node1
vip2 (ocf::heartbeat:IPaddr): Started node2
vip3 (ocf::heartbeat:IPaddr): Started node3

------------新建httpd资源
crm(live)configure# primitive httpd systemd:httpd op start timeout=100s op stop timeout=100s op monitor interval=15s timeout=100s
crm(live)configure# commit

-------------新建mysql资源
crm(live)configure# primitive mysql systemd:mariadb op start timeout=100s op stop timeout=100s op monitor interval=15s timeout=100s
crm(live)configure# commit
------------在node0上的nfs服务器上。
[root@node0 ~]# mkdir /httpd
[root@node0 ~]# mkdir /mysql
[root@node0 ~]# mkdri /php

[root@node0 ~]# vim /etc/exports
/httpd 172.16.19.0/24(rw,no_root_squash)
/mysql 172.16.19.0/24(rw,no_root_squash)
/php 172.16.19.0/24(rw,no_root_squash)

------------在node2上查看一下
[root@node2 ~]# showmount -e node0
Export list for node0:
/php 172.16.19.0/24
/mysql 172.16.19.0/24
/httpd 172.16.19.0/24
-----------在node1上,nfs1
crm(live)configure# primitive nfs1 ocf:Filesystem parameters device="172.16.19.171:/httpd" directory="/var/www/html" fstype=nfs
crm(live)configure# commit
WARNING: nfs1: default timeout 20s for start is smaller than the advised 60
WARNING: nfs1: default timeout 20s for stop is smaller than the advised 60
记得加上各种超时时长不然会有警告

-------nfs2
crm(live)configure# primitive nfs2 ocf:Filesystem parameters device="172.16.19.171:/php" directory="/var/www/html" fstype=nfs
crm(live)configure# commit
WARNING: nfs2: default timeout 20s for start is smaller than the advised 60
WARNING: nfs2: default timeout 20s for stop is smaller than the advised 60

--------设置php-fpm资源
crm(live)configure# primitive php systemd:php-fpm op start timeout=100s op stop timeout=100s op monitor interval=15s timeout=100s
crm(live)configure# commit
-------------查看下
crm(live)# status
Stack: corosync
Current DC: node3 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Thu Oct 26 14:23:43 2017
Last change: Thu Oct 26 14:23:10 2017 by root via cibadmin on node1

3 nodes configured
8 resources configured

Online: [ node1 node2 node3 ]

Full list of resources:

vip1 (ocf::heartbeat:IPaddr): Started node1
vip2 (ocf::heartbeat:IPaddr): Started node2
vip3 (ocf::heartbeat:IPaddr): Started node3
httpd (systemd:httpd): Started node1
mysql (systemd:mariadb): Started node2
nfs1 (ocf::heartbeat:Filesystem): Started node3
nfs2 (ocf::heartbeat:Filesystem): Started node3
php (systemd:php-fpm): Started node1
----此时所有的资源弄好了,要开始建组资源,在node1上
crm(live)configure# group httpd-vip1-nfs vip1 httpd nfs1
crm(live)configure# group php-vip2-nfs2 vip2 php nfs2
crm(live)configure# group mysql-vip3 vip3 mysql
crm(live)configure# commit
----查看下
crm(live)# status
Stack: corosync
Current DC: node3 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Thu Oct 26 14:33:49 2017
Last change: Thu Oct 26 14:33:16 2017 by root via cibadmin on node1

3 nodes configured
8 resources configured

Online: [ node1 node2 node3 ]

Full list of resources:

Resource Group: httpd-vip1-nfs
vip1 (ocf::heartbeat:IPaddr): Started node1
httpd (systemd:httpd): Started node1
nfs1 (ocf::heartbeat:Filesystem): Started node1
Resource Group: php-vip2-nfs2
vip2 (ocf::heartbeat:IPaddr): Started node2
php (systemd:php-fpm): Started node2
nfs2 (ocf::heartbeat:Filesystem): Started node2
Resource Group: mysql-vip3
vip3 (ocf::heartbeat:IPaddr): Started node3
mysql (systemd:mariadb): Started node3
----此时分好组了,而且自动帮我们分好了。如果没分好,你需要自己将其均衡。

------现在要约束一波组资源,让其更加均衡,约束更倾向node1的httpd、vip1、nfs1、
crm(live)configure# location httpd-vip1-nfs_node1 httpd-vip1-nfs inf: node1 ------这里是设置在node1上倾向性是无穷大,
crm(live)configure# location httpd-vip1-nfs_node2 httpd-vip1-nfs 500: node2 ------这里是在node2上为500
crm(live)configure# location httpd-vip1-nfs_node3 httpd-vip1-nfs -100:node3 ------这里是在node3上为-100,这样就是说这个组资源会在node1和node2上跳,基本不会在node3上出现。
crm(live)configure# commit
------约束更倾向node2的php、vip2、nfs2
crm(live)configure# location php-vip2-nfs2_node2 php-vip2-nfs2 inf: node2
crm(live)configure# location php-vip2-nfs2_node3 php-vip2-nfs2 500: node3
crm(live)configure# location php-vip2-nfs2_node1 php-vip2-nfs2 -100: node1
crm(live)configure# commit
这个是约束组资源php-vip2-nfs2的更倾向于node2节点
------约束更倾向node3的mysql-vip3
crm(live)configure# location mysql-vip3_node3 mysql-vip3 inf: node3
crm(live)configure# location mysql-vip3_node1 mysql-vip3 500: node1
crm(live)configure# location mysql-vip3_node2 mysql-vip3 -100: node2
crm(live)configure# commit

 

关于动静分离的在

 

然后在node0上将wordpress源码传上去
在node0上
----------
在/php目录下将wordpress源码传上去,并相应修改,改名删无用目录
然后在/php目录下删除静态文件
rm -f ./*.jpg
rm -f ./*.gif
rm -f ./*.jpeg
rm -f ./*.js
rm -f ./*.html
rm -f ./*.txt
rm -f ./*.htm
rm -f ./*.ico


在/httpd目录下将wordpress源码传上去,并相应修改,改名删无用目录
然后在/httpd目录下删除动态文件
rm -f ./*.php


-----------最后
在浏览器上访问vip1 172.16.19.175

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2017-10-25 20:52  凉凉家园  阅读(355)  评论(0编辑  收藏  举报