[实验] Nginx + Keepalived 网站服务负载均衡加高可用的实现

纪念:站主于 2021 年 2 月完成了此开源实验,并将过程中的所有命令经过整理和注释以后,形成以下教程

步骤一:拓扑图

1.1 服务器列表

client enp1s0: 172.16.1.99

proxy1 enp1s0: 172.16.0.101
enp7s0: 172.16.1.101
virtual IP: 172.16.1.100

proxy2 enp1s0: 172.16.0.102
enp7s0: 172.16.1.102

web1 enp1s0: 172.16.0.11

web2 enp1s0: 172.16.0.12

1.2 拓扑图

                      proxy1                                       web1
                      enp7s0:172.16.1.101 enp1s0:172.16.0.101      enp1s0:172.16.0.11
                      virtual IP:172.16.1.100
client
enp1s0:172.16.1.99
                      proxy2                                       web2
                      enp7s0:172.16.1.102 enp1s0:172.16.0.102      enp1s0:172.16.0.12

1.3 拓扑图简介

1) web1 安装 Nginx,web2 安装 Apache 实现网站服务
2) proxy1 和 proxy2 安装 Nginx 实现网站代理,轮询代理 web1、web2 上的网站服务实现负载均衡
3) 虚拟 IP 172.16.1.90 通过 Keepalived 默认放在 proxy1 的 enp7s0 网卡上,如果 proxy1 宕机或者检测到自己 Nginx 代理进程死掉,则虚拟 IP 172.16.1.90 则挂在 proxy2 的 enp7s0 网卡上实现高可用
4) 如果 web1 和 web2 中有一台服务器宕机,则 proxy1 和 proxy2 会自动不再向这台服务器请求网站服务,直到它恢复正常
5) 最终达到的效果是 client 向虚拟 IP 请求网站服务,此时如果 proxy1 正常就代表虚拟 IP 轮询调度 web1 和 web2 上的网站服务,再返回给 client。如果 proxy1 宕机则由 proxy2 代表虚拟 IP 完成次操作

步骤二: 系统环境要求

1) 所有服务器的系统都需要是 CentOS 8 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器都要关闭 SELinux
4) 所有服务器系统都要配置好可用的软件源
5) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
6) client 的 enp1s0 网卡、proxy1 的 enp7s0 网卡和 proxy2 的 enp7s0 网卡要可以相互 ping 通自己和对方的 IP
7) proxy1 的 enp1s0 网卡、proxy2 的 enp1s0 网卡、web1 的 enp1s0 网卡和 web2 的 enp1s0 网卡要可以相互 ping 通自己和对方的 IP 地址

步骤三:搭建网站服务

3.1 在 web1 上搭建网站服务

3.1.1 在 web1 上安装 Nginx

(只在 web1 上执行以下步骤)

# yum -y install nginx
3.1.2 给 web1 制定网页

(只在 web1 上执行以下步骤)

# echo web1 > /usr/share/nginx/html/index.html
3.1.3 启动 Nginx 并将它设置为开机自启

(只在 web1 上执行以下步骤)

# systemctl enable --now nginx

3.2 在 web2 上搭建网站服务

3.2.1 在 web2 上安装 Apache

(只在 web2 上执行以下步骤)

# yum -y install httpd
3.2.2 给 web2 制定网页

(只在 web2 上执行以下步骤)

# echo web2 > /var/www/html/index.html
3.2.3 启动 Apache 并将它设置为开机自启

(只在 web2 上执行以下步骤)

# systemctl enable --now httpd

步骤四:搭建代理服务

4.1 安装 Nginx

(分别在 proxy1 和 proxy2 上执行以下步骤)

# yum -y install nginx

4.2 修改 Nginx 配置文件

(分别在 proxy1 和 proxy2 上执行以下步骤)

# vi /etc/nginx/nginx.conf

将部分内容修改如下:

......
http {
    upstream webserver {
        server 172.16.0.11:80;
        server 172.16.0.12:80;
    }
......
    server {
        listen       80;

        location / {
        proxy_pass http://webserver;
        }
    }
......
}

4.3 启动 Nginx 并将它设置为开机自启

(分别在 proxy1 和 proxy2 上执行以下步骤)

# systemctl enable --now nginx

