|
HA专题: corosync+pacemaker实现nginx高可用
前言
这几天都会学习高可用集群, 也会将其中的一些实验写出来分享给大家, 这个专题估计会写5篇左右, p.s: 写博客很累的
实验介绍
这次的实验比较简单, 在CentOS7使用corosync+pacemaker实现两个节点的nginx高可用
实验拓扑
实验环境
注意: 本文实验中所有主机SElinux和iptables都是关闭的
[td]
主机 IP 功用
node1.anyisalin.com 172.16.1.2 web服务, HA节点
node2.anyisalin.com 172.16.1.3 web服务, HA节点
nfs.anyisalin.com 172.16.1.4 提供nfs服务,网站页面文件
实验步骤准备工作
高可用集群必须保证所有节点主机互信, 主机名解析一致, 时间同步
配置hosts文件同步
配置互信
时间同步
安装HA集群组件
在RH系6.4之后就可以通过RedHat提供的pcs来进行对集群”全生命周期”的管理, 我们这里先通过pcs安装集群并自动生成配置文件
[iyunv@node1 ~]# yum install pcs -y #node1安装pcs
[iyunv@node1 ~]# ssh node2.anyisalin.com 'yum install pcs -y ' #node2安装pcs
[iyunv@node1 ~]# echo passwd | passwd --stdin hacluster #设置hacluster用户密码
[iyunv@node1 ~]# ssh node2.anyisalin.com "echo passwd | passwd --stdin hacluster" #为node2设置hacluster密码
[iyunv@node1 ~]# systemctl start pcsd ; ssh node2.anyisalin.com "systemctl start pcsd" #启动pcsd
[iyunv@node1 ~]# pcs cluster auth node1.anyisalin.com node2.anyisalin.com -u hacluster #进行认证
Password:
node1.anyisalin.com: Authorized
node2.anyisalin.com: Authorized
[iyunv@node1 ~]# pcs cluster setup --name mycluster node1.anyisalin.com node2.anyisalin.com #安装集群
[iyunv@node1 ~]# pcs cluster start --all #启动集群
安装nginx和配置nfs
安装nginx
[iyunv@node1 ~]# yum install nginx -y 注意: 需epel源
[iyunv@node1 ~]# ssh node2.anyisalin.com "yum install nginx -y "
[iyunv@node1 ~]# systemctl enable nginx #由于systemd的特性, 需将nginx设置开机自动启动
[iyunv@node1 ~]# ssh node2.anyisalin.com "systemctl enable nginx"
配置nfs
[iyunv@nfs ~]# mkdir /www/htdocs -pv
[iyunv@nfs ~]# echo "This is NFS on 172.16.1.4" > /www/htdocs/index.html #创建网页文件
[iyunv@nfs ~]# vim /etc/exports
/www/htdocs 172.16.1.0/24(ro)
[iyunv@nfs ~]# systemctl start rpcbind.service
[iyunv@nfs ~]# systemctl start nfs-server.service #启动nfs-server
测试nfs
[iyunv@node1 ~]# mount -t nfs 172.16.1.4:/www/htdocs /usr/share/nginx/html/ #挂载成功
[iyunv@node1 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
172.16.1.4:/www/htdocs 52403200 1134336 51268864 3% /usr/share/nginx/html
[iyunv@node1 ~]# umount /usr/share/nginx/html/
使用crmsh配置集群资源
pcs用来安装集群还可以, 但是配置集群资源, 我用了一次再也不想用了, 实在是不好用, 个人比较喜欢crmsh来配置集群,crmsh大家可以自行去下载, 安装时可能需要epel源 中的一些软件
[iyunv@node1 ~]# yum install crmsh-2.1.4-1.1.x86_64.rpm #安装crmsh 我们的rpm包是自行下载的
配置资源
[iyunv@node1 ~]# crm configure
crm(live)configure# primitive webip ocf:heartbeat:IPaddr params ip=172.16.1.8
crm(live)configure# primitive nginx systemd:nginx
crm(live)configure# primitive webstore ocf:heartbeat:Filesystem params device="172.16.1.4:/www/htdocs" directory="/usr/share/nginx/html" fstype="nfs" op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
定义资源组
crm(live)configure# group webservice webip webstore nginx
crm(live)configure# verify
crm(live)configure# property stonith-enabled=false
crm(live)configure# commit
测试
|
|
|