使用模板
模板可让你一次性声明 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. 没有关联,也未获其认可或赞助。所有商标均归其各自所有者所有,此处仅用于标识目的。