步骤五:搭建高可用服务

5.1 安装 Keepalived

(分别在 proxy1 和 proxy2 上执行以下步骤)

# yum -y install keepalived

5.2 创建 Keepalived 检查脚本

5.2.1 创建 Keepalived 检查脚本

(分别在 proxy1 和 proxy2 上执行以下步骤)

# vi /etc/keepalived/nginx_check.sh

创建以下内容:

#!/bin/bash

if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
    systemctl stop nginx
    sleep 5
    if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

(补充:这里以检测 Nginx 没启动就启动 Nginx,5 秒后 Nginx 要是还没有启动就关闭 keepalived 为例)

5.2.2 给 Keepalived 检查脚本执行权限

(分别在 proxy1 和 proxy2 上执行以下步骤)

# chmod u+x /etc/keepalived/nginx_check.sh

5.3 修改 proxy1 上的 Keepalived 配置文件

(只在 proxy1 上执行以下步骤)

# vim /etc/keepalived/keepalived.conf

将全部内容修改如下:

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id proxy1
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight 20
}

vrrp_instance VI_1 {
    state MASTER
    interface enp7s0
    virtual_router_id 90
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        172.16.1.100
    }
}


补充:
1) script “/etc/keepalived/nginx_check.sh” 代表使用的检测脚本是 /etc/keepalived/nginx_check.sh
2) interface enp7s0 代表虚拟 IP 将挂载在 enp7s0 网卡上
3) priority 代表修建级是 101,数字越大优先级越高
4) 172.16.1.100 代表虚拟 IP 是 172.16.1.100

5.4 修改 proxy2 上的 Keepalived 配置文件

(只在 proxy2 上执行以下步骤)

# vim /etc/keepalived/keepalived.conf

将全部内容修改如下:

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id proxy1
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
     script "/etc/keepalived/nginx_check.sh"
     interval 2
     weight 20
}

vrrp_instance VI_1 {
    state BACKUP
    interface enp7s0
    virtual_router_id 90
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
        172.16.1.100
    }
}


补充:
1) script “/etc/keepalived/nginx_check.sh” 代表使用的检测脚本是 /etc/keepalived/nginx_check.sh
2) interface enp7s0 代表虚拟 IP 将挂载在 enp7s0 网卡上
3) priority 代表修建级是 99,数字越大优先级越高
4) 172.16.1.100 代表虚拟 IP 是 172.16.1.100

5.5 启动 Keepalived 并将它设置为开机自启

(分别在 proxy1 和 proxy2 上执行以下步骤)

# systemctl enable --now keepalived.service

步骤六:测试网站负载均衡加高可用

6.1 正常情况下测试网站服务

(只在 client 上执行以下步骤)

# curl 172.16.1.100

(补充:重复以上命令会发现重复显示 web1 和 web2)

6.2 在单节点故障的情况下测试网站服务

6.2.1 关闭 proxy1、proxy2、web1、web2 中的任意一台服务器

(只在 proxy1、proxy2、web1、web2 中的任意一台服务器上执行以下步骤)

# poweroff
6.2.2 测试网站服务

(只在 client 上执行以下步骤)

# curl 172.16.1.100

(补充:重复以上命令会发现重复显示 web1 和 web2)

[实验] HAproxy 代理的设置

纪念:站主于 2021 年 2 月完成了此开源实验,并将过程中的所有命令经过整理和主是以后,形成以下教程

步骤一:拓扑规划

1.1 服务器列表

client eth0:10.0.0.10/24
proxy eth0:10.0.0.5/24
eth1:10.0.1.5/24
web1 eth1:10.0.1.100/24
web2 eht1:10.0.1.200/24

1.2 拓扑图

                                           web1
                                           eth1:10.0.1.100/24
     client                proxy     
eth0:10.0.0.10/24    eth0:10.0.0.5/24  
                     eth1:10.0.1.5/24
                                           web2
                                           eht2:10.0.1.200/24

1.3 拓扑图简介

(1)client 向 proxy 的 eth0:10.0.0.5/24 发送 web 请求
(2)proxy 收到 web 请求后通过 eth1:10.0.1.5/24 将请求发往 web1 或 web2
(3)web1 或 web2 回应 web 请求,并通过 proxy 返回给 client
(4)最终实现单点代理器,双网站热备份

