NGINX 实战操作(yarn-proxy)

一、概述

NGINX 代理是非常常见的,内网生成环境一般不让在本机直接访问,这时候就得利用代理,但是一般代理就得脱敏或去掉页面上的高危操作。这里就拿 YARN 代理来讲解。这里也会涉及到 subs_filter 讲解,如果对 subs_filter 不理解的小伙伴,可以先查阅我这篇文章:NGINX sub_filter和subs_filter 指令讲解

二、部署 Hadoop 环境

Hadoop 的部署可以参考我之前的以下几篇文章:

三、部署 yarn-proxy on docker

这里部署的是基于GitHub上的一个项目:github.com/HBigdata/ya...

1)部署 docker

bash 复制代码
# 安装yum-config-manager配置工具
yum -y install yum-utils

# 建议使用阿里云yum源:(推荐)
#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装docker-ce版本
yum install -y docker-ce
# 启动并开机启动
systemctl enable --now docker
docker --version

2)部署 docker-compose

bash 复制代码
curl -SL https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose
docker-compose --version

3)部署 dnsmasq

  1. dnsmasq 先去解析 /etc/hosts 文件。
  2. 再去解析 /etc/dnsmasq.d/ 下的 *.conf 文件,并且这些文件的优先级要高于 /etc/dnsmasq.conf
  3. 我们自定义的 resolv.dnsmasq.conf中的 DNS 也被称为上游 DNS ,这是最后去查询解析的。
bash 复制代码
yum install -y dnsmasq
systemctl start dnsmasq.service

hadoop 相关的主机名映射都提前配置到 /etc/hosts 文件中。

想了解更多,可以参考我这篇文章:dnsmasq( DNS和DHCP)服务

4)下载 yarn-proxy

bash 复制代码
git clone https://github.com/HBigdata/yarn-reverse-proxy.git
cd yarn-reverse-proxy-master
cp docker-compose.example.yml docker-compose.yml

5)修改配置

bash 复制代码
version: '3'
services:
  app:
    build: .
    #expose:
    #  - 80
    ## WARNING: uncomment the following only if you are
    ## not directly exposing a non-kerberized cluster
    ## to the Internet
    # ports:
    #   - "5555:80"
    environment:
      TZ : 'Asia/Shanghai'
    dns:
      - localhost
    ports:
      - "80:80"
    # 指定 DNS 服务
    
    environment:
      # Because your primary resource-manager is at /rm1
      # access your main status page at $SERVER_URL_BASE/rm1
      SERVERS: "hostname-rm-110:8088@rm110
                hostname-rm-111:8088@rm111
                hostname-nm-112:8042@nm112
                hostname-nm-113:8042@nm113
                hostname-nm-114:8042@nm114
                hostname-nm-115:8042@nm115
                hostname-nm-116:8042@nm116
                hostname-nm-117:8042@nm117
                hostname-nm-118:8042@nm118
                hostname-jn-119:19888@jh119"
      RESOLVER: YOUR_NETWORKS_DNS_SERVER # or 127.0.0.11 for the system resolver
      SERVER_URL_BASE: http://REVERSE_PROXY_HOSTNAME:80

需要修改的地方:

  • SERVERSREVERSE_PROXY_HOSTNAME
  • dns 地址记得更好成自己的 dns 机器。

6)开始部署

bash 复制代码
docker-compose build
docker-compose up -d

7)通过 subs_filter 进行改进

如果nm节点太多了,都遍历代理的话,就导致超过NGINX配置的长度的,太长配置也影响阅读,所以这里就通过 nginx subs_filter 进行改进。

subs_filter 的部署与用法可以参考我这篇文章:NGINX sub_filter和subs_filter 指令讲解

通过正则表达式修改 run.sh

