1. 程式人生 > >KVM實現分散式部署lamp並安裝WordPress

KVM實現分散式部署lamp並安裝WordPress

實驗思維導圖:

分析:

本實驗要求使用KVM安裝三臺虛擬機器,實現mysql,php,httpd,分散式部署,並完成lamp環境搭建WordPress

搭建整體環境,克隆一臺新的虛擬機器,在啟動之前選擇如下,從而使得本機支援虛擬化實現

首先安裝kvm環境

[[email protected] ~]# yum -y install libvirt-daemon-kvm qemu-kvm virt-manager libvirt
[[email protected] ~]# systemctl restart libvirt

建立http-fpm網橋

[[email protected] ~]# cd /etc/libvirt/qemu/networks/
[[email protected] networks]# cp default.xml httpfpm.xml
[[email protected] networks]# vim httpfpm.xml 
#修改指定的mac,uuid,
<network>
  <name>httpfpm</name>
  <uuid>06a6d52e-6c03-4b29-bdf7-2cfe6d5e937a</uuid>
  <bridge name='httpfpm' stp='on' delay='0'/>
  <mac address='52:54:00:c6:21:33'/>
  <ip address='10.0.0.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='10.0.0.2' end='10.0.0.254'/>
    </dhcp>
  </ip>
</network>

定義並啟動httpfpm網橋

[[email protected] ~]# virsh net-define /etc/libvirt/qemu/networks/httpfpm.xml 
[[email protected] ~]# virsh net-start httpfpm
[[email protected] ~]# virsh net-autostart httpfpm

建立php-mysql網橋

要求同上,具體如下

[[email protected] ~]# cd /etc/libvirt/qemu/networks/
[[email protected]
networks]# cp httpfpm.xml phpmysql.xml <network> <name>phpmysql</name> <uuid>06a6d52e-6c03-4b29-bdf7-2cfe6d5e937b</uuid> <bridge name='phpmysql' stp='on' delay='0'/> <mac address='52:54:00:c6:21:34'/> <ip address='20.0.0.1' netmask='255.255.255.0'> <dhcp> <range start='20.0.0.2' end='20.0.0.254'/> </dhcp> </ip> </network> [[email protected] ~]# virsh net-define /etc/libvirt/qemu/networks/phpmysql.xml Network phpmysql defined from /etc/libvirt/qemu/networks/phpmysql.xml [[email protected] ~]# virsh net-start phpmysql Network phpmysql started [[email protected] ~]# virsh net-autostart phpmysql Network phpmysql marked as autostarted

檢視建立的網橋狀態

[[email protected] ~]# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
 httpfpm              active     yes           yes
 phpmysql             active     yes           yes

全部執行

建立物理橋 

[[email protected] ~]# nmcli connection add type bridge con-name br0 ifname br0
[[email protected] ~]# nmcli connection modify br0 ipv4.method manual ipv4.addresses 172.18.253.64/16 ipv4.gateway 172.18.0.1 ipv4.dns 172.18.0.1
[[email protected] ~]# nmcli connection up br0
[[email protected] ~]# nmcli connection add type bridge-slave ifname ens33 master br0
[[email protected] ~]# nmcli connection up bridge-slave-ens33

檢視物理網橋,以及虛擬網橋,和自己指定網路端一樣