步骤二: 系统环境要求

(1)所有服务器的系统都需要是 CentOS 8 版本
(2)所有服务器都要关闭防火墙
(3)所有服务器都要关闭 SELinux
(4)所有服务器系统都要配置好可用的软件源
(5)需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
(6)所有服务器都要可以相互 ping 通自己和对方的 IP 地址

步骤三:部署集群环境

3.1 在 proxy 上安装 HAporxy

(只在 proxy 上执行以下步骤)

# yum -y install haproxy

3.2 在 web1 上安装 web 服务

3.2.1 安装 httpd

(只在 web1 上执行以下步骤)

# yum -y install httpd
3.2.2 创建网页文件

(只在 web1 上执行以下步骤)

# echo "10.0.1.100" > /var/www/html/index.html
3.2.3 启动 web 服务并设置为开机自启

(只在 web1 上执行以下步骤)

# systemctl start httpd ; systemctl enable httpd

3.3 在 web2 上安装 web 服务

3.3.1 安装 httpd

(只在 web2 上执行以下步骤)

# yum -y install httpd
3.3.2 创建网页文件

(只在 web2 上执行以下步骤)

# echo "10.0.1.200" > /var/www/html/index.html
3.3.3 启动 web 服务并设置为开机自启

(只在 web2 上执行以下步骤)

# systemctl start httpd ; systemctl enable httpd

步骤四:配置 HAproxy 实现 web 负载均衡代理集群

4.1 开启 proxy 的路由转发

4.1.1 在 sysctl.conf 文件里添加路由转发功能

(只在 proxy 上执行以下步骤)

# vim /etc/sysctl.conf

添加以下内容:

net.ipv4.ip_forward = 1
4.1.2 使刚刚添加的功能生效

(只在 proxy 上执行以下步骤)

# sysctl -p

4.2 修改 proxy 上的 HAproxy 配置文件

(只在 proxy 上执行以下步骤)

# vim /etc/haproxy/haproxy.cfg

将全部内容修改如下:

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
    bind *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:80 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 10.0.1.100:80 check
    server  app2 10.0.1.200:80 check

4.3 设置开机自动启动 HAproxy

(只在 proxy 上执行以下步骤)

# systemctl start haproxy ; systemctl enable haproxy

步骤五:测试 Haproxy 代理集群

(只在 client 上执行以下步骤)

# curl 10.10.10.5

(注意:这一步需要多做几次)

[实验] Samba 远程共享服务的搭建

纪念:站主于 2020 年 7 月完成了此开源实验,并将过程中的所有命令经过整理和注释以后,形成以下教程

步骤一:规划拓扑

1.1 服务器列表

服务端 192.168.101.41
客户端 192.168.101.42

1.2 服务器列表简介

1) 服务器提供 Samba 服务将自己的目录分享
2) 客户端挂载和使用 Samba 服务将服务端分享的目录挂载在自己的目录上

步骤二:系统环境要求

1) 所有服务器的系统都需要是 CentOS 8 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器都要打开 SELinux
4) 所有服务器系统都要配置好可用的软件源
5) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
6) 所有服务器都要可以相互 ping 通自己和对方的 IP 地址和主机名

步骤三:在服务端上安装 Samba 服务

(只在服务端上执行以下步骤)

# yum -y install samba

步骤四:在服务端上配置 Samba 服务

4.1 在服务端上配置 Samba 服务文件

(只在服务端上执行以下步骤)

# vim /etc/samba/smb.conf

将全部内容修改如下:

# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.

[global]
workgroup = WORKGROUP
realm = zhumingyu
netbios name = zhumingyu
#encrypt passwords = yes
map to guest = NEVER
security = user
password server = *
name resolve order = bcast host
restrict anonymous = 2
#null passwords = no
#guest account = smb_nobody
#use spnego = yes
client use spnego = yes
server string = ""
host msdfs = no
msdfs root = no
domain master = no
preferred master = no
local master = no
os level = 0
browse list = no
browseable = no
dns proxy = no
wide links = no
public= no
guest ok = no
hosts deny = ALL EXCEPT 192.168.101.42

[sharetest]
valid users = zhumingyu
write list = zhumingyu
read list = zhumingyu
path = /share
guest ok = no
read only = no
browseable = no
writable = yes
public = no
create mask = 0755
directory mask = 0755


补充:
1) 这里的 workgroup = WORKGROUP 是让 Samba 服务属于 WORKGROUP
2) 这里的 hosts deny = ALL EXCEPT 192.168.101.42 是只让客户端 192.168.101.42 能够访问服务端的 Samba
3) 这里的 sharetest 是这个 Samba 挂载点的名称,挂载这个挂载点的格式就是://192.168.101.41/sharetest
4) 这里的 valid users = zhumingyu 是 Samba 服务共享用户需要手动生成,如果换成让所有的服务共享用户都可以使用则可以写成 valid users = @users
5) 这里的 path = /share 是 Samba 服务共享目录需要手动生成

4.2 在服务端上生成 Samba 服务共享用户

4.2.1 在服务端上生成 Samba 服务共享用户

(只在服务端上执行以下步骤)

# useradd zhumingyu

(补充:这里以创建用户 zhumingyu 为例)

4.2.2 在服务端上给 Samba 服务共享用户设置系统密码

(只在服务端上执行以下步骤)

# passwd zhumingyu

(补充:这里以给 zhumingyu 设置密码为例)

4.2.3 在服务端上给 Samba 服务共享用户设置 Samba 共享密码

(只在服务端上执行以下步骤)

# smbpasswd -a zhumingyu

(补充:这里以给 zhumingyu 用户设置 Samba 共享密码为例)

4.2.4 显示Samba 服务共享用户是否可用

(只在服务端上执行以下步骤)

# pdbedit -L

4.3 在服务端上生成 Samba 服务共享目录

4.3.1 在服务端上生成 Samba 服务共享目录

(只在服务端上执行以下步骤)

# mkdir /share

(补充:这里以创建目录 /share 为例)

4.3.2 在服务端上给 Samba 服务共享目录设置权限

(只在服务端上执行以下步骤)

# chmod 755 /share

(补充:这里以给 /share 目录设置 755 权限为例)

4.3.3 在服务端上给 Samba 服务共享目录设置所属主和所属组

(只在服务端上执行以下步骤)

如果是 CentOS Linux & RHEL:

# chown zhumingyu:zhumingyu /share

如果是 openSUSE & SLE:

# chown zhumingyu:users /share

(补充:这里以将 /share 目录的所属主设置成 zhumingyu 为例)

4.3.4 在服务端上给 Samba 服务共享目录设置 SELinux 标签

(只在服务端上执行以下步骤)

# semanage fcontext -a -t samba_share_t '/share(/.*)?'

(补充:这里以给 /share 目录打上 samba_share_t SELinux 标签为例)

4.3.5 在服务端上让 Samba 服务共享目录上的 SELinux 标签立刻生效

(只在服务端上执行以下步骤)

# restorecon -RFvv /share/

步骤五:启动 Samba 服务并设置为开机自动启动

(只在服务端上执行以下步骤)

# systemctl enable --now smb

步骤六:客户端使用服务端 Samba 服务

6.1 在客户端上安装 Samba 客户端软件

(只在客户端上执行以下步骤)

# yum -y install samba-client cifs-utils

6.2 在客户端上测试服务端的 Samba 服务

(只在客户端上执行以下步骤)

# smbclient --user=zhumingyu -L //192.168.101.41

或者:

# smbclient //192.168.101.41/sharetest -U zhumingyu
smb: \> ls
smb: \> exit

(补充:这里以通过用户 zhumingyu 测试 IP 地址 192.168.101.41 的 /sharetest Samba 共享目录为例)

6.3 在客户端上挂载服务端的 Samba 目录

6.3.1 手动挂载的方法

(只在客户端上执行以下步骤)

# mount -t cifs -o dir_mode=0755,file_mode=0755,username=zhumingyu,password=1,sec=ntlmssp //192.168.101.41/sharetest /tmp

(补充:这里以通过用户 zhumingyu 密码为 1,目录权限为 0755,文件权限为 0755,挂载 IP 地址 192.168.101.41 的 /sharetest Samba 共享目录到本地的 /tmp 目录为例)

(注意:用户和密码不能一样,否则会报错)

6.3.2 自动挂载的方法

(只在客户端上执行以下步骤)

