Kubernetes的有状态应用示例:使用StatefulSet部署Cassandra

本文主要是介绍Kubernetes的有状态应用示例:使用StatefulSet部署Cassandra,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 环境
  • 概述
  • 准备
  • 为Cassandra创建headless service
  • 验证Cassandra StatefulSet
  • 修改Cassandra StatefulSet
  • 清理
  • Cassandra容器环境变量
  • 参考

环境

  • RHEL 9.3
  • Docker Community 24.0.7
  • minikube v1.32.0

概述

本例展示了如何在Kubernetes上运行Apache Cassandra。Cassandra是一个数据库,需要永久性存储来提供数据持久性(应用状态)。在此示例中,自定义的Cassandra种子提供者使得数据库在新的Cassandra实例接入Cassandra集群时能够发现它们。

注意:Cassandra和Kubernetes都使用术语 node 来表示集群的成员。在本例中,StatefulSet的pod是Cassandra node,并且是Cassandra集群的成员(称为 ring )。当这些pod在Kubernetes集群中运行时,Kubernetes control plane会把这些pod调度到Kubernetes的node上。

当 Cassandra node启动时,使用 seed 列表来引导发现ring中的其它node。本例部署了一个自定义的Cassandra种子提供者,使得数据库在新的Cassandra pod出现在Kubernetes集群中时,可以发现它们。

准备

Minikube默认需要2048MB内存和2个CPU。本例中使用默认资源配置运行Minikube会出现资源不足的错误。为避免这些错误,请使用以下设置启动Minikube:

$ minikube start --memory 5120 --cpus=4
😄  minikube v1.32.0 on Redhat 9.3
✨  Using the docker driver based on existing profile
❗  You cannot change the memory size for an existing minikube cluster. Please first delete the cluster.
❗  You cannot change the CPUs for an existing minikube cluster. Please first delete the cluster.
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔄  Restarting existing docker container for "minikube" ...
🐳  Preparing Kubernetes v1.28.3 on Docker 24.0.7 ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
🔎  Verifying Kubernetes components...Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

注意这两行:

❗  You cannot change the memory size for an existing minikube cluster. Please first delete the cluster.
❗  You cannot change the CPUs for an existing minikube cluster. Please first delete the cluster.

注:如果不管,按默认设置启动minikube,则在后面的步骤会报错,如:

$ kubectl get pods -l="app=cassandra"
Unable to connect to the server: http2: client connection lost$ kubectl get nodes
Unable to connect to the server: net/http: TLS handshake timeout

要改变设置,貌似需要先删除cluster才行:

$ minikube delete
🔥  Deleting "minikube" in docker ...
🔥  Deleting container "minikube" ...
🔥  Removing /home/ding/.minikube/machines/minikube ...
💀  Removed all traces of the "minikube" cluster.

然后再运行:

$ minikube start --memory 5120 --cpus=4
😄  minikube v1.32.0 on Redhat 9.3
✨  Automatically selected the docker driver
📌  Using Docker driver with root privileges
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
❗  minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.42, but successfully downloaded docker.io/kicbase/stable:v0.0.42 as a fallback image
🔥  Creating docker container (CPUs=4, Memory=5120MB) ...
🐳  Preparing Kubernetes v1.28.3 on Docker 24.0.7 ...▪ Generating certificates and keys ...▪ Booting up control plane ...▪ Configuring RBAC rules ...
🔗  Configuring bridge CNI (Container Networking Interface) ...Using image gcr.io/k8s-minikube/storage-provisioner:v5
🔎  Verifying Kubernetes components...
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

为Cassandra创建headless service

以下 Service 用于在Cassandra pod和客户端之间进行DNS查找:

创建文件 cassandra-service.yaml 如下:

apiVersion: v1
kind: Service
metadata:labels:app: cassandraname: cassandra
spec:clusterIP: Noneports:- port: 9042selector:app: cassandra
kubectl apply -f cassandra-service.yaml
$ kubectl get svc cassandra
NAME        TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
cassandra   ClusterIP   None         <none>        9042/TCP   24s

创建文件 cassandra-statefulset.yaml 如下:

