使用模板
模板可让您声明一次 pod 形状、存储和 Service 形状,并从实例中引用它们。模板有两种形式:
spec.templates 下的内联模板,属于一个 ClickHouseInstallation。
- 位于
ClickHouseInstallationTemplate(CHIT)中的共享模板,实例通过 spec.useTemplates 引入。
两种情况下使用的机制相同;CHIT 只是将一个定义集中在一个位置的方式。
模板类型
在 spec.defaults.templates 下设置选择器,即可将其应用于整个实例;也可以在集群、分片或从节点级别进行覆盖。
NOTE
volumeClaimTemplate(单数形式)也存在于架构中,但已弃用。请使用 dataVolumeClaimTemplate 和 logVolumeClaimTemplate。
内联 pod 和卷模板
apiVersion: clickhouse.altinity.com/v1
kind: ClickHouseInstallation
metadata:
name: e1-templated
spec:
defaults:
templates:
podTemplate: ch-pod
dataVolumeClaimTemplate: data-volume
configuration:
clusters:
- name: main
layout:
shardsCount: 2
replicasCount: 1
templates:
podTemplates:
- name: ch-pod
podDistribution:
- type: ShardAntiAffinity
spec:
securityContext:
runAsUser: 101
runAsGroup: 101
fsGroup: 101
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: clickhouse
resources:
requests:
cpu: "2"
memory: 8Gi
limits:
cpu: "4"
memory: 16Gi
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
volumeClaimTemplates:
- name: data-volume
reclaimPolicy: Retain
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 200Gi
需要注意的事项:
- 服务器容器必须命名为
clickhouse。名称不同的容器是额外的 sidecar,而不是服务器,因此您的资源和安全上下文不会应用于服务器。
- 在 pod 模板中省略
image。未设置镜像时,operator 会使用其在安装时配置的 operand 镜像,即经过此版本验证的版本。固定使用您自己的镜像将不再享受这一行为。
- 卷声明模板中的
reclaimPolicy: Retain 会在删除 StatefulSet 时保留声明。默认值为 Delete。
podDistribution 用于表达分布规则——例如,ShardAntiAffinity 可让不同分片的从节点不位于同一节点上——无需手动编写亲和性条件。
使用 CHIT 共享模板
定义一次模板:
apiVersion: clickhouse.altinity.com/v1
kind: ClickHouseInstallationTemplate
metadata:
name: standard-pod
spec:
templates:
podTemplates:
- name: ch-pod
spec:
securityContext:
runAsUser: 101
runAsGroup: 101
fsGroup: 101
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: clickhouse
resources:
requests:
cpu: "2"
memory: 8Gi
limits:
cpu: "4"
memory: 16Gi
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
从实例中引用它:
spec:
useTemplates:
- name: standard-pod
- name: shared-storage
namespace: platform-templates
defaults:
templates:
podTemplate: ch-pod
useTemplates 条目接受一个 name、一个可选的 namespace,以及一个可选的 useType,后者可以为空或为 merge。模板会按照列出的顺序应用,直接在实例上设置的任何内容优先于模板提供的内容。在实时对象上检查结果——完成默认值设置和模板合并后的协调规范会发布在实例的状态中,这是确认模板已生效的最快方式。
还可以通过为 CHIT 标记 spec.templating.policy: auto,将模板自动应用于每个实例。请谨慎使用:这会更改完全未提及该模板的实例。
从 operator 自有模板目录加载的模板文件必须以 .yaml 或 .json 结尾,并按照排序后的顺序读取。任何其他扩展名的文件都会被忽略。
日志容器逃生舱
设置 logVolumeClaimTemplate 时,operator 会向 pod 添加一个名为 clickhouse-log 的容器。其生成的安全上下文仅设置用户 ID,而强制执行 Pod Security Admission restricted 配置文件的命名空间会拒绝这种设置。
在 pod 模板中自行声明该容器,operator 就会复用您声明的容器,而不是生成一个容器。只有在您未设置镜像时,它才会填充镜像:
templates:
podTemplates:
- name: ch-pod
spec:
securityContext:
runAsUser: 101
runAsGroup: 101
fsGroup: 101
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: clickhouse
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
- name: clickhouse-log
securityContext:
runAsUser: 65534
runAsNonRoot: true
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
seccompProfile:
type: RuntimeDefault
容器名称必须完全匹配,否则您将同时得到自己的 sidecar 和 operator 生成的容器。
更改模板
编辑正在运行的实例所引用的模板,会触发这些实例的滚动协调。主机会逐个更新:operator 将主机从集群中排除,更新其配置和 StatefulSet,等待主机恢复,然后再次将其纳入集群。因此,无法成功执行的更改——例如不存在的镜像,或小到无法启动服务器的内存限制——会卡在第一个主机上,而不会一次性导致所有主机中断。
ClickHouse 是 ClickHouse, Inc. 的注册商标。https://clickhouse.com
Alauda 是独立供应商。本产品与 ClickHouse, Inc. 没有任何关联,也未获得其认可或赞助。所有商标均为其各自所有者的财产,此处仅用于标识目的。