K8ssandra入门-详细记录在Linux上部署K8ssandra到Kubernetes

时间:2023-12-16 15:59:14

1 什么是K8ssandra

Cassandra是一款非常优秀的开源的分布式NoSQL数据库,被许多优秀的大公司采用,具有高可用、弹性扩展、性能好等特点。

K8ssandra入门-详细记录在Linux上部署K8ssandra到Kubernetes

正应Cassandra的优势,我们经常需要在云上服务使用,则需要部署Cassandra到K8s上,这就有了K8ssandra。K8ssandra不仅帮助我们可以快速可靠地在Kubernetes上部署Cassandra,同时提供了许多组件,如监控、备份、同步、访问等。而这些都是一个Production-Ready的产品不可或缺的。

K8ssandra的组件架构图如下:

K8ssandra入门-详细记录在Linux上部署K8ssandra到Kubernetes

  • Cass-operator:保证整个集群正常运行;
  • Reaper:保证一致性的同步工具;
  • Medusa:数据备份工具,支持S3、GCP Cloud Storage等;
  • Stargate:对数据访问提供API;
  • Prometheus+Grafana:云原生的常用监控组件。

2 安装K8ssandra

2.1 安装Kubenetes

如何在Ubuntu上通过Minikube快速启动一个Kubernetes,在文章《服务网格Istio入门-详细记录Kubernetes安装Istio并使用》已经有详细的介绍,这里不再赘述。为了更好的兼容性,我们指定了Kubernetes的版本,命令如下:

minikube start --driver=none --kubernetes-version=v1.19.13

因为要用到PVC,我们创建一个StorageClass:

$ kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml

2.2 安装helm3

我们需要使用Helm来部署K8ssandra,下载Helm3如下:

# 下载安装包
curl -LO https://get.helm.sh/helm-v3.7.0-linux-amd64.tar.gz
# 解压
tar -zxvf helm-v3.7.0-linux-amd64.tar.gz
# 移动到bin目录
mv linux-amd64/helm /usr/local/bin/helm

添加Helm的仓库:

helm repo add k8ssandra https://helm.k8ssandra.io/stable

$ helm repo list
NAME URL
k8ssandra https://helm.k8ssandra.io/stable
traefik https://helm.traefik.io/traefik

查找一下K8ssandra相关的包:

$ helm search repo k8ssandra
NAME CHART VERSION APP VERSION DESCRIPTION
k8ssandra/k8ssandra 1.3.1 Provisions and configures an instance of the en...
k8ssandra/k8ssandra-common 0.28.4 Helper library containing functions used by man...
k8ssandra/k8ssandra-operator 0.31.0 1.0.0 Kubernetes operator which handles the provision...
k8ssandra/backup 0.26.0 Creates a CassandraBackup custom resource insta...
k8ssandra/cass-operator 0.31.0 1.8.0 Kubernetes operator which handles the provision...
k8ssandra/medusa-operator 0.30.1 0.1.0 Installs and configures the Medusa Operator for...
k8ssandra/reaper-operator 0.32.1 0.1.0 Configures and installs the Reaper Operator for...
k8ssandra/restore 0.27.1 Creates a CassandraRestore custom resource inst...

我们安装k8ssandra/k8ssandra就可以了。

2.3 用Helm安装K8ssandra

Helm是一个Chart+Value的管理方式,我们准备一个yaml文件(k8ssandra-values.yaml)来放一些变量:

cassandra:
version: "4.0.0"
cassandraLibDirVolume:
storageClass: local-path
size: 5Gi
allowMultipleNodesPerWorker: true
heap:
size: 1G
newGenSize: 1G
resources:
requests:
cpu: 1000m
memory: 2Gi
limits:
cpu: 1000m
memory: 2Gi
datacenters:
- name: dc1
size: 1
racks:
- name: default
kube-prometheus-stack:
grafana:
adminUser: admin
adminPassword: admin123
stargate:
enabled: true
replicas: 1
heapMB: 256
cpuReqMillicores: 200
cpuLimMillicores: 1000

安装K8ssandra:

$ helm install -f k8ssandra-values.yaml k8ssandra k8ssandra/k8ssandra
NAME: k8ssandra
LAST DEPLOYED: Thu Sep 30 21:20:49 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1

做一些必要的检查如下:

$ helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
k8ssandra default 1 2021-09-30 21:20:49.409672869 +0800 CST deployed k8ssandra-1.3.1 $ kubectl get cassandradatacenters
NAME AGE
dc1 4m34s $ kubectl describe CassandraDataCenter dc1 | grep "Cassandra Operator Progress:"
Cassandra Operator Progress: Ready

检查一下Pod和Service:

K8ssandra入门-详细记录在Linux上部署K8ssandra到Kubernetes

获取K8ssandra超级用户的用户名和密码

$ kubectl get secret k8ssandra-superuser -o jsonpath="{.data.username}" | base64 --decode ; echo
k8ssandra-superuser $ kubectl get secret k8ssandra-superuser -o jsonpath="{.data.password}" | base64 --decode ; echo
TNE5xOk45C1aQsj29qxw

2.4 增加节点

我们为了高可用和容量,创建更多的Cassandra Node,直接修改k8ssandra-values.yaml如下:

cassandra:
version: "4.0.0"
cassandraLibDirVolume:
storageClass: local-path
size: 5Gi
allowMultipleNodesPerWorker: true
heap:
size: 1G
newGenSize: 1G
resources:
requests:
cpu: 1000m
memory: 2Gi
limits:
cpu: 1000m
memory: 2Gi
datacenters:
- name: dc1
size: 3
racks:
- name: racks1
- name: racks2
- name: racks3
kube-prometheus-stack:
grafana:
adminUser: admin
adminPassword: admin123
stargate:
enabled: true
replicas: 1
heapMB: 256
cpuReqMillicores: 200
cpuLimMillicores: 1000

修改完之后,升级配置:

$ helm upgrade -f k8ssandra-values.yaml k8ssandra k8ssandra/k8ssandra
Release "k8ssandra" has been upgraded. Happy Helming!
NAME: k8ssandra
LAST DEPLOYED: Fri Oct 1 00:40:08 2021
NAMESPACE: default
STATUS: deployed
REVISION: 2

查看Kubernetes的相关资源:

K8ssandra入门-详细记录在Linux上部署K8ssandra到Kubernetes

3 查看监控

我们对外暴露Grafana服务来看看:

kubectl expose deployment k8ssandra-grafana --type=NodePort --name=grafana-out

找到对应的NodePort的端口30348,访问:http://外网IP:30348

账号:admin/admin123

界面如下,提供良好的监控界面:

K8ssandra入门-详细记录在Linux上部署K8ssandra到Kubernetes

4 总结

K8ssandra真是一个不错的开源项目,后续再介绍如何在开发中通过K8ssandra使用Cassandra吧。

代码请查看:https://github.com/LarryDpk/pkslow-samples