[[email protected] ~]# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.253.64  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::fa1a:9524:4b58:9ad3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c7:a9:bb  txqueuelen 1000  (Ethernet)
        RX packets 142114  bytes 2595479371 (2.4 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 99313  bytes 14884523 (14.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
httpfpm: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 10.0.0.1  netmask 255.255.255.0  broadcast 10.0.0.255
        ether 52:53:00:c6:21:33  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
phpmysql: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 20.0.0.1  netmask 255.255.255.0  broadcast 20.0.0.255
        ether 52:53:00:c6:21:34  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

建立虛擬磁碟檔案,因為本機的根環境容量較小,就選擇了掛載伺服器的指定系統映象目錄,自己在做實驗時候,可以下載下來要虛擬機器centos映象檔案

[[email protected] ~]# mkdir -pv /virtualmachines/centos7_base
[[email protected] ~]# qemu-img create -f qcow2 -o size=80G,preallocation=metadata,compat=1.1 /virtualmachines/centos7_base/centos7.img

[[email protected] ~]# mount 172.18.0.1:/var/ftp/pub /mnt/
[[email protected] ~]# virt-manager 

啟動虛擬化安裝centos7,我們先安裝一個模板機,然後直接複製模板機為我們想要的機器

 

選擇網絡卡為br0這樣方便待會我們方便下載倉庫

 

過我已經安裝過了,選擇指定的centos7映象,然後,安裝到本地的指定目錄下/virtualmachines/centos7_base/centos7.img

為了方便,選擇了最小化安裝,經過幾分鐘的centos7安裝,和平常安裝centos7一樣

root登陸,在這個模板機器裡面我們可以先下載好一些常用命令工具包,net-tools,以及服務,httpd,php-fpm,php-mysql,mariadb-server,php,,這樣一會就可以不用再手動配置倉庫.

安裝完成之後,就可以進行虛擬機器的複製了,我們先準備指定目錄以及檔案

[[email protected] ~]# mkdir -pv /virtualmachines/{httpd,php,mysql}
mkdir: created directory ‘/virtualmachines/httpd’
mkdir: created directory ‘/virtualmachines/php’
mkdir: created directory ‘/virtualmachines/mysql’
[[email protected] ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/httpd/httpd.img
[[email protected] ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/php/php.img
[[email protected] ~]# cp /virtualmachines/centos7_base/centos7.img /virtualmachines/mysql/mysql.img

通過模板機,配置實際需要三臺機器的mac,uuid,以及安裝原始檔,以httpd為例

[[email protected] ~]# cd /etc/libvirt/qemu/
[[email protected] qemu]# cp centos7.5.xml httpd.xml
[[email protected] qemu]# vim httpd.xml 

 <name>httpd</name>
  <uuid>a8dbf8ae-d691-4f00-835b-3c29ce8972a0</uuid>
 <source file='/virtualmachines/httpd/httpd.img'/>
      <mac address='52:54:00:cc:f6:d1'/>

設定並啟動httpd虛擬機器,其他兩臺一樣操作

[[email protected] qemu]# virsh define httpd.xml 
Domain httpd defined from httpd.xml

[[email protected] qemu]# virsh autostart httpd
Domain httpd marked as autostarted

[[email protected] qemu]# virsh start httpd
Domain httpd started

完成之後,再次

[[email protected] ~]# virt-manager 

發現已經變成了四臺虛擬機器了,這時候開始配置網絡卡

httpd需要兩個網絡卡,一個是172.%  一個為10.%  如圖所示分別選擇br0和httpfpm網絡卡最後點右下角apply,再次進入系統後重啟網路服務。需要的指定ip段就出來了
 

php需要20.%和10.%兩個網絡卡,同樣設定,在此略

mysql只需20.%網絡卡就行

至此,三臺機器環境搭建完畢。

在httpd機器上設定虛擬網絡卡

[[email protected] ~]# mkdir -pv /vhosts/pam/htdocs
[[email protected] ~]# cd /etc/httpd/conf.d/
​[[email protected] ~]# vim pma.conf
    DirectoryIndex index.php                
	<VirtualHost *:80>                      
	ServerName bbs.magedu.com
	DocumentRoot /vhosts/pam/htdocs
	<Directory "/vhosts/pam/htdocs">                                     
	Require all granted                                     
	</Directory>  
    ProxyRequests Off                               
	ProxyPassMatch ^/(.*\.php)$  fcgi://10.0.0.231:9000/vhosts/pam/htdocs/$1                                  
	</VirtualHost>       

修改httpd主配置檔案。

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

修改/etc/hosts/配置

[[email protected] ~]# cd /etc/hosts
172.18.253.74  bbs.magedu.com

之後重啟httpd服務

在php-fpm機器上修改主配置檔案/etc/php-fpm.d/www.conf

修改以下配置

listen =0.0.0.o:9000
;listen.allowed_clients = 127.0.0.1

在mysql伺服器上,啟動mysql,建立資料庫,db1, 使用者root,密碼user1

MariaDB [(none)]> create database db1;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on db1.* to 'root'@'20.0.0.60' identified by 'user1';
Query OK, 0 rows affected (0.02 sec)

在php-fpm上安裝wordpress

[[email protected] ~]# mkdir -pv /vhosts/pam
[[email protected] ~]# cd  /vhosts/pam
[[email protected] pam]# tar xvf wordpress-4.9.4-zh_CN.tar.gz 
[[email protected] pam]# ln -sv wordpress htdocs

現在可以訪問htppd伺服器了
在物理機瀏覽器輸入172.18.253.74就可以訪問你的wordpress了,接著填入你的資訊,完成安裝即可