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

Posted 何必从头

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
代理服务器的配置

修改的位置

Nginx搭建部署Web服务器并与NFS结合搭建负载均衡服务器

Nginx负载均衡NFS配置

Centos7搭建HAproxy+Nginx+NFS负载均衡实现高可用集群

EG:nginx反向代理两台web服务器,实现负载均衡 所有的web服务共享一台nfs的存储

搭建NFS共享目录,解决wordpress负载均衡图片上传问题

lb-nginx