Example: Deploying Cassandra with a StatefulSet
本教程向你展示如何在 Kubernetes 上运行 Apache Cassandra。Cassandra 作为一种数据库,需要持久存储来确保数据的持久性(应用程序状态)。在本示例中,一个自定义的 Cassandra 种子节点提供程序使得数据库能够在新的 Cassandra 实例加入 Cassandra 集群时发现它们。
有状态集(StatefulSets)让将有状态应用程序部署到 Kubernetes 集群中变得更加容易。关于本教程中所使用特性的更多信息,请查阅 “有状态集(StatefulSet)” 相关内容。
注意:
Cassandra 和 Kubernetes 都使用 “节点(node)” 这一术语来表示集群中的成员。在本教程中,属于有状态集(StatefulSet)的 Pod 是 Cassandra 节点,并且是 Cassandra 集群(称为环)的成员。当这些 Pod 在你的 Kubernetes 集群中运行时,Kubernetes 控制平面会将这些 Pod 调度到 Kubernetes 节点上。
当一个 Cassandra 节点启动时,它会使用一个种子节点列表来引导对环中其他节点的发现。本教程部署了一个自定义的 Cassandra 种子节点提供程序,它使得数据库能够在新的 Cassandra Pod 出现在你的 Kubernetes 集群中时发现它们。
Objectives
- 创建并验证一个 Cassandra 无头服务。
- 使用有状态集创建一个 Cassandra 集群环。
- 验证该有状态集。
- 修改有状态集。
- 删除有状态集及其 Pod。
Before you begin
你需要拥有一个 Kubernetes 集群,并且必须配置好 kubectl 命令行工具,以便与你的集群进行通信。建议在一个至少有两个并非充当控制平面主机的节点的集群上运行本教程。 要完成本教程,你应该已经对 Pod(容器组)、服务(Service)和有状态集(StatefulSet)有了基本的了解。
Additional Minikube setup instructions
注意:
Minikube 的默认配置为 2048MB 内存和 2 个 CPU 核心。在本教程中,使用默认资源配置运行 Minikube 会导致资源不足的错误。为避免这些错误,请使用以下设置启动 Minikube:
minikube start --memory 5120 --cpus=4Creating a headless Service for Cassandra
在 Kubernetes 中,一个服务(Service)描述了一组执行相同任务的容器组(Pods)。
以下这个服务用于在你的集群内的 Cassandra 容器组(Pods)与客户端之间进行 DNS 查找:
apiVersion: v1
kind: Service
metadata:
labels:
app: cassandra
name: cassandra
spec:
clusterIP: None
ports:
- port: 9042
selector:
app: cassandra从 cassandra-service.yaml 文件创建一个服务,用于跟踪所有 Cassandra 有状态集(StatefulSet)的成员:
kubectl apply -f https://k8s.io/examples/application/cassandra/cassandra-service.yamlValidating (optional)
获取Cassandra 服务:
kubectl get svc cassandra响应内容如下类似:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cassandra ClusterIP None <none> 9042/TCP 45s如果你没有看到名为 cassandra 的服务,那就意味着创建失败了。请阅读《调试服务》以获取帮助,排查常见问题。
Using a StatefulSet to create a Cassandra ring
下面所包含的有状态集(StatefulSet)清单文件,会创建一个由三个容器组(Pods)组成的 Cassandra 集群环。
注意: 本示例使用了 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 (opens in a new tab) 下载该文件,然后在保存修改版本的文件夹中应用该清单文件。
# Use this if you needed to modify cassandra-statefulset.yaml locally
kubectl apply -f cassandra-statefulset.yamlValidating the Cassandra StatefulSet
获取Cassandra 状态集:
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 命令,以显示集群环的状态。
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-3dc5Modifying the Cassandra StatefulSet
使用 kubectl edit 命令来修改 Cassandra 有状态集(StatefulSet)的规模。
运行以下命令:
kubectl edit statefulset cassandr此命令会在你的终端中打开一个编辑器。你需要修改的是 replicas 字段这一行。以下示例是有状态集文件的节选内容。
# 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,然后保存清单文件。
此时,有状态集(StatefulSet)会进行扩缩容,以运行 4 个 Pod。
获取 Cassandra 有状态集的信息,以验证你的更改是否生效。
kubectl get statefulset cassandrThe response should be similar to
NAME DESIRED CURRENT AGE
cassandra 4 4 36mCleaning up
删除有状态集(StatefulSet)或将其缩容并不会删除与该有状态集相关联的存储卷。这项设置是为了保障你的数据安全,因为你的数据比自动清除所有与有状态集相关的资源更具价值。
警告: 根据存储类(Storage Class)和回收策略(Reclaim Policy)的不同,删除持久卷声明(PersistentVolumeClaims)可能会导致相关联的存储卷也被删除。如果持久卷声明被删除,千万不要认为你还能够访问其中的数据。
运行以下命令(将这些命令串联成一条单独的命令)来删除 Cassandra 有状态集中的所有内容。
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=cassandr运行以下命令来删除你为 Cassandra 设置的服务:
kubectl delete service -l app=cassandraCassandra container environment variables
本教程中的 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