# vim /etc/fstable

添加以下内容:

......
//192.168.101.41/sharetest /tmp cifs defaults,rw,dir_mode=0755,file_mode=0755,username=zhumingyu,password=1 0 0

(补充:这里以通过用户 zhumingyu 密码为 1,目录权限为 0755,文件权限为 0755,挂载 IP 地址 192.168.101.41 的 /sharetest Samba 共享目录到本地的 /tmp 目录为例)

(注意:用户和密码不能一样,否则会报错)

[实验] 自动挂载服务的搭建 (通过 Autofs 和 NFS 实现) (CentOS Linux 8 版)

纪念:站主于 2020 年 6 月完成了此开源实验,并将过程中的所有命令经过整理和注释以后,形成以下教程

步骤一:规划拓扑

1.1 服务器列表

服务端 192.168.101.10
客户端 192.168.101.11

1.2 服务器列表简介

1) 服务器提供 NFS 服务将自己的目录分享
2) 客户端挂载和使用 NFS 服务将服务端分享的目录挂载在自己的目录上

步骤二:系统环境要求

1) 所有服务器的系统都需要是 CentOS 8 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器系统都要配置好可用的软件源
4) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
5) 所有服务器都要可以相互 ping 通自己和对方的 IP 地址和主机名

步骤三:所有服务器安装 NFS 服务

3.1 所有服务器安装 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# yum -y install rpcbind nfs-utils

3.2 设置所有服务器开机自启 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# systemctl enable nfs-server

3.3 所有服务器启动 NFS 服务

(分别在服务端和客户端上执行以下步骤)

# systemctl start nfs-server

步骤四:配置 NFS 服务

4.1 创建用于 NFS 服务的目录

4.1.1 创建被 NFS 服务共享的目录

(只在服务端上执行以下步骤)

# mkdir /nfsshare
4.1.2 创建用于自动挂载 NFS 服务分享目录的目录

(只在客户端上执行以下步骤)

# mkdir /autofs

4.2 配置服务端的 NFS 服务配置文件

4.2.1 在服务端上添加可被 NFS 服务挂载的选项

(只在服务端上执行以下步骤)

# vim /etc/exports

添加以下内容:

......
/nfsshare 192.168.101.0/24(rw,sync,no_root_squash,no_subtree_check)

(补充:这里的 192.168.101.0.24 是客户端的 IP 地址所在的网段)

4.2.2 让刚刚修改的 NFS 服务配置文件生效

(只在服务端上执行以下步骤)

# exportfs -a

4.3 部署客户端的 Autofs 自动挂载服务

4.3.1 安装 Autofs 服务

(只在客户端上执行以下步骤)

# yum -y install autofs
4.3.2 设置客户端开机自启 Autofs 服务

(只在客户端上执行以下步骤)

# systemctl enable autofs

4.3.3 在客户端上设置 Autofs 自动挂载服务

4.3.3.1 在客户端上设置 Autofs 自动挂载的主配置文件

(只在客户端上执行以下步骤)

# vim /etc/auto.master

将以下内容:

......
#
/misc   /etc/auto.misc
#
......

修改为:

......
/misc   /etc/auto.misc
/autofs /etc/auto.autofs
......

(补充:在这里指定了 /etc/auto.autofs 为 Autofs 的从配置文件,并且将 autofs 的主目录设置为 /autofs)

4.3.3.2 在客户端上设置 Autofs 的从配置文件

(只在客户端上执行以下步骤)

# cp /etc/auto.misc /etc/auto.autofs
# vim /etc/auto.autofs

将以下内容:

......
cd              -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
......

修改为:

......
cd              -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
directory01             -fstype=nfs,rw 192.168.101.10:/nfsshare
......

(补充:在这里指定了 Autofs 的次级目录为 directory01,即:/autofs/directory01)

4.3.4 让刚刚修改的 Autofs 自动挂载服务配置文件生效

(只在客户端上执行以下步骤)

# systemctl restart autofs

步骤五:显示 Autofs 自动挂载服务是否设置成功

5.1 显示客户端当前的目录挂载情况

(只在客户端上执行以下步骤)

# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        957M     0  957M   0% /dev
tmpfs           971M     0  971M   0% /dev/shm
tmpfs           971M   17M  954M   2% /run
tmpfs           971M     0  971M   0% /sys/fs/cgroup
/dev/vda1        10G  1.6G  8.5G  16% /
tmpfs           195M     0  195M   0% /run/user/0

5.2 进入到 Autofs 自动挂载的目录

(只在客户端上执行以下步骤)

# cd /autofs/directory01

5.3 再次显示客户端当前的目录挂载情况

(只在客户端上执行以下步骤)

# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  957M     0  957M   0% /dev
tmpfs                     971M     0  971M   0% /dev/shm
tmpfs                     971M   17M  955M   2% /run
tmpfs                     971M     0  971M   0% /sys/fs/cgroup
/dev/vda1                  10G  1.6G  8.5G  16% /
tmpfs                     195M     0  195M   0% /run/user/0
192.168.101.10:/nfsshare   10G  1.6G  8.5G  16% /autofs/directory01

(补充:在进入到 Autofs 自动挂载的目录后,自动挂载就在系统中自动出现了)

[实验] Django 服务的搭建

纪念:站主于 2020 年 3 月完成了此开源实验,并将过程中的所有命令经过整理和注释以后,形成以下教程

注意:

文中的 python 系统名和 mysite 项目只是站主在本次操作中随意取的名称,读者可以根据自己的喜好换成任意别的名称

正文:

步骤一:系统环境要求

1) 服务器的系统需要是 CentOS Linux 7 版本
2) 服务器要关闭防火墙
3) 服务器要关闭 SELinux
4) 服务器系统要配置好可用的软件源
5) 服务器要能够连接外网

步骤二:安装 Django

2.1 安装 Python3

[root@python ~]# yum -y install python3

2.2 创建并进入 Django 项目的目录

[root@python ~]# mkdir project
[root@python ~]# cd project

2.3 将 Django 项目的目录指定为 Django 环境

[root@python project]# python3 -m venv django_env

2.4 进入 Django 环境

[root@python project]# source django_env/bin/activate
(django_env) [root@python project]# pip install django==1.11.6
Collecting django==1.11.6
  Downloading https://files.pythonhosted.org/packages/82/33/f9d2871f3aed5062661711bf91b3ebb03daa52cc0e1c37925f3e0c4508c5/Django-1.11.6-py2.py3-none-any.whl (6.9MB)
    100% |████████████████████████████████| 7.0MB 12kB/s 
Collecting pytz (from django==1.11.6)
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)",)': /simple/pytz/
  Downloading https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl (509kB)
    100% |████████████████████████████████| 512kB 15kB/s 
Installing collected packages: pytz, django
Successfully installed django-1.11.6 pytz-2019.3
You are using pip version 9.0.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(django_env) [root@python project]# 


补充:
1) 这里以安装 1.11.6 版本的 django 为例
2) 如果向直接安装最新版本的 django 可以使用 pip install django 命令

2.5 检验 Django 环境是否成功

(django_env) [root@python project]# python
Python 3.6.8 (default, Aug  7 2019, 17:28:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.__version__
'1.11.6'
>>> exit()

步骤三:创建 mysite 项目

3.1 创建 mysite 项目

(django_env) [root@python project]# django-admin startproject mysite

3.2 mysite 项目的目录

3.2.1 安装 tree 目录显示软件
# yum -y install tree
3.2.2 显示 mysite 项目的目录
(django_env) [root@python project]# cd mysite
(django_env) [root@python mysite]# tree
.
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 5 files

3.3 Django 项目目录介绍

1) mysite 此 Django 项目的容器
2) manage.py 命令行工具,与 Django 项目进行交互
3) mysite/init.py 空文件,通知 Python 此项目是一个 Python 包
4) mysite/settings.py 此 Django 项目的配置文件
5) mysite/urls.py 此 Django 项目的 URL 声明和 Django 的网站“目录”
6) mysite/wsgi.py WSGI 兼容 Web 服务器的入口

步骤四:启动 Django 服务

4.1 启动 Django 服务

(django_env) [root@python mysite]# python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
February 27, 2020 - 05:35:30
Django version 1.11.6, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

(补充:这里以使用 8000 端口开启网站服务为例)

4.2 测试 Django 服务

通过浏览器访问以下网址:

http://127.0.0.1:8000/admin