目錄
day02
深入理解程序的數(shù)據(jù)存儲(chǔ)
驗(yàn)證
配置NFS服務(wù)器
配置代理服務(wù)器
配置名稱解析
day02
深入理解程序的數(shù)據(jù)存儲(chǔ)
- 程序?qū)⑽淖謹(jǐn)?shù)據(jù)保存到數(shù)據(jù)庫(kù)中
- 程序?qū)⒎俏淖謹(jǐn)?shù)據(jù)(如圖片、視頻、壓縮包等)保存到相應(yīng)的文件目錄中
驗(yàn)證
- 發(fā)一篇文章,文章內(nèi)容包含文字和圖片
- 在數(shù)據(jù)庫(kù)中查看文字?jǐn)?shù)據(jù)。在最新的一條記錄中,可以查看到圖片的保存位置
[root@database ~]# mysql
mysql> use wordpress;
mysql> select * from wp_posts\G
- 在文件系統(tǒng)中查看圖片文件。
/usr/share/nginx/html/wp-content/uploads/
是固定位置,其后的2023/01
是年和月目錄。每個(gè)月上傳的圖片,都會(huì)保存到相應(yīng)的月份目錄。
[root@web1 html]# ls /usr/share/nginx/html/wp-content/uploads/2023/01/
snow.jpg
配置NFS服務(wù)器
- 準(zhǔn)備環(huán)境
虛擬機(jī)ip與名稱:nfs 192.168.88.31文章來源:http://www.zghlxwxcb.cn/news/detail-829451.html
- 配置ansible環(huán)境
[root@pubserver project01]# vim inventory
[webservers]
web1 ansible_host=192.168.88.11
web2 ansible_host=192.168.88.12
web3 ansible_host=192.168.88.13
[dbs]
database ansible_host=192.168.88.21
[storages]
nfs ansible_host=192.168.88.31
[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=a
- 配置nfs服務(wù)
# 1. 配置yum
[root@pubserver project01]# ansible-playbook 01-upload-repo.yml
# 2. 配置nfs服務(wù)
[root@pubserver project01]# vim 08-config-nfs.yml
---
- name: config nfs
hosts: nfs
tasks:
- name: install nfs # 安裝nfs
yum:
name: nfs-utils
state: present
- name: mkdir /nfs_root # 創(chuàng)建共享目錄
file:
path: /nfs_root
state: directory
mode: "0755"
- name: nfs share # 修改配置文件
lineinfile:
path: /etc/exports
line: '/nfs_root 192.168.88.0/24(rw)'
- name: start service # 循環(huán)啟動(dòng)服務(wù)
service:
name: "{{item}}"
state: started
enabled: yes
loop:
- rpcbind # nfs服務(wù)依賴rpcbind服務(wù)
- nfs-server
[root@pubserver project01]# ansible-playbook 08-config-nfs.yml
# 3. 查看共享輸出
[root@nfs ~]# showmount -e
Export list for nfs:
/nfs_root 192.168.88.0/24
- 遷移文件至nfs共享
# 1. 重新下載web1的html目錄
[root@pubserver project01]# cp 06-fetch-web1.yml 09-fetch-web1.yml
---
- name: copy web
hosts: web1
tasks:
- name: compress html # 壓縮html目錄到/root下
archive:
path: /usr/share/nginx/html
dest: /root/html2.tar.gz
format: gz
- name: download html
fetch:
src: /root/html2.tar.gz # 下載壓縮文件
dest: files/
flat: yes
[root@pubserver project01]# ansible-playbook 09-fetch-web1.yml
# 2. 釋放壓縮包到nfs服務(wù)器
[root@pubserver project01]# cp 07-deploy-web23.yml 10-deploy-nfs.yml
[root@pubserver project01]# vim 10-deploy-nfs.yml
---
- name: deploy nfs
hosts: nfs
tasks:
- name: unarchive to web # 將控制端壓縮文件解壓到指定位置
unarchive:
src: files/html2.tar.gz
dest: /nfs_root/
[root@pubserver project01]# ansible-playbook 10-deploy-nfs.yml
# 3. 清除web服務(wù)器的html目錄
[root@pubserver project01]# vim 11-rm-html.yml
---
- name: rm html
hosts: webservers
tasks:
- name: rm html
file:
path: /usr/share/nginx/html
state: absent
- name: create html
file:
path: /usr/share/nginx/html
state: directory
owner: apache
group: apache
mode: "0755"
[root@pubserver project01]# ansible-playbook 11-rm-html.yml
# 4. 掛載nfs到web服務(wù)器
[root@pubserver project01]# vim 12-mount-nfs.yml
---
- name: mount nfs
hosts: webservers
tasks:
- name: install nfs
yum:
name: nfs-utils
state: present
- name: mount nfs
mount:
path: /usr/share/nginx/html
src: 192.168.88.31:/nfs_root/html
fstype: nfs
state: mounted
[root@pubserver project01]# ansible-playbook 12-mount-nfs.yml
配置代理服務(wù)器
- 準(zhǔn)備環(huán)境
虛擬機(jī)ip與名稱:haproxy1 192.168.88.5 ? haproxy2 192.168.88.6文章來源地址http://www.zghlxwxcb.cn/news/detail-829451.html
- 配置ansible環(huán)境
[root@pubserver project01]# vim inventory
[webservers]
web1 ansible_host=192.168.88.11
web2 ansible_host=192.168.88.12
web3 ansible_host=192.168.88.13
[dbs]
database ansible_host=192.168.88.21
[storages]
nfs ansible_host=192.168.88.31
[lb]
haproxy1 ansible_host=192.168.88.5
haproxy2 ansible_host=192.168.88.6
[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=a
- 配置高可用、負(fù)載均衡功能
# 1. 配置yum
[root@pubserver project01]# ansible-playbook 01-upload-repo.yml
# 2. 配置調(diào)度服務(wù)器
[root@pubserver project01]# vim 13-install-lb.yml
---
- name: install lb
hosts: lb
tasks:
- name: install pkg
yum:
name: haproxy,keepalived
state: present
[root@pubserver project01]# ansible-playbook 13-install-lb.yml
# 3. 修改配置文件并啟動(dòng)服務(wù)
[root@pubserver project01]# vim 14-config-lb.yml
---
- name: config haproxy
hosts: lb
tasks:
- name: rm lines
shell: sed -i '64,$d' /etc/haproxy/haproxy.cfg
- name: add lines
blockinfile:
path: /etc/haproxy/haproxy.cfg
block: |
listen wordpress
bind 0.0.0.0:80
balance roundrobin
server web1 192.168.88.11:80 check inter 2000 rise 2 fall 5
server web2 192.168.88.12:80 check inter 2000 rise 2 fall 5
server web3 192.168.88.13:80 check inter 2000 rise 2 fall 5
listen mon
bind 0.0.0.0:1080
stats refresh 30s
stats uri /mon
stats auth admin:admin
- name: start service
service:
name: haproxy
state: started
enabled: yes
[root@pubserver project01]# ansible-playbook 14-config-lb.yml
# 4. haproxy1配置keepalived,實(shí)現(xiàn)高可用集群
[root@haproxy1 ~]# vim /etc/keepalived/keepalived.conf
...略...
12 router_id haproxy1 # 為本機(jī)取一個(gè)唯一的id
13 vrrp_iptables # 自動(dòng)開啟iptables放行規(guī)則
...略...
20 vrrp_instance VI_1 {
21 state MASTER # 主服務(wù)器狀態(tài)是MASTER
22 interface eth0
23 virtual_router_id 51
24 priority 100
25 advert_int 1
26 authentication {
27 auth_type PASS
28 auth_pass 1111
29 }
30 virtual_ipaddress {
31 192.168.88.80 # vip地址
32 }
33 }
# 以下全部刪除
# 5. haproxy2配置keepalived
[root@haproxy1 ~]# scp /etc/keepalived/keepalived.conf 192.168.88.6:/etc/keepalived/
[root@haproxy2 ~]# vim /etc/keepalived/keepalived.conf
...略...
12 router_id haproxy2 # 為本機(jī)取一個(gè)唯一的id
13 vrrp_iptables # 自動(dòng)開啟iptables放行規(guī)則
...略...
20 vrrp_instance VI_1 {
21 state BACKUP # 備份服務(wù)器狀態(tài)是BACKUP
22 interface eth0
23 virtual_router_id 51
24 priority 80 # 備份服務(wù)器優(yōu)先級(jí)低于主服務(wù)器
25 advert_int 1
26 authentication {
27 auth_type PASS
28 auth_pass 1111
29 }
30 virtual_ipaddress {
31 192.168.88.80
32 }
33 }
# 6. 啟動(dòng)服務(wù)
[root@haproxy1 ~]# systemctl enable keepalived.service --now
[root@haproxy2 ~]# systemctl enable keepalived.service --now
# 7. 驗(yàn)證。haproxy1上出現(xiàn)VIP??蛻舳嗽L問http://192.168.88.80即可
[root@haproxy1 ~]# ip a s | grep 192
inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute eth0
inet 192.168.88.80/32 scope global eth0
配置名稱解析
- 通過本機(jī)hosts文件實(shí)現(xiàn)名稱解析
[root@myhost ~]# echo "192.168.88.80 www.danei.com" >> /etc/hosts
- 如果客戶端是windows主機(jī),則使用記事本程序打開
C:\windows\System32\drivers\etc\hosts
添加名稱解析 - 當(dāng)點(diǎn)擊http://www.danei.com頁(yè)面中任意鏈接時(shí),地址欄上的地址,都會(huì)變成
192.168.88.11
。通過以下方式修復(fù)它:
# 在nfs服務(wù)器上修改配置文件
[root@nfs ~]# vim /nfs_root/html/wp-config.php
# define('DB_NAME', 'wordpress')它的上方添加以下兩行:
define('WP_SITEURL', 'http://www.danei.com');
define('WP_HOME', 'http://www.danei.com');
到了這里,關(guān)于網(wǎng)站架構(gòu)演變、LNP+Mariadb數(shù)據(jù)庫(kù)分離、Web服務(wù)器集群、Keepalived高可用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!