首页 > 其他分享 >单节点多集群流量转发方法

单节点多集群流量转发方法

时间:2024-02-01 17:07:19浏览次数:24  
标签:http nginx 转发 server ssl 集群 https example1 节点

引言

在实验环境(裸机)中部署多个 有雀CRC集群(CRC介绍请看 容器云平台本地集群UCCPS CRC介绍 ),导致集群间抢占宿主机80、443端口情况,本文用外部负载均衡方案解决端口冲突问题。 当然本方法也对裸机搭建的Kubernete的Ingress 或者gateway API 也有效。

本方案适用于 根据域名转发流量的场景,但有一些小问题无法避免(后面会提到),因此比较适合测试场景。


步骤

环境预设:

设: 有A、B两个单节点集群在同一个宿主机C上。且A、B均用NAT 网络搭建,但需要两个集群的80、443都映射(连接到宿主机80、443)

A集群基域: apps.example1.com

  节点IP: 192.168.2.10 

 B集群基域: apps.example2.com

  节点IP: 192.168.3.10 

宿主机: 10.10.10.12

Nginx 反代方案

  • 省略 Nginx 介绍.....
  • 安装Nginx 包或者用docker镜像均可,本文仅涉及配置。
  1. 配置 nginx.conf (本文件基本可以保持默认)
