首页
友链
统计
留言
更多
直播
壁纸
推荐
我的毛线
哔哔点啥
院长科技
Search
1
本站官方群:894703859------|诚邀各位大佬的入驻!
580 阅读
2
pxe 自动化安装系统
570 阅读
3
软件安装
434 阅读
4
新款螺旋帽子编织#夏凉帽#合股线夏凉帽编织
379 阅读
5
10 个Linux Awk文本处理经典案例
372 阅读
linux
yaml
iptables
shell
ansible
ssl
awk
sed
pxe
prometheus
Nginx
k8s
fish
dev
go占位符
clickhouse
html标签
vue基础
html表格
vue项目
vscode
css基础
css定位
css精灵图
code
html5
project
js
jQuery
面向对象
编织
编织视频
常用工具
微软
登录
/
注册
Search
标签搜索
基础
js
Nginx
css
webapi
jQuery
面向对象
command
项目
ansible
用户权限
go
html
文件管理
命令
k8s
shell
pxe
awk
vscode
JustDoIt
累计撰写
114
篇文章
累计收到
4
条评论
首页
栏目
linux
yaml
iptables
shell
ansible
ssl
awk
sed
pxe
prometheus
Nginx
k8s
fish
dev
go占位符
clickhouse
html标签
vue基础
html表格
vue项目
vscode
css基础
css定位
css精灵图
code
html5
project
js
jQuery
面向对象
编织
编织视频
常用工具
微软
页面
友链
统计
留言
直播
壁纸
推荐
我的毛线
哔哔点啥
院长科技
搜索到
58
篇与
的结果
2023-11-22
k8s 1.28高可用搭建基础环境01
Kubernetes(简称为:k8s)是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效,Kubernetes提供了资源调度、部署管理、服务发现、扩容缩容、监控,维护等一整套功能,努力成为跨主机集群的自动部署、扩展以及运行应用程序容器的平台。 它支持一系列容器工具, 包括Docker、Containerd等。一、集群环境准备1.1 主机规划 主机IP地址 主机名 主机配置 主机角色 软件列表 192.168.31.34 k8s-master01 4C4G master kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubelet、kube-proxy、Containerd、runc 192.168.31.35 k8s-master02 4C4G master kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubelet、kube-proxy、Containerd、runc 192.168.31.36 k8s-master03 4C4G master kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubelet、kube-proxy、Containerd、runc 192.168.31.37 k8s-node01 4C4G worker kubelet、kube-proxy、Containerd、runc 192.168.31.38 k8s-node02 4C4G worker kubelet、kube-proxy、Containerd、runc 192.168.31.32 k8s-ha01 1C2G LB nginx、keepalived 192.168.31.33 k8s-ha02 1C2G LB nginx、keepalived 192.168.31.100 / / VIP(虚拟IP) 1.2 软件版本 软件名称 版本 备注 CentOS7 kernel版本:5.4.260 kubernetes v1.28.4 etcd v3.5.10 最新版本 calico v3.26.4 coredns v1.11.1 containerd 1.7.9 runc 1.1.10 nginx 1.21.6 YUM源默认 keepalived 1.3.5 YUM源默认 1.3 网络分配 网络名称 网段 备注 Node网络 192.168.31.0/24 Service网络 10.96.0.0/16 Pod网络 10.244.0.0/16 二、集群部署2.1主机准备2.1.1 主机名设置hostnamectl set-hostname xxx 2.1.2 主机与IP地址解析cat >> /etc/hosts << EOF 192.168.31.32 k8s-ha01 192.168.31.33 k8s-ha02 192.168.31.34 k8s-master01 192.168.31.35 k8s-master02 192.168.31.36 k8s-master03 192.168.31.37 k8s-node01 192.168.31.38 k8s-node02 EOF 2.1.3 主机安全设置2.1.3.1 关闭防火墙systemctl stop firewalld systmctl disable firewalld firewall-cmd --state 2.1.3.2 关闭selinuxsetenforce 0 sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config sestatus 2.1.4 交换分区设置swapoff -a sed -ri 's/.*swap.*/#&/' /etc/fstab echo "vm.swappiness=0" >> /etc/sysctl.conf sysctl -p 2.1.5 主机系统时间同步安装软件 yum -y install ntpdate 制定时间同步计划任务 crontab -e 0 */1 * * * ntpdate time1.aliyun.com 2.1.6 主机系统优化limit优化ulimit -SHn 65535 cat <<EOF >> /etc/security/limits.conf * soft nofile 655360 * hard nofile 131072 * soft nproc 655350 * hard nproc 655350 * soft memlock unlimited * hard memlock unlimited EOF 2.1.7 ipvs管理工具安装及模块加载为集群节点安装,负载均衡节点不用安装yum -y install ipvsadm ipset sysstat conntrack libseccomp 所有节点配置ipvs模块,在内核4.19+版本nf_conntrack_ipv4已经改为nf_conntrack, 4.18以下使用nf_conntrack_ipv4即可: modprobe -- ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack 创建 /etc/modules-load.d/ipvs.conf 并加入以下内容: cat >/etc/modules-load.d/ipvs.conf <<EOF ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_fo ip_vs_nq ip_vs_sed ip_vs_ftp ip_vs_sh nf_conntrack ip_tables ip_set xt_set ipt_set ipt_rpfilter ipt_REJECT ipip EOF 2.1.8 加载containerd相关内核模块临时加载模块 modprobe overlay modprobe br_netfilter 永久性加载模块 cat > /etc/modules-load.d/containerd.conf << EOF overlay br_netfilter EOF 设置为开机启动 systemctl enable --now systemd-modules-load.service 2.1.9 Linux内核升级在所有节点中安装,需要重新操作系统更换内核。[root@wangmanyuan ~]# yum -y install perl [root@wangmanyuan ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org [root@wangmanyuan ~]# yum -y install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm [root@wangmanyuan ~]# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * elrepo-kernel: mirrors.tuna.tsinghua.edu.cn Available Packages elrepo-release.noarch 7.0-6.el7.elrepo elrepo-kernel kernel-lt.x86_64 5.4.260-1.el7.elrepo elrepo-kernel kernel-lt-devel.x86_64 5.4.260-1.el7.elrepo elrepo-kernel kernel-lt-doc.noarch 5.4.260-1.el7.elrepo elrepo-kernel kernel-lt-headers.x86_64 5.4.260-1.el7.elrepo elrepo-kernel kernel-lt-tools.x86_64 5.4.260-1.el7.elrepo elrepo-kernel kernel-lt-tools-libs.x86_64 5.4.260-1.el7.elrepo elrepo-kernel kernel-lt-tools-libs-devel.x86_64 5.4.260-1.el7.elrepo elrepo-kernel kernel-ml.x86_64 6.6.1-1.el7.elrepo elrepo-kernel kernel-ml-devel.x86_64 6.6.1-1.el7.elrepo elrepo-kernel kernel-ml-doc.noarch 6.6.1-1.el7.elrepo elrepo-kernel kernel-ml-headers.x86_64 6.6.1-1.el7.elrepo elrepo-kernel kernel-ml-tools.x86_64 6.6.1-1.el7.elrepo elrepo-kernel kernel-ml-tools-libs.x86_64 6.6.1-1.el7.elrepo elrepo-kernel kernel-ml-tools-libs-devel.x86_64 6.6.1-1.el7.elrepo elrepo-kernel perf.x86_64 5.4.260-1.el7.elrepo elrepo-kern [root@wangmanyuan ~]# yum --enablerepo="elrepo-kernel" install kernel-lt.x86_64 [root@wangmanyuan ~]# grub2-set-default 0 [root@wangmanyuan ~]# grub2-mkconfig -o /boot/grub2/grub.cfg [root@wangmanyuan ~]# reboot 2.1.10 Linux内核优化cat <<EOF > /etc/sysctl.d/k8s.conf net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 fs.may_detach_mounts = 1 vm.overcommit_memory=1 vm.panic_on_oom=0 fs.inotify.max_user_watches=89100 fs.file-max=52706963 fs.nr_open=52706963 net.netfilter.nf_conntrack_max=2310720 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_keepalive_intvl =15 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_max_orphans = 327680 net.ipv4.tcp_orphan_retries = 3 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.ip_conntrack_max = 131072 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_timestamps = 0 net.core.somaxconn = 16384 EOF sysctl --system 所有节点配置完内核后,重启服务器,保证重启后内核依旧加载 reboot -h now 重启后查看ipvs模块加载情况: lsmod | grep --color=auto -e ip_vs -e nf_conntrack 重启后查看containerd相关模块加载情况: lsmod | egrep 'br_netfilter | overlay' 2.1.11 其它工具安装(选装)yum install wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 git lrzsz -y
2023年11月22日
37 阅读
0 评论
0 点赞
2023-11-22
k8s 1.28高可用搭建nginx/keepalived基础环境02
1. nginx安装1.1 所有的master节点创建运行nginx的用户useradd nginx -s /sbin/nologin -M 1.2 安装依赖mkdir -p /data/k8s-work cd /data/k8s-work yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ automake autoconf libtool make 1.3 下载nginx软件包wget http://nginx.org/download/nginx-1.21.6.tar.gz 1.4 解压软件包tar xf nginx-1.21.6.tar.gz 1.5 配置nginxcd nginx-1.21.6 ./configure --prefix=/usr/local/nginx/ \ --with-pcre \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-stream \ --with-http_stub_status_module \ --with-http_gzip_static_module 1.6 编译并安装nginxmake -j 4 && make install 1.7 使用systemctl管理,并设置开机启动cat > /usr/lib/systemd/system/nginx.service <<EOF [Unit] Description=The nginx HTTP and reverse proxy server After=network.target sshd-keygen.service [Service] Type=forking EnvironmentFile=/etc/sysconfig/sshd ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target EOF 1.8 检查nginx服务是否启动systemctl enable --now nginx.service systemctl status nginx ps -ef|grep nginx 1.9 同步nginx软件包和脚本到集群的k8s-ha02scp -rp k8s-ha02:/usr/local/nginx/ scp -rp k8s-ha02:/usr/lib/systemd/system/nginx.service nginx配置文件2.1 编辑nginx配置文件cat > /usr/local/nginx/conf/nginx.conf <<EOF user nginx nginx; worker_processes auto; events { worker_connections 20240; use epoll; } error_log /var/log/nginx_error.log info; stream { upstream kube-servers { hash $remote_addr consistent; server k8s-master01:6443 weight=5 max_fails=1 fail_timeout=3s; server k8s-master02:6443 weight=5 max_fails=1 fail_timeout=3s; server k8s-master03:6443 weight=5 max_fails=1 fail_timeout=3s; } server { listen 6443; proxy_connect_timeout 3s; proxy_timeout 3000s; proxy_pass kube-servers; } } EOF 2.2 同步nginx的配置文件到k8s-ha02scp -rp k8s-ha02:/usr/local/nginx/conf/nginx.conf 2.3 所有节点启动nginx服务systemctl enable --now nginx systemctl reload nginx [root@k8s-ha01 nginx-1.21.6]# netstat -lntp|grep 6443 tcp 0 0 0.0.0.0:6443 0.0.0.0:* LISTEN 5119/nginx: master 3.部署keepalived3.1 安装keepalived组件yum -y install keepalived 3.2 修改keepalive的配置文件(根据实际环境,interface eth0可能需要修改为interface ens33)3.2.1 编写配置文件,ha节点需要修改router_id和mcast_src_ip的值即可。3.2.1.1 k8s-ha01节点cat > /etc/keepalived/keepalived.conf <<EOF ! Configuration File for keepalived global_defs { router_id 192.168.31.32 } vrrp_script chk_nginx { script "/etc/keepalived/check_port.sh" interval 2 weight -20 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 100 priority 100 advert_int 1 mcast_src_ip 192.168.31.32 nopreempt authentication { auth_type PASS auth_pass wangmanyuan.com } track_script { chk_nginx } virtual_ipaddress { 192.168.31.100 } } EOF 3.2.1.1 k8s-ha02节点cat > /etc/keepalived/keepalived.conf <<EOF ! Configuration File for keepalived global_defs { router_id 192.168.31.33 } vrrp_script chk_nginx { script "/etc/keepalived/check_port.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 100 priority 99 advert_int 2 mcast_src_ip 192.168.31.33 nopreempt authentication { auth_type PASS auth_pass wangmanyuan.com } track_script { chk_nginx } virtual_ipaddress { 192.168.31.100 } } EOF 3.2.2 各节点编写健康检查脚本cat > /etc/keepalived/check_port.sh <<\EOF #!/bin/bash err=0 for k in $(seq 1 3) do check_code=$(pgrep nginx) if [[ $check_code == "" ]]; then err=$(expr $err + 1) sleep 1 continue else err=0 break fi done if [[ $err != "0" ]]; then echo "systemctl stop keepalived" /usr/bin/systemctl stop keepalived exit 1 else exit 0 fi EOF chmod +x /etc/keepalived/check_port.sh 3.3 启动keepalivedsystemctl enable --now keepalived 3.4 测试keepalivedip a # 查看VIP在哪个节点 systemct stop keepalived # 停止服务,观察是否飘逸VIP 3.5 参数说明温馨提示: router_id: 节点ip,master每个节点配置自己的IP mcast_src_ip: 节点IP,master每个节点配置自己的IP virtual_ipaddress: 虚拟IP,即VIP。 interface: 指定接口的名称。 virtual_router_id: 有效值为0-255,可以理解为一个组ID,只有相同的ID才被确认为一个组。 如果每个keepalived实例修改的ID不一致,则会出现各自有一个VIP的现象。 ```
2023年11月22日
30 阅读
0 评论
0 点赞
2023-11-03
Nginx负载均衡
1.Nginx代理服务代理我们往往并不陌生, 该服务我们常常用到如(代理租房、代理收货等等)那么在互联网请求里面, 客户端无法直接向服务端发起请求, 那么就需要用到代理服务, 来实现客户端和服务通信Nginx作为代理服务可以实现很多的协议代理, 我们主要以http代理为主正向代理(内部上网) 客户端<-->代理->服务端反向代理 客户端->代理<-->服务端正向与反向代理的区别区别在于代理的对象不一样正向代理代理的对象是客户端反向代理代理的对象是服务端1.1 Nginx反向代理配置语法1.Nginx代理配置语法Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except http: http: http: 2.url跳转修改返回Location[不常用]参考URLSyntax: proxy_redirect default; proxy_redirect off;proxy_redirect redirect replacement; Default: proxy_redirect default; Context: http, server, location 3.添加发往后端服务器的请求头信息, 如图Syntax: proxy_set_header field value; Default: proxy_set_header Host $proxy_host; proxy_set_header Connection close; Context: http, server, location proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 4.代理到后端的TCP连接、响应、返回等超时时间, 如图 Syntax: proxy_connect_timeout time; Default: proxy_connect_timeout 60s; Context: http, server, location Syntax: proxy_read_timeout time; Default: proxy_read_timeout 60s; Context: http, server, location Syntax: proxy_send_timeout time; Default: proxy_send_timeout 60s; Context: http, server, location 5.proxy_buffer代理缓冲区, 如图 Syntax: proxy_buffering on | off; Default: proxy_buffering on; Context: http, server, location Syntax: proxy_buffer_size size; Default: proxy_buffer_size 4k|8k; Context: http, server, location Syntax: proxy_buffers number size; Default: proxy_buffers 8 4k|8k; Context: http, server, location 6.Proxy代理网站常用优化配置如下,将配置写入新文件,调用时使用include引用即可[root@Nginx ~] proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k; 7.代理配置location时调用, 方便后续多个Location重复使用location / { proxy_pass http://127.0.0.1:8080; include proxy_params; } 1.2 Nginx反向代理配置场景Nginx反向代理配置实例1.环境准备 角色 外网IP(NAT) 内网IP(LAN) 主机名 Proxy eth0:10.0.0.5 eth1:172.16.1.5 lb01 web01 eth0:10.0.0.7 eth1:172.16.1.7 web01 2.web01服务器, 配置一个网站,监听在8080,仅运行172网段能访问[root@web01 ~] [root@web01 conf.d] server { listen 8080; server_name 172.16.1.7; location / { root /code_8080; index index.html; deny 10.0.0.0/24; allow all; } } [root@web01 conf.d] [root@web01 conf.d] [root@web01 conf.d] 2.proxy代理服务器, 配置监听在80,让10.0.0.1客户端,能够通过代理访问到后端的172.16.1.7网站[root@lb01 ~] [root@lb01 conf.d] server { listen 80; server_name nginx.oldboy.com; location / { proxy_pass http://172.16.1.7:8080; include proxy_params; } } [root@lb01 conf.d] [root@lb01 conf.d] 2.Nginx负载均衡Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾Nginx是一个典型的SLB(Server Load Balance)网络负载均衡器2.1 Nginx负载均衡按层划分负载均衡按层划分应用场景: 四层负载均衡负载均衡按层划分应用场景: 七层负载均衡, Nginx最常用2.2 Nginx负载均衡配置场景Nginx实现负载均衡需要用到proxy_pass代理模块配置.Nginx负载均衡是将客户端请求代理转发至一组upstream虚拟服务池Nginx upstream虚拟配置语法Syntax: upstream name { ... } Default: - Context: http upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; server backup1.example.com:8080 backup; } server { location / { proxy_pass http: } } 0.环境规划 角色 外网IP(NAT) 内网IP(LAN) 主机名 LB01 eth0:10.0.0.5 eth1:172.16.1.5 lb01 web01 eth0:10.0.0.7 eth1:172.16.1.7 web01 web02 eth0:10.0.0.8 eth1:172.16.1.8 web02 1.Web01服务器上配置nginx, 并创建对应html文件[root@web01 ~] [root@web01 conf.d] server { listen 80; server_name node.oldboy.com; location / { root /node; index index.html; } } [root@web01 conf.d] [root@web01 conf.d] [root@web01 conf.d] 2.Web02服务器上配置nginx, 并创建对应html文件[root@web02 ~] [root@web02 conf.d] server { listen 80; server_name node.oldboy.com; location / { root /node; index index.html; } } [root@web02 conf.d] [root@web02 conf.d] [root@web02 conf.d] 3.配置Nginx负载均衡[root@lb01 ~] [root@lb01 conf.d] upstream node { server 172.16.1.7:80; server 172.16.1.8:80; } server { listen 80; server_name node.oldboy.com; location / { proxy_pass http://node; include proxy_params; } } [root@lb01 conf.d] 4.准备Nginx负载均衡调度使用的proxy_params[root@Nginx ~] proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k; 5.使用浏览器访问node.oldboy.com, 然后不断刷新测试2.3 Nginx负载均衡后端状态后端Web服务器在前端Nginx负载均衡调度中的状态 状态 概述 down 当前的server暂时不参与负载均衡 backup 预留的备份服务器 max_fails 允许请求失败的次数 fail_timeout 经过max_fails失败后, 服务暂停时间 max_conns 限制最大的接收连接数 1.测试down状态, 测试该Server不参与负载均衡的调度upstream load_pass { server 10.0.0.7:80 down; } 2.测试backup以及down状态upstream load_pass { server 10.0.0.7:80 down; server 10.0.0.8:80 backup; server 10.0.0.9:80 max_fails=1 fail_timeout=10s; } location / { proxy_pass http://load_pass; include proxy_params; } 3.测试max_fails失败次数和fail_timeout多少时间内失败多少次则标记downupstream load_pass { server 10.0.0.7:80; server 10.0.0.8:80 max_fails=2 fail_timeout=10s; } 4.测试max_conns最大TCP连接数upstream load_pass { server 10.0.0.7:80; server 10.0.0.8:80 max_conns=1; } 2.4 Nginx负载均衡调度算法 调度算法 概述 轮询 按时间顺序逐一分配到不同的后端服务器(默认) weight 加权轮询,weight值越大,分配到的访问几率越高 ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 least_conn 最少链接数,那个机器链接数少就分发 1.Nginx负载均衡[wrr]轮询具体配置upstream load_pass { server 10.0.0.7:80; server 10.0.0.8:80; } 2.Nginx负载均衡[weight]权重轮询具体配置upstream load_pass { server 10.0.0.7:80 weight=5; server 10.0.0.8:80; } 3.Nginx负载均衡ip_hash具体配置, 不能和weight一起使用。//如果客户端都走相同代理, 会导致某一台服务器连接过多 upstream load_pass { ip_hash; server 10.0.0.7:80 weight=5; server 10.0.0.8:80; } 2.5 Nginx负载均衡TCP实践配置Nginx4层负载均衡实现如下需求1.通过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务。2.通过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。1.Nginx四层负载均衡配置语法stream { upstream backend { hash $remote_addr consistent; server backend1.example.com:12345 weight=5; server 127.0.0.1:12345 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; } server { listen 12345; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass backend; } } 2.Nginx四层负载均衡实战[root@lb01 ~] [root@lb01 ~] include /etc/nginx/conf.c/*.conf; [root@lb01 ~] [root@lb01 conf.c] stream { upstream ssh { server 172.16.1.7:22; } upstream mysql { server 172.16.1.51:3306; } server { listen 5555; proxy_connect_timeout 1s; proxy_timeout 300s; proxy_pass ssh; } server { listen 6666; proxy_connect_timeout 1s; proxy_timeout 300s; proxy_pass mysql; } } [root@lb01 conf.c] 3.Nginx四层负载均衡应用场景3.Nginx动静分离动静分离, 通过中间件将动态请求和静态请求进行分离, 分离资源, 减少不必要的请求消耗, 减少请求延时。好处: 动静分离后, 即使动态服务不可用, 但静态资源不会受到影响通过中间件将动态请求和静态请求分离3.1 Nginx动静分离应用案例0.环境准备 系统 服务 服务 地址 CentOS7.5 负载均衡 Nginx Proxy 10.0.0.5 CentOS7.5 静态资源 Nginx Static 10.0.0.7 CentOS7.5 动态资源 Tomcat Server 10.0.0.8 1.在10.0.0.7服务器上配置静态资源[root@web01 conf.d] server{ listen 80; server_name ds.oldboy.com; root /soft/code; index index.html; location ~* .*\.(png|jpg|gif)$ { root /soft/code/images; } } [root@web01 ~] [root@web01 ~] [root@web01 ~] 2.在10.0.0.8服务器上配置动态资源[root@web01 ~] [root@web01 ~] [root@web01 ~] [root@web01 ~] <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <HTML> <HEAD> <TITLE>JSP Test Page</TITLE> </HEAD> <BODY> <% Random rand = new Random(); out.println("<h1>Random number:</h1>"); out.println(rand.nextInt(99)+100); %> </BODY> </HTML> 3.在负载均衡10.0.0.5上配置调度, 实现访问jsp和pngroot@lb01 conf.d]# cat ds_proxy.conf upstream static { server 10.0.0.7:80; } upstream java { server 10.0.0.8:8080; } server { listen 80; server_name ds.oldboy.com; location / { root /soft/code; index index.html; } location ~ .*\.(png|jpg|gif)$ { proxy_pass http: include proxy_params; } location ~ .*\.jsp$ { proxy_pass http: include proxy_params; } } [root@lb01 conf.d]# systemctl restart nginx 4.通过负载测试访问静态资源5.通过负载测试访问动态资源6.在负载均衡10.0.0.5上整合动态和静态资源的html文件[root@lb01 ~]# mkdir /soft/code -p [root@lb01 ~]# cat /soft/code/index.html <html lang="en"> <head> <meta charset="UTF-8" /> <title>测试ajax和跨域访问</title> <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> </head> <script type="text/javascript"> $(document).ready(function(){ $.ajax({ type: "GET", url: "http://ds.oldboy.com/java_test.jsp", success: function(data) { $("#get_data").html(data) }, error: function() { alert("fail!!,请刷新再试!"); } }); }); </script> <body> <h1>测试动静分离</h1> <img src="http://ds.oldboy.com/nginx.png"> <div id="get_data"></div> </body> </html> 7.测试动态和静态资源是否能正常加载在一个html文件中8.当使用systemctl stop nginx停止Nginx后, 会发现静态内容无法访问, 动态内容依旧运行正常9.当使用systemctl stop tomcat停止tomcat后, 静态内容依旧能正常访问, 动态内容将不会被请求到3.2 Nginx通过来源设备拆分2.根据不同的浏览器, 以及不同的手机, 访问的效果都将不一样。 http { ... upstream firefox { server 172.31.57.133:80; } upstream chrome { server 172.31.57.133:8080; } upstream iphone { server 172.31.57.134:8080; } upstream android { server 172.31.57.134:8081; } upstream default { server 172.31.57.134:80; } ... } server { listen 80; server_name www.xuliangwei.com; location / { if ($http_user_agent ~* "Safari"){ proxy_pass http: } if ($http_user_agent ~* "Firefox"){ proxy_pass http: } if ($http_user_agent ~* "Chrome"){ proxy_pass http: } if ($http_user_agent ~* "iphone"){ proxy_pass http: } if ($http_user_agent ~* "android"){ proxy_pass http: } proxy_pass http: include proxy.conf; } } } 3.根据访问不同目录, 代理不同的服务器 upstream static_pools { server 10.0.0.9:80 weight=1; } upstream upload_pools { server 10.0.0.10:80 weight=1; } upstream default_pools { server 10.0.0.9:8080 weight=1; } server { listen 80; server_name www.xuliangwei.com; #url: http: location / { proxy_pass http: include proxy.conf; } #url: http: location /static/ { proxy_pass http: include proxy.conf; } #url: http: location /upload/ { proxy_pass http: include proxy.conf; } } if ($request_uri ~* "^/static/(.*)$") { proxy_pass http: } if ($request_uri ~* "^/upload/(.*)$") { proxy_pass http: } location / { proxy_pass http: include proxy.conf; } 转 xuliangwei.com
2023年11月03日
33 阅读
0 评论
1 点赞
2023-11-03
LNMP架构概述
1.LNMP架构概述LNMP就是Linux+Nginx+MySQL+PHP,Linux作为服务器的操作系统,Nginx作为Web服务器、PHP作为解析动态脚本语言、MySQL即为数据库。Linux作为服务器的操作系统。Nginx作为WebServer服务器。PHP 作为动态解析服务(php)。MySQL作为后端存储数据库服务。Nginx服务本身不能处理PHP的请求,那么当用户发起PHP动态请求, Nginx又是如何进行处理的。用户-->http协议-->Nginx-->fastcgi协议-->php-fpm注意: fatcgi是nginx连接php-fpm之间的协议。Nginx与Fast-CGI详细工作流程如下:1.用户通过http协议发起请求,请求会先抵达LNMP架构中的Nginx2.Nginx会根据用户的请求进行判断,这个判断是有Location进行完成3.判断用户请求的是静态页面,Nginx直接进行处理4.判断用户请求的是动态页面,Nginx会将该请求交给fastcgi协议下发5.fastgi会将请求交给php-fpm管理进程, php-fpm管理进程接收到后会调用具体的工作线程warrap6.warrap线程会调用php进行解析,如果只是解析代码php直接返回7.如果有查询数据库操作,则由php连接数据库(用户 密码 IP)然后发起查询的操作8.最终数据由mysql->php->php-fpm->fastcgi->nginx->http->user2.安装LNMP架构1) 使用官方仓库安装Nginx[root@nginx ~] [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 2) 安装Nginx 并启动Nginx 加入开机自启[root@nginx ~] [root@nginx ~] [root@nginx ~] 3) 使用第三方扩展源安装php7.1[root@nginx ~] [root@nginx ~] [root@nginx ~] [root@nginx ~] 3) 配置php-fpm 启动php-fpm 并加入开机自启[root@nginx ~] [root@nginx ~] [root@nginx ~] [root@nginx ~] 4) 使用MySQL官方仓库安装MySQL5.7数据库[root@nginx ~] [root@nginx ~] 5) 启动数据库, 并加入开机自动[root@nginx ~] [root@nginx ~] 6) mysql5.7默认配置了密码, 需要提取temporary password中的密码, 登陆对应数据库[root@nginx ~]# grep "temporary password" /var/log/mysqld.log [root@nginx ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log) 7) 重新初始化 MySQL 的密码mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Bgx123.com'; 2.配置LNMP架构1) 为了验证Nginx能否正常解析php动态请求, 需配置如下locaiton[root@nginx ~] server { server_name www.bgx.com; listen 80; root /code; index index.php index.html; location ~ \.php$ { root /code; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 2) 新增.php后缀的文件名, 使用phpinfo函数测试php能否正常解析[root@nginx ~] <?php phpinfo(); ?> 3) 验证php是否能正常连接mysql数据库服务[root@nginx ~]# cat /code/mysqli.php <?php $servername = "localhost"; $username = "root"; $password = "Bgx123.com"; $conn = mysqli_connect($servername, $username, $password); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "php连接MySQL数据库成功"; ?> 4) 通过浏览器访问info.php文件, 如能出现php相关的信息, 则表示nginx与php能正常工作5) 访问mysqli.php验证php-mysqli模块是否正常工作4.部署博客产品Wordpress1) 配置Nginx虚拟主机站点,域名为blog.bgx.com [root@nginx ~] server { listen 80; server_name blog.bgx.com; root /code/wordpress; index index.php index.html; location ~ \.php$ { root /code/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 2) 重启nginx服务[root@nginx ~] 3) 获取wordpress产品,解压并部署wordress[root@nginx ~] [root@nginx ~] [root@nginx code] [root@nginx ~] [root@nginx ~] 4) 由于wordpress产品需要依赖数据库, 所以需要手动建立数据库[root@nginx ~] mysql> create database wordpress; mysql> exit 5) 通过浏览器访问wordpress, 并部署该产品5.部署知乎产品Wecenter1.配置Nginx虚拟主机站点,域名为zh.bgx.com [root@http-server ~] server { listen 80; server_name zh.bgx.com; root /code/zh; index index.php index.html; location ~ \.php$ { root /code/zh; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@http-server ~] 2.下载Wecenter产品,部署Wecenter并授权[root@web02 ~] [root@web02 ~] [root@web02 ~] [root@web02 ~] 3.由于wecenter产品需要依赖数据库, 所以需要手动建立数据库 [root@http-server ~] MariaDB [(none)]> create database zh; MariaDB [(none)]> exit 3.通过浏览器访问网站6.部署网校产品Edusohu1.配置Nginx虚拟主机站点,域名为edu.bgx.com [root@http-server ~] server { listen 80; server_name edu.bgx.com; root /code/edu; index index.php index.html; location ~ \.php$ { root /code/edu; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@http-server ~] 2.下载edusohu产品,部署edusohu并授权 //获取wordpress代码 [root@http-server ~] [root@http-server /soft/src]# wget http://download.edusoho.com/edusoho-8.2.17.tar.gz //解压软件网站源码文件, 并授权站点目录,不然会导致无法安装 [root@http-server /soft/src] [root@http-server /soft/src] [root@http-server ~] [root@http-server ~] //由于edusohu会自动创建数据库, 所以无需创建数据库 3.通过浏览器访问网站7.迁移数据至独立服务器拆分LNMP的数据库到独立的数据库服务器步骤1.老服务器操作1) 指定导出对应的数据库文件(Bgx123.com是数据库密码)[root@web02 ~]# mysqldump -uroot -p'Bgx123.com' --all-databases --single-transaction > `date +%F%H`-mysql-all.sql 2) 拷贝备份数据库文件至新的数据库服务器上[root@web02 zh] 2.新服务器操作1) 导入旧的数据库至新的数据库中[root@db01 ~] 2) 登录新的数据库, 并检查数据库已被导入成功[root@db01 ~] mysql> show databases; 3) 在新数据库上授权, 允许所有网段, 通过all账户连接数据库 mysql> grant all on *.* to all@'%' identified by 'Bgx123.com'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) 4) 修改Wordpress产品代码连接数据库的配置文件[root@web01 ~] define('DB_NAME', 'wordpress'); define('DB_USER', 'all'); define('DB_PASSWORD', 'Bgx123.com'); define('DB_HOST', '172.16.1.51'); 5) 修改wecenter产品代码连接数据库的配置文件[root@web01 zh] system/config/database.php: 'password' => 'Bgx123.com', [root@web01 zh] 'host' => '172.16.1.51', 'username' => 'all', 'password' => 'Bgx123.com', 'dbname' => 'zh', 6) 修改edusoho产品代码连接数据库的配置文件[root@web01 edu] app/config/parameters.yml: database_password: 'Bgx123.com' [root@web01 edu] parameters: database_driver: pdo_mysql database_host: 172.16.1.51 database_port: 3306 database_name: edu database_user: all database_password: 'Bgx123.com' [root@web01 edu] 8.迁移图片至独立服务器1.nfs-server服务端操作1) 配置nfs共享的目录[root@nfs01 ~] /data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) /data/edu 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) /data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) 2) 创建对应共享的目录[root@nfs01 ~] [root@nfs01 ~] [root@nfs01 ~] 2.web01端操作1) WEB客户端验证NFS是否安装成功[root@web01 ~] [root@web01 ~] Export list for 172.16.1.31: /data/zh 172.16.1.0/24 /data/edu 172.16.1.0/24 /data/blog 172.16.1.0/24 2) 获取Wordpress产品的附件和图片存放的位置浏览器->右键->检查->Network->选择按钮->点击一下图片 3) 备份web服务器上的Wordpress图片和附件[root@web01 wp-content] [root@web01 wp-content] 4) 客户端执行挂载操作[Wordpress][root@web01 wp-content] [root@web01 wp-content] 5) 将挂载信息加入开机自启[root@web01 wp-content] 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0 [root@web01 wp-content] 3.web02端操作1) 客户端挂载nfs服务端存储[root@web02 ~] 2) 将挂载信息加入开机自启[root@web02 ~] 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0 [root@web02 wp-content] 9.扩展相同的Web服务器快速的扩展一台web服务器10.0.0.9, 数据库使用10.0.0.51,图片与附件使用10.0.0.311) 创建www用户[root@web03 ~] [root@web03 ~] 2) 安装LNP[root@web03 ~] [root@web03 ~] [root@web03 ~] [root@web03 ~] 3) 将web01的nginx配置文件导入到web03[root@web03 ~] 4) 将web01的php配置文件导入到web03[root@web03 ~] 5) 将web01的产品代码导到web03,在web1上线进行打包操作[root@web01 ~] [root@web03 ~] [root@web03 ~] 6) 启动nginx与php-fpm服务[root@web03 ~] [root@web03 ~] 7) 在web03上进行挂载[root@web03 ~] 转 xuliangwei.com
2023年11月03日
57 阅读
0 评论
0 点赞
2023-11-03
Nginx基础应用
1.Nginx目录索引1.Nginx默认是不允许列出整个目录浏览下载。Syntax: autoindex on | off; Default: autoindex off; Context: http, server, location autoindex_exact_size off; 默认为on, 显示出文件的确切大小,单位是bytes。 修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。 autoindex_localtime on; 默认为off,显示的文件时间为GMT时间。 修改为on, 显示的文件时间为文件的服务器时间。 charset utf-8,gbk; 默认中文目录乱码,添加上解决乱码。 2.配置站点目录实现浏览功能server { listen 80; server_name module.bgx.com; location / { root /code/down; charset utf-8,gbk; autoindex on; autoindex_localtime on; autoindex_exact_size off; } } 2.Nginx状态监控1.ngx_http_stub_status_module用于展示Nginx连接状态信息, 需要--with-http_stub_status_module模块支持Syntax: stub_status; Default: — Context: server, location 2.配置Nginx status示例server { listen 80; server_name module.bgx.com; access_log off; location /nginx_status { stub_status; } } 3.使用浏览器访问http://IP/nginx_status访问后得到的结果4.Nginx 状态详细信息如下Active connections accepts handled requests Reading Writing Waiting keepalive_timeout 0; keepalive_timeout 65; 3.Nginx访问控制基于IP的访问控制 http_access_module基于用户登陆认证 http_auth_basic_module1.Nginx基于IP的访问控制 Syntax: allow address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except Syntax: deny address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except 1) 访问控制配置示例, 拒绝指定的IP, 其他全部允许server { listen 80; server_name module.bgx.com; access_log off; location /nginx_status { stub_status; deny 10.0.0.1; allow all; } } 2) 访问控制配置示例, 只允许谁能访问, 其它全部拒绝server { listen 80; server_name module.bgx.com; access_log off; location /nginx_status { stub_status; allow 10.0.0.0/24; allow 127.0.0.1; deny all; } } 2.Nginx基于用户登陆认证1) 基于用户登陆认证配置语法 Syntax: auth_basic string| off; Default: auth_basic off; Context: http, server, location, limit_except Syntax: auth_basic_user_file file; Default: - Context: http, server, location, limit_except 2) 基于用户登陆认证配置实践 [root@xuliangwei ~] [root@xuliangwei ~] server { listen 80; server_name module.bgx.com; access_log off; location /nginx_status { stub_status; auth_basic "Auth access Blog Input your Passwd!"; auth_basic_user_file /etc/nginx/auth_conf; } } 4.Nginx访问限制经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问, 会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,并发数进行限制。ngx_http_limit_conn_module模块可以根据定义的key来限制每个键值的连接数,如同一个IP来源的连接数。limit_conn_module 连接频率限制limit_req_module 请求频率限制http协议的连接与请求HTTP是建立在TCP, 在完成HTTP请求需要先建立TCP三次握手(称为TCP连接),在连接的基础上在HTTP请求。HTTP请求建立在一次TCP连接基础上,一次TCP请求至少产生一次HTTP请求注:客户端的IP地址作为键。$remote_addr 变量的长度为7字节到15字节$binary_remote_addr 变量的长度是固定的4字节1.Nginx连接限制配置实战1)Nginx连接限制配置语法Syntax: limit_conn_zone key zone=name:size; Default: — Context: http Syntax: limit_conn zone number; Default: — Context: http, server, location 2)Nginx连接限制配置实践 limit_conn_zone $binary_remote_addr zone=conn_zone:10m; server { limit_conn conn_zone 1; location / { root /code; index index.html; } 3).使用ab工具进行压力测试[root@xuliangwei ~] [root@xuliangwei ~] 4).nginx日志结果2018/10/24 18:04:49 [error] 28656#28656: *1148 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com" 2018/10/24 18:04:49 [error] 28656#28656: *1155 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com" 2018/10/24 18:04:49 [error] 28656#28656: *1156 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com" 2.Nginx请求限制配置实战1)Nginx请求限制配置语法Syntax: limit_req_zone key zone=name:size rate=rate; Default: — Context: http Syntax: limit_conn zone number [burst=number] [nodelay]; Default: — Context: http, server, location 2)Nginx请求限制配置实战 limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; server { listen 80; server_name module.bgx.com; limit_req zone=req_zone; limit_req zone=req_zone burst=3 nodelay; location / { root /code; index index.html; } } 3).使用ab工具进行压力测试[root@xuliangwei ~] [root@xuliangwei ~] 4).nginx日志结果2018/10/24 07:38:53 [error] 81020#0: *8 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10" 2018/10/24 07:38:53 [error] 81020#0: *9 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10" 2018/10/24 07:38:53 [error] 81020#0: *10 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10" 3.Nginx连接限制没有请求限制有效?我们前面说过, 多个请求可以建立在一次的TCP连接之上, 那么我们对请求的精度限制,当然比对一个连接的限制会更加的有效因为同一时刻只允许一个连接请求进入, 但是同一时刻多个请求可以通过一个连接进入。所以请求限制才是比较优的解决方案。5.Nginx虚拟站点所谓虚拟主机,及在一台服务器上配置多个网站如: 公司主页、博客、论坛看似三个网站, 实则可以运行在一台服务器上。1.基于域名虚拟主机配置实战1.创建web站点目录[root@bgx ~] [root@bgx ~] [root@bgx ~] 2.配置不同域名的虚拟主机[root@bgx ~] server { listen 80; server_name www.xuliangwei.com; root /soft/code/www; index index.html; ... } [root@bgx ~] server { ... listen 80; server_name bbs.xuliangwei.com; root /soft/code/bbs; index index.html; } 2.基于端口虚拟主机配置实战//仅修改listen监听端口即可, 但不能和系统端口出现冲突 server { ... listen 8001; ... } server { ... listen 8002; ... } 3.基于虚拟主机别名配置实战实现用户访问多个域名对应同一个网站, 比如用户访问www.xuliangwei.com和访问xuliangwei.com内容一致 [root@bgx ~] server { listen 80; server_name www.bgx.com; } server { listen 80; server_name bgx.com; } [root@bgx ~] server { listen 80; server_name www.bgx.com bgx.com; ... } [root@bgx ~] Go [root@bgx ~] Go 6.Nginx日志配置在学习日志之前, 我们需要先回顾下HTTP请求和返回Nginx有非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志。日志格式 通过log_format命令定义格式。1.log_format定义日志格式语法# 配置语法: 包括: error.log access.log Syntax: log_format name [escape=default|json] string ...; Default: log_format combined "..."; Context: http 2.默认Nginx定义语法格式如下log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; 3.Nginx日志格式允许包含的内置变量$remote_addr $remote_user $time_local $time_iso8601 $request $status $body_bytes_sent $bytes_sent $msec $http_referer $http_user_agent $http_x_forwarded_for $request_length $request_time 4.access_log日志配置语法Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; Default: access_log logs/access.log combined; Context: http, server, location, if in location, limit_except 5.Nginx Access日志配置实践 server { access_log /var/log/nginx/www.bgx.com.log main; } location / { access_log /var/log/nginx/www.bgx.com.log main; } access_log off; 7.Nginx Location使用Nginx Location可以控制访问网站的路径, 但一个server可以有多个location配置, 多个location的优先级该如何区分1.Location语法示例location [=|^~|~|~*|!~|!~*|/] /uri/ { ... } 2.Location语法优先级排列 匹配符 匹配规则 优先级 = 精确匹配 1 ^~ 以某个字符串开头 2 ~ 区分大小写的正则匹配 3 ~* 不区分大小写的正则匹配 4 !~ 区分大小写不匹配的正则 5 !~* 不区分大小写不匹配的正则 6 / 通用匹配,任何请求都会匹配到 7 3.配置网站验证Location优先级[root@Nginx conf.d] server { listen 80; server_name bgx.com; location / { default_type text/html; return 200 "location /"; } location =/ { default_type text/html; return 200 "location =/"; } location ~ / { default_type text/html; return 200 "location ~/"; } } 4.测试Location效果 [root@Nginx conf.d] location =/ [root@Nginx ~] location ~/ [root@Nginx ~] location / 5.Locaiton应用场景 location / { } location ~ \.php$ { fastcgi_pass http://127.0.0.1:9000; } location ~ \.jsp$ { proxy_pass http://127.0.0.1:8080; } location ~* .*\.(jpg|gif|png|js|css)$ { rewrite (.*) http://cdn.xuliangwei.com$request_uri; } location ~* "\.(sql|bak|tgz|tar.gz|.git)$" { default_type text/html; return 403 "启用访问控制成功"; } 转 xuliangwei.com
2023年11月03日
34 阅读
0 评论
0 点赞
2023-11-03
nginx基础
1.Nginx基本简述Nginx是一个开源且高性能、可靠的HttpWeb服务、代理服务。开源: 直接获取源代码高性能: 支持海量并发可靠: 服务稳定1.常见的 HTTP Web服务Httpd 由Apache基金会IIS 微软服务器版GWS Google开发Openrestry 基于nginx+luaTengline 淘宝基于Nginx开发2.为什么选择 Nginx1.Nginx非常轻量1.功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)2.代码模块化(易读,便于二次开发,对于开发人员非常友好)2.互联网公司都选择Nginx1.Nginx技术成熟, 国内公司基本大规模使用2.适合当前主流架构趋势, 微服务、云架构、中间层3.统一技术栈, 降低维护成本, 降低技术更新成本。3.Nginx采用Epool网络模型, Apache采用Select模型Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。3.Nginx 应用场景2.Nginx快速安装Nginx软件安装的方式有很多种1.源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐)2.epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)3.官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读 √)1.安装Nginx软件所需依赖 [root@web ~] pcre pcre-devel make automake wget httpd-tools vim tree 2.配置nginx yum源[必须使用官方源][root@web ~] [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 3.安装Nginx服务[root@web ~] 4.检查Nginx软件版本[root@web ~] nginx version: nginx/1.14.0 5.为了让大家更清晰的了解Nginx软件的全貌,可使用rpm -ql nginx查看整体的目录结构及对应的功能如下表格对Nginx安装目录做详细概述 路径 类型 作用 /etc/nginx /etc/nginx/nginx.conf /etc/nginx/conf.d /etc/nginx/conf.d/default.conf 配置文件 Nginx主配置文件 /etc/nginx/fastcgi_params /etc/nginx/scgi_params /etc/nginx/uwsgi_params 配置文件 Cgi、Fastcgi、Uwcgi配置文件 /etc/nginx/win-utf /etc/nginx/koi-utf /etc/nginx/koi-win 配置文件 Nginx编码转换映射文件 /etc/nginx/mime.types 配置文件 http协议的Content-Type与扩展名 /usr/lib/systemd/system/nginx.service 配置文件 配置系统守护进程管理器 /etc/logrotate.d/nginx 配置文件 Nginx日志轮询,日志切割 /usr/sbin/nginx /usr/sbin/nginx-debug 命令 Nginx终端管理命令 /etc/nginx/modules /usr/lib64/nginx /usr/lib64/nginx/modules 目录 Nginx模块目录 /usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html 目录 Nginx默认站点目录 /usr/share/doc/nginx-1.12.2 /usr/share/man/man8/nginx.8.gz 目录 Nginx的帮助手册 /var/cache/nginx 目录 Nginx的缓存目录 /var/log/nginx 目录 Nginx的日志目录 6.通过nginx -v查看Nginx编译选项 编译选项 作用 --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock 程序安装目录和路径 --http-client-body-temp-path=/var/cache/nginx/client_tem --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp 临时缓存文件 --user=nginx --group=nginx 设定Nginx进程启动用户和组(安全) --with-cc-opt 设置额外的参数将被添加到CFLAGS变量 --with-ld-opt 设置附加的参数, 链接系统库 3.Nginx配置文件Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。1.CoreModule 核心模块2.EventModule 事件驱动模块3.HttpCoreModule http内核模块需了解扩展项CoreModule层下可以有Event、HTTPHTTP模块层允许有多个Server层, Server主要用于配置多个网站Server层又允许有多个Location, Location主要用于定义网站访问路径CoreModule核心模块user www; worker_processes 1; error_log /log/nginx/error.log pid /var/run/nginx.pid events事件模块events { worker_connections use epool; } http内核模块 http { ... 'server' { listen 80; server_name localhost; access_log host.access.log 'location' / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; } ... 'server' { ... } include /etc/nginx/conf.d/*.conf; } 4.Nginx配置网站1.新增nginx配置文件[root@web01 conf.d] server { listen 80; server_name game.wangmanyuan.com; location / { root /wangmanyuan_code; index index.html; } } 2.放置对应的源代码文件至指定的目录[root@web01 conf.d] [root@web01 conf.d] [root@web01 wangmanyuan_code] [root@web01 wangmanyuan_code] [root@web01 wangmanyuan_code] ceshi game html5.zip img index.html readme.txt 3.检查nginx的语法是否存在错误[root@web01 wangmanyuan_code] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 4.重载Nginx [reload|restart][root@web01 wangmanyuan_code] [root@web01 wangmanyuan_code] 5.如何访问对应的网站1.通过服务器的IP直接访问(不推荐) 2.通过假域名方式访问(推荐方式) Windows修改 C:\Windows\System32\drivers\etc\hosts 10.0.0.7 game.wangmanyuan.com 3.使用ping命令测试域名解析是否正常 转 xuliangwei.com
2023年11月03日
41 阅读
0 评论
0 点赞
2023-11-03
NFS基本概述
1.NFS基本概述NFS是Network File System的缩写及网络文件系统。主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS为什么要使用NFS服务进行数据存储1.实现多台服务器之间数据共享2.实现多台服务器之间数据的一致2.NFS应用场景下面我将通过图解给大家展示集群需要共享存储服务的理由。1.A用户传图片到WEB1服务器2.B用户访问这张图片,结果B用户访问的请求分发到了WEB2,因为WEB2上没有这张图片,结果B用户无法看到A用户传的图片。1.如果有共享存储的情况, A用户上传图片无论分发到WEB1还是WEB2, 最终都存储至共享存储2.在当B用户访问图片时,无论分发到WEB1还是WEB2上,最终也都会去共享存储上访问,这样就可以访问到资源了3.NFS实现原理本地文件操作方式1.当用户执行mkdir命令, 该命令会通过shell解释器翻译给内核,由内核解析完成后驱动硬件,完成相应的操作。NFS实现原理(需要先了解[程序|进程|线程])1.用户进程访问NFS客户端,使用不同的函数对数据进行处理2.NFS客户端通过TCP/IP的方式传递给NFS服务端。3.NFS服务端接收到请求后,会先调用portmap进程进行端口映射。4.nfsd进程用于判断NFS客户端是否拥有权限连接NFS服务端。5.Rpc.mount进程判断客户端是否有对应的权限进行验证。6.idmap进程实现用户映射和压缩7.最后NFS服务端会将对应请求的函数转换为本地能识别的命令,传递至内核,由内核驱动硬件。rpc是一个远程过程调用,那么使用nfs必须有rpc服务4.NFS服务安装1.环境准备 服务器系统 角色 外网IP 内网IP CentOS 7.5 NFS服务端 eth0:10.0.0.31 eth1:172.16.1.31 CentOS 7.5 NFS客户端 eth0:10.0.0.41 eth1:172.16.1.41 注意: 不要忘记关闭防火墙, 以免默认的防火墙策略禁止正常的NFS共享服务//关闭Firewalld防火墙 [root@xuliangwei ~] [root@xuliangwei ~] //关闭selinux防火墙 [root@xuliangwei ~] [root@xuliangwei ~] 2.安装NFS-Server[root@nfs-server ~] 3.NFS服务程序的配置文件为/etc/exports默认里面没有任何内容。我们可以按照共享目录的路径 允许访问的NFS客户端(共享权限参数)格式,定义要共享的目录与相应的权限。exports配置文件格式NFS共享目录 NFS客户端地址1(参数1,参数2,...) 客户端地址2(参数1,参数2,...)NFS共享目录 NFS客户端地址(参数1,参数2,...)如果想要把/data目录共享给172.16.1.0/24网段内的所有主机1.主机都拥有读写权限2.在将数据写入到NFS服务器的硬盘中后才会结束操作,最大限度保证数据不丢失3.将所有用户映射为本地的匿名用户(nfsnobody)//注意: NFS客户端地址与权限之间没有空格 [root@nfs-server ~] /data 172.16.1.0/24(rw,sync,all_squash) //在NFS服务器上建立用于NFS文件共享的目录,并设置对应权限 [root@nfs-server ~] [root@nfs-server ~] //NFS共享目录会记录至/var/lib/nfs/etab,如果该目录不存在共享信息,请检查/etc/exports是否配置错误 4.在使用NFS服务进行文件共享之前,需要使用RPC(Remote Procedure Call远程过程调用, 服务将NFS服务器的IP地址和端口号信息发送给客户端。因此,在启动NFS服务之前,需要先重启并启用rpcbind服务程序,同时都加入开机自启动[root@nfs-server ~] [root@nfs-server ~] [root@nfs-server ~] [root@nfs-server ~] 5.NFS挂载卸载NFS客户端的配置步骤也十分简单。先使用showmount命令,查询NFS服务器的远程共享信息,其输出格式为“共享的目录名称 允许使用客户端地址”。1.安装客户端工具,仅启动rpcbind服务[root@nfs-client ~] [root@nfs-client ~] [root@nfs-client ~] 2.客户端使用showmount -e查看远程服务器rpc提供的可挂载nfs信息[root@nfs-client ~] Export list for 172.16.1.31: /data 172.16.1.0/24 3.在NFS客户端创建一个挂载目录, 使用mount命令并结合-t参数, 指定要挂载的文件系统的类型, 并在命令后面写上服务器的IP地址, 以及服务器上的共享目录, 最后需要写上要挂载到本地系统(客户端)的目录。[root@nfs-client ~] [root@nfs-client ~] //查看挂载信息(mount也可以查看) [root@nfs-client ~] Filesystem Size Used Avail Use% Mounted on /dev/sda3 62G 845M 58G 2% / tmpfs 244M 0 244M 0% /dev/shm /dev/sda1 190M 26M 155M 14% /boot 172.16.1.31:/data 62G 880M 58G 2% /nfsdir 4.挂载成功后可以进行增删改操作 [root@nfs-client ~]# echo "nfs-client" >> /mnt/test.txt [root@nfs-client ~]# cat /data/test.txt nfs-client 5.如果希望NFS文件共享服务能一直有效,则需要将其写入到fstab文件中[root@nfs-client ~] 172.16.1.31:/data /nfsdir nfs defaults 0 0 6.如果不希望使用NFS共享, 可进行卸载[root@nfs-client ~] 注意:卸载的时候如果提示”umount.nfs: /nfsdir: device is busy” 1.切换至其他目录, 然后在进行卸载。 2.NFS Server宕机, 强制卸载umount -lf /nfsdir 7.在企业工作场景,通常情况NFS服务器共享的只是普通静态数据(图片、附件、视频),不需要执行suid、exec等权限,挂载的这个文件系统只能作为数据存取之用,无法执行程序,对于客户端来讲增加了安全性。例如: 很多木马篡改站点文件都是由上传入口上传的程序到存储目录。然后执行的。通过mount -o指定挂载参数,禁止使用suid,exec,增加安全性能 [root@nfs-client ~] 8.有时也需要考虑性能相关参数[可选]通过mount -o指定挂载参数,禁止更新目录及文件时间戳挂载 [root@nfs-client ~] 6.NFS配置文件执行man exports命令,然后切换到文件结尾,可以快速查看如下样例格式: nfs共享参数 参数作用 rw* 读写权限 ro 只读权限 root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户(不常用) no_root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员(不常用) all_squash 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户(常用) no_all_squash 无论NFS客户端使用什么账户访问,都不进行压缩 sync* 同时将数据写入到内存与硬盘中,保证不丢失数据 async 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据 anonuid* 配置all_squash使用,指定NFS的用户UID,必须存在系统 anongid* 配置all_squash使用,指定NFS的用户UID,必须存在系统 1.验证ro权限1.服务端修改rw为ro参数 [root@nfs01 ~] /data 172.16.1.0/24(ro,sync,all_squash) [root@nfs01 ~] 2.客户端验证 [root@backup ~] [root@backup ~] Filesystem Size Used Avail Use% Mounted on 172.16.1.31:/data 98G 1.7G 97G 2% /mnt [root@backup mnt] touch: cannot touch ‘file’: Read-only file system 2.验证all_squash、anonuid、anongid权限//1.服务端配置 [root@nfs01 ~] /data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) //2.服务端需要创建对应的用户 [root@nfs01 ~] [root@nfs01 ~] [root@nfs01 ~] uid=666(www) gid=666(www) groups=666(www) //3.重载nfs-server [root@nfs01 ~] [root@nfs01 ~] /data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,secure,root_squash,all_squash) //4.授权共享目录为www [root@nfs01 ~] [root@nfs01 ~] drwxr-xr-x 3 www www 53 Sep 3 02:08 /data/ //5.客户端验证 [root@backup ~] [root@backup ~] //6.客户端查看到的文件,身份是666 [root@backup ~] drwxr-xr-x 2 666 666 6 Sep 3 02:08 rsync_dir -rw-r--r-- 1 666 666 0 Sep 3 02:08 rsync_file //7.客户端依旧能往/mnt目录下写文件 [root@backup mnt] [root@backup mnt] [root@backup mnt] drwxr-xr-x 2 666 666 6 Sep 3 03:05 111 -rw-r--r-- 1 666 666 0 Sep 3 03:05 fff //8.建议:将客户端也创建一个uid为666,gid为666,统一身份,避免后续出现权限不足的情况 [root@backup mnt] [root@backup mnt] [root@backup mnt] uid=666(www) gid=666(www) groups=666(www) //9.最后检查文件的身份 [root@backup mnt] total 4 drwxr-xr-x 2 www www 6 Sep 3 03:05 111 -rw-r--r-- 1 www www 0 Sep 3 03:05 fff 6.NFS存储小结NFS存储优点1.NFS文件系统简单易用、方便部署、数据可靠、服务稳定、满足中小企业需求。2.NFS文件系统内存放的数据都在文件系统之上,所有数据都是能看得见。NFS存储局限1.存在单点故障, 如果构建高可用维护麻烦。2.NFS数据明文, 并不对数据做任何校验。3.客户端挂载无需账户密码, 安全性一般(内网使用)生产应用建议1.生产场景应将静态数据尽可能往前端推, 减少后端存储压力2.必须将存储里的静态资源通过CDN缓存(jpg\png\mp4\avi\css\js)3.如果没有缓存或架构本身历史遗留问题太大, 在多存储也无用7.NFS案例实践准备3台虚拟机服务器,并且请按照要求搭建配置NFS服务。NFS服务端(A)NFS客户端(B)NFS客户端(C)1.在NFS服务端(A)上共享/data/w(可写)及/data/r(只读)2.在NFS客户端(B/C)上进行挂载环境准备 服务器系统 角色 IP CentOS 7.5 NfsServer(A) 172.16.1.31 CentOS 7.5 NfsClient(B) 172.16.1.41 CentOS 7.5 NfsClient(C) 172.16.1.7 1.NFS服务端配置//1.安装nfs [root@nfs01 ~] //2.配置nfs [root@nfs01 ~] /data/r 172.16.1.0/24(ro,sync,all_squash,anonuid=666,anongid=666) /data/w 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) //3.创建对应用户 [root@web01 ~] [root@web01 ~] [root@nfs01 ~] uid=666(www) gid=666(www) groups=666(www) //4.创建共享目录 [root@nfs01 ~] [root@nfs01 ~] //5.重启NFS [root@nfs01 ~] 2.NFS-客户端配置//1.安装nfs工具 [root@backup-41 ~]# yum install nfs-utils -y [root@backup-41 ~]# systemctl restart rpcbind //2.准备本地挂载点目录 [root@backup-41 ~]# mkdir /data/{r,w} -p //3.准备对应用户 [root@backup-41 ~]# groupadd -g 666 www [root@backup-41 ~]# useradd -u 666 -g www www [root@backup-41 ~]# id www uid=666(www) gid=666(www) groups=666(www) //4.查看远端共享的nfs目录 [root@backup-41 ~]# showmount -e 172.16.1.31 Export list for 172.16.1.31: /data/w 172.16.1.0/24 /data/r 172.16.1.0/24 //5.挂载对应目录站点 [root@backup-41 ~]# mount -t nfs 172.16.1.31:/data/w /data/w/ [root@backup-41 ~]# mount -t nfs 172.16.1.31:/data/r /data/r/ [root@backup-41 ~]# df -h Filesystem Size Used Avail Use% Mounted on 172.16.1.31:/data/w 50G 1.6G 49G 4% /data/w 172.16.1.31:/data/r 50G 1.6G 49G 4% /data/r //6.测试/data/r读权限 [www@backup-41 r]$ pwd /data/r [www@backup-41 r]$ cat edu.com This is Nfs to Oldboy [www@backup-41 r]$ touch edu touch: cannot touch ‘edu’: Read-only file system //7.测试/data/w写权限 [root@backup-41 r]# cd /data/w/ [root@backup-41 w]# pwd /data/w [root@backup-41 w]# touch bbbback [root@backup-41 w]# ll total 0 -rw-r -rw-r //8.实现开机自动挂载 [root@backup-41 ~]# echo "172.16.1.31:/data/r /data/r nfs defaults 0 0" >>/etc/fstab [root@backup-41 ~]# echo "172.16.1.31:/data/w /data/w nfs defaults 0 0" >>/etc/fstab #注意: 当将远程挂载设备写入/etc/fstab文件后,一定要执行mount -a [root@backup-41 ~]# mount -a [root@backup-41 ~]# df -h Filesystem Size Used Avail Use% Mounted on 172.16.1.31:/data/r 50G 1.6G 49G 4% /data/r 172.16.1.31:/data/w 50G 1.6G 49G 4% /data/w #如果编写错误会有如下提示 [root@backup-41 ~]# mount -a mount.nfs: access denied by server while mounting 172.16.1.31:/dataa/w //9.卸载nfs #1.正常卸载 [root@backup-41 ~]# umount /data/w/ #2.强制卸载 [root@backup-41 ~]# umount -lf /data/w/ 3.NFS扩展项//1.扩展:无需重启NFS服务平滑加载配置文件 [root@nfs01 r] /data/r 172.16.1.0/24(ro) /data/p 172.16.1.0/24(ro) /data/w 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) [root@nfs01 r] exporting 172.16.1.0/24:/data/w exporting 172.16.1.0/24:/data/p exporting 172.16.1.0/24:/data/r //2.扩展:nfs客户端挂载参数 [root@backup-41 ~] //3.扩展:nfs客户端永久挂载参数 [root@backup-41 ~] 172.16.1.31:/data/r /data/r nfs defaults,noatime,nodiratime,noexec,nodev,nosuid 0 0 172.16.1.31:/data/w /data/w nfs defaults,noatime,nodiratime,noexec,nodev,nosuid 0 0 //4.扩展:客户端检查挂载参数是否生效 [root@backup-41 ~] 等价于 [root@backup-41 ~] 8.AutoFS自动挂载无论是Samba服务还是NFS服务, 都要把挂载信息写入到/etc/fstab中, 这样远程共享资源就会自动随服务器开机而进行挂载。1.虽然很方便,但挂载资源过多会造成网络带宽以及服务器硬件资源带来很大的负载2.如果在资源挂载后长期不使用,也会造成服务器硬件资源的浪费。3.每次使用之前执行mount手动挂载,这是个不错的选择, 但每次都需要先挂载在使用, 会非常的麻烦那么autofs自动挂载服务可以帮我们解决这一问题。autofs服务程序是一种守护进程, 当检测到用户试图访问一个尚未挂载的文件系统时,将自动挂载该文件系统。autofs服务程序是在用户需要使用该文件系统时才去动态挂载, 从而节约了网络资源和服务器的硬件资源autofs自动挂载服务, 有两种挂载方式:直接:direct /- 子配置文件必须写本地绝对路径间接: indirect /path 子配置文件写相对于/path的子目录1.客户端安装autofs自动挂载[root@nfs-client ~] [root@nfs-client ~] [root@nfs-client ~] 2.不要将设备挂载信息都写入到autofs服务的主配置/etc/auto.master文件中,会让主配置文件臃肿不堪,不利于服务执行效率,也不利于日后修改里面的配置内容 [root@http-server ~]# grep "dir" /etc/auto.master +dir:/etc/auto.master.d 3.直接挂载方式: 本地的挂载点是绝对路径 [root@nfs-client ~]# vim /etc/auto.master.d/nfs.autofs /- /etc/auto.nfs_direct [root@nfs-client ~]# vim /etc/auto.nfs_direct /nfsdir -fstype=nfs,rw,sync,soft,nosuid,nodev 192.168.56.11:/data [root@nfs-client ~]# systemctl restart autofs 间接挂载: 本地路径和服务端的路径都没有直接写绝对路径//名称后缀必须是autofs,访问本地对应目录自动远端nfs [root@nfs-client ~] /nfsdir /etc/auth.nfs_share [root@nfs-client ~] * -rw,sync,soft,nosuid,nodev,'sec=krb5p' 192.168.56.11:/data/& //重启autofs服务 [root@nfs-client ~] 注意:如果有kerberos验证则需要启动nfs-secure并加入开机自启动使用df命令无法查看具体挂载内容, 使用mount命令可以转 xuliangwei.com
2023年11月03日
27 阅读
0 评论
0 点赞
2023-11-03
Rsync基本概述
1.Rsync基本概述rsync是一款开源的备份工具,可以在不同主机之间进行同步,可实现全量备份与增量备份,因此非常适合用于架构集中式备份或异地备份等应用。rsync官方地址:传送门rsync监听端口:873rsync运行模式:C/Srsync常见的两种备份方式完全备份增量备份假设客户端上有file1 file2 file3文件,服务端上有file1文件,现要将客户端上的数据备份至服务端完全备份,将客户端所有的数据内容file1 file2 file3全部备份至服务端 (效率低下, 占用空间)增量备份,将客户端的file2 file3增量备份至服务端 (提高备份效率,节省空间, 适合异地备份 )2.Rsync应用场景关于数据同步的两种方式1.推: 所有主机推送本地数据至Rsync备份服务器,会导致数据同步缓慢(适合少量数据备份)2.拉: rsync备份服务端拉取所有主机上的数据,会导致备份服务器开销大3.大量服务器备份场景4.异地备份实现思路3.Rsync传输模式Rsync大致使用三种主要的数据传输方式本地方式远程方式守护进程//Local: 本地传输 rsync [OPTION...] SRC... [DEST] Access via remote shell: 远程通道传输 Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST Access via rsync daemon: 守护进程方式传输 Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST Rsync命令对应选项-a -v -z -r -t -o -p -g -l -P -D -L -e --exclude=PATTERN --exclude-from=file --bwlimit=100 --partial --delete 1.本地传输方式: 单个主机本地之间的数据传输(此时类似于cp命令)Local: rsync [OPTION...] SRC... [DEST] rsync [options] SRC... [DEST] rsync -avz /etc/passwd /tmp/ 2.远程通道传输方式: 通过ssh通道传输数据,类似scp命令 Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] rsync -avz root@172.16.1.41:/root/oldboyedu.com ./ rsync -avz root@172.16.1.41:/root/ /backup/ rsync -avz root@172.16.1.41:/root /backup/ Pull rsync [options] [USER@] HOST SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST rsync -avz /backup/2018-07-23 root@172.16.1.41:/tmp/ Push rsync [options] SRC... [USER@] HOST [DEST] 远程通道传输方式问题必须通过ssh协议才可以传输使用ssh系统用户,不安全3.守护进程传输方式: rsync自身非常重要的功能(不使用系统用户,更加安全) Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] [root@nfs01 ~]# rsync -avz rsync_backup@192.172.16.1.41::backup/ /mnt/ rsync [OPTION...] [USER@] HOST:: SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST [root@nfs01 ~]# rsync -avz /mnt/ rsync_backup@192.172.16.1.41::backup/ rsync [OPTION...] SRC... [USER@] HOST:: [DEST] 4.Rsync服务实践 角色 外网IP(NAT) 内网IP(LAN) 主机名 Rsync服务端 eth0:10.0.0.41 eth1:172.16.1.41 backup Rsync客户端 eth0:10.0.0.31 eth1:172.16.1.31 nfs 1.第一个里程碑,安装rsync软件[root@backup ~] 2.第二个里程碑,配置/etc/rsyncd.conf[root@backup ~]# cat /etc/rsyncd.conf uid = rsync gid = rsync port = 873 fake super = yes use chroot = no max connections = 200 timeout = 600 ignore errors read only = false list = false auth users = rsync_backup secrets file = /etc/rsync.password log file = /var/log/rsyncd.log ##################################### [backup] comment = welcome to oldboyedu backup! path = /backup [root@backup ~]# vim /etc/rsyncd.conf # 全局模块 uid = rsync gid = rsync port = 873 fake super = yes use chroot = no max connections = 200 timeout = 600 ignore errors read only = false list = false auth users = rsync_backup secrets file = /etc/rsync.passwd ##局部模块 [backup] comment = commit path = /backup 3.第三个里程碑,创建用户(运行rsync服务的用户身份) [root@backup ~] [root@backup ~] [root@backup ~] 4.第四个里程碑,创建虚拟用户密码文件(用于客户端连接时使用的用户) [root@backup ~] [root@backup ~] 5.第五个里程碑,启动rsync服务,并加入开机自启[root@backup ~] [root@backup ~] 启动后检查对应端口 [root@bogon ~] Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 4758/rsync 6.第六个里程碑,Rsync客户端配置, 配置密码并设置权限方式一:适合终端执行指定用户密码文件 [root@nfs01 ~] [root@nfs01 ~] [root@nfs01 ~] 方式二:适合写脚本,强烈推荐方式 [root@nfs01 ~] 实战一: 客户端推送数据至Rsync服务端[root@nfs01 ~]# rsync -avz /backup/ rsync_backup@172.16.1.41::backup/ 实战二: 客户端拉取Rsync服务端数据至本地[root@nfs01 ~]#rsync -avz rsync_backup@172.16.1.41::backup /backup/ --password-file=/etc/rsync.password 实战三: Rsync实现数据无差异同步 [root@nfs01 ~]# rsync -avz --delete rsync_backup@172.16.1.41::backup/ /data/ --password-file=/etc/rsync.password [root@nfs01 ~]# rsync -avz --delete /data/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password 5.Rsync备份案例已知 3 台服务器主机名分别为 web01、backup 、nfs,主机信息见下表: 角色 外网IP(NAT) 内网IP(LAN) 主机名 WEB eth0:10.0.0.7 eth1:172.16.1.7 web01 NFS eth0:10.0.0.31 eth1:172.16.1.31 nfs01 Rsync eth0:10.0.0.41 eth1:172.16.1.41 backup 客户端需求1.客户端每天凌晨01点在服务器本地打包备份(系统配置文件、日志文件、其他目录、应用配置等文件)2.客户端备份的数据必须存放至以主机名_IP地址_当前时间命名的目录中, 例/backup/nfs-server_172.16.1.31_2018-09-023.客户端最后通过rsync推送本地已打包好的备份文件至backup服务器4.客户端服务器本地保留最近7天的数据, 避免浪费磁盘空间服务端需求1.服务端部署rsync,用于接收客户端推送过来的备份数据2.服务端需要每天校验客户端推送过来的数据是否完整3.服务端需要每天校验的结果通知给管理员4.服务端仅保留6个月的备份数据,其余的全部删除注意:所有服务器的备份目录必须都为/backup建议备份的数据内容如下 /etc/rc.local /etc/fstab /etc/hosts /var/spool/cron/ /etc/firewalld /server/scripts /var/log/ 1.客户端备份脚本实现思路[low版][root@nfs01 backup] #!/bin/bash Path=/backup Host=$(hostname) Addr=$(ifconfig eth1|awk 'NR==2{print $2}') Date=$(date +%F) Dest=${Path}/${Host}_${Addr}_${Date} [ -d $Dest ] || mkdir -p $Dest cp -rp /etc/passwd /etc/fstab /etc/rc.d/rc.local /var/spool/cron $Dest/ cp -rp /server/ $Dest/ cp -rp /etc/rsyncd.conf $Dest/ Rsync_User=rsync_backup Rsync_Addr=172.16.1.41 Rsync_Module=backup export RSYNC_PASSWORD=123456 rsync -avz $Path/ ${Rsync_User}@${Rsync_Addr}::${Rsync_Module} find $Path/ -type d -mtime +7 -exec rm -rf {} \; 2.客户端备份脚本实现思路,并将客户端备份的数据进行md5加密[优化版] [root@nfs01 ~] #!/bin/bash Path=/backup Host=$(hostname) Addr=$(ifconfig eth1|awk 'NR==2{print $2}') Date=$(date +%F) Dest=${Path}/${Host}_${Addr}_${Date} [ -d $Dest ] || mkdir -p $Dest cd / && \ tar czf $Dest/system.tar.gz etc/passwd etc/fstab etc/rc.d/rc.local var/spool/cron tar czf $Dest/user.tar.gz server/ tar czf $Dest/config.tar.gz etc/rsyncd.conf md5sum $Dest/*.tar.gz > $Dest/flag_${Date} Rsync_User=rsync_backup Rsync_Addr=172.16.1.41 Rsync_Module=backup export RSYNC_PASSWORD=123456 rsync -avz $Path/ ${Rsync_User}@${Rsync_Addr}::${Rsync_Module} find $Path/ -type d -mtime +7|xargs rm -rf 3.客户端编写定时任务,让备份每天凌晨1点进行备份[root@nfs01 ~] 00 01 * * * /bin/bash /server/scripts/backup_rsync.sh &>/dev/null 4.服务端校验客户端数据, 并将结果以邮件方式发给管理员1).服务端实现邮件功能[root@backup /]# yum install mailx -y [root@backup /]# vim /etc/mail.rc set from=552408925@qq.com set smtp=smtps://smtp.qq.com:465 set smtp-auth-user=552408925@qq.com set smtp-auth-password= #客户端授权码 set smtp-auth=login set ssl-verify=ignore set nss-config-dir=/etc/pki/nssdb/ 2).服务端使用md5校验客户端数据,并将检查结果发送给管理员[root@backup ~] Path=/backup Date=$(date +%F) find $Path -type f -name "flag_$Date"|xargs md5sum -c >$Path/result_$Date mail -s "Rsync_backup $(date +%F)" 572891887@qq.com <$Path/result_$Date find $Path/ -type f -name "result*" -mtime +3 -exec rm -f {} \; find $Path/ -type d -mtime +180|xargs rm -rf 3).服务端编写定时任务脚本 [root@backup backup]# crontab -l 00 05 * * * /bin/bash /server/scripts/check_backup.sh &>/dev/null 6.Rsync备份思考1.要备份什么?WEB APP部署程序配置文件 MYSQL数据库部署程序及其配置文件 MySQL数据文件 MySQK的binlog日志、慢查询日志、错误日志 系统的安全日志、内核的日志、sudo日志、rsyslog日志 应用程序日志 access.log error.log 静态数据文件 /usr/local/nginx /opt/mysql /etc/php /etc/my.cnf /soft/webapp/nginx /soft/webapp/apache /soft/webapp/tomcat /soft/scripts/crond_nginx.sh /soft/scripts/crond_rsync.sh /data/mysql/mysql3306/{data,logs,conf,tmp} /data/mysql/mysql3307/{data,logs,conf,tmp} /data/mysql/mysql3308/{data,logs,conf,tmp} 思考2:如何备份?1.根据时间维护划分 实时备份(同步备份) 非实时备份(异步备份) 2.根据地域划分 同IDC备份部署 跨IDC备份部署 本地->云主机->异地主机 备份3:备份可恢复性?时间成本 维护成本 备份4:备份策略备份保留时间? 1个月, 1年或永久? 备份恢复校验 1个月模拟一次,半年演练一次? 这里牵扯到备份有效性和备份时效性的一个校验。 万一备份服务器挂了怎么办? 备份之后是否还有备份 转 xuliangwei.com
2023年11月03日
38 阅读
0 评论
0 点赞
2023-11-03
CentOS7系统特性
Centos7新的主流操作系统, Centos7带来了很多的功能改变。本地虚拟机环境VmWare、KVM、Virtaulbox 操作系统 Centos6.9 Centos7.5 系统基础服务变化 操作 Centos6 Centos7 对比 自动补全 只支持命令、文件名 支持命令、选项、文件名 文件系统 ext4 xfs 随机读写更快 repo仓库 yum yum-config-manager 添加仓库便捷 修改主机名 /etc/sysconfig/network /etc/hostname hostnamectl 修改时区 /etc/sysconfig/clock timedatectl set-timezone 更方便 修改字符集 /etc/sysconfig/il8n /etc/locale.conf localectl 防火墙 iptables firewalld 服务管理 System V init systemd 时间同步服务 ntp chrony 1.系统主机名 操作 centos6 cetos7 临时修改 hostname hostname 永久修改 /etc/sysconfig/network /etc/hostname hostnamectl set-hostname #centos7永久修改 2.系统文件目录结构 centos6 cetos7 bin bin -> usr/bin sbin sbin -> usr/sbin lib lib -> usr/lib 3.网络接口变化net.ifnames 基于固件、拓扑、进行自动分配网卡名称,缺点比eth0、更难读,如ens32biosdevname 根据戴尔服务器系统的BIOS提供的信息对网络接口进行重命名,如em1默认命名规则 eth0 eth1 eth2biosdevname em1 em2 em3net.ifnames ens33 ens34 ens35centos6与centos7使用网络接口规则 Centos6 Centos7 net.ifnames=0 biosdevname=1 默认命名规则 net.ifnames=1 biosdevname=1 默认命名规则 centos7使用ip命令查看ip地址方法1.查看ip地址信息 ip addr2.添加多个IP地址 ip addr add 192.168.56.200/24 dev eth0:13.控制网络接口 ip link set dev eth0 downSystemd服务概述Systemd初始Systemd是Centos7新采用的一套管理体系,可以实现启动及进程服务管理等,对比Centos6系统之前所采用sysVini体系,带来了很多变化。Centos7支持并行启动,显著提高开机启动效率(测试6与7区别)Centos7关机只关闭正在运行的服务,Centos6关机会从头关到尾Centos7服务的启动与停止不在需要init.d下的脚本 Centos6 Centos7 启动项管理 chkconfig systemctl 服务管理 service systemctl 系统启动级别 init systemctl 日志管理 syslog Systemd-journal systemd启动级别在Centos7中没有级别的概念,而是使用target目标来涵盖启动级别的概念设置系统启动运行级别 SysVinit Systemd 关闭系统 0 runlevel0.target,poweroff.target 单用户模式 1,s,single runlevel1.target,rescue.target 多用户模式 2 runlevel2.target,multi-user.target 多用户带网络模式 3 runlevel3.target,multi-user.target 多用户图形化模式 5 runlevel5.target,graphical-user.target 重启操作系统 6 runlevel6.target,reboot.target 设置系统启动运行级别 Centos6 Centos7 设置启动级别 init3 systemctl set-default multi-user.target 获取当前启动级别 runlevel systemctl get-default systemd服务管理命令 选项(非必须) 执行命令 单元名称(非必须)systemctl [OPTIONS...]COMMAND[NAME...] 操作 Centos6 Centos7 启动服务 /etc/init.d/crond start systemctl start crond 停止服务 /etc/init.d/crond stop systemctl stop crond 重启服务 /etc/init.d/crond restart systemctl restart crond 查看状态 /etc/init.d/crond status systemctl status crond 开机启动 chkconfig --level 35 crond on systemctl enable crond 开机禁用 chkconfig crond off systemctl disable crond 禁止运行 systemctl umask crond Centos7系统优化 yum install net-tools vim tree htop iftop \ iotop lrzsz sl wget unzip telnet nmap nc psmisc \ dos2unix bash-completion iotop iftop sysstat -y systemctl disable firewalld systemctl stop firewalld systemctl status firewalld sed -ri 's#(^SELINUX=).*#\1disabled#g' /etc/selinux/config sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config vim /etc/selinux/config setenforce 0 echo '* - nofile 65535' >> /etc/security/limits.conf 核心架构地址规划网站核心架构所需虚拟机IP及主机名规划: wanip lanip hostname 10.0.0.5 172.16.1.5 lb01 10.0.0.6 172.16.1.6 lb02 10.0.0.7 172.16.1.7 web01 10.0.0.8 172.16.1.8 web02 10.0.0.9 172.16.1.9 web03 10.0.0.31 172.16.1.31 nfs01 10.0.0.41 172.16.1.41 backup 10.0.0.51 172.16.1.51 db01 10.0.0.61 172.16.1.61 m01 10.0.0.71 172.16.1.71 zabbix 网站核心架构vmware和xshell软件里虚拟机名字规划: 01-10.0.0.5-keepalived-lb01 02-10.0.0.6-keepalived-lb02 03-10.0.0.7-nginx-web01 04-10.0.0.8-nginx-web02 05-10.0.0.9-nginx-web03 06-10.0.0.31-nfsfilesystem-nfs01 07-10.0.0.41-rsync-backup 08-10.0.0.51-mysql-db01 09-10.0.0.61-manage-m01 10-10.0.0.71-zabbix 转 xuliangwei.com
2023年11月03日
36 阅读
0 评论
0 点赞
2023-11-03
Firewalld防火墙
1.防火墙基本概述RHEL/CentOS 7系统中集成了多款防火墙管理工具,其中firewalld(Dynamic Firewall Manager of Linux systems, Linux系统的动态防火墙管理器)服务是默认的防火墙配置管理工具,它拥有基于CLI(命令行界面)和基于GUI(图形用户界面)的两种管理方式。相较于传统的Iptables防火墙管理工具firewalld支持动态更新,并加入了区域zone的概念。简单来说,区域就是firewalld预先准备了几套防火墙策略集合(策略模板),用户可以根据生产场景的不同而选择合适的策略集合,从而实现防火墙策略之间的快速切换。注意一个zone区域仅能绑定一个网卡, 设定不同的匹配规则一个zone区域又可以针对不同的源地址设定不同的规则Firewalld相关配置文件默认定义的模板配置文件/usr/lib/firewalld存储规则配置文件 /etc/firewalld/2.防火墙区域概述 区域 默认规则策略 trusted 允许所有的数据包流入与流出 home 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、mdns、ipp-client、amba-client与dhcpv6-client服务相关,则允许流量 internal 等同于home区域 work 拒绝流入的流量,除非与流出的流量数相关;而如果流量与ssh、ipp-client与dhcpv6-client服务相关,则允许流量 public 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、dhcpv6-client服务相关,则允许流量 external 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量 dmz 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量 block 拒绝流入的流量,除非与流出的流量相关 drop 拒绝流入的流量,除非与流出的流量相关 3.防火墙基本指令参数为了能够使用firwalld服务和相关工具去管理防火墙,必须启动firwalld服务,并且需要禁用以前旧防火墙相关服务, firewall-config(图形界面), firewall-cmdfirewalld的规则分两种状态:runtime运行时: 修改规则马上生效,但是临时生效 [不建议]permanent持久配置: 修改后需要reload重载才会生效 [强烈推荐]firewall-cmd命令分类 参数 作用 zone区域相关指令 --get-default-zone 查询默认的区域名称 --set-default-zone=<区域名称> 设置默认的区域,使其永久生效 --get-active-zones 显示当前正在使用的区域与网卡名称 --get-zones 显示总共可用的区域 --new-zone= 新增区域 services服务相关指令 --get-services 显示预先定义的服务 --add-service=<服务名> 设置默认区域允许该服务的流量 --remove-service=<服务名> 设置默认区域不再允许该服务的流量 Port端口相关指令 --add-port=<端口号/协议> 设置默认区域允许该端口的流量 --remove-port=<端口号/协议> 设置默认区域不再允许该端口的流量 Interface网卡相关指令 --add-interface=<网卡名称> 将源自该网卡的所有流量都导向某个指定区域 --change-interface=<网卡名称> 将某个网卡与区域进行关联 其他相关指令 --list-all 显示当前区域的网卡配置参数、资源、端口以及服务等信息 --reload 让“永久生效”的配置规则立即生效,并覆盖当前的配置规则 4.防火墙区域配置策略1.为了能正常使用firwalld服务和相关工具去管理防火墙,必须启动firwalld服务, 同时关闭以前旧防火墙相关服务//禁用旧版防火墙服务 [root@xuliangwei ~] [root@xuliangwei ~] //启动firewalld防火墙, 并加入开机自启动服务 [root@xuliangwei ~] [root@xuliangwei ~] //备份firewalld相关配置文件(重要) [root@Firewalld ~] 2.zone区域相关指令 [root@xuliangwei ~]# firewall-cmd --get-default-zone public [root@xuliangwei ~]# firewall-cmd --set-default-zone=work [root@xuliangwei ~]# firewall-cmd --permanent --add-service=http --zone=work [root@xuliangwei ~]# firewall-cmd --change-interface=eth0 --zone=work 3.将某网段IP路由至home区域, 由该区域规则进行匹配决定是否放行[root@Firewalld ~] [root@Firewalld ~] success [root@Firewalld ~] success //检查相关规则 [root@Firewalld ~] home sources: 192.168.56.0/24 public interfaces: eth0 br0 vnet0 4.查询firewald相关规则 [root@Firewalld ~]# firewall-cmd --get-active-zones home sources: 192.168.69.0/24 work interfaces: eth0 br0 vnet0 [root@xuliangwei ~]# firewall-cmd --list-all --zone=work work (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: ssh dhcpv6-client 'http' ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: 5.查询public区域是否允许请求SSH HTTPS协议的流量[root@Firewalld ~] yes [root@Firewalld ~] no 5.防火墙服务访问策略1.配置防火墙, 允许请求http https协议的流量设置为永久允许,并立即生效[root@Firewalld ~] success //重启加载生效 [root@Firewalld ~] success //检查相关配置 [root@Firewalld ~] ssh dhcpv6-client 'http https' 2.配置防火墙, 允许请求php-fpm服务的流量设置为永久允许,并立即生效[root@Firewalld ~]# cd /usr/lib/firewalld/services/ [root@Firewalld services]# cp http.xml php-fpm.xml [root@Firewalld services]# cat php-fpm.xml <?xml version="1.0" encoding="utf-8"?> <service> <short>PHP-FPM</short> <description> php-fpm </description> <port protocol="tcp" port="9000"/> </service> [root@Firewalld ~]# firewall-cmd --permanent --add-service=php-fpm [root@Firewalld ~]# firewall-cmd --list-services ssh dhcpv6-client 'http https php-fpm' [root@Firewalld ~]# yum install php-fpm -y [root@Firewalld ~]# systemctl start php-fpm ~ telnet 192.168.56.11 9000 Trying 192.168.56.11... Connected to 192.168.56.11. Escape character is '^]'. 3.配置防火墙, 请求https协议的流量设置为永久拒绝,并立即生效[root@Firewalld ~] success [root@Firewalld ~] success //检查当前活动服务 [root@Firewalld ~] ssh dhcpv6-client http 6.防火墙端口访问策略1.配置防火墙, 访问8080/tcp 8080/udp端口的流量策略设置为永久允许, 并立即生效 [root@Firewalld ~]# firewall-cmd --permanent --add-port=8080/udp --add-port=8080/tcp [root@Firewalld ~]# firewall-cmd --reload success [root@Firewalld ~]# firewall-cmd --list-ports 8080/tcp 8080/udp 2.配置防火墙, 访问8080/udp的端口流量设置为永久拒绝,并立即生效[root@Firewalld ~] success [root@Firewalld ~] success 8080/tcp 7.防火墙端口转发策略端口转发是指传统的目标地址映射,实现外网访问内网资源流量转发命令格式为firewall-cmd --permanent --zone=<区域> --add-forward-port=port=<源端口号>:proto=<协议>:toport=<目标端口号>:toaddr=<目标IP地址>1.把访问本机555/tcp端口的流量转发到22/tcp端口,要求当前和长期有效[root@Firewalld ~] success [root@Firewalld ~] success 2.把访问本机6666/tcp端口的流量转发到80/tcp端口,要求当前和长期有效[root@Firewalld ~] [root@Firewalld ~] success [root@Firewalld ~] success 3.移除本地555/tcp端口转发策略, 要求当前和长期有效[root@Firewalld ~] success [root@Firewalld ~] success 8.防火墙富规则策略firewalld中的富规则表示更细致、更详细的防火墙策略配置,它可以针对系统服务、端口号、源地址和目标地址等诸多信息进行更有针对性的策略配置, 优先级在所有的防火墙策略中也是最高的。1.富规则帮助手册man firewall-cmd man firewalld.richlanguage rule [source] [destination] service|port|protocol|icmp-block|masquerade|forward-port [log] [audit] [accept|reject|drop] rule [family="ipv4|ipv6"] source address="address[/mask]" [invert="True"] destination address="address[/mask]" invert="True" service name="service name" port port="port value" protocol="tcp|udp" protocol value="protocol value" forward-port port="port value" protocol="tcp|udp" to-port="port value" to-addr="address" log [prefix="prefix text"] [level="log level"] [limit value="rate/duration"] accept | reject [type="reject type"] | drop //区里的富规则按先后顺序匹配,按先匹配到的规则生效。 1.允许来自于192.168.69.113/32主机请求8081-8083端口, 当前和永久生效[root@Firewalld ~] --add-rich-rule='rule family=ipv4 source address=192.168.69.113/32 port port=8081-8083 protocol=tcp accept' success [root@Firewalld ~] success 2.拒绝来自192.168.69.113/32主机所有的请求, 当前和永久生效[root@Firewalld ~] --add-rich-rule='rule family=ipv4 source address=192.168.69.113/32 reject' success [root@Firewalld ~] success 3.拒绝来自于192.168.69.0/24网段请求ssh服务, 当前和永久生效[root@Firewalld ~] --add-rich-rule='rule family=ipv4 source address=192.168.69.0/24 service name=ssh reject' success [root@Firewalld ~] success 4.限制所有请求ftp服务流量, 每分钟1个并发, 当前和永久生效[root@Firewalld ~] --add-rich-rule='rule service name=ftp log prefix="ftp " level=notice limit value=1/m accept' success [root@Firewalld ~] success //开启多个ftp连接, 查看日志 [root@Firewalld ~] Apr 21 11:16:04 kvm-node1 kernel: ftp IN=br0 OUT= MAC=00:0c:29:72:6a96:00:50:56:c0:00:02:08:00 SRC=192.168.56.1 DST=192.168.56.11 LEN=64 TOS=0x00 PREC=0x00 TTL=64 ID=21160 DF PROTO=TCP SPT=57050 DPT=21 WINDOW=65535 RES=0x00 SYN URGP=0 5.防止规则设定错误导致网络连接断开,用于调试,规则在300秒后失效[root@Firewalld ~] --add-rich-rule='rule family=ipv4 source address=192.168.69.113/32 service name=ssh reject' --timeout=300 6.将192.168.69.113/32主机访问443/tcp的数据包转发到本机的22/tcp端口, 当前和永久生效//端口转发规则 [root@Firewalld ~] --add-rich-rule='rule family=ipv4 source address=192.168.56.1/32 forward-port port=443 protocol=tcp to-port=22' success [root@Firewalld ~] success 7.允许192.168.69.113/32访问httpd, 并且记录日志, 日志级别为notice,日志前缀为NEW HTTP, 限制每秒最多3个并发, 当前和永久生效 [root@Firewalld soft] --add-rich-rule='rule family=ipv4 source address=192.168.69.113/32 service name=http,https log level=notice prefix="New Http " limit value="3/s" accept' success [root@Firewalld soft] success 转 xuliangwei.com
2023年11月03日
48 阅读
0 评论
0 点赞
2023-11-01
cron 命令
1.Crond计划任务概述什么是计划任务,计划任务类似于我们平时生活中的闹钟。在Linux系统的计划任务服务crond可以满足周期性执行任务的需求。crond进程每分钟会处理一次计划任务, 计划任务主要是做一些周期性的任务目前最主要的用途是定时备份数据Schedule one-time tasks with at. 一次性调度执行 at Schedule recurring jobs with cron. 循环调度执行 cron Schedule recurring system jobs. 所有计划任务执行中的输出都会以邮件的方式发送给指定用户, 除非重定向 //循环调度执行cron 进程每分钟会处理一次计划任务 [root@xuliangwei ~] [root@xuliangwei ~] root 1201 0.0 0.0 126264 1640 ? Ss 11:15 0:00 /usr/sbin/crond -n 计划任务分为以下两种情况:1.系统级别的定时任务:清理系统缓存临时文件清理系统信息采集日志文件切割2.用户级别的定时任务:定时同步互联网时间定时备份系统配置文件定时备份数据库文件2.crond配置文件详解文件 说明 /etc/crontab /etc/cron.deny /var/spool/cron 3.crond计划任务管理crond任务管理参数 含义 指定示例 [root@xuliangwei ~] -e 编辑crontab文件内容 crontab -e -l 查看crontab文件内容 crontab -l -r 删除crontab文件内容 crontab -r -u 管理其他用户的计划任务 crontab -u xuliangwei -l 注意: crontab {-l -e}实际上就是在操作/var/spool/cron/username crond时间含义# 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 # | | | | | # * * * * * command to be executed crond编写示例00 02 * * * ls //每天2:00整 00 02 1 * * ls //每月1号2:00整 00 02 14 2 * ls //每年2月14号2:00整 00 02 * * 7 ls //每周日2:00整 00 02 * 6 5 ls //每年6月的周五2:00整 00 02 14 * 7 ls //每月14号2:00整或每周日2:00整,这两个时间都执行 00 02 14 2 7 ls //每年 2 月 14 号 2:00 整 或者 每周日 2:00 整,这两个时间都执行 00 02 * * * ls //每天2:00整 * 02 * * * ls //每天2:00中的每一分钟 (逻辑错误) * * * * * ls //每分钟执行 ls * * 14 2 * ls //2月14号的每分钟(逻辑错误) */5 * * * * ls //每隔5分钟 00 02 * 1,5,8 * ls //每年1,5,8月的每天2:00整 00 02 1-8 * * ls //每月1到8号的2:00整 crond书写规范 [root@xuliangwei ~]# crontab -l ##time sync by xuliangwei 2018-00-00 */5 * * * * /usr/sbin/ntpdate time.windows.com &>/dev/null [root@xuliangwei ~]# crontab -l ##backup www to /backup xuliangwei 2018-00-00 00 01 * * * /bin/sh /soft/scripts/www_backup.sh &>/dev/null [root@xuliangwei ~]# crontab -l ####backup www to /backup xuliangwei 2018-00-00 00 01 * * * /bin/sh /soft/scripts/www_backup.sh &>/dev/null 4.crond配置编写实例[root@xuliangwei ~]# crontab -e #每天凌晨切割nginx日志 00 00 * * * /bin/sh -x /soft/scripts/cut_nginx.sh &> /soft/scripts/log/nginx.log #每天5点备份数据库 00 05 * * * /bin/sh -x /soft/scripts/dump_sql.sh &>/soft/scripts/log/mysql.log #每5分钟检测数据库是否正常 */5 * * * * /bin/sh /soft/scripts/start_mysql.sh &>/dev/null 1.我们所有的crond服务是运行的程序。而crontab命令用户用来设置定时规则的命令。 2.crond服务是企业生产工作中常用的重要服务,at很少使用,可以忽略。 3.几乎每个服务器都会用到crond服务。 5.crond计划任务调试1.调整任务每分钟执行, 检测是否是否正常, 有些任务不要频繁执行2.调整系统时间然后在检测任务, 生产不建议直接使用此方式3.执行脚本, 将脚本执行输出写入指定日志文件, 观察日志内容是否正常4.注意一些任务命令带来的问题echo “xuliangwei” >>/tmp/xlw.log &>/dev/null5.命令使用绝对路径, 防止无法找到命令导致定时任务执行故障6.查看/var/log/cron日志进行调试建议: 将需要定期执行的任务写入脚本中, 建立/soft/scripts目录统一存放脚本, 脚本中命令必须使用绝对路径,手动执行脚本检测输出是否正常, 然后将脚本加入计划任务测试, 测试后无问题将脚本输出写入对应的日志文件中即可。 1.手动执行保留执行命令的正确结果 2.编写脚本 脚本需要统一路径/soft/scripts 脚本开头建议填写注释信息, 包括执行时间、周期、任务 脚本内容复制执行成功的命令至脚本文件中(减少每个环节出错几率) 脚本内容尽可能的优化, 使用一些变量或使用简单的判断语句 脚本执行的输出信息不要随意打印, 可以重定向至其他位置保留或丢入黑洞 3.执行脚本 使用bash执行, 防止脚本没有增加执行权限(/usr/bin/bash) 执行命令以及脚本成功后并复制该命令 4.编写计划任务 加上必要的注释信息, 人、时间、任务 设定计划任务执行的周期 粘贴执行脚本的命令(不要手敲) 5.调试计划任务 增加任务频率测试、调整系统时间测试(不能用于生产) 检查环境变量问题、检查crond服务产生日志进行排查 转 xuliangwei.com
2023年11月01日
41 阅读
0 评论
0 点赞
2023-11-01
find 命令
文件查找概述Linux系统中的find命令在查找文件时非常有用而且方便。它可以根据不同的条件来进行查找文件:例如权限、拥有者、修改日期/时间、文件大小等等。 同时find命令是Linux下必须掌握的。find 命令的基本语法如下 命令 路径 选项 表达式 动作 find [path...] [options] [expression] [action] 查找 地区 犯罪嫌疑人 性别男25-30岁 枪决行动 find名称查找 touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1} [root@xuliangwei ~]# find /etc -name "ifcfg-eth1" [root@xuliangwei ~]# find /etc -iname "ifcfg-eth1" [root@xuliangwei ~]# find /etc/ -name "ifcfg-eth*" [root@xuliangwei ~]# find /etc -iname "ifcfg-eth*" find大小查找 [root@xuliangwei ~]# find /etc -size +5M [root@xuliangwei ~]# find /etc -size 5M [root@xuliangwei ~]# find /etc -size -5M find时间查找 [root@xuliangwei ~]# for i in {01..28};do date -s 201802$i && touch file-$i;done [root@xuliangwei ~]# find ./ -iname "file-*" -mtime +7 [root@xuliangwei ~]# find ./ -iname "file-*" -mtime -7 [root@xuliangwei ~]# find ./ -iname "file-*" -mtime 7 find /backup/ -iname "*.bak" -mtime +7 -delete find /backup/ -iname "*.bak" -mtime +90 -delete find用户查找//查找属主是jack [root@xuliangwei ~] //查找属组是admin [root@xuliangwei ~] //查找属主是jack, 属组是admin [root@xuliangwei ~] //查找属主是jack, 并且属组是admin [root@xuliangwei ~] //查找属主是jack, 或者属组是admin [root@xuliangwei ~] //查找没有属主 [root@xuliangwei ~] //查找没有属组 [root@xuliangwei ~] //查找没有属主或属组 [root@xuliangwei ~] find类型查找 [root@xuliangwei ~]# find /dev -type f [root@xuliangwei ~]# find /dev -type d [root@xuliangwei ~]# find /dev -type l [root@xuliangwei ~]# find /dev -type b [root@xuliangwei ~]# find /dev -type c [root@xuliangwei ~]# find /dev -type s [root@xuliangwei ~]# find /dev -type p find权限查找 [root@xuliangwei ~]# find . -perm 644 -ls [root@xuliangwei ~]# find /home -perm -324 [root@xuliangwei ~]# find . -perm -222 -ls [root@xuliangwei ~]# find /home -perm /442 [root@xuliangwei ~]# find /usr/sbin -perm -4000 -ls [root@xuliangwei ~]# find /usr/sbin -perm -2000 -ls [root@xuliangwei ~]# find /usr/sbin -perm -1000 -ls find处理动作当查找到一个文件后, 需要对文件进行如何处理, 默认动作 -print-print -ls -delete -exec -ok [root@xuliangwei ~]# find /etc -name "ifcfg*" [root@xuliangwei ~]# find /etc -name "ifcfg*" -print [root@xuliangwei ~]# find /etc -name "ifcfg*" -ls [root@xuliangwei ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; [root@xuliangwei ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; [root@xuliangwei ~]# find /etc -name "ifcfg*" -exec rm -f {} \; [root@xuliangwei ~]# find /etc -name "ifcfg*" -delete find结合xargs//xargs将查找到结果一个一个的处理 [root@xuliangwei ~] [root@xuliangwei ~] [root@xuliangwei ~] 转 xuliangwei.com
2023年11月01日
28 阅读
0 评论
0 点赞
1
2
3
...
5