bash 复制代码
# 将这段内容替换成正则
    cat <<EOF
    # Rewrite references without the scheme, e.g. //google.com
    sub_filter 'src="//$REWRITE_NAME:$REWRITE_PORT' 'src="$SERVER_URL_BASE/$REWRITE_ALIAS';
    sub_filter 'href="//$REWRITE_NAME:$REWRITE_PORT' 'href="$SERVER_URL_BASE/$REWRITE_ALIAS';
    sub_filter 'href=\'//$REWRITE_NAME:$REWRITE_PORT' 'href=\'$SERVER_URL_BASE/$REWRITE_ALIAS';

    # Rewrite all absolute references, regardless of where
    # they appear in the page
    sub_filter 'http://$REWRITE_NAME:$REWRITE_PORT/' '$SERVER_URL_BASE/$REWRITE_ALIAS/';
    sub_filter 'http://$REWRITE_NAME:$REWRITE_PORT' '$SERVER_URL_BASE/$REWRITE_ALIAS';

    proxy_redirect http://$REWRITE_NAME:$REWRITE_PORT/ $SERVER_URL_BASE/$REWRITE_ALIAS/;
EOF


# 示例替换
    cat <<EOF
    # Rewrite references without the scheme, e.g. //google.com
    sub_filter 'src="//local-168-(\w+)-(\d+):$REWRITE_PORT' 'src="$SERVER_URL_BASE/\$1\$2' g r;
    sub_filter 'href="//local-168-(\w+)-(\d+):$REWRITE_PORT' 'href="$SERVER_URL_BASE/\$1\$2' g r;
    sub_filter 'href=\'//local-168-(\w+)-(\d+):$REWRITE_PORT' 'href=\'$SERVER_URL_BASE/\$1\$2' g r;

    # Rewrite all absolute references, regardless of where
    # they appear in the page
    sub_filter 'http://local-168-(\w+)-(\d+):$REWRITE_PORT/' '$SERVER_URL_BASE/\$1\$2/' g r;
    sub_filter 'http://local-168-(\w+)-(\d+):$REWRITE_PORT' '$SERVER_URL_BASE/\$1\$2' g r;

    proxy_redirect http://local-168-(\w+)-(\d+):$REWRITE_PORT/ $SERVER_URL_BASE/\$1\$2/ g r;
EOF

四、部署 yarn-proxy on k8s

yarn-proxy on k8s 部署跟 docker-compose 差不多,这里就不再重复讲解了,有任何疑问欢迎给我留言或私信。


NGINX 实战操作(yarn-proxy)讲解就先到这里了,有任何疑问也可关注我公众号:大数据与云原生技术分享,进行技术交流,如本篇文章对您有所帮助,麻烦帮忙一键三连(点赞、转发、收藏)~

相关推荐
Jackilina_Stone1 小时前
一个高性能的HTTP和反向代理服务器:Nginx
nginx·http·ai 推理·nv
IT成长日记5 小时前
【Nginx开荒攻略】静态文件服务深度解析:MIME类型映射与优化实战
linux·运维·服务器·nginx·mime
zz-zjx8 小时前
Web接入层的“铁三角”---防盗链、反向代理,负载均衡(nginx)
前端·nginx·负载均衡
珊瑚礁的猪猪侠9 小时前
Nginx从入门到精通:小白实战教程
运维·nginx
邂逅星河浪漫9 小时前
【DockerFile+Nginx+DockerCompose】前后端分离式项目部署(docker容器化方式)
nginx·docker·centos·部署·docker-compose·dockerfile·容器化部署
天空之外13611 小时前
nginx xxs漏铜修复、nginx 域名配置、nginx https证书配置、Http不安全处理方法
运维·nginx
神秘人X70711 小时前
Nginx 访问控制、用户认证与 HTTPS 配置指南
nginx·https
失因13 小时前
Nginx 反向代理、负载均衡与 Keepalived 高可用
运维·nginx·负载均衡
码界奇点14 小时前
Nginx 502 Bad Gateway从 upstream 日志到 FastCGI 超时深度复盘
运维·nginx·阿里云·性能优化·gateway
问道飞鱼18 小时前
【服务器知识】HTTP 请求头信息及其用途详细说明
运维·服务器·nginx·http·http头信息