apiVersion: apps/v1
kind: StatefulSet
metadata:name: cassandralabels:app: cassandra
spec:serviceName: cassandrareplicas: 3selector:matchLabels:app: cassandratemplate:metadata:labels:app: cassandraspec:terminationGracePeriodSeconds: 1800containers:- name: cassandra# image: gcr.io/google-samples/cassandra:v13image: docker.io/kaiding1/cassandra:v13imagePullPolicy: Alwaysports:- containerPort: 7000name: intra-node- containerPort: 7001name: tls-intra-node- containerPort: 7199name: jmx- containerPort: 9042name: cqlresources:limits:cpu: "500m"memory: 1Girequests:cpu: "500m"memory: 1GisecurityContext:capabilities:add:- IPC_LOCKlifecycle:preStop:exec:command: - /bin/sh- -c- nodetool drainenv:- name: MAX_HEAP_SIZEvalue: 512M- name: HEAP_NEWSIZEvalue: 100M- name: CASSANDRA_SEEDSvalue: "cassandra-0.cassandra.default.svc.cluster.local"- name: CASSANDRA_CLUSTER_NAMEvalue: "K8Demo"- name: CASSANDRA_DCvalue: "DC1-K8Demo"- name: CASSANDRA_RACKvalue: "Rack1-K8Demo"- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIPreadinessProbe:exec:command:- /bin/bash- -c- /ready-probe.shinitialDelaySeconds: 15timeoutSeconds: 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-datamountPath: /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 pdvolumeClaimTemplates:- metadata:name: cassandra-dataspec:accessModes: [ "ReadWriteOnce" ]storageClassName: fastresources:requests:storage: 1Gi
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:name: fast
provisioner: k8s.io/minikube-hostpath
parameters:type: pd-ssd

注:因为访问不了 gcr.io ,所以事先把image pull下来,并push到了可访问的位置。

$ kubectl apply -f cassandra-statefulset.yaml
statefulset.apps/cassandra created
storageclass.storage.k8s.io/fast created

验证Cassandra StatefulSet

注:启动pod特别慢,本例中花了半个多小时。

$ kubectl get statefulset cassandra
NAME        READY   AGE
cassandra   3/3     53m
$ kubectl get pods -l="app=cassandra"
NAME          READY   STATUS    RESTARTS   AGE
cassandra-0   1/1     Running   0          53m
cassandra-1   1/1     Running   0          47m
cassandra-2   1/1     Running   0          37m
$ 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  10.244.0.4  142.7 KiB  32           72.8%             913518a2-c96e-4915-9b95-dc26c8c4e9a7  Rack1-K8Demo
UN  10.244.0.5  143.1 KiB  32           70.6%             e5689d71-a4b9-4a20-af0b-147ea2ec4c57  Rack1-K8Demo
UN  10.244.0.3  135.94 KiB  32           56.6%             af529f81-7da7-4129-b379-d41554390c6f  Rack1-K8Demo

修改Cassandra StatefulSet

kubectl edit statefulset cassandra
# 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:58Zgeneration: 1labels:app: cassandraname: cassandranamespace: defaultresourceVersion: "323"uid: 7a219483-6185-11e6-a910-42010a8a0fc0
spec:replicas: 3

把replica数量从3改为4,然后保存退出。

$ kubectl edit statefulset cassandra
statefulset.apps/cassandra edited

大约5分钟后,查看状态:

$ kubectl get pods -l="app=cassandra"
NAME          READY   STATUS    RESTARTS   AGE
cassandra-0   1/1     Running   0          101m
cassandra-1   1/1     Running   0          96m
cassandra-2   1/1     Running   0          85m
cassandra-3   1/1     Running   0          5m
$ kubectl get statefulset cassandra
NAME        READY   AGE
cassandra   4/4     101m

清理

删除或缩小StatefulSet不会删除与之关联的volume。这是出于安全考虑,因为与自动清除所有相关的StatefulSet资源相比,数据更有价值。

注意:根据Storage Class和回收策略,删除PVC可能导致相关联的volume也被删除。不要想当然认为PVC被删除后,还能访问数据。

$ 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
statefulset.apps "cassandra" deleted
Sleeping 1800 seconds
persistentvolumeclaim "cassandra-data-cassandra-0" deleted
persistentvolumeclaim "cassandra-data-cassandra-1" deleted
persistentvolumeclaim "cassandra-data-cassandra-2" deleted
persistentvolumeclaim "cassandra-data-cassandra-3" deleted
$ kubectl delete service -l app=cassandra
service "cassandra" deleted

Cassandra容器环境变量

本例中的pod 使用来自Google容器registry的 gcr.io/google-samples/cassandra:v13 image。该Docker image基于debian-base,且包含 OpenJDK 8。

注:因为访问不了 gcr.io ,所以事先把image pull下来,并push到了可访问的位置。

该镜像包括Apache Debian repo的标准Cassandra安装。通过环境变量,你可以更改插入到 cassandra.yaml 中的值。

环境变量缺省值
CASSANDRA_CLUSTER_NAME'Test Cluster'
CASSANDRA_NUM_TOKENS32
CASSANDRA_RPC_ADDRESS0.0.0.0

参考

  • https://kubernetes.io/docs/tutorials/stateful-application/cassandra

这篇关于Kubernetes的有状态应用示例:使用StatefulSet部署Cassandra的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/681289

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释

MobaXterm远程登录工具功能与应用小结

《MobaXterm远程登录工具功能与应用小结》MobaXterm是一款功能强大的远程终端软件,主要支持SSH登录,拥有多种远程协议,实现跨平台访问,它包括多会话管理、本地命令行执行、图形化界面集成和... 目录1. 远程终端软件概述1.1 远程终端软件的定义与用途1.2 远程终端软件的关键特性2. 支持的

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring