通过Gateway访问Presto

本文涉及的产品
EMR Serverless StarRocks,5000CU*H 48000GB*H
简介: 本文介绍使用HAProxy反向代理实现通过Gateway节点访问Presto服务的方法。该方法也很容扩展到其他组件,如Impala等。

Gateway是与EMR集群处于同一个内网中的ECS服务器,可以使用Gateway实现负载均衡和安全隔离。用户可以通过:控制台页面>配置管理>概览>创建Gateway来创建对应集群的Gateway节点。

NOTE: Gateway节点默认已经安装了HAProxy服务,但没有启动。

普通集群

普通集群配置Gateway代理比较简单,只需要配置HAProxy反向代理,对EMR集群上Header节点的Presto Coodrinator的9090端口实现反向代理即可。配置步骤如下:

配置HAProxy

通过ssh登入Gateway节点,修改HAProxy的配置文件/etc/haproxy/haproxy.cfg。添加如下内容:

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global

......

## 配置代理,将Gateway的9090端口映射到
## emr-header-1.cluster-xxxx的9090端口
listen prestojdbc :9090
    mode tcp
    option tcplog
    balance source
    server presto-coodinator-1 emr-header-1.cluster-xxxx:9090
    

保存退出,使用如下命令重启HAProxy服务:

$> service haproxy restart

配置安全组

需要配置的规则如下:

方向 配置规则 说明
公网入 自定义TCP,开放9090端口 该端口用于HAProxy代理Header节点Coodinator端口

现在就可以通过ECS控制台删除Header节点的公网IP,在自己的客户机上通过Gateway访问Presto服务了。

  • 命令行使用Presto的示例请查看这里:Presto CLI
  • JDBC访问Presto的示例请看这里:Presto JDBC

高安全集群

EMR高安全集群中的Presto服务使用Kerberos服务进行认证,其中Kerberos KDC服务位于emr-header-1上,端口为88,同时支持TCP/UDP协议。 使用Gateway访问高安全集群中的Presto服务,需要同时对Presto Coodinator服务端口和Kerberos KDC实现代理。
另外,EMR Presto Coodinator集群默认使用keystore配置的CN为emr-header-1,只能内网使用,因此需要重新生成CN=emr-header-1.cluster-xxx的keystore。

HTTPs认证相关

创建服务端CN=emr-header-1.cluster-xxx的keystore:

[root@emr-header-1 presto-conf]# keytool -genkey -dname "CN=emr-header-1.cluster-xxx,OU=Alibaba,O=Alibaba,L=HZ, ST=zhejiang, C=CN" -alias server -keyalg RSA -keystore keystore -keypass 81ba14ce6084 -storepass 81ba14ce6084 -validity 36500

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore keystore -destkeystore keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。

导出证书:

[root@emr-header-1 presto-conf]# keytool -export -alias server -file server.cer -keystore keystore -storepass 81ba14ce6084
存储在文件 <server.cer> 中的证书

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore keystore -destkeystore keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。

制作客户端keystore:

[root@emr-header-1 presto-conf]# keytool -genkey -dname "CN=myhost,OU=Alibaba,O=Alibaba,L=HZ, ST=zhejiang, C=CN" -alias client -keyalg RSA -keystore client.keystore -keypass 123456 -storepass 123456 -validity 36500

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore client.keystore -destkeystore client.keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。

将证书导入到客户端keystore中:

[root@emr-header-1 presto-conf]# keytool -import -alias server -keystore client.keystore -file server.cer -storepass 123456
所有者: CN=emr-header-2.cluster-xxx, OU=Alibaba, O=Alibaba, L=HZ, ST=zhejiang, C=CN
发布者: CN=emr-header-2.cluster-xxx, OU=Alibaba, O=Alibaba, L=HZ, ST=zhejiang, C=CN
序列号: 4247108
有效期为 Thu Mar 01 09:11:31 CST 2018 至 Sat Feb 05 09:11:31 CST 2118
证书指纹:
     MD5:  75:2A:AA:40:01:5B:3F:86:8F:9A:DB:B1:85:BD:44:8A
     SHA1: C7:25:B9:AD:5F:FE:FC:05:8E:A0:24:4A:1C:AA:6A:8D:6C:39:28:16
     SHA256: DB:86:69:65:73:D5:C6:E2:98:7C:4A:3B:31:EF:70:80:F0:3C:3B:0C:14:94:37:9F:9C:22:47:EA:7E:1E:DE:8C
签名算法名称: SHA256withRSA
主体公共密钥算法: 2048 位 RSA 密钥
版本: 3

