Trino对接haproxy开启ssl

櫰木2年前技术文章988

1、背景

客户在开启https后,在高并发的情况下,集群性能下降严重。初步估计是由于worker和Coordinator交互都需要进行加密和解密,会提高cpu使用率,导致服务变慢。现在采用haproxy作为load balancer开启https,转发到trino http

2、操作步骤

2.1、安装haproxy

环境:centos7.9

yum install haproxy -y

检测是否支持https

haproxy -vv

image.png

2.2、生成ssl证书

1、创建私钥
openssl genpkey -algorithm RSA -out haproxy.key
2、创建证书签名请求(CSR)
openssl req -new -sha256 -key haproxy.key -out haproxy.csr   -subj "/C=CN/ST=Shanghai/L=Shanghai/O=Organization/OU=Unit/CN=fzcn.org"
3、创建扩展配置文件(extfile.cnf)
vim extfile.cnf
[ v3_req ]
subjectAltName=DNS:cm-server.open.hadoop,IP:172.16.121.6
4、生成带有扩展的证书
openssl x509 -req -days 3650 -in haproxy.csr -signkey haproxy.key -out haproxy.crt  -extfile extfile.cnf -extensions v3_req
5、将证书和私钥合并到一个PEM文件
cat haproxy.crt haproxy.key > haproxy.pem
6、导出证书crt
openssl x509 -in haproxy.pem -out haproxy.crt
7、创建PKCS12文件(输入的密码默认为admin@123)
openssl pkcs12 -export -in haproxy.crt -inkey haproxy.key -out haproxy.p12
8、将PKCS12转换为JKS(输入的密码默认为admin@123)
keytool -importkeystore -srckeystore haproxy.p12 -srcstoretype PKCS12 -destkeystore haproxy.jks -deststoretype JKS

查看jks证书

keytool -list -v -keystore haproxy.jks  -storepass admin@123

2.3、配置haproxy

在/etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

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


    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
    tune.ssl.default-dh-param 2048
    nbproc 4
  
    cpu-map 1 0
    cpu-map 2 1
    cpu-map 3 2
    cpu-map 4 3
    stats bind-process 4
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    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

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------

frontend https_frontend
   bind *:18082 ssl crt /opt/haproxy/extssl/haproxy.pem
   mode http
   #option httpclose
   option forwardfor
   reqadd X-Forwarded-Proto:\ https
   default_backend web_server
   bind-process 1 2 3
backend web_server
  mode http
  option http-server-close
  balance roundrobin
  cookie SERVERID insert indirect nocache
  server s1 172.16.121.6:18081 check cookie s1 inter 15s fastinter 15s downinter 15s rise 2 fall 4

# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
listen admin_stats
        stats   enable
        bind    *:8080    #监听的ip端口号
        mode    http    #开关
        option  httplog
        log     global
        maxconn 10
        stats   refresh 30s   #统计页面自动刷新时间
        stats   uri /admin    #访问的uri   ip:8080/admin
        stats   realm haproxy
        stats   auth admin:admin  #认证用户名和密码
        stats   hide-version   #隐藏HAProxy的版本号
        stats   admin if TRUE   #管理界面,如果认证成功了,可通过webui管理节点

修改frontend 中的/opt/haproxy/extssl/haproxy.pem 为上边生成的证书的路径

修改backend 172.16.121.6:18081 为实际Coordinator的http地址

添加日志

在/etc/rsyslog.conf添加以下内容

#添加
local2.*                                                /var/log/haproxy.log #这个local2要和haproxy.cfg中的log字段配置的相同
#搜索message,将其中的info删掉,不然haproxy的日志也会写到message中
mail.none;authpriv.none;cron.none                /var/log/messages
$ModLoad imudp
$UDPServerRun 514

重启rsyslog

systemctl restart rsyslog

重启haproxy

systemctl start haproxy && systemctl enable  haproxy && systemctl status  haproxy

启动成功的状态

image.png

2.4、Trino添加配置

在config.properties添加配置

internal-communication.shared-secret=abc123
http-server.process-forwarded=true
##添加ldap配置
http-server.authentication.type=PASSWORD

password-authenticator.properties

password-authenticator.name=ldap
ldap.url=ldap://172.16.105.69:389
#多ou,使用:作为分隔符
ldap.user-base-dn=CN=Users,DC=fzcdh,DC=com:OU=owner1,DC=fzcdh,DC=com
ldap.user-bind-pattern=${USER}@fzcdh.com
ldap.allow-insecure=true

重启trino 服务

3、测试验证

3.1、 登录web页面

查看worker 是否注册成功

https://172.16.121.6:18082/ui/

image.png

3.2、使用trino client

trino-cli --server https://172.16.121.6:18082 --keystore-path /opt/haproxy/extssl/haproxy.jks  --keystore-password admin@123 --catalog hive   --user test --password

image.png

其中此--keystore-path为前面生成的jks文件 --keystore-password 为jks的密码(默认为admin@123)

--user test --password 指集成了ldap中存储的用户密码

(如何集成ldap请看文档Trino 对接ldap)

3.3、python代码测试

from trino import dbapi
from trino.auth import BasicAuthentication

# 依赖
# python 3.11.5
# trino

# 连接到Trino集群
# user = "test",
# pwd = "ranger123",
conn = dbapi.connect(
    # host='172.16.121.6',
    host='cm-server.open.hadoop',
    # user="test",
    auth=BasicAuthentication("test", "ranger@123"),
    http_scheme="https",
    verify=False,
    # host='hd1.dtstack.com',
    port=18082,
    # user='test',
    catalog='hive',
    schema='default'
)

# 创建游标对象
cursor = conn.cursor()

# 执行查询
cursor.execute('SELECT count(*) FROM test1')

# 获取查询结果
results = cursor.fetchall()

# 处理查询结果
for row in results:
    print(row)

# 关闭连接
cursor.close()
conn.close()

image.png



相关文章

MongoDB 单机安装部署

MongoDB 单机安装部署

说明本篇文章介绍 MongoDB 二进制安装的步骤,整个过程还是比较简单。1. 下载安装包进入 MongoDB 官网,获取安装包的下载链接:https://www.mongodb.com/try/do...

MySQL日期相关函数

一、当前时间--当前时间select current_timestamp();--当前时间 等价于 select current_timestamp();select now();--基本等价于以上两...

DRDS 整库恢复介绍

DRDS 整库恢复介绍

1 整库恢复注意事项1、PolarDB-X 1.0自动备份策略默认关闭,需要您手动开启。PolarDB-X 1.0日志备份能力依赖下层RDS,PolarDB-X1.0控制台设置的日志备份策略会自动同步...

Linux 会话管理

Linux 会话管理

在 terminal 终端中输入命令,这种用户与计算机的临时交互称为一次会话(session)。会话的一个重要特点:与其中启动的进程是连在一起的,打开窗口、会话开始,关闭窗口、会话结束,会话内部的进程...

MySQL运维实战(7)建立复制

建立复制的基本步骤1、主库开启binlog主库需要配置的关键参数server_id:主备库需要设置为不同。log_bin:binlog文件的前缀,可以指定绝对路径,也可以只指定文件名。若不指定路径,b...

HDFS业务类型与相关架构优化措施

低延迟业务计算密集型业务,低延迟业务,这一类业务通常是对大量NameNode文件目录访问为主的(权限判断,文件存在性,文件信息查询等对NameNode元数据操作为主的)。因此需要针对NameNode的...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。