架构
stroge的加入将数据和请求处理独立 iscsi是以块的形式存储,服务器端是看不到数据的,保证了数据的安全
Fence主要在架构的作用为防止二个服务器同时向资源写数据,破坏了资源的安全性和一致性从而导致脑裂的发生
(脑裂:vm1突然夯住了,不响应了,但是没有关,集群检测到就把vip这些资源迁移到vm2,重新挂载存储,但是vm1也挂载在存储上,过一会反映过来后,两个节点同时写入就会脑裂)
nginx
安装nginx
- 下载解压
tar xzf nginx-1.18.0.tar.gz - 移动到安装目录
[root@server2 nginx-1.18.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
- configure
./configure --help 查看安装选项
[root@server2 nginx-1.18.0]# ./configure --help | grep http_ssl
--with-http_ssl_module enable ngx_http_ssl_module
[root@server2 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
加入http ssl 模块 支持https
安装gcc
yum install pcre-devel
yum install openssl-devel
此时生成了Makefile文件
- make
make 成功`
make install
启动服务
使用systemd来管理nginx
官方wiki
编写 /lib/systemd/system/nginx.service
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target ## 根据自己安装目录编写
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl deamon-reload
nginx 配置
参数说明
user nobody;
指定nginx是以那个用户身份进行
nginx默认是以nobody的身份进行的
worker_processes 1;
这是nginx的默认并发量 如果把它改成4
一般这个参数与cpu个数有关 也可以设置为auto 但设置成静态可以避免cpu上下文切换
## 记录日志信息 默认的就好
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
## nginx的pid
#pid logs/nginx.pid;
nginx允许用户最大连接数
events {
worker_connections 1024;
}
如果要改成 65535
查看硬件支持打开文件数
内核没问题
接下来查看系统最大打开文件数
所以nginx默认并发量是1024
去除系统限制
先创建一个nginx用户
修改nginx 默认用户
更改/etc/security/limits.conf 这个文件是即时生效的 只需要重新加载nginx就行
配置nginx负载均衡
http {
upstream westos {
server server3;
server server4;
}
server {
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos;
}
}
效果
使用加权算法
使用ip-hash
nginx 自带健康检查
假如关闭server3 httpd
nginx+ iscsi存储分离
- 1 在server4上新加一块磁盘
- 2 server4上安装targetcli(设置开机自启)
argetcli是Linux-IO Target的用户态的管理配置工具。用户可以使用yum或apt-get直接从各大发行版的官方仓库安装,对于较老的linux版本需要自己编译源码安装。targetcli提供一个类似shell的界面,各种Target、TPG、LUN、backstore对象则被组织成目录树的形式,用户可以用ls、cd命令来浏览目录树,用create、delete命令来创建和删除各种对象。浏览配置,就是浏览目录树,创建删除各种对象,就是在目录树的各级节点中创建新的节点。同时targetcli还提供了常见的shell命令辅助编辑功能,例如TAB智能补全、上下键切换命令历史、Ctrl + R搜索命令历史。凡是有不熟悉的命令,都可以用help命令查询使用说明。 - 3 配置共享磁盘
(1) targetcli
(2) /> cd /backstores/block/
(3) /backstores/block> create mydisk /dev/vdb
(4) /> cd /iscsi/
(5) /iscsi> create iqn.2020-08.org.westos:storage1
(6) /iscsi> cd iqn.2020-08.org.westos:storage1/tpg1/acls/
(7) /iscsi/iqn.20...ge1/tpg1/acls> create iqn.2020-08.org.westos:client
(8) /iscsi/iqn.20...ge1/tpg1/acls> cd ..
(9) /iscsi/iqn.20...storage1/tpg1> cd luns/
(10) /iscsi/iqn.20...ge1/tpg1/luns> create /backstores/block/mydisk
(11) /iscsi/iqn.20...ge1/tpg1/luns> exit
一共三步: (1) : 创建名为mydisk的磁盘 (2): 创建存储对象 (3) 设置acl控制
- 4 配置客户端
[root@server2 conf]# yum install -y iscsi-*
[root@server2 conf]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2020-08.org.westos:client
[root@server2 conf]# iscsiadm -m discovery -t st -p 172.25.254.104 # -t:类型send target ,-p:指定服务端
172.25.254.104:3260,1 iqn.2020-08.org.westos:storage1 #从服务端识别到了存储目标
[root@server2 conf]# iscsiadm -m node -l # -l:注册,注册discovery的存储目标,执行后已经缓存
Logging in to [iface: default, target: iqn.2020-08.org.westos:storage1, portal: 172.252.54.104,3260] (multiple)
Login to [iface: default, target: iqn.2020-08.org.westos:storage1, portal: 172.25.254.104,3260] successful.
这时候一般就能看到系统里多出来了一个/dev/sda
如果登录出错一般是没有在修改initiatorname.iscsi后重启iscsi服务
如果登录成功 但没有效果 直接删掉下面这两个文件 重新设置
给/dev/sda分区 并格式化为ext4格式,这一步只执行一次
在server1上将/dev/sda 挂载到html目录
建立test页面
[root@server1 nginx]# echo server1 > html/test.html
挂载到server2上
[root@server2 ~]# mount /dev/sda /usr/local/lnmp/nginx/html/
server2 也能访问到 说明挂载没问题 只需要使用pcsd来管理自动挂载就可以实现存储分离
fence
- 安装
宿主机:
安装fence_virt
server1 server2 安装
[root@server1 ~]# yum install -y fence-virt.x86_64
[root@server2 ~]# yum install -y fence-virt.x86_64
- 服务端配置
[root@zhq ~] fence_virtd -c
Module search path [/usr/lib/fence-virt]:
Available backends:
libvirt 0.1 ##使用libvirt后端
Available listeners:
serial 0.4
multicast 1.2 ## 多播
Listener modules are responsible for accepting requests
from fencing clients.
Listener module [multicast]:
The multicast listener module is designed for use environments
where the guests and hosts may communicate over a network using
multicast.
The multicast address is the address that a client will use to
send fencing requests to fence_virtd.
Multicast IP Address [225.0.0.12]: #监听ip段
Using ipv4 as family.
Multicast IP Port [1229]: ##端口
Setting a preferred interface causes fence_virtd to listen only
on that interface. Normally, it listens on all interfaces.
In environments where the virtual machines are using the host
machine as a gateway, this *must* be set (typically to virbr0).
Set to 'none' for no interface.
Interface [br0]: ##桥接接口 brctl show 查看 vnet在那个网桥上
The key file is the shared key information which is used to
authenticate fencing requests. The contents of this file must
be distributed to each physical host and virtual machine within
a cluster.
Key File [/etc/cluster/fence_xvm.key]: ##认证文件
Backend modules are responsible for routing requests to
the appropriate hypervisor or management layer.
Backend module [libvirt]:
Configuration complete.
=== Begin Configuration ===
backends {
libvirt {
uri = "qemu:///system";
}
}
listeners {
multicast {
port = "1229";
family = "ipv4";
interface = "br0";
address = "225.0.0.12";
key_file = "/etc/cluster/fence_xvm.key";
}
}
fence_virtd {
module_path = "/usr/lib/fence-virt";
backend = "libvirt";
listener = "multicast";
}
=== End Configuration ===
Replace //etc/fence_virt.conf with the above [y/N]? y
生成密钥文件
dd if=/dev/urandom of=/etc/cluster/fence_xvm.key bs=128 count=1
- 客户端配置
mkdir /etc/cluster
scp fence_xvm.key root@172.25.254.101:/etc/cluster
server2 同上 - pcs配置
添加vmfence资源
pcs property set stonith-enabled=true
pcs stonith create vmfence fence_xvm pcmk_host_map="server1:vm1;server2:vm2" op monitor interval=60s
效果
- 1
[root@server1 ~]# fence_xvm -H server2
[root@server1 ~]# watch -n 1 "pcs status"
当一方发生问题时,他们互相认为是对方的问题,会告诉fence让重启对方,但是server1已经挂了,无法告诉fence,但是server2会连接到fence,重启sevrer1
- 2 如果server1网卡down掉
自动重启
使用pacemaker将资源整合
pcs resource create vip ofc:heartbeat:IPaddr2 ip=172.25.254.150 op monitor interval=30s
pcs resource create nginx systemd:nginx op monitor interval=30s
pcs resource create webdata ocf:heartbeat:Filesystem device="/dev/sda" directory="/usr/local/lnmp/nginx/html" fstype=ext4 op monitor interval=60s
添加到资源组webgroup
pcs resource group add webgroup vip webdata nginx
pcs resource defaults resource-stickiness=100 ##防止集群节点恢复后资源自动迁移