扩展:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 45 1D A9 C7 D5 4E BB CF   BD CE B4 5E E2 16 FB 2F  E....N.....^.../
0010: E9 5D 4A B6                                        .]J.
]
]

是否信任此证书? [否]:  是
证书已添加到密钥库中

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore client.keystore -destkeystore client.keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。

将生成的文件拷贝到客户端:

$> scp root@xxx.xxx.xxx.xxx:/etc/ecm/presto-conf/client.keystore ./

Kerberos认证相关

添加客户端用户principal:

[root@emr-header-1 presto-conf]# sh /usr/lib/has-current/bin/hadmin-local.sh /etc/ecm/has-conf -k /etc/ecm/has-conf/admin.keytab
[INFO] conf_dir=/etc/ecm/has-conf
Debug is  true storeKey true useTicketCache false useKeyTab true doNotPrompt true ticketCache is null isInitiator true KeyTab is /etc/ecm/has-conf/admin.keytab refreshKrb5Config is true principal is kadmin/EMR.xxx.COM@EMR.xxx.COM tryFirstPass is false useFirstPass is false storePass is false clearPass is false
Refreshing Kerberos configuration
principal is kadmin/EMR.xxx.COM@EMR.xxx.COM
Will use keytab
Commit Succeeded

Login successful for user: kadmin/EMR.xxx.COM@EMR.xxx.COM
enter "cmd" to see legal commands.
HadminLocalTool.local: addprinc -pw 123456 clientuser
Success to add principal :clientuser
HadminLocalTool.local: ktadd -k /root/clientuser.keytab clientuser
Principal export to keytab file : /root/clientuser.keytab successful .
HadminLocalTool.local: exit

将生成的文件拷贝到客户端:

$> scp root@xxx.xxx.xxx.xxx:/root/clientuser.keytab ./
$> scp root@xxx.xxx.xxx.xxx:/etc/krb5.conf ./

修改拷贝到客户端的krb5.conf文件,修改如下两处:

[libdefaults]
    kdc_realm = EMR.xxx.COM
    default_realm = EMR.xxx.COM
    # 改成1,使客户端使用TCP协议与KDC通信(因为HAProxy不支持UDP协议)
    udp_preference_limit = 1 
    kdc_tcp_port = 88
    kdc_udp_port = 88
    dns_lookup_kdc = false

[realms]
    EMR.xxx.COM = {
        # 设置为Gateway的外网IP
        kdc = xxx.xxx.xxx.xxx:88
    }

修改客户端主机的hosts文件,添加如下内容:

#  gateway ip
xxx.xxx.xxx.xxx emr-header-1.cluster-xxx

配置Gateway HAProxy

通过SSH登入到Gateway节点,修改/etc/haproxy/haproxy.cfg。添加如下内容:

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global

......
listen prestojdbc :7778
    mode tcp
    option tcplog
    balance source
    server presto-coodinator-1 emr-header-1.cluster-xxx:7778

listen kdc :88
    mode tcp
    option tcplog
    balance source
    server emr-kdc emr-header-1:88

保存退出,使用如下命令重启HAProxy服务:

$> service haproxy restart

配置安全组规则

需要配置的规则如下:

方向 配置规则 说明
公网入 自定义UDP,开放88端口 该端口用户HAProxy代理Header节点上的KDC
公网入 自定义TCP,开放88端口 该端口用户HAProxy代理Header节点上的KDC
公网入 自定义TCP,开放7778端口 该端口用于HAProxy代理Header节点Coodinator端口

现在就可以通过ECS控制台删除Header节点的公网IP,在自己的客户机上通过Gateway访问Presto服务了。

使用JDBC访问Presto示例

废话不说,直接上代码:

try {
    Class.forName("com.facebook.presto.jdbc.PrestoDriver");
} catch(ClassNotFoundException e) {
    LOG.error("Failed to load presto jdbc driver.", e);
    System.exit(-1);
}

