试验声明:
尝试验证:
以后的累累试验都将以Linux平台下的 KVM虚拟机为主,所以不可少的就是
Virt-Manager 虚拟机管理器。
本章将对怎么设置和应用Virt-Manager管理器举办讲解,并且会对(实验用)虚拟机举行简要陈设,方便前期实验。
实验证实:
在自动化安排时,会时时SSH其余机械去操作,但是每趟的密码验证却很令人惶恐不安,越发是很长的密码,因而SSH免密登陆就展现必要;
在机械数目很多的时候,使用更过的往往是Ansible分发并施行SSH免密登陆脚本,使得每台机械之间都能免密登陆。
试行阐明:
以后的过多尝试都将以Linux平台下的 KVM虚拟机为主,所以不可少的就是
Virt-Manager 虚拟机管理器。
本章将对怎么着设置和拔取Virt-Manager管理器举行教学,并且会对(实验用)虚拟机进行简短陈设,方便中期实验。
试行环境:
- 宿主机系统 :Fedora 28 WorkStation
- 虚拟机管理器 :Virt-Manager 1.5.1
- 虚拟机配置 :ha1 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.57
虚拟机以及,多节点SSH免密登陆。ha2 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.58
ha3 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.59
实验环境:
- 宿主机系统 :Fedora 28 WorkStation
- 虚拟机管理器 :Virt-Manager 1.5.1
- 虚拟机配置 :ha1 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.57 (待创建)
ha2 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.58 (待创建)
ha3 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.59 (待创建)
试行环境:
- 宿主机系统 :Fedora 28 WorkStation
- 虚拟机管理器 :Virt-Manager 1.5.1
- 虚拟机配置 :ha1 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.57
ha2 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.58
ha3 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.59
实验环境:
- 宿主机系统
:Fedora 28
WorkStation - 虚拟机管理器 :Virt-Manager 1.5.1
- 虚拟机配置 :ha1
CentOS 7.2 1511
(minimal) virbr0: 192.168.122.57 (待创建)
ha2 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.58 (待创建)
ha3 CentOS 7.2 1511 (minimal) virbr0:
192.168.122.59 (待创建)
试行步骤:
试验步骤:
-
### 准备镜像CentOS 7.4 1804
-
### 安装 Virt-Manager
# dnf install virt-manager libvirt -y
### 打开 Virt-Manager
# virt-manager # 弹框输入密码即可
-
### 创建 KVM 虚拟机 选择 Local install media(ISO image or CDROM) –> Forward
选择ISO镜像,点击 Browse
会进来拔取镜像界面
点击左下角添加镜像原文件存储地方 例如我的镜像在 /home/Documents 文件夹,现在加上它 Name为 Documents,Type为dir,Target Path默许即可
添加事业有成,采用镜像
上边设置虚拟机内存和CPU数目
其次是安装卷(磁盘)大小及岗位,可修改
确认配置参数以及安装互联网 –> Finish
开头设置系统
-
### 安装系统
-
### 系统联网伊始化详细见 此链接
### 设置网卡开机自启
# sed -i '/ONBOOT/s/no/yes/' /etc/sysconfig/network-scripts/ifcfg-eth0 # systemctl restart network # ip a ##先设置ip,方便通过主机ssh到虚拟机
### 设置静态IP地址
-
### 设置主机名
# hostnamectl set-hostname HOSTNAME
-
### 配置hosts文件
# vi /etc/hosts
-
### 关闭防火墙
# systemctl stop firewalld && systemctl disable firewalld
-
### 关闭 SElinux
# sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
-
### 更换 YUM 源
# mkdir -p /etc/yum.repos.d/bak # mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/bak # mv Cent* bak # curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # yum clean all # yum makecache
-
### 更新系统
# yum upgrade
-
### COMPLETE!!!
试验步骤:
-
### 安装系统并布署网络(所有虚拟机都需联网)
-
### 先操作第一台虚拟机(ha1)
-
### 编写主机名与IP的炫耀关系
1 [root@ha1 ~]# vi /etc/hosts 2 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 3 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 4 192.168.122.57 ha1 5 192.168.122.58 ha2 6 192.168.122.59 ha3
-
### 创立公有密钥
1 [root@ha1 ~]# ssh-keygen -t rsa 2 Generating public/private rsa key pair. 3 Enter file in which to save the key (/root/.ssh/id_rsa): 4 /root/.ssh/id_rsa already exists. 5 Overwrite (y/n)? y 6 Enter passphrase (empty for no passphrase): 7 Enter same passphrase again: 8 Your identification has been saved in /root/.ssh/id_rsa. 9 Your public key has been saved in /root/.ssh/id_rsa.pub. 10 The key fingerprint is: 11 40:c3:81:eb:60:49:2e:f7:fe:59:bb:ef:7d:ad:bb:06 root@ha2 12 The key's randomart image is: 13 +--[ RSA 2048]----+ 14 | o+. | 15 | . .... | 16 | o . .. | 17 |. * . . | 18 | + + S | 19 | o E | 20 | . . . . | 21 | . o . . o .| 22 | .o o+o .o++ | 23 +-----------------+
-
### 发送公有密钥至远程机器
1 [root@ha1 ~]# ssh-copy-id root@192.168.122.58 2 [root@ha1 ~]# ssh-copy-id root@192.168.122.59
-
### 以上是单台虚拟机的一一执行命令的艺术,将以上操作写成脚本(脚本在本文末尾PS处)
-
### 下边操作其他虚拟机(ha2、ha3)
1 # 虚拟机ha2 2 [root@ha2 ~]# chmod 777 build-ssh-credit.sh 3 [root@ha2 ~]# ./build-ssh-credit.sh 1 # 虚拟机ha3 2 [root@ha3 ~]# chmod 777 build-ssh-credit.sh 3 [root@ha3 ~]# ./build-ssh-credit.sh
-
### 至此,三台虚拟机之间互相拜访都无需输入密码,落成了SSH的免密登陆
-
### Complete!!!
试验步骤:
-
#### 准备镜像CentOS 7.4 1804
-
#### 安装 Virt-Manager
# dnf install virt-manager libvirt -y
#### 打开 Virt-Manager
# virt-manager # 弹框输入密码即可
-
#### 创建 KVM 虚拟机 选择 Local install media(ISO image or CDROM) –> Forward
选择ISO镜像,点击 Browse
会进来选拔镜像界面
点击左下角添加镜像原文件存储地方 例如我的镜像在 /home/Documents 文件夹,现在加上它 Name为 Documents,Type为dir,Target Path默许即可
添加事业有成,选取镜像
上边设置虚拟机内存和CPU数目
其次是安装卷(磁盘)大小及任务,可修改
确认配置参数以及安装互联网 –> Finish
开首设置系统
-
#### 安装系统
-
#### 系统联网开端化详细见 此链接
#### 设置网卡开机自启
# sed -i '/ONBOOT/s/no/yes/' /etc/sysconfig/network-scripts/ifcfg-eth0 # systemctl restart network # ip a ##先设置ip,方便通过主机ssh到虚拟机
#### 设置静态IP地址
-
#### 设置主机名
# hostnamectl set-hostname HOSTNAME
-
#### 配置hosts文件
# vi /etc/hosts
-
#### 关闭防火墙
# systemctl stop firewalld && systemctl disable firewalld
-
#### 关闭 SElinux
# sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
-
#### 更换 YUM 源
# mkdir -p /etc/yum.repos.d/bak # mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/bak # mv Cent* bak # curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # yum clean all # yum makecache
-
#### 更新系统
# yum upgrade
-
#### COMPLETE!!!
Linux公社的RSS地址:
本文永久更新链接地址:
宿主机上操作:紧要涉及ntp服务器的搭建以及fencing的配置
-
### 确保宿主机系统是联网的,配置网络请参考 此链接
-
### 安装 virt-manager
# dnf install virt-manager libvirt
-
### 使用virt-manager创设kvm虚拟机并配备kvm虚拟机 请参考 此链接
-
### 安装fence、ntpd等软件
# dnf install -y ntp fence-virtd fence-virtd-multicast fence-virtd-libvirt fence-virt*
-
### 配置ntpd 设置区时为Hong Kong
# timedatectl list-timezones | grep Shanghai # timedatectl set-timezone Asia/Shanghai
### 修改ntp配置文件
# 删除原配置中的相关配置项 # sed -e '/^server/d' -e '/^#server/d' -e '/^fudge/d' -e '/^#fudge/d' -i /etc/ntp.conf # 在结尾添加ntp服务器配置 # sed -e '$a server 127.127.1.0' -e '$a fudge 127.127.1.0 stratum' -i /etc/ntp.conf
### 安装开机自启、开启服务并查阅景况
# systemctl enable ntpd.service && systemctl start ntpd.service && systemctl status ntpd.service
ntpq -c peers ntpq -c assoc timedatectl
-
### 配置fence-virtd
### 创建 /etc/cluster 文件夹
# mkdir -p /etc/cluster
### 生成fence_xvm.key文件
# echo fecb9e62cbcf4e54dcfb > /etc/cluster/fence_xvm.key
### 初始化fence_virtd
# fence_virtd -c
### 确认模块搜索路径
确认监听方式
确认监听IP地址以及端口
确认监听网卡接口
确认密钥路径
确认后端虚拟化模块
确认Libvirt URL
是或不是替换文件
### 设置开机自启、开启服务并查阅情状
# systemctl enable fence_virtd && systemctl start fence_virtd && systemctl status fence_virtd
-
### 验证fencevirtd 查询所有情形的虚拟机
# fence_xvm -o list
### 查询结果如下图:
-
### Fencing操作 关闭所有已开启的虚拟机
# 启动(on)、关机(off)、重启(reboot)、状态获取(status) # fence_xvm -o off -H HA1 # fence_xvm -o off -H HA2 # fence_xvm -o off -H HA3
### 再度询问,结果如下图:
PS:公钥初步化和促成SSH免密登陆的剧本(build-ssh-credit.sh),间接拷贝就可利用。
#!/usr/bin/bash
# 安装expect,minimal没有此rpm包,需联网或有本地yum源
yum install expect -y
expect << EOF
set timeout 10
# 创建公有密钥
spawn ssh-keygen -t rsa
expect {
"*to save the key" {send "\n";exp_continue}
"*(y/n)" {send "y\r";exp_continue}
"Enter passphrase" {send "\n";exp_continue}
"Enter same passphrase" {send "\n";exp_continue}
}
EOF
# 获取/etc/hosts文件中除localhost的映射关系
ip_list=`grep -v 'localhost' /etc/hosts | awk -F ' ' '{print $1,$2}'`
for ip in $ip_list
do
expect << EOF
set timeout 2
# 发送公有密钥
spawn ssh-copy-id root@$ip
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "000000\r";exp_continue}
}
# 拷贝/etc/hosts文件到远程机器
spawn scp /etc/hosts $ip:/etc
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "root\r";exp_continue}
}
EOF
done
kvm虚拟机上操作:主要涉及Ntp服务一同和Pacemaker集群的搭建
-
### 配置kvm虚拟机
-
### 安装ntpd、pcs等软件
# yum install -y ntp pcs pacemaker corosync fence-agents-all resource-agents
-
### 配置ntpd 与宿主机上操作看似,此处不再举办
1 # timedatectl set-timezone Asia/Shanghai 2 3 # sed -i s/^server.*// /etc/ntp.conf 4 # echo "server 192.168.43.177 iburst" >> /etc/ntp.conf 5 # echo "SYNC_HWCLOCK=yes" >> /etc/sysconfig/ntpdate 6 # systemctl enable ntpd.service && systemctl start ntpd.service && systemctl status ntpd.service
### 查询当明天子设置
# timedatectl
-
澳门金沙国际 ,### 配置Pacemaker集群 检查是或不是安装Pacemaker软件 创制Fencing设备密钥Key
# mkdir -p /etc/cluster # echo fecb9e62cbcf4e54dcfb > /etc/cluster/fence_xvm.key
### 设置hacluster用户密码
# echo 000000|passwd --stdin hacluster
### 创造Pacemaker集群 创造时须求开展授权认证,以及须要加上到集群的主机名
# pcs cluster auth ha1 ha2 ha3 -u hacluster -p 000000 --force
### 设置集群的名目时索要指定集群名称和内需添加到集群的主机名
# pcs cluster setup --force --name openstack-ha ha1 ha2 ha3
### 设置在集群拥有节点启动时启用corosync和pacemaker
# pcs cluster enable --all
### 设置在集群拥有节点都张开集群
# pcs cluster start --all
### 查询集群状态
# pcs status
### 为了兑现对虚拟机的Fencing操作,需求为Pacemaker集群配置Stonith资源
1 # pcs stonith create fence1 fence_xvm multicast_address=225.0.0.12 2 # pcs stonith create fence2 fence_xvm multicast_address=225.0.0.12 3 # pcs stonith create fence3 fence_xvm multicast_address=225.0.0.12
### 查询集群状态
[root@ha1 ~]# pcs status Cluster name: openstack-ha Stack: corosync Current DC: ha1 (version 1.1.18-11.el7_5.3-2b07d5c5a9) - partition with quorum Last updated: Thu Aug 16 15:30:59 2018 Last change: Thu Aug 16 12:44:03 2018 by root via cibadmin on ha1 3 nodes configured 3 resources configured Online: [ ha1 ha2 ha3 ] Full list of resources: fence1 (stonith:fence_xvm): Started ha1 fence2 (stonith:fence_xvm): Started ha2 fence3 (stonith:fence_xvm): Started ha3 Daemon Status: corosync: active/enabled pacemaker: active/enabled pcsd: active/enabled
### 查询当前 Pacemaker 集群中的 Stonith 资源
[root@ha1 ~]# pcs stonith show fence1 (stonith:fence_xvm): Started ha1 fence2 (stonith:fence_xvm): Started ha2 fence3 (stonith:fence_xvm): Started ha3