user nginx;
worker_processes 8; # 如果负载不是特别大,此处可是设置4或者8。
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# 导入本地模块
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
	# 日志格式可以默认,或者根据nginx文档修改。
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
	
    # 以下就是根据需要开启或者设置参数。
    # 可保持默认
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
	
    # 导入其余 配置文件。
    include /etc/nginx/conf.d/*.conf;

}
  1. 生成https(SSL)证书

在此借用rancher的脚本 

利用脚本生成证书 (我是放在 /etc/nginx/certs/ 下,根据需求调整)

# 仅对单个节点生成证书
bash create_self-signed-cert.sh \
	--ssl-domain=apps.example1.com \
  --ssl-trusted-ip=192.168.2.10
  
# 对两个集群生成同一套证书
bash create_self-signed-cert.sh \
	--ssl-domain=apps.example1.com \
  --ssl-trusted-domain=apps.example2.com \
  --ssl-trusted-ip=192.168.2.10,192.168.3.10

# 测试环境为了方便两套集群使用同一份证书。
  1. 写反代配置。
  1. 集群A /etc/nginx/conf.d/crc_example1.conf
  2. 集群B 也是一样的,仅需替换 IP和 基域即可。
# 设置上游worker节点
upstream example1_workers {
    least_conn;
    server 192.168.2.10;
    # 多节点可以设置多个后端server
}

# 设置http强制转用https
server {
        listen       80;
        server_name  *.apps.example1.com;
        rewrite ^ https://$http_host$request_uri? permanent;
}

# 设置443端口转发
server {
    listen              443 ssl;
    server_name         *.apps.example1.com;
    # 刚才生成的证书。
    ssl_certificate     /etc/nginx/certs/tls.crt;
    ssl_certificate_key /etc/nginx/certs/tls.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    # dns解析器
    resolver 192.168.2.10;

    proxy_set_header    Host $host;
    proxy_set_header    Cookie $http_cookie;
    proxy_set_header    X-Real-IP $remote_addr;
    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_ssl_server_name on;
    proxy_ssl_name      $host;
    proxy_ssl_protocols TLSv1.2 TLSv1.3;

    location /  {
        # 转发
        proxy_pass  https://example1_workers;
    }
}
  1. 启动 nginx 进行测试。
  1. 查看https 地址时会出现 证书不安全提示忽略即可,如有强迫症 可以拷贝/etc/nginx/certs/ 下生成的tls.crt 对客户端进行授权。

Haproxy 方案

haproxy 介绍省略.....

按照Haproxy包或者docker镜像均可。

  1. 先生成ssl证书,与nginx类似。
  2. 生成pem证书
# 合并两个证书即可
cat tls.crt tls.key | tee tls.pem
  1. 从集群获取ingress 的证书
oc get secret -n openshift-ingress router-certs-default -ojson | jq -r .data[] | base64 -d > /etc/haproxy/uccps.pem
  1. haproxy配置
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    stats socket /var/lib/haproxy/stats

    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM

defaults
    mode                    http
    log                     global
    option                  dontlognull
    option                  http-server-close
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
    mode http

frontend http
    bind *:80
    acl  example1_http hdr_reg(host) -i -n -m sub apps\.example1\.com
    acl  example2_http hdr_reg(host) -i -n -m sub apps\.example2\.com

    use_backend example1_http if example1_http
    use_backend example2_http if example2_http

frontend https
    bind *:443 ssl crt /etc/haproxy/tls.pem
    acl  example1_https hdr_reg(host) -i -n -m sub apps\.example1\.com
    acl  example2_https hdr_reg(host) -i -n -m sub apps\.example2\.com

    use_backend example1_https if example1_https
    use_backend example2_https if example2_https

backend example1_http
    balance     leastconn
    server      cluster_1 192.168.2.10:80 check

backend example2_http
    balance     leastconn
    server      cluster_2 192.168.3.10:80 check

backend example1_https
    balance     leastconn
    server      cluster_1 192.168.2.10:443 check /etc/haproxy/uccps.pem ssl verify required

backend example2_https
    balance     leastconn
    server      cluster_2 192.168.3.10:443 check /etc/haproxy/uccps.pem ssl verify required
  1. 启动haproxy,访问 两个集群地址。


存在的问题

  1. 证书自签问题, 因为 需要进行域名的解析判断,因此只能走http 7层代理,无法避免自签证书喂给代理。 (如有大佬有想法可留言)
  2. haproxy 集群需要有雀的tls证书问题,haproxy https to https 代理似乎必须要有两套证书,或者用uccps的一套证书也行,但访问上游节点必须要有证书。
  3. 还有其余的代理方式也可以实现这些功能,只是我还要打工,没时间写了。


感谢







标签:http,nginx,转发,server,ssl,集群,https,example1,节点
From: https://blog.51cto.com/mahmut/9533532

相关文章

  • 代码随想录算法训练营第四天 |24. 两两交换链表中的节点 | 19.删除链表的倒数第N个节
    142.环形链表II 已解答中等 相关标签相关企业 给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。为了表示给定链表中的环,......
  • 搭建K8S集群
    搭建K8S集群部署环境三台2核2G20G硬盘的服务器或虚拟机,文中使用的系统是centos7.9docker:19.03.15-3.el7kubernetes:1.23.6初始化操作关闭防火墙systemctlstopfirewalldsystemctldisablefirewalld关闭selinuxsed-i's/enforcing/disabled/'/etc/selinux/config......
  • 节点与链表
    classListNode:def__init__(self,val=0,next=None):self.val=valself.next=nextclassSingleLinkList:def__init__(self,node=None):self.__head=nodedefis_empty(self):'''判断列表是否为空'&#......
  • 动力节点RabbitMQ教程|12小时学会rabbitmq消息中间件-02
    RabbitMQ集群cluster与高可用RabbitMQ的集群分两种模式,一种是默认集群模式,一种是镜像集群模式;在RabbitMQ集群中所有的节点(一个节点就是一个RabbitMQ的broker服务器)被归为两类:一类是磁盘节点,一类是内存节点;磁盘节点会把集群的所有信息(比如交换机、绑定、队列等信息)持久化......
  • Redis三种集群模式:主从模式、哨兵模式和Cluster模式
    Redis三种集群模式:主从模式、哨兵模式和Cluster模式1、背景及介绍Redis支持三种不同的集群模式:主从模式、哨兵模式和Cluster模式,各具特色,应对不同的应用场景。初始阶段,Redis采用主从模式进行集群构建。在此模式中,主节点(master)负责数据写入,而从节点(slave)则用于数据读取和备份......
  • Redis集群方案和数据分区原理介绍
    Redis集群方案主从模式一主多从,主节点负责写数据,从节点负责读数据,主节点定期把数据同步到从节点保证数据的一致性。避免单点故障,实现了读写分离。优点:主从结构具有读写分离、提高效率、数据备份、提供多个副本等优点。缺点:不具备恢复功能,如果主节点宕机,则不能提供服务,需......
  • ipv6 转发 ipv4 实现 FiveM 联机
    socat公网ipv6转发内网ipv4实现FiveM联机不花一分钱研究了三天终于研究明白了,首先大家需要测试家里的宽带是否有IPv6本教程基于Linux实现,因为socat目前我没找到好用的Windows版本进入[https://www.test-ipv6.com/index.html.zh_CN]如果有如下图说明是支持的如果不......
  • 在K8S中,flannel能固定节点IP和Pod的IP地址吗?
    Flannel作为一个Kubernetes集群的网络插件,其设计目标之一是为Pod分配固定的IP地址,并确保不同节点上的PodIP地址不会冲突。具体来说:PodIP固定:Flannel在每个节点上预分配一个子网供Pod使用,当创建新Pod时,Flannel会从该节点的子网中分配一个唯一的IP地址给Pod,从而确保Pod在整个生......
  • AirSim多无人机协同集群编队
    首先要修改settings.json文件,来设置多无人机的初始位置{"SeeDocsAt":"https://github.com/Microsoft/AirSim/blob/main/docs/settings.md","SettingsVersion":1.2,"SimMode":"Multirotor","Vehicles":{&qu......
  • Java连接kubernates集群最优雅的两种方式
    创建maven工程,pom.xml中引入连接k8s的客户端jar包:<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><fabric.io.version>6.10.0</fabric.io.version></properties......