一、使用helm安装redis
执行以下命令添加redis的repo
helm repo add bitnami https://charts.bitnami.com/bitnami
创建的master和replica pod的默认size是8Gi,如果k8s的node没有足够的空间,会抛出如下错误:default-scheduler 0/3 nodes are available: pod has unbound immediate PersistentVolumeClaims. preemption: 0/3 nodes are available: 3 No preemption victims found for incoming pod为此,我们可以扩大node的size,或在安装时重新设置pod的size,设置方法如下:
helm install --set replica.persistence.size=2Gi --set master.persistence.size=2Gi my-redis bitnami/redis
其他参数请参见redis 17.11.6 · bitnami/bitnami
二、创建并部署storageclass
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: manual
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
三、创建并部署PV (persistent volumes)
apiVersion: v1
kind: PersistentVolume
metadata:
name: $pv_name
spec:
storageClassName: manual
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: $data_path
pv的storageClassName指向先前创建的storageclass (manual)。此外,还需要指定data的存放路径 hostPath,这要求在k8s的各node上创建该路径,并修改路径权限;
chmod 777 $data_path
否则pod会抛出如下错误: Can't open or create append-only dir appendonlydir: Permission denied
四、修改pvc
修改redis下master & replica pod使用的pvc,使其指向步骤3中创建的pv
metadata:
annotations:
pv.kubernetes.io/bind-completed: "yes"
pv.kubernetes.io/bound-by-controller: "yes"
creationTimestamp: "2023-06-29T11:18:24Z"
finalizers:
- kubernetes.io/pvc-protection
labels:
app.kubernetes.io/component: master
app.kubernetes.io/instance: my-redis
app.kubernetes.io/name: redis
name: redis-data-my-redis-master-0
namespace: default
resourceVersion: "2608115"
uid: 4f1b0e39-8078-4fdc-8aff-388437ab9922
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
storageClassName: $storage_name #指定storage name
volumeMode: Filesystem
volumeName: $pv_name #指定pv
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 2Gi
phase: Bound
~
五、重要参考文献
1. storage、pv、pvc的关联关系及配置方法
kubernetes - Error "no persistent volumes available for this claim and no storage class is set" - Stack Overflow
2. 详细的安装过程
iDeploying Redis Cluster on Kubernetes | AirplaneiyIn this guide, learn how to run Redis on Kubernetes and explore tips for improving performance, security, and more.https://www.airplane.dev/blog/deploy-redis-cluster-on-kubernetes
六、相关知识点
通过层层的关联关系实现了host上的path与container上path的绑定(mount),进而实现在container销毁的情况下,其在mountpath下内容会存储在hostpath中。
volume
On-disk files in a container are ephemeral, which presents some problems for non-trivial applications when running in containers. One problem occurs when a container crashes or is stopped. Container state is not saved so all of the files that were created or modified during the lifetime of the container are lost. During a crash, kubelet restarts the container with a clean state. Another problem occurs when multiple containers are running in a Pod
and need to share files. It can be challenging to setup and access a shared filesystem across all of the containers. The Kubernetes volume abstraction solves both of these problems. Familiarity with Pods is suggested.
a volume is a directory, possibly with some data in it, which is accessible to the containers in a pod. How that directory comes to be, the medium that backs it, and the contents of it are determined by the particular volume type used.
Ephemeral volume types have a lifetime of a pod, but persistent volumes exist beyond the lifetime of a pod. When a pod ceases to exist, Kubernetes destroys ephemeral volumes; however, Kubernetes does not destroy persistent volumes. For any kind of volume in a given pod, data is preserved across container restarts.