示例:使用 StatefulSet 部署 Cassandra
本教程向你展示如何在 Kubernetes 上运行 Apache Cassandra。Cassandra 是一种数据库,它需要持久性存储来提供数据持久性(应用 状态)。在这个例子中,一个定制的 Cassandra 种子提供者使得数据库能够发现新加入 Cassandra 集群的 Cassandra 实例。
StatefulSet 使得在 Kubernetes 集群中部署有状态应用变得更容易。要了解本教程中使用的特性,请参见StatefulSet。
注意
Cassandra 和 Kubernetes 都使用术语 node 来表示集群的成员。在本教程中,属于 StatefulSet 的 Pod 是 Cassandra 节点,它们是 Cassandra 集群(称为 ring)的成员。当这些 Pod 运行在你的 Kubernetes 集群中时,Kubernetes 控制平面将这些 Pod 调度到 Kubernetes 节点上。
当 Cassandra 节点启动时,它使用 seed list(种子列表)来引导发现 ring 中的其他节点。本教程部署了一个定制的 Cassandra 种子提供者,使得数据库能够发现出现在 Kubernetes 集群中的新 Cassandra Pod。
目标
- 创建和验证一个 Cassandra 无头 Service。
- 使用 StatefulSet 创建一个 Cassandra ring。
- 验证 StatefulSet。
- 修改 StatefulSet。
- 删除 StatefulSet 及其 Pod。
开始之前
你需要有一个 Kubernetes 集群,并且 kubectl 命令行工具已经配置为与你的集群通信。建议在本教程中使用至少两个不是控制平面主机的节点组成的集群。如果你还没有集群,可以使用 minikube 创建一个,或者使用以下 Kubernetes 体验环境之一:
完成本教程前,你应该已经对 Pod、Service 和 StatefulSet 有基本了解。
Minikube 额外设置说明
注意
Minikube 默认分配 2048MB 内存和 2 CPU。使用默认资源配置运行 Minikube 会导致本教程中资源不足错误。为避免这些错误,请使用以下设置启动 Minikube:
minikube start --memory 5120 --cpus=4
为 Cassandra 创建无头 Service
在 Kubernetes 中,Service 描述执行相同任务的一组 Pod。
以下 Service 用于 Cassandra Pod 与集群内客户端之间的 DNS 查询:
apiVersion: v1
kind: Service
metadata:
labels:
app: cassandra
name: cassandra
spec:
clusterIP: None
ports:
- port: 9042
selector:
app: cassandra
从 cassandra-service.yaml
文件创建 Service,以跟踪所有 Cassandra StatefulSet 成员:
kubectl apply -f https://k8s.io/examples/application/cassandra/cassandra-service.yaml
验证 (可选)
获取 Cassandra Service。
kubectl get svc cassandra
响应如下:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cassandra ClusterIP None <none> 9042/TCP 45s
如果你没有看到名为 cassandra
的 Service,则表示创建失败。请阅读调试 Service,以获取有关常见问题的帮助。
使用 StatefulSet 创建 Cassandra ring
下面包含的 StatefulSet Manifest 创建了一个包含三个 Pod 的 Cassandra ring。
注意
此示例使用 Minikube 的默认供应者。请根据你使用的云环境更新以下 StatefulSet。apiVersion: apps/v1
kind: StatefulSet
metadata:
name: cassandra
labels:
app: cassandra
spec:
serviceName: cassandra
replicas: 3
selector:
matchLabels:
app: cassandra
template:
metadata:
labels:
app: cassandra
spec:
terminationGracePeriodSeconds: 500
containers:
- name: cassandra
image: gcr.io/google-samples/cassandra:v13
imagePullPolicy: Always
ports:
- containerPort: 7000
name: intra-node
- containerPort: 7001
name: tls-intra-node
- containerPort: 7199
name: jmx
- containerPort: 9042
name: cql
resources:
limits:
cpu: "500m"
memory: 1Gi
requests:
cpu: "500m"
memory: 1Gi
securityContext:
capabilities:
add:
- IPC_LOCK
lifecycle:
preStop:
exec:
command:
- /bin/sh
- -c
- nodetool drain
env:
- name: MAX_HEAP_SIZE
value: 512M
- name: HEAP_NEWSIZE
value: 100M
- name: CASSANDRA_SEEDS
value: "cassandra-0.cassandra.default.svc.cluster.local"
- name: CASSANDRA_CLUSTER_NAME
value: "K8Demo"
- name: CASSANDRA_DC
value: "DC1-K8Demo"
- name: CASSANDRA_RACK
value: "Rack1-K8Demo"
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
readinessProbe:
exec:
command:
- /bin/bash
- -c
- /ready-probe.sh
initialDelaySeconds: 15
timeoutSeconds: 5
# These volume mounts are persistent. They are like inline claims,
# but not exactly because the names need to match exactly one of
# the stateful pod volumes.
volumeMounts:
- name: cassandra-data
mountPath: /cassandra_data
# These are converted to volume claims by the controller
# and mounted at the paths mentioned above.
# do not use these in production until ssd GCEPersistentDisk or other ssd pd
volumeClaimTemplates:
- metadata:
name: cassandra-data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: fast
resources:
requests:
storage: 1Gi
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: fast
provisioner: k8s.io/minikube-hostpath
parameters:
type: pd-ssd
从 cassandra-statefulset.yaml
文件创建 Cassandra StatefulSet:
# Use this if you are able to apply cassandra-statefulset.yaml unmodified
kubectl apply -f https://k8s.io/examples/application/cassandra/cassandra-statefulset.yaml
如果你需要修改 cassandra-statefulset.yaml
以适应你的集群,请下载 https://k8s.io/examples/application/cassandra/cassandra-statefulset.yaml,然后从保存修改后版本的文件夹中应用该 Manifest:
# Use this if you needed to modify cassandra-statefulset.yaml locally
kubectl apply -f cassandra-statefulset.yaml
验证 Cassandra StatefulSet
获取 Cassandra StatefulSet:
kubectl get statefulset cassandra
响应应类似于:
NAME DESIRED CURRENT AGE cassandra 3 0 13s
StatefulSet
资源按顺序部署 Pod。获取 Pod 以查看有序的创建状态:
kubectl get pods -l="app=cassandra"
响应应类似于:
NAME READY STATUS RESTARTS AGE cassandra-0 1/1 Running 0 1m cassandra-1 0/1 ContainerCreating 0 8s
部署所有三个 Pod 可能需要几分钟时间。部署完成后,相同的命令将返回类似于以下内容的输出:
NAME READY STATUS RESTARTS AGE cassandra-0 1/1 Running 0 10m cassandra-1 1/1 Running 0 9m cassandra-2 1/1 Running 0 8m
在第一个 Pod 中运行 Cassandra nodetool,以显示 ring 的状态。
kubectl exec -it cassandra-0 -- nodetool status
响应应类似于:
Datacenter: DC1-K8Demo ====================== Status=Up/Down |/ State=Normal/Leaving/Joining/Moving -- Address Load Tokens Owns (effective) Host ID Rack UN 172.17.0.5 83.57 KiB 32 74.0% e2dd09e6-d9d3-477e-96c5-45094c08db0f Rack1-K8Demo UN 172.17.0.4 101.04 KiB 32 58.8% f89d6835-3a42-4419-92b3-0e62cae1479c Rack1-K8Demo UN 172.17.0.6 84.74 KiB 32 67.1% a6a1e8c2-3dc5-4417-b1a0-26507af2aaad Rack1-K8Demo
修改 Cassandra StatefulSet
使用 kubectl edit
修改 Cassandra StatefulSet 的大小。
运行以下命令:
kubectl edit statefulset cassandra
此命令将在终端中打开一个编辑器。你需要更改的行是
replicas
字段。以下是 StatefulSet 文件的一部分示例:# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: apps/v1 kind: StatefulSet metadata: creationTimestamp: 2016-08-13T18:40:58Z generation: 1 labels: app: cassandra name: cassandra namespace: default resourceVersion: "323" uid: 7a219483-6185-11e6-a910-42010a8a0fc0 spec: replicas: 3
将副本数更改为 4,然后保存 Manifest。
StatefulSet 现在将扩缩到运行 4 个 Pod。
获取 Cassandra StatefulSet 以验证你的更改:
kubectl get statefulset cassandra
响应应类似于:
NAME DESIRED CURRENT AGE cassandra 4 4 36m
清理
删除或缩减 StatefulSet 不会删除与 StatefulSet 关联的 Volume。此设置是为了你的数据安全,因为你的数据比自动清除所有相关的 StatefulSet 资源更有价值。
警告
根据存储类和回收策略的不同,删除 PersistentVolumeClaim 可能会导致关联的 Volume 也被删除。切勿假设如果 Volume 声明被删除,你仍然可以访问其数据。运行以下命令(链式组合成单个命令)以删除 Cassandra StatefulSet 中的所有内容:
grace=$(kubectl get pod cassandra-0 -o=jsonpath='{.spec.terminationGracePeriodSeconds}') \ && kubectl delete statefulset -l app=cassandra \ && echo "Sleeping ${grace} seconds" 1>&2 \ && sleep $grace \ && kubectl delete persistentvolumeclaim -l app=cassandra
运行以下命令以删除你为 Cassandra 设置的 Service:
kubectl delete service -l app=cassandra
Cassandra 容器环境变量
本教程中的 Pod 使用 Google 容器镜像仓库中的 gcr.io/google-samples/cassandra:v13
镜像。上述 Docker 镜像基于 debian-base 并包含 OpenJDK 8。
此镜像包含来自 Apache Debian 仓库的标准 Cassandra 安装。通过使用环境变量,你可以更改插入到 cassandra.yaml
中的值。
环境变量 | 默认值 |
---|---|
CASSANDRA_CLUSTER_NAME | 'Test Cluster' |
CASSANDRA_NUM_TOKENS | 32 |
CASSANDRA_RPC_ADDRESS | 0.0.0.0 |
接下来
- 了解如何扩缩 StatefulSet。
- 详细了解 KubernetesSeedProvider
- 查看更多定制的种子提供者配置