目录背景ElasticsearchvsClickHouse成本分析环境部署总结 背景 saas服务未来会面临数据安全、合规等问题。公司的业务需要沉淀一套私有化部署能力,帮助业务提升行业竞争力。 为了完善平台系统能力、我们需要沉淀一套数据体系帮助运营分析活动效果、提升运营能力。 然而在实际的开发过程中,如果直接部署一套大数据体系,对于使用者来说将是一笔比较大的服务器开销。为此我们选用折中方案完善数据分析能力。 ElasticsearchvsClickHouse ClickHouse是一款高性能列式分布式数据库管理系统,我们对ClickHouse进行了测试,发现有下列优势: ClickHouse写入吞吐量大 单服务器日志写入量在50MB到200MBs,每秒写入超过60w记录数,是ES的5倍以上。 在ES中比较常见的写Rejected导致数据丢失、写入延迟等问题,在ClickHouse中不容易发生。 查询速度快 官方宣称数据在pagecache中,单服务器查询速率大约在230GBs;没在pagecache的情况下,查询速度取决于磁盘的读取速率和数据的压缩率。经测试ClickHouse的查询速度比ES快530倍以上。 ClickHouse比ES服务器成本更低 一方面ClickHouse的数据压缩比比ES高,相同数据占用的磁盘空间只有ES的13到130,节省了磁盘空间的同时,也能有效的减少磁盘IO,这也是ClickHouse查询效率更高的原因之一。 另一方面ClickHouse比ES占用更少的内存,消耗更少的CPU资源。我们预估用ClickHouse处理日志可以将服务器成本降低一半。 成本分析 备注:在没有任何折扣的情况下,基于aliyun分析。 环境部署 zookeeper集群部署 yuminstalljava1。8。0openjdkdevel。x8664 etcprofile配置环境变量 更新系统时间 yuminstallntpdate ntpdateasia。pool。ntp。org mkdirzookeeper mkdir。zookeeperdata mkdir。zookeeperlogs wgetnocheckcertificatehttps:mirrors。tuna。tsinghua。edu。cnapachezookeeperzookeeper3。7。1apachezookeeper3。7。1bin。tar。gz tarzvxfapachezookeeper3。7。1bin。tar。gzCusrzookeeper exportZOOKEEPERHOMEusrzookeeperapachezookeeper3。7。1bin exportPATHZOOKEEPERHOMEbin:PATH 进入ZooKeeper配置目录 cdZOOKEEPERHOMEconf 新建配置文件 vizoo。cfg tickTime2000 initLimit10 syncLimit5 dataDirusrzookeeperdata dataLogDirusrzookeeperlogs clientPort2181 server。1zk1:2888:3888 server。2zk2:2888:3888 server。3zk3:2888:3888 在每台服务器上执行,给zookeeper创建myid echo1usrzookeeperdatamyid echo2usrzookeeperdatamyid echo3usrzookeeperdatamyid 进入ZooKeeperbin目录 cdZOOKEEPERHOMEbin shzkServer。shstart Kafka集群部署 mkdirpusrkafka chmod777Rusrkafka wgetnocheckcertificatehttps:mirrors。tuna。tsinghua。edu。cnapachekafka3。2。0kafka2。123。2。0。tgz tarzvxfkafka2。123。2。0。tgzCusrkafka 不同的brokerId设置不一样,比如1,2,3 broker。id1 listenersPLAINTEXT:ip:9092 socket。send。buffer。bytes102400 socket。receive。buffer。bytes102400 socket。request。max。bytes104857600 log。dirusrkafkalogs num。partitions5 num。recovery。threads。per。data。dir3 offsets。topic。replication。factor2 transaction。state。log。replication。factor3 transaction。state。log。min。isr3 log。retention。hours168 log。segment。bytes1073741824 log。retention。check。interval。ms300000 zookeeper。connectzk1:2181,zk2:2181,zk3:2181 zookeeper。connection。timeout。ms30000 group。initial。rebalance。delay。ms0 后台常驻进程启动kafka nohupusrkafkakafka2。123。2。0binkafkaserverstart。shusrkafkakafka2。123。2。0configserver。propertiesusrkafkalogskafka。log1 usrkafkakafka2。123。2。0binkafkaserverstop。sh KAFKAHOMEbinkafkatopics。shlistbootstrapserverip:9092 KAFKAHOMEbinkafkaconsoleconsumer。shbootstrapserverip:9092topictestfrombeginning KAFKAHOMEbinkafkatopics。shcreatebootstrapserverip:9092replicationfactor2partitions3topicxxxdata FileBeat部署 sudorpmimporthttps:packages。elastic。coGPGKEYelasticsearch Createafilewitha。repoextension(forexample,elastic。repo)inyouretcyum。repos。ddirectoryandaddthefollowinglines: 在etcyum。repos。d目录下创建elastic。repo 〔elastic8。x〕 nameElasticrepositoryfor8。xpackages baseurlhttps:artifacts。elastic。copackages8。xyum gpgcheck1 gpgkeyhttps:artifacts。elastic。coGPGKEYelasticsearch enabled1 autorefresh1 typerpmmd yuminstallfilebeat systemctlenablefilebeat chkconfigaddfilebeat FileBeat配置文件说明,坑点1(需设置keysunderroot:true)。如果不设置kafka的消息字段如下: 文件目录:etcfilebeatfilebeat。yml filebeat。inputs: type:log enabled:true paths: rootlogsxxxinner。log json: 如果不设置该索性,所有的数据都存储在message里面,这样设置以后数据会平铺。 keysunderroot:true output。kafka: hosts:〔kafka1:9092,kafka2:9092,kafka3:9092〕 topic:xxxdataclickhouse partition。roundrobin: reachableonly:false requiredacks:1 compression:gzip processors: 剔除filebeat无效的字段数据 dropfields: fields:〔input,agent,ecs,log,metadata,timestamp〕 ignoremissing:false nohup。filebeatecetcfilebeatfilebeat。ymluserfilebeatfilebeat。log 输出到filebeat。log文件中,方便排查 clickhouse部署 检查当前CPU是否支持SSE4。2,如果不支持,需要通过源代码编译构建 grepqsse42proccpuinfoechoSSE4。2supportedechoSSE4。2notsupported 返回SSE4。2supported表示支持,返回SSE4。2notsupported表示不支持 创建数据保存目录,将它创建到大容量磁盘挂载的路径 mkdirpdataclickhouse 修改etchosts文件,添加clickhouse节点 举例: 10。190。85。92bigdataclickhouse01 10。190。85。93bigdataclickhouse02 服务器性能参数设置: cpu频率调节,将CPU频率固定工作在其支持的最高运行频率上,而不动态调节,性能最好 echoperformanceteesysdevicessystemcpucpucpufreqscalinggovernor 内存调节,不要禁用overcommit echo0teeprocsysvmovercommitmemory 始终禁用透明大页(transparenthugepages)。它会干扰内存分配器,从而导致显着的性能下降 echoneverteesyskernelmmtransparenthugepageenabled 首先,需要添加官方存储库: yuminstallyumutils rpmimport yumconfigmanageraddrepo 查看clickhouse可安装的版本: yumlistgrepclickhouse 运行安装命令: yumyinstallclickhouseserverclickhouseclient 修改etcclickhouseserverconfig。xml配置文件,修改日志级别为information,默认是trace information 执行日志所在目录: 正常日志 varlogclickhouseserverclickhouseserver。log 异常错误日志 varlogclickhouseserverclickhouseserver。err。log 查看安装的clickhouse版本: clickhouseserverversion clickhouseclientpassword sudoclickhousestop sudoclickhousetart sudoclickhousestart clickhouse部署过程中遇到的一些问题如下: clickhouse创建kafka引擎表: CREATETABLEdefault。kafkaclickhouseinnerlogONCLUSTERclickhousecluster( loguuidString, datepartitionUInt32, eventnameString, activitynameString, activitytypeString, activityidUInt16 )ENGINEKafkaSETTINGS kafkabrokerlistkafka1:9092,kafka2:9092,kafka3:9092, kafkatopiclistdataclickhouse, kafkagroupnameclickhousexxx, kafkaformatJSONEachRow, kafkarowdelimiter, kafkanumconsumers1; 问题1:clikhouse客户端无法查询kafka引擎表 Directselectisnotallowed。Toenableusesettingstreamlikeengineallowdirectselect。(QUERYNOTALLOWED)(version22。5。2。53(officialbuild)) 解决方案: 需要在clickhouseclient创建加上streamlikeengineallowdirectselect1 clickhouseclientstreamlikeengineallowdirectselect1passwordxxxxx clickhouse创建本地节点表 问题2:无法开启本地表macro Code:62。DB::Exception:Therewasanerroron〔10。74。244。57:9000〕:Code:62。DB::Exception:Nomacroshardinconfigwhileprocessingsubstitutionsinclickhousetablesdefaultbiinnerloglocal{shard}at50ormacroisnotsupportedhere。(SYNTAXERROR)(version22。5。2。53(officialbuild))。(SYNTAXERROR)(version22。5。2。53(officialbuild)) 创建本地表(使用复制去重表引擎) createtabledefault。biinnerloglocalONCLUSTERclickhousecluster( loguuidString, datepartitionUInt32, eventnameString, activitynameString, creditsbringInt16, activitytypeString, activityidUInt16 )ENGINEReplicatedReplacingMergeTree(clickhousetablesdefaultbiinnerloglocal{shard},{replica}) PARTITIONBYdatepartition ORDERBY(eventname,datepartition,loguuid) SETTINGSindexgranularity8192; 解决方案:在不同的clickhouse节点上配置不同的shard,每一个节点的shard名称不能一致。 macros spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282shard01spanshardspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282replicaspanexample01011spanreplicaspan spanmacrosspanspan 问题3:clickhouse中节点数据已经存在 Code:253。DB::Exception:Therewasanerroron:Code:253。DB::Exception:Replicaclickhousetablesdefaultbiinnerloglocal01replicasexample01011alreadyexists。(REPLICAISALREADYEXIST)(version22。5。2。53(officialbuild))。(REPLICAISALREADYEXIST)(version22。5。2。53(officialbuild)) 解决方案:进入zookeeper客户端删除相关节点,然后再重新创建ReplicatedReplacingMergeTree表。这样可以保障每一个clickhouse节点都会去消费kafkapartition的数据。 clickhouse创建集群表 创建分布式表(根据loguuid对数据进行分发,相同的loguuid会发送到同一个shard分片上,用于后续合并时的数据去重): CREATETABLEdefault。biinnerlogallONCLUSTERclickhouseclusterASdefault。biinnerloglocal ENGINEDistributed(clickhousecluster,default,biinnerloglocal,xxHash32(loguuid)); 问题4:分布式集群表无法查询 Code:516。DB::Exception:Receivedfrom10。74。244。57:9000。DB::Exception:default:Authenticationfailed:passwordisincorrectorthereisnouserwithsuchname。(AUTHENTICATIONFAILED)(version22。5。2。53(officialbuild)) 解决方案: spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282remoteservers spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282clickhouseclusterspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282shardspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282internalreplicationspantruespaninternalreplicationspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282replicaspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282hostspanip1spanhostspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282portspan9000spanportspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282userspandefaultspanuserspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282passwordspanxxxxspanpasswordspan spanreplicaspan spanshardspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282shardspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282internalreplicationspantruespaninternalreplicationspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282replicaspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282hostspanip2spanhostspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282portspan9000spanportspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282userspandefaultspanuserspan spanstylecolor:F82375;ttdarkmodecolor:FF3282;classdatacolorttdarkmodeff3282passwordspanxxxxspanpasswordspan spanreplicaspan spanshardspan spanclickhouseclusterspan spanremoteserversspanspan clickhouse创建物化视图 创建物化视图,把Kafka消费表消费的数据同步到ClickHouse分布式表。 CREATEMATERIALIZEDVIEWdefault。viewbiinnerlogONCLUSTERclickhouseclusterTOdefault。biinnerlogallAS SELECT loguuid, datepartition, eventname, activityname, creditsbring, activitytype, activityid FROMdefault。 小结:功夫不负有心人,解决完以上所有的问题。数据流转通了!本文所有组件都是比较新的版本,所以过程中问题的解决基本都是官方文档或操作手册一步一步的解决。 总结一句话:遇到问题去官方文档或help去尝试解决,慢慢的你就会升华。 总结 整个部署的过程踩了不少坑,尤其是filebeatyml的参数设置和clickhouse的配置说明。 很久没有更新博客了,经常看到博客35岁以后怎么办的问题。说实话我自己也没想好以后怎么办,核心还是持续的学习输出。不断的构建自己的护城河,不管是技术专家、业务专家、架构、管理等。 个人建议如果能持续写代码就奋战在一线,管理彻底与公司绑定。除非你是有名的大厂,这另外看。 如果所在的公司缺乏较大的行业影响力,个人感觉可以奋战在一线,未来选择新的工作。考量更多的还是行业影响、商业sense、技术架构能力。现在的我已35,从容的面对每一天。