• day10 nfs服务,nginx负载均衡,定时任务


    ==================nginx 负载均衡====================

    实现nginx负载均衡的效果,并运用nfs服务共享目录,使所有nginx服务拥有共同的http目录

    nginx安装:http://www.cnblogs.com/alwaysInMe/p/6924859.html

    nfs安装:NFS 是Network File System的缩写,即网络文件系统。一种使用于分散式文件系统的协定。

    ===>  环境配置及软件安装

    注:本次安装用的是centos7系统光盘自带的rpm文件进行安装,已提前将光盘镜像路径加载到了repo文件中。

    [root@localhost ~]# iptables -F                           # 清除防火墙配置
    [root@localhost ~]# systemctl stop firewalld              # 关闭防火墙
    [root@localhost ~]# setenforce 0                          # 关闭策略组,临时
    [root@localhost ~]# vim /etc/sysconfig/selinux            # 文件中关闭策略组
    [root@localhost ~]# systemctl status firewalld            # 查看防火墙状态
    [root@bogon ~]# yum -y install rpcbind nfs-utils         # 安装rpcbind、nfs-utils。其中nfs依赖于rpcbind
    
    软件包 rpcbind-0.2.0-32.el7.x86_64 已安装并且是最新版本      # 这里提示已经安装,不需要处理 
    软件包 1:nfs-utils-1.3.0-0.21.el7.x86_64 已安装并且是最新版本
    无须任何处理

    ====>  文件配置

    [root@bogon ~]# mkdir /share                            # 创建共享目录
    [root@bogon ~]# vim /etc/exports                        # 设定nfs配置文件,如下:
    /share *(rw,sync,fsid=0)       #<输出目录> [客户端1 选项(访问权限,用户映射,其他)]

    ====>  启动服务

    [root@bogon ~]# systemctl start nfs                  # 启动服务-这里演示的事二进制的
    [root@bogon ~]# systemctl status nfs                 # 查看文件启动情况
     nfs-server.service - NFS server and services
       Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
       Active: active (exited) since Thu 2017-06-01 03:32:51 PDT; 1min 6s ago
      Process: 11099 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
      Process: 11098 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
     Main PID: 11099 (code=exited, status=0/SUCCESS)
       CGroup: /system.slice/nfs-server.service
    
    Jun 01 03:32:51 bogon systemd[1]: Starting NFS server and services...
    Jun 01 03:32:51 bogon systemd[1]: Started NFS server and services.
    [root@bogon ~]# exportfs                   # 查看nfs服务所开放的文件夹及开放给谁
    /share            <world>

    ====>  测试功能

    注:测试需要用另外一台linux系统进行挂载链接,所有测试的机器中需要安装nfs,但不需要启动,安装方法见前面。

    [root@bogon ~]# mount 192.168.128.181:/share /opt/        # 将共享的文件挂载在/opt 上,如果没有这个目录,可以先使用mkdir命另创建这个文件夹
    [root@bogon ~]# df                                        # 查看是否挂载成功
    文件系统                  1K-块    已用     可用 已用% 挂载点
    /dev/sda3              18555904 3797620 14758284   21% /
    devtmpfs                 486144       0   486144    0% /dev
    tmpfs                    500664      88   500576    1% /dev/shm
    tmpfs                    500664    7224   493440    2% /run
    tmpfs                    500664       0   500664    0% /sys/fs/cgroup
    /dev/sda1                303788  146768   157020   49% /boot
    tmpfs                    100136      16   100120    1% /run/user/0
    /dev/sr0                4227724 4227724        0  100% /media
    192.168.128.181:/share 18555904 3797632 14758272   21% /opt

    我这里一共用了四台电脑,重复以上操作,分别进行连接

    下面进行nginx负载均衡文件的配置

    注:我这里是先配置web服务器(工作的),测试没问题后再配置代理服务器(分配任务的)

    [root@bogon ~]# vim /usr/local/nginx/conf/nginx.conf             # 修改nginx配置文件,由于我用的是源码安装,所以我自定义了路径 /usr/local/nginx
      1 #user  nobody;
      2 worker_processes  1;
      3 
      4 #error_log  logs/error.log;
      5 #error_log  logs/error.log  notice;
      6 #error_log  logs/error.log  info;
      7 
      8 #pid        logs/nginx.pid;
      9 
     10 
     11 events {
     12     worker_connections  1024;
     13 }
     14 
     15 
     16 http {
     17     include       mime.types;
     18     default_type  application/octet-stream;
     19 
     20     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     21                       '$status $body_bytes_sent "$http_referer" '
     22                       '"$http_user_agent" "$http_x_forwarded_for"';
     23 #  日志功能
     24     access_log  logs/access.log  main;
     25 
     26     sendfile        on;
     27     tcp_nopush     on;
     28 
     29     #keepalive_timeout  0;
     30     keepalive_timeout  65;
     31 
     32     #gzip  on;
     33 
     34     server {
     35         listen       8084;                      # 修改软件使用端口为 8084
     36         server_name  localhost;
     37 
     38         #charset koi8-r;
     39 
     40         #access_log  logs/host.access.log  main;
     41 
     42         location / {
     43             root   /opt;                                          #  修改默认的 html 文件路径
     44             index  index.html index.htm;
     45         }
     46 
     47         #error_page  404              /404.html;
     48 
     49         # redirect server error pages to the static page /50x.html
     50         #
     51         error_page   500 502 503 504  /50x.html;
     52         location = /50x.html {
     53             root   html;
     54         }
     55 
     56         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     57         #
     58         #location ~ .php$ {
     59         #    proxy_pass   http://127.0.0.1;
     60         #}
     61 
     62         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     63         #
     64         #location ~ .php$ {
     65         #    root           html;
     66         #    fastcgi_pass   127.0.0.1:9000;
     67         #    fastcgi_index  index.php;
     68         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     69         #    include        fastcgi_params;
     70         #}
     71 
     72         # deny access to .htaccess files, if Apache's document root
     73         # concurs with nginx's one
     74         #
     75         #location ~ /.ht {
     76         #    deny  all;
     77         #}
     78     }
     79 
     80 
     81     # another virtual host using mix of IP-, name-, and port-based configuration
     82     #
     83     #server {
     84     #    listen       8000;
     85     #    listen       somename:8080;
     86     #    server_name  somename  alias  another.alias;
     87 
     88     #    location / {
     89     #        root   html;
     90     #        index  index.html index.htm;
     91     #    }
     92     #}
     93 
     94 
     95     # HTTPS server
     96     #
     97     #server {
     98     #    listen       443 ssl;
     99     #    server_name  localhost;
    100 
    101     #    ssl_certificate      cert.pem;
    102     #    ssl_certificate_key  cert.key;
    103 
    104     #    ssl_session_cache    shared:SSL:1m;
    105     #    ssl_session_timeout  5m;
    106 
    107     #    ssl_ciphers  HIGH:!aNULL:!MD5;
    108     #    ssl_prefer_server_ciphers  on;
    109 
    110     #    location / {
    111     #        root   html;
    112     #        index  index.html index.htm;
    113     #    }
    114     #}
    115 
    116 }
    配置文件
    [root@bogon ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
    # 启动服务

    成功!!!!!

    其它三个重复以上配置,注意修改html存放的默认位置,上面的端口我改成了8084,这个地方改不改都行,只要记住了是多少就好了

    下面修改代理服务器:同时在代理服务器上配置一个web服务器,原理是通过不同的配置文件打开相同的软件,实现端口不同从而同时工作

    [root@bogon ~]# cd /usr/local/nginx/conf/                  # 进入nginx的目录
    [root@bogon conf]# ll
    总用量 60
    -rw-r--r--. 1 root root 1077 May 31 23:37 fastcgi.conf
    -rw-r--r--. 1 root root 1077 May 31 23:37 fastcgi.conf.default
    -rw-r--r--. 1 root root 1007 May 31 23:37 fastcgi_params
    -rw-r--r--. 1 root root 1007 May 31 23:37 fastcgi_params.default
    -rw-r--r--. 1 root root 2837 May 31 23:37 koi-utf
    -rw-r--r--. 1 root root 2223 May 31 23:37 koi-win
    -rw-r--r--. 1 root root 3957 May 31 23:37 mime.types
    -rw-r--r--. 1 root root 3957 May 31 23:37 mime.types.default
    -rw-r--r--. 1 root root 2656 May 31 23:37 nginx.conf
    -rw-r--r--. 1 root root 2656 May 31 23:37 nginx.conf.default
    -rw-r--r--. 1 root root  636 May 31 23:37 scgi_params
    -rw-r--r--. 1 root root  636 May 31 23:37 scgi_params.default
    -rw-r--r--. 1 root root  664 May 31 23:37 uwsgi_params
    -rw-r--r--. 1 root root  664 May 31 23:37 uwsgi_params.default
    -rw-r--r--. 1 root root 3610 May 31 23:37 win-utf
    [root@bogon conf]# cp nginx.conf web1.conf    # 复制一份nginx的配置文件,用作web端
    [root@bogon conf]# vim web1.conf       # 修改web1的配置文件,如下,修改了端口以及默认html文件位置
      1 #user  nobody;
      2 worker_processes  1;
      3 
      4 error_log  logs/error.log;
      5 error_log  logs/error.log  notice;
      6 error_log  logs/error.log  info;
      7 
      8 #pid        logs/nginx.pid;
      9 
     10 
     11 events {
     12     worker_connections  1024;
     13 }
     14 
     15 
     16 http {
     17     include       mime.types;
     18     default_type  application/octet-stream;
     19 
     20     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     21                       '$status $body_bytes_sent "$http_referer" '
     22                       '"$http_user_agent" "$http_x_forwarded_for"';
     23 
     24     access_log  logs/access.log  main;
     25 
     26     sendfile        on;
     27     #tcp_nopush     on;
     28 
     29     #keepalive_timeout  0;
     30     keepalive_timeout  65;
     31 
     32     #gzip  on;
     33 
     34     server {
     35         listen       8081;                   # 修改端口为8081,防止与代理服务冲突
     36         server_name  localhost;
     37 
     38         #charset koi8-r;
     39 
     40         #access_log  logs/host.access.log  main;
     41 
     42         location / {
     43             root   /share;                      # 修改html文件默认存放位置为 /share 保证一致性
     44             index  index.html index.htm;
     45         }
     46 
     47         #error_page  404              /404.html;
     48 
     49         # redirect server error pages to the static page /50x.html
     50         #
     51         error_page   500 502 503 504  /50x.html;
     52         location = /50x.html {
     53             root   html;
     54         }
     55 
     56         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     57         #
     58         #location ~ .php$ {
     59         #    proxy_pass   http://127.0.0.1;
     60         #}
     61 
     62         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     63         #
     64         #location ~ .php$ {
     65         #    root           html;
     66         #    fastcgi_pass   127.0.0.1:9000;
     67         #    fastcgi_index  index.php;
     68         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     69         #    include        fastcgi_params;
     70         #}
     71 
     72         # deny access to .htaccess files, if Apache's document root
     73         # concurs with nginx's one
     74         #
     75         #location ~ /.ht {
     76         #    deny  all;
     77         #}
     78     }
     79 
     80 
     81     # another virtual host using mix of IP-, name-, and port-based configuration
     82     #
     83     #server {
     84     #    listen       8000;
     85     #    listen       somename:8080;
     86     #    server_name  somename  alias  another.alias;
     87 
     88     #    location / {
     89     #        root   html;
     90     #        index  index.html index.htm;
     91     #    }
     92     #}
     93 
     94 
     95     # HTTPS server
     96     #
     97     #server {
     98     #    listen       443 ssl;
     99     #    server_name  localhost;
    100 
    101     #    ssl_certificate      cert.pem;
    102     #    ssl_certificate_key  cert.key;
    103 
    104     #    ssl_session_cache    shared:SSL:1m;
    105     #    ssl_session_timeout  5m;
    106 
    107     #    ssl_ciphers  HIGH:!aNULL:!MD5;
    108     #    ssl_prefer_server_ciphers  on;
    109 
    110     #    location / {
    111     #        root   html;
    112     #        index  index.html index.htm;
    113     #    }
    114     #}
    115 
    116 }
    web1配置文件修改
    [root@bogon conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/web1.conf 
    # 启动刚才的web服务,从 /usr/local/nginx/conf/web1.conf  读取配置文件

    修改代理服务器配置

    [root@bogon conf]# vim /usr/local/nginx/conf/nginx.conf    # 配置内容如下
      1 #user  nobody;
      2 worker_processes  1;
      3 
      4 #error_log  logs/error.log;
      5 #error_log  logs/error.log  notice;
      6 #error_log  logs/error.log  info;
      7 
      8 #pid        logs/nginx.pid;
      9 
     10 
     11 events {
     12     worker_connections  1024;
     13 }
     14 
     15 
     16 http {
     17     include       mime.types;
     18     default_type  application/octet-stream;
     19 
     20 
     21     upstream myapp1 {
     22         server 192.168.181:8081;
     23         server 192.168.180:80;
     24         server 192.168.183:80;
     25         server 192.168.184:8084;
     26 
     27  }
     28     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     29     #                  '$status $body_bytes_sent "$http_referer" '
     30     #                  '"$http_user_agent" "$http_x_forwarded_for"';
     31 
     32     #access_log  logs/access.log  main;
     33 
     34     sendfile        on;
     35     #tcp_nopush     on;
     36 
     37     #keepalive_timeout  0;
     38     keepalive_timeout  65;
     39 
     40     #gzip  on;
     41 
     42     server {
     43         listen       80;
     44 
     45         location / {
     46             proxy_pass http://myapp1;
     47         }
     48 
     49         #charset koi8-r;
     50 
     51         #access_log  logs/host.access.log  main;
     52 
     53 
     54         #error_page  404              /404.html;
     55 
     56         # redirect server error pages to the static page /50x.html
     57         #
     58         error_page   500 502 503 504  /50x.html;
     59         location = /50x.html {
     60             root   html;
     61         }
     62 
     63         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     64         #
     65         #location ~ .php$ {
     66         #    proxy_pass   http://127.0.0.1;
     67         #}
     68 
     69         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     70         #
     71         #location ~ .php$ {
     72         #    root           html;
     73         #    fastcgi_pass   127.0.0.1:9000;
     74         #    fastcgi_index  index.php;
     75         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     76         #    include        fastcgi_params;
     77         #}
     78 
     79         # deny access to .htaccess files, if Apache's document root
     80         # concurs with nginx's one
     81         #
     82         #location ~ /.ht {
     83         #    deny  all;
     84         #}
     85     }
     86 
     87 
     88     # another virtual host using mix of IP-, name-, and port-based configuration
     89     #
     90     #server {
     91     #    listen       8000;
     92     #    listen       somename:8080;
     93     #    server_name  somename  alias  another.alias;
     94 
     95     #    location / {
     96     #        root   html;
     97     #        index  index.html index.htm;
     98     #    }
     99     #}
    100 
    101 
    102     # HTTPS server
    103     #
    104     #server {
    105     #    listen       443 ssl;
    106     #    server_name  localhost;
    107 
    108     #    ssl_certificate      cert.pem;
    109     #    ssl_certificate_key  cert.key;
    110 
    111     #    ssl_session_cache    shared:SSL:1m;
    112     #    ssl_session_timeout  5m;
    113 
    114     #    ssl_ciphers  HIGH:!aNULL:!MD5;
    115     #    ssl_prefer_server_ciphers  on;
    116 
    117     #    location / {
    118     #        root   html;
    119     #        index  index.html index.htm;
    120     #    }
    121     #}
    122 
    123 }
    代理服务器的配置

    修改的位置

             

    配置详情:http://nginx.org/en/docs/http/load_balancing.html

    注:配置文件经常会报错,它会告诉你第几行出现问题,这个时候我们可以用vim进去,使用命令  :set number   设置显示行号

    [root@bogon conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    #  用nginx.conf的配置,运行nginx

    ===================定时任务================

    什么是计划任务:
    后台运行,到了预定的时间就会自动执行的任务,前提是:事先手动将计划任务设定好。这就用到了crond服务

    crond服务相关的软件包

    [root@MiWiFi-R3-srv ~]# rpm -qa |grep cron
    cronie-anacron-1.4.11-14.el7.x86_64
    crontabs-1.11-6.20121102git.el7.noarch
    cronie-1.4.11-14.el7.x86_64

    这些包在最小化安装系统时就已经安装了,并且会开机自启动crond服务,并为我们提供好编写计划任务的crontab命令。

    计划任务分为两类:系统级和用户级

    首先需要知道的是,无论是系统级还是用户级的cron计划都是文本文件,系 统的计划文件存放在/etc/crontab路径下。用户的计划文件放在/var/spool/cron/用户名,不管是哪一种,都可以满足我们定制计划任务的需求。

    root用户可以直接对文件进行修改来编写计划任务也可以使用 crontab -e命令,而普通用户只能使用后者。除此之外,系统crontab文件中任务的定义也有所不同,在前五个部分之后插入了一个“用户”部分。

    [root@MiWiFi-R3-srv ~]# cat /etc/crontab #查看全局计划任务
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root

    # For details see man 4 crontabs

    # Example of job definition:
    # .---------------- minute (0 - 59)
    # | .------------- hour (0 - 23)
    # | | .---------- day of month (1 - 31)
    # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
    # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # | | | | |
    # * * * * * user-name command to be executed
    * * * * * root run-parts /test #run-parts命令,可以执行一个目录下所有的可执行文件,目录下文件必须有执行权限

    You have new mail in /var/spool/mail/root


    [root@MiWiFi-R3-srv ~]# crontab -u tom -l #通过命令查看用户tom的计划任务
    */1 * * * * echo 123213123213


    [root@MiWiFi-R3-srv ~]# cat /var/spool/cron/tom #从文件中查看用户tom的计划任务
    */1 * * * * echo 123213123213

    crontab命令编写计划任务

    语  法:crontab [-u <用户名称>][配置文件] 或 crontab [-u <用户名称>][-elr]

    crontab任务配置基本格式:
    *  *  *  *  *  command
    分钟(0-59) 小时(0-23) 日期(1-31) 月份(1-12) 星期(0-6,0代表星期天)  命令

    第1列表示分钟1~59 每分钟用*或者 */1表示
    第2列表示小时1~23(0表示0点)
    第3列表示日期1~31
    第4列表示月份1~12
    第5列标识号星期0~6(0表示星期天)
    第6列要运行的命令

    参  数: 
    -e  编辑该用户的计时器设置。 
    -l  列出该用户的计时器设置。 
    -r  删除该用户的计时器设置。 
    -u<用户名称>  指定要设定计时器的用户名称。

    注意:

    1 查看计划任务的执行:tail -f /var/log/cron

    2 写计划任务时,命令必须加上绝对路径,否则会出现这种情况:从日志中看,确实触发了计划任务的执行,但是命令却没有执行成功,比如* * * * * reboot就会出现这种情况,需要将reboot写成/usr/sbin/reboot

    以上来自:http://www.cnblogs.com/linhaifeng/articles/6045600.html

    ================举例子===============

    编写日志切割脚本,结合计划任务,每天凌晨两点,自动备份并切割nginx的访问日志

    [root@bogon nginx]# cd /usr/local/nginx/logs/          # 打开软件日志位置
    [root@bogon logs]# ll            # 这里我以 access.log 这个文件为例
    总用量 12
    -rw-r--r--  1 root root    1 Jun  1 05:13 access.log
    -rw-r--r--. 1 root root 3410 Jun  1 04:51 error.log
    -rw-r--r--  1 root root    6 Jun  1 04:47 nginx.pid
    drwxr-xr-x  3 root root   18 Jun  1 05:09 usr

    设想的方法是:用txt文件存放bash命令,最后用cron来定时执行  bash *.txt

    [root@bogon logs]# mkdir /timed_task                   # 创建一个存放定时备份的目录
    [root@bogon logs]# touch nginx_logs_bak.txt            # 创建nginx日志备份文件
    [root@bogon logs]# vim /timed_task/nginx_logs_bak.txt     # 编辑文件,内容如下
    tar -czf /usr/local/nginx/logs/$(date "+%Y-%m-%d_%T").access.log.tar.gz /usr/local/nginx/logs/access.log   # 这个是简单的tar压缩命令
    echo '' > /usr/local/nginx/logs/access.log            # 这一条的主要内容是用来清空access.log
    [root@bogon logs]# ll /usr/local/nginx/logs/     # 先查看目录情况,查看下执行前效果
    总用量 12
    -rw-r--r--  1 root root    1 Jun  1 05:13 access.log
    -rw-r--r--. 1 root root 3410 Jun  1 04:51 error.log
    -rw-r--r--  1 root root    6 Jun  1 04:47 nginx.pid
    drwxr-xr-x  3 root root   18 Jun  1 05:09 usr

    执行后查看结果

    [root@bogon logs]# bash nginx_logs_bak.txt 
    
    [root@bogon ~]# ll /usr/local/nginx/logs/
    总用量 16
    -rw-r--r--  1 root root  138 Jun  1 06:42 2017-06-01_06:42:33.access.log.tar.gz
    -rw-r--r--  1 root root    1 Jun  1 06:42 access.log
    -rw-r--r--. 1 root root 3410 Jun  1 04:51 error.log
    -rw-r--r--  1 root root    0 Jun  1 06:33 nginx_logs_bak.txt
    -rw-r--r--  1 root root    6 Jun  1 04:47 nginx.pid
    drwxr-xr-x  3 root root   18 Jun  1 05:09 usr

    # 证明脚本没有问题,我们现在把它放在cron命令里面 /etc/crontab

    配置cron文件

    [root@bogon logs]# vim /etc/crontab     # 配置如下
    0 2 * * * root /usr/bin/bash /timed_task/nginx_logs_bak.txt

    第一个0表示每个小时的零分中,第二个2表示每天的两点   用 root 的身份,执行 后面的命令

    本人菜鸟,如有错误,还请多多指出!!!!

  • 相关阅读:
    精益产品探索
    vue 之 pdf预览
    arcgis js 之 渔网工具(调用地图服务)
    arcgis js之卷帘工具
    arcgis js之调用wms服务
    vue-cli3 本地数据模拟后台接口
    cmd设置电脑自动关机
    Arcgis js之web墨卡托(3857)转经纬度坐标(4326)
    arcgis js之地图分屏同步
    arcgis之gp服务发布
  • 原文地址:https://www.cnblogs.com/alwaysInMe/p/6930348.html
Copyright © 2020-2023  润新知