Connection connection = null;
Statement statement = null;
try {
    String url = "jdbc:presto://emr-header-1.cluster-59824:7778/hive/default";
    Properties properties = new Properties();
    properties.setProperty("user", "hadoop");
    // https相关配置
    properties.setProperty("SSL", "true");
    properties.setProperty("SSLTrustStorePath", "resources/59824/client.keystore");
    properties.setProperty("SSLTrustStorePassword", "123456");
    // Kerberos相关配置
    properties.setProperty("KerberosRemoteServiceName", "presto");
    properties.setProperty("KerberosPrincipal", "clientuser@EMR.59824.COM");
    properties.setProperty("KerberosConfigPath", "resources/59824/krb5.conf");
    properties.setProperty("KerberosKeytabPath", "resources/59824/clientuser.keytab");
    // 创建连接对象
    connection = DriverManager.getConnection(url, properties);
    // 创建Statement对象
    statement = connection.createStatement();
    // 执行查询
    ResultSet rs = statement.executeQuery("select * from table1");
    // 获取结果
    int columnNum = rs.getMetaData().getColumnCount();
    int rowIndex = 0;
    while (rs.next()) {
        rowIndex++;
        for(int i = 1; i <= columnNum; i++) {
            System.out.println("Row " + rowIndex + ", Column " + i + ": " + rs.getString(i));
        }
    }
} catch(SQLException e) {
    LOG.error("Exception thrown.", e);
} finally {
    // 销毁Statement对象
    if (statement != null) {
        try {
            statement.close();
            } catch(Throwable t) {
              // No-ops
            }
    }
   // 关闭连接
   if (connection != null) {
          try {
           connection.close();
       } catch(Throwable t) {
         // No-ops
       }
    }
}

小结

本文介绍使用HAProxy反向代理实现通过Gateway节点访问Presto服务的方法。该方法也很容扩展到其他组件,如Impala等。

相关实践学习
基于EMR Serverless StarRocks一键玩转世界杯
基于StarRocks构建极速统一OLAP平台
快速掌握阿里云 E-MapReduce
E-MapReduce 是构建于阿里云 ECS 弹性虚拟机之上,利用开源大数据生态系统,包括 Hadoop、Spark、HBase,为用户提供集群、作业、数据等管理的一站式大数据处理分析服务。 本课程主要介绍阿里云 E-MapReduce 的使用方法。
目录
相关文章
|
26天前
|
Kubernetes API 网络架构
【Azure APIM】解决APIM Self-hosted Gateway在AKS上,最开始访问时候遇见的404问题
【Azure APIM】解决APIM Self-hosted Gateway在AKS上,最开始访问时候遇见的404问题
|
10月前
|
前端开发 应用服务中间件 nginx
用docker和nginx部署前端项目访问本地java网关gateway服务
本地开发 java 微服务项目,但是拿到的对应的web前端项目只有打包编译过后的 dist 目录里的静态资源(里面只有一个index.html和一些编译过后的 js、css文件),前端接口需要先访问到 java 的网关服务,然后网关里再做转发
407 1
|
负载均衡 Java 应用服务中间件
Nginx+SpringCloud Gateway搭建项目访问环境
Nginx+SpringCloud Gateway搭建项目访问环境
980 0
Nginx+SpringCloud Gateway搭建项目访问环境
可以公开访问的 SAP gateway demo system ES5/002
SAP gateway demo system,给 SAP 开发人员提供了可以 free 使用的 OData 服务,用来学习 SAP Fiori 和 SAP Fiori Elements 相关的开发。
可以公开访问的 SAP gateway demo system ES5/002
|
7天前
|
负载均衡 Java Nacos
SpringCloud基础2——Nacos配置、Feign、Gateway
nacos配置管理、Feign远程调用、Gateway服务网关
SpringCloud基础2——Nacos配置、Feign、Gateway
|
19天前
|
安全 Java 开发者
强大!Spring Cloud Gateway新特性及高级开发技巧
在微服务架构日益盛行的今天,网关作为微服务架构中的关键组件,承担着路由、安全、监控、限流等多重职责。Spring Cloud Gateway作为新一代的微服务网关,凭借其基于Spring Framework 5、Project Reactor和Spring Boot 2.0的强大技术栈,正逐步成为业界的主流选择。本文将深入探讨Spring Cloud Gateway的新特性及高级开发技巧,助力开发者更好地掌握这一强大的网关工具。
72 6
|
2月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
290 15
|
2月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
61 3
|
2月前
|
Java 微服务 Spring
SpringCloud gateway自定义请求的 httpClient
SpringCloud gateway自定义请求的 httpClient
112 3
|
2月前
|
JSON 前端开发 Java
SpringCloud怎么搭建GateWay网关&统一登录模块
本文来分享一下,最近我在自己的项目中实现的认证服务,目前比较简单,就是可以提供一个公共的服务,专门来处理登录请求,然后我还在API网关处实现了登录拦截的效果,因为在一个博客系统中,有一些地址是可以不登录的,比方说首页;也有一些是必须登录的,比如发布文章、评论等。所以,在网关处可以支持自定义一些不需要登录的地址,一些需要登录的地址,也可以在网关处进行校验,如果未登录,可以返回JSON格式的出参,前端可以进行相关处理,比如跳转到登录页面等。