网站架构演变、LNP+Mariadb数据库分离、Web服务器集群、Keepalived高可用

day02

day02深入理解程序的数据存储验证配置NFS服务器配置代理服务器配置名称解析服务器配置

深入理解程序的数据存储

  • 程序将文字数据保存到数据库中
  • 程序将非文字数据(如图片、视频、压缩包等)保存到相应的文件目录中

验证

  • 发一篇文章,文章内容包含文字和图片
  • 在数据库中查看文字数据。在最新的一条记录中,可以查看到图片的保存位置
[root@database ~]# mysql
mysql> use wordpress;
mysql> select * from wp_posts\G
  • 在文件系统中查看图片文件。/usr/share/nginx/html/wp-content/uploads/是固定位置,其后的2023/01是年和月目录。每个月上传的图片,都会保存到相应的月份目录。
[root@web1 html]# ls /usr/share/nginx/html/wp-content/uploads/2023/01/
snow.jpg

配置NFS服务器

  • 准备环境
[root@myhost ~]# vm clone nfs
[root@myhost ~]# vm setip nfs 192.168.88.31
  • 配置ansible环境
[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服务
# 1. 配置yum
[root@pubserver project01]# ansible-playbook 01-upload-repo.yml 
 
# 2. 配置nfs服务
[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    # 创建共享目录
      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      # 循环启动服务
      service:
        name: "{{item}}"
        state: started
        enabled: yes
      loop:
        - rpcbind       # nfs服务依赖rpcbind服务
        - 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服务器
[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服务器的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服务器
[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

配置代理服务器

  • 准备环境
# 1. 创建两台虚拟机
[root@myhost ~]# vm clone haproxy{1..2}
[root@myhost ~]# vm setip haproxy1 192.168.88.5
[root@myhost ~]# vm setip haproxy2 192.168.88.6
  • 配置ansible环境
[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
  • 配置高可用、负载均衡功能
# 1. 配置yum
[root@pubserver project01]# ansible-playbook 01-upload-repo.yml 
 
# 2. 配置调度服务器
[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. 修改配置文件并启动服务
[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,实现高可用集群
[root@haproxy1 ~]# vim /etc/keepalived/keepalived.conf 
...略...
 12    router_id haproxy1   # 为本机取一个唯一的id
 13    vrrp_iptables        # 自动开启iptables放行规则
...略...
 20 vrrp_instance VI_1 {
 21     state MASTER        # 主服务器状态是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   # 为本机取一个唯一的id
 13    vrrp_iptables        # 自动开启iptables放行规则
...略...
 20 vrrp_instance VI_1 {
 21     state BACKUP        # 备份服务器状态是BACKUP
 22     interface eth0
 23     virtual_router_id 51
 24     priority 80         # 备份服务器优先级低于主服务器
 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. 启动服务
[root@haproxy1 ~]# systemctl enable keepalived.service --now
[root@haproxy2 ~]# systemctl enable keepalived.service --now
 
# 7. 验证。haproxy1上出现VIP。客户端访问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

配置名称解析

  • 通过本机hosts文件实现名称解析
[root@myhost ~]# echo "192.168.88.80 www.lab.com" >> /etc/hosts
  • 如果客户端是windows主机,则使用记事本程序打开C:\windows\System32\drivers\etc\hosts添加名称解析
  • 当点击http://www.lab.com页面中任意链接时,地址栏上的地址,都会变成192.168.88.11。通过以下方式修复它:
# 在nfs服务器上修改配置文件
[root@nfs ~]# vim /nfs_root/html/wp-config.php 
# define('DB_NAME', 'wordpress')它的上方添加以下两行:
define('WP_SITEURL', 'http://www.lab.com');
define('WP_HOME', 'http://www.lab.com');

服务器配置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值