• Kubernetes 单节点安装Clickhouse


    ClickHouse 简介

    ClickHouse是一个面向列的数据库管理系统 (DBMS),用于查询的在线分析处理 (OLAP)。

    Clickhouse 特点

    • 真正的面向列的数据库管理系统
      在真正的面向列的 DBMS 中,没有额外的数据与值一起存储。除此之外,这意味着必须支持常量长度值,以避免将它们的长度“数字”存储在值旁边。例如,十亿个 UInt8 类型的值应该消耗大约 1 GB 未压缩,否则这会严重影响 CPU 使用。即使在未压缩的情况下,也必须紧凑地存储数据(没有任何“垃圾”),因为解压缩的速度(CPU 使用率)主要取决于未压缩的数据量。
      ClickHouse 是一个数据库管理系统,而不是单个数据库。ClickHouse 允许在运行时创建表和数据库、加载数据和运行查询,而无需重新配置和重新启动服务器。

    • 数据压缩
      在磁盘空间和 CPU 消耗之间具有不同权衡的高效通用压缩编解码器之外,ClickHouse 还为特定类型的数据提供专门的编解码器,这使 ClickHouse 能够与更多的小众数据库竞争并胜过更多的小众数据库,如时间序列数据库。

    • 数据的磁盘存储
      保持数据按主键物理排序可以以低延迟(不到几十毫秒)提取特定值或值范围的数据。一些面向列的 DBMS(例如 SAP HANA 和 Google PowerDrill)只能在 RAM 中工作。这种方法鼓励分配比实时分析所需的更大的硬件预算。
      ClickHouse 设计用于普通硬盘驱动器,这意味着每 GB 数据存储的成本很低,但如果可用,也可以充分利用 SSD 和额外的 RAM。

    • 多核并行处理
      大型查询自然并行化,占用当前服务器上可用的所有必要资源。

    • 多台服务器上的分布式处理
      在 ClickHouse 中,数据可以驻留在不同的分片上。每个分片可以是一组用于容错的副本。所有分片都用于并行运行查询,对用户透明。

    • SQL 支持
      ClickHouse 支持基于 SQL的声明式查询语言,该语言在许多情况下与 ANSI SQL 标准相同。
      支持的查询包括GROUP BY、ORDER BY、FROM 中的子查询、JOIN子句、IN运算符、窗口函数和标量子查询。

    • 矢量计算引擎
      数据不仅按列存储,还通过向量(列的一部分)进行处理,从而实现高 CPU 效率。

    • 实时数据更新
      ClickHouse 支持带有主键的表。为了快速对主键的范围执行查询,使用合并树对数据进行增量排序。因此,数据可以不断添加到表中。摄取新数据时不会锁定。

    • 一级索引
      通过主键对数据进行物理排序可以以低延迟(不到几十毫秒)为其特定值或值范围提取数据。

    • 二级索引
      与其他数据库管理系统不同,ClickHouse 中的二级索引不指向特定的行或行范围。相反,它们让数据库提前知道某些数据部分中的所有行都不会匹配查询过滤条件并且根本不读取它们,因此它们被称为数据跳过索引。

    • 适合在线查询
      大多数 OLAP 数据库管理系统并不针对具有亚秒级延迟的在线查询。在替代系统中,几十秒甚至几分钟的报告构建时间通常被认为是可以接受的。有时甚至需要更多的力量来离线准备报告
      在 ClickHouse 中,低延迟意味着可以在加载用户界面页面的同时毫不延迟地处理查询,而无需尝试提前准备答案。换句话说,在线。

    • 支持近似计算
      ClickHouse 提供了多种方式来交换性能的准确性:
      用于近似计算不同值、中位数和分位数的聚合函数。
      基于部分(样本)数据运行查询并获得近似结果。在这种情况下,从磁盘中检索的数据按比例减少。
      为有限数量的随机密钥而不是所有密钥运行聚合。在数据中密钥分布的特定条件下,这提供了相当准确的结果,同时使用了更少的资源。

    • 自适应连接算法
      ClickHouse自适应选择如何JOIN多个表,通过宁愿散列连接算法,并回落至合并连接算法,如果有一个以上的大表。

    • 数据复制和数据完整性支持
      ClickHouse 使用异步多主复制。在写入任何可用的副本后,所有剩余的副本都会在后台检索它们的副本。系统在不同的副本上维护相同的数据。大多数故障后的恢复是自动执行的,或者在复杂情况下半自动执行。

    • 基于角色的访问控制
      ClickHouse 使用 SQL 查询实现用户帐户管理,并允许基于角色的访问控制配置,类似于 ANSI SQL 标准和流行的关系数据库管理系统中的配置。

    更多功能参考官方文档:https://clickhouse.com/docs/en/introduction/performance/#

    Clickhouse 持久化配置

    这里数据持久化使用NFS进行持久化数据

    安装NFS

    1. #这里我使用单独服务器进行演示,实际上顺便使用一台服务器安装nfs都可以 (建议和kubernetes集群分开,找单独一台机器)
    2. [root@nfs ~]# yum install nfs-utils -y rpcbind
    3. #接下来设置nfs存储目录
    4. [root@nfs ~]# mkdir /data/k8s-volume
    5. [root@nfs ~]# chmod 755 /data/k8s-volume/
    6. #编辑nfs配置文件
    7. [root@nfs ~]# cat /etc/exports
    8. /data/k8s-volume *(rw,no_root_squash,sync)
    9. #存储目录,*允许所有人连接,rw读写权限,sync文件同时写入硬盘及内存,no_root_squash 使用者root用户自动修改为普通用户
    10. 接下来启动rpcbind
    11. [root@nfs ~]# systemctl start rpcbind
    12. [root@nfs ~]# systemctl enable rpcbind
    13. [root@nfs ~]# systemctl status rpcbind
    14. ● rpcbind.service - RPC bind service
    15. Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
    16. Active: active (running) since 四 2019-12-19 18:44:29 CST; 11min ago
    17. Main PID: 3126 (rpcbind)
    18. CGroup: /system.slice/rpcbind.service
    19. └─3126 /sbin/rpcbind -w
    20. #由于nfs需要向rpcbind进行注册,所以我们需要优先启动rpcbind
    21. #启动NFS
    22. [root@nfs ~]# systemctl restart nfs
    23. [root@nfs ~]# systemctl enable nfs
    24. [root@nfs ~]# systemctl status nfs
    25. ● nfs-server.service - NFS server and services
    26. Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
    27. Drop-In: /run/systemd/generator/nfs-server.service.d
    28. └─order-with-mounts.conf
    29. Active: active (exited) since 四 2019-12-19 18:44:30 CST; 13min ago
    30. Main PID: 3199 (code=exited, status=0/SUCCESS)
    31. CGroup: /system.slice/nfs-server.service
    32. #检查rpcbind及nfs是否正常
    33. [root@nfs ~]# rpcinfo |grep nfs
    34. 100003 3 tcp 0.0.0.0.8.1 nfs superuser
    35. 100003 4 tcp 0.0.0.0.8.1 nfs superuser
    36. 100227 3 tcp 0.0.0.0.8.1 nfs_acl superuser
    37. 100003 3 udp 0.0.0.0.8.1 nfs superuser
    38. 100003 4 udp 0.0.0.0.8.1 nfs superuser
    39. 100227 3 udp 0.0.0.0.8.1 nfs_acl superuser
    40. 100003 3 tcp6 ::.8.1 nfs superuser
    41. 100003 4 tcp6 ::.8.1 nfs superuser
    42. 100227 3 tcp6 ::.8.1 nfs_acl superuser
    43. 100003 3 udp6 ::.8.1 nfs superuser
    44. 100003 4 udp6 ::.8.1 nfs superuser
    45. 100227 3 udp6 ::.8.1 nfs_acl superuser
    46. #查看nfs目录挂载权限
    47. [root@nfs ~]# cat /var/lib/nfs/etab
    48. /data/k8s-volume *(rw,sync,wdelay,hide,nocrossmnt,secure,no_root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,no_root_squash,no_all_squash)
    49. #检查挂载是否正常
    50. [root@nfs ~]# showmount -e 127.0.0.1
    51. Export list for 127.0.0.1:
    52. /data/k8s-volume *

    创建nfs client
    本次nfs服务器地址192.168.31.101
    数据存储目录 /data/k8s-volume

    1. kind: Deployment
    2. apiVersion: apps/v1
    3. metadata:
    4. name: nfs-client-provisioner
    5. spec:
    6. replicas: 1
    7. selector:
    8. matchLabels:
    9. app: nfs-client-provisioner
    10. strategy:
    11. type: Recreate
    12. template:
    13. metadata:
    14. labels:
    15. app: nfs-client-provisioner
    16. spec:
    17. serviceAccountName: nfs-client-provisioner
    18. containers:
    19. - name: nfs-client-provisioner
    20. image: quay.io/external_storage/nfs-client-provisioner:latest
    21. volumeMounts:
    22. - name: nfs-client-root
    23. mountPath: /persistentvolumes
    24. env:
    25. - name: PROVISIONER_NAME
    26. value: fuseim.pri/ifs
    27. - name: NFS_SERVER
    28. value: 192.168.31.101 #nfs server 地址
    29. - name: NFS_PATH
    30. value: /data/k8s-volume #nfs共享目录
    31. volumes:
    32. - name: nfs-client-root
    33. nfs:
    34. server: 192.168.31.101
    35. path: /data/k8s-volume
    36. 接下来我们还需要创建一个serveraccount,用于将nfs-client-provisioner中的ServiceAccount绑定到一个nfs-client-provisioner-runner的ClusterRole。而该ClusterRole声明了一些权限,其中就包括了对persistentvolumes的增删改查,所以我们就可以利用ServiceAccount来自动创建PV
    37. apiVersion: v1
    38. kind: ServiceAccount
    39. metadata:
    40. name: nfs-client-provisioner
    41. ---
    42. kind: ClusterRole
    43. apiVersion: rbac.authorization.k8s.io/v1
    44. metadata:
    45. name: nfs-client-provisioner-runner
    46. rules:
    47. - apiGroups: [""]
    48. resources: ["persistentvolumes"]
    49. verbs: ["get", "list", "watch", "create", "delete"]
    50. - apiGroups: [""]
    51. resources: ["persistentvolumeclaims"]
    52. verbs: ["get", "list", "watch", "update"]
    53. - apiGroups: ["storage.k8s.io"]
    54. resources: ["storageclasses"]
    55. verbs: ["get", "list", "watch"]
    56. - apiGroups: [""]
    57. resources: ["events"]
    58. verbs: ["list", "watch", "create", "update", "patch"]
    59. - apiGroups: [""]
    60. resources: ["endpoints"]
    61. verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
    62. ---
    63. kind: ClusterRoleBinding
    64. apiVersion: rbac.authorization.k8s.io/v1
    65. metadata:
    66. name: run-nfs-client-provisioner
    67. subjects:
    68. - kind: ServiceAccount
    69. name: nfs-client-provisioner
    70. namespace: default
    71. roleRef:
    72. kind: ClusterRole
    73. name: nfs-client-provisioner-runner
    74. apiGroup: rbac.authorization.k8s.io
    75. #检查pod是否ok
    76. [root@k8s-01 nfs]# kubectl get pod
    77. NAME READY STATUS RESTARTS AGE
    78. nfs-client-provisioner-7995946c89-n7bsc 1/1 Running 0 13m

    创建storageclass
    这里我们声明了一个名为managed-nfs-storage的Storageclass对象

    1. apiVersion: storage.k8s.io/v1
    2. kind: StorageClass
    3. metadata:
    4. name: managed-nfs-storage
    5. provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
    6. #检查
    7. [root@k8s-01 nfs]# kubectl get storageclasses.storage.k8s.io
    8. NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
    9. managed-nfs-storage fuseim.pri/ifs Delete Immediate false 104d

    为clickhouse创建pvc

    1. 首先需要创建一个namespace,放ck相关
    2. kubectl create ns test
    3. # pvc yaml文件如下
    4. apiVersion: v1
    5. kind: PersistentVolumeClaim
    6. metadata:
    7. name: clickhouse-pvc
    8. namespace: test
    9. spec:
    10. resources:
    11. requests:
    12. storage: 10Gi #数据大小
    13. accessModes:
    14. - ReadWriteMany # pvc数据访问类型
    15. storageClassName: "managed-nfs-storage" #storageclass 名称
    16. # 检查状态
    17. [root@k8s-01 clickhouse]# kubectl get pvc -n test
    18. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
    19. clickhouse-pvc Bound pvc-ee8a47fc-a196-459f-aca4-143a8af58bf3 10Gi RWX managed-nfs-storage 25s

    Clickhouse 安装

    由于我们这里需要对users.xml配置进行修改,做一下配置参数跳转,我这里将users.xml下载下来修改后使用configmap进行挂载

    1. #这里可以直接下载配置,或者是启动ck在复制users.xml拷贝下来修改
    2. wget https://d.frps.cn/file/kubernetes/clickhouse/users.xml
    3. [root@k8s-01 clickhouse]# kubectl create cm -n test clickhouse-users --from-file=users.xml #不做配置持久化可以跳过
    4. configmap/clickhouse-users created
    5. [root@k8s-01 clickhouse]# kubectl get cm -n test
    6. NAME DATA AGE
    7. clickhouse-users 1 5s

    clickhouse yaml文件如下

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. labels:
    5. app: clickhouse
    6. name: clickhouse
    7. namespace: test
    8. spec:
    9. replicas: 1
    10. revisionHistoryLimit: 10
    11. selector:
    12. matchLabels:
    13. app: clickhouse
    14. template:
    15. metadata:
    16. labels:
    17. app: clickhouse
    18. spec:
    19. containers:
    20. - image: clickhouse/clickhouse-server
    21. imagePullPolicy: IfNotPresent
    22. name: clickhouse
    23. ports:
    24. - containerPort: 8123
    25. protocol: TCP
    26. resources:
    27. limits:
    28. cpu: 1048m
    29. memory: 2Gi
    30. requests:
    31. cpu: 1048m
    32. memory: 2Gi
    33. volumeMounts:
    34. - mountPath: /var/lib/clickhouse
    35. name: clickhouse-volume
    36. - mountPath: /etc/clickhouse-server/users.xml
    37. subPath: users.xml
    38. name: clickhouse-users
    39. volumes:
    40. - name: clickhouse-users
    41. configMap:
    42. name: clickhouse-users
    43. defaultMode: 511
    44. - name: clickhouse-volume
    45. persistentVolumeClaim:
    46. claimName: clickhouse-pvc
    47. restartPolicy: Always
    48. terminationGracePeriodSeconds: 30
    49. ---
    50. apiVersion: v1
    51. kind: Service
    52. metadata:
    53. name: clickhouse
    54. namespace: test
    55. spec:
    56. ports:
    57. - port: 8123
    58. protocol: TCP
    59. targetPort: 8123
    60. selector:
    61. app: clickhouse
    62. type: ClusterIP

    检查服务是否正常

    1. Events:
    2. Type Reason Age From Message
    3. ---- ------ ---- ---- -------
    4. Normal Scheduled <unknown> default-scheduler Successfully assigned test/clickhouse-bd6cb4f4b-8b6lx to k8s-02
    5. Normal Pulling 6m17s kubelet, k8s-02 Pulling image "clickhouse/clickhouse-server"
    6. Normal Pulled 4m25s kubelet, k8s-02 Successfully pulled image "clickhouse/clickhouse-server"
    7. Normal Created 4m20s kubelet, k8s-02 Created container clickhouse
    8. Normal Started 4m17s kubelet, k8s-02 Started container clickhouse

    检查pod svc状态

    1. [root@k8s-01 clickhouse]# kubectl get pod -n test
    2. NAME READY STATUS RESTARTS AGE
    3. clickhouse-bd6cb4f4b-8b6lx 1/1 Running 0 7m4s
    4. [root@k8s-01 clickhouse]# kubectl get svc -n test
    5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    6. clickhouse ClusterIP 10.100.88.207 <none> 8123/TCP 7m23s

    pod内部调用测试

    1. [root@k8s-01 clickhouse]# kubectl exec -it -n test clickhouse-bd6cb4f4b-8b6lx bash #进入到容器
    2. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
    3. root@clickhouse-bd6cb4f4b-8b6lx:/# clickhouse-client #连接客户端
    4. ClickHouse client version 21.12.3.32 (official build).
    5. Connecting to localhost:9000 as user default.
    6. Connected to ClickHouse server version 21.12.3 revision 54452.
    7. clickhouse-bd6cb4f4b-8b6lx :) show databases; #查看数据库
    8. SHOW DATABASES
    9. Query id: d89a782e-2fb5-47e8-a4e0-1ab3aa038bdf
    10. ┌─name───────────────┐
    11. │ INFORMATION_SCHEMA │
    12. default
    13. │ information_schema │
    14. system
    15. └────────────────────┘
    16. 4 rows in set. Elapsed: 0.003 sec.
    17. clickhouse-bd6cb4f4b-8b6lx :) create database abcdocker #创建测试库
    18. CREATE DATABASE abcdocker
    19. Query id: 3a7aa992-9fe1-49fe-bc54-f537e0f4a104
    20. Ok.
    21. 0 rows in set. Elapsed: 3.353 sec.
    22. clickhouse-bd6cb4f4b-8b6lx :) show databases;
    23. SHOW DATABASES
    24. Query id: c53996ba-19de-4ffa-aa7f-2f3c305d5af5
    25. ┌─name───────────────┐
    26. │ INFORMATION_SCHEMA │
    27. │ abcdocker │
    28. default
    29. │ information_schema │
    30. system
    31. └────────────────────┘
    32. 5 rows in set. Elapsed: 0.006 sec.
    33. clickhouse-bd6cb4f4b-8b6lx :) use abcdocker;
    34. USE abcdocker
    35. Query id: e8302401-e922-4677-9ce3-28c263d162b1
    36. Ok.
    37. 0 rows in set. Elapsed: 0.002 sec.
    38. clickhouse-bd6cb4f4b-8b6lx :) show tables
    39. SHOW TABLES
    40. Query id: 29b3ec6d-6486-41f5-a526-28e80ea17107
    41. Ok.
    42. 0 rows in set. Elapsed: 0.003 sec.
    43. clickhouse-bd6cb4f4b-8b6lx :)

    接下来我们创建一个Telnet容器,测试直接使用svc name访问容器是否正常

    1. kubectl run -n test --generator=run-pod/v1 -i --tty busybox --image=busybox --restart=Never -- sh
    2. / # telnet clickhouse 8123
    3. Connected to clickhouse
    4. #如果不在同一个命名空间就需要使用clickhouse.test.svc.cluster.local

    外部访问Clickhouse

    k8s内部调用我们采用的是svc name,外部可以通过nodeport实现

    1. #svc 外部yaml如下
    2. apiVersion: v1
    3. kind: Service
    4. metadata:
    5. name: clickhouse-node
    6. namespace: test
    7. spec:
    8. ports:
    9. - port: 8123
    10. protocol: TCP
    11. targetPort: 8123
    12. selector:
    13. app: clickhouse
    14. type: NodePort
    15. [root@k8s-01 clickhouse]# kubectl get svc -n test
    16. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    17. clickhouse ClusterIP 10.100.88.207 8123/TCP 33m
    18. clickhouse-node NodePort 10.99.147.187 8123:32445/TCP 8s
    19. #如果用的阿里云托管可以直接使用阿里云LoadBalancer
    20. apiVersion: v1
    21. kind: Service
    22. metadata:
    23. annotations:
    24. service.beta.kubernetes.io/alibaba-cloud-loadbalancer-id: "xxxx"
    25. service.beta.kubernetes.io/alibaba-cloud-loadbalancer-force-override-listeners: "true"
    26. name: clickhouse-ck
    27. namespace: test
    28. spec:
    29. ports:
    30. - port: 8123
    31. protocol: TCP
    32. targetPort: 8123
    33. selector:
    34. app: clickhouse
    35. type: LoadBalancer

    首先需要下载Windows工具

    https://d.frps.cn/file/kubernetes/clickhouse/dbeaver-ce-7.1.4-x86_64-setup.exe

    接下来连接ck,查看我们创建的库是否存在 (安装下载的软件包)

     添加clickhouse连接

     

     

     这里已经可以看到我们创建的库,里面只是一个空库

     如果我们需要给ck设置密码,需要修改我们挂载的configmap即可

    1. root@clickhouse-bd6cb4f4b-8b6lx:/etc/clickhouse-server# cat users.xml |grep pass
    2. <!-- See also the files in users.d directory where the password can be overridden.
    3. If you want to specify password in plaintext (not recommended), place it in 'password' element.
    4. Example: <password>qwerty</password>.
    5. If you want to specify SHA256, place it in 'password_sha256_hex' element.
    6. Example: <password_sha256_hex>65e84be33532fb784c48129675f9eff3a682b27168c0ea744b2cf58ee02337c5</password_sha256_hex>
    7. If you want to specify double SHA1, place it in 'password_double_sha1_hex' element.
    8. Example: <password_double_sha1_hex>e395796d6546b1b65db9d665cd43f0e858dd4303</password_double_sha1_hex>
    9. place 'kerberos' element instead of 'password' (and similar) elements.
    10. How to generate decent password:
    11. In first line will be password and in second - corresponding SHA256.
    12. In first line will be password and in second - corresponding double SHA1.
    13. <password></password> #设置免密参数
  • 相关阅读:
    001 Python开发环境搭建
    洛谷P1242 新汉诺塔
    SPA项目之动态树&数据表格&分页
    3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊
    04_BFC
    MySQL驱动包下载
    SpringBoot系列之动态定时程序改进版
    C/C++教程 从入门到精通《第十六章》—— 网络编程详解
    docker启动容器报错
    Function 洛谷P1464
  • 原文地址:https://blog.csdn.net/zfw_666666/article/details/126121436