NGINX 实战操作(yarn-proxy)

一、概述

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

在这里插入图片描述

二、部署 Hadoop 环境

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

三、部署 yarn-proxy on docker

这里部署的是基于GitHub上的一个项目:https://github.com/HBigdata/yarn-reverse-proxy

1)部署 docker

# 安装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

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 ,这是最后去查询解析的。
yum install -y dnsmasq
systemctl start dnsmasq.service

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

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

4)下载 yarn-proxy

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

5)修改配置

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)开始部署

docker-compose build
docker-compose up -d

7)通过 subs_filter 进行改进

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

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

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

# 将这段内容替换成正则
    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)讲解就先到这里了,有任何疑问也可关注我公众号:大数据与云原生技术分享,进行技术交流,如本篇文章对您有所帮助,麻烦帮忙一键三连(点赞、转发、收藏)~

posted @ 2024-01-28 17:36  大数据老司机  阅读(100)  评论(0编辑  收藏  举报