k8s安装redis,使用持久化数据配置

参考地址:https://www.cnblogs.com/pythonlx/p/17766242.html

NFS服务器IP地址为192.168.1.230,共享目录为/data/nfs

1.在master主机上安装nfs服务

# yum install -y nfs-utils nfs-server rpcbind
# mkdir -p /data/nfs
# echo "/data/nfs/ *(insecure,rw,sync,no_root_squash)" >> /etc/exports
# cat /etc/exports
/data/nfs/ *(insecure,rw,sync,no_root_squash)

# systemctl enable rpcbind
# systemctl enable nfs-server
# systemctl start rpcbind
# systemctl start nfs-server

nfs 配置生效命令:exportfs -r

2.安装SC

使用NFS的sc需要安装NFS provisioner,可以自动创建NFS的pv。使用gitee 导入 https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner

# git clone https://gitee.com/ikubernetesi/nfs-subdir-external-provisioner.git
正克隆到 'nfs-subdir-external-provisioner'...
remote: Enumerating objects: 7784, done.
remote: Counting objects: 100% (7784/7784), done.
remote: Compressing objects: 100% (3052/3052), done.
remote: Total 7784 (delta 4443), reused 7784 (delta 4443), pack-reused 0
接收对象中: 100% (7784/7784), 8.44 MiB | 1.57 MiB/s, done.
处理 delta 中: 100% (4443/4443), done.

# cd nfs-subdir-external-provisioner/deploy/
# sed -i 's/namespace: default/namespace: kube-system/' rbac.yaml  # 这一步可以不用操作
# kubectl  apply  -f rbac.yaml 
serviceaccount/nfs-client-provisioner created
clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner created
clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner created
role.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
rolebinding.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created

修改deployment.yaml
# sed -i 's/namespace: default/namespace: kube-system/' deployment.yaml ##修改命名空间为kube-system # 这一步可以不用操作

修改nfs-subdir-external-provisioner镜像仓库和nfs地址
 # cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          #image: registry.k8s.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
          image: registry.cn-hangzhou.aliyuncs.com/smallsoup/nfs-subdir-external-provisioner:v4.0.2
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: k8s-sigs.io/nfs-subdir-external-provisioner
            - name: NFS_SERVER
              value: 192.168.1.230
            - name: NFS_PATH
              value: /data/nfs
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.1.230
            path: /data/nfs

应用YAML

# kubectl  apply -f deployment.yaml 
deployment.apps/nfs-client-provisioner created

# kubectl  apply -f class.yaml 
storageclass.storage.k8s.io/nfs-client created

SC YAML示例

# cat class.yaml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"

3.redis使用的yaml文件

# vim redis-sts.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-sts
spec:
  serviceName: redis-svc ##这里要有一个serviceName,Sts必须和service关联
  replicas: 2
  selector:
    matchLabels:
      app: redis-sts
  template:
    metadata:
      labels:
        app: redis-sts
    spec:
      containers:
      - image: redis:6.2
        name: redis
        ports:
        - containerPort: 6379
        volumeMounts:
        - name: redis-pvc
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: redis-pvc
    spec:
      storageClassName: nfs-client
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 500Mi

# vi  redis-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: redis-svc
spec:
  selector:
    app: redis-sts
  ports:
  - port: 6379
    protocol: TCP
    targetPort: 6379

 应用YAML 文件

# kubectl apply -f redis-sts.yaml -f redis-svc.yaml
statefulset.apps/redis-sts created
service/redis-svc created

对于Sts的Pod,有如下特点:

Pod名固定有序,后缀从0开始
域名固定,格式为:Pod名.Svc名,例如 redis-sts-0.redis-svc
每个Pod对应的PVC也是固定

4.测试

ping 域名

# kubectl exec -it redis-sts-0 -- bash   ##进去可以ping redis-sts-0.redis-svc 和  redis-sts-1.redis-svc
# apt-get update
# apt-get install iputils-ping  -y

root@redis-sts-0:/data# ping redis-sts-0.redis-svc

创建key

kubectl exec -it redis-sts-0 -- redis-cli
127.0.0.1:6379> set name 'zhang san'
OK
127.0.0.1:6379> set age '18'
OK

模拟故障,删除一个pod: kubectl delete pod redis-sts-0

删除后,它会自动重新创建同名Pod,再次进入查看redis key

kubectl exec -it redis-sts-0 -- redis-cli
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> get age
"18"

数据没有丢失

nfs里的对应目录里存放的是redis的dump.rdb文件

posted @ 2023-11-15 16:42  哈喽哈喽111111  阅读(353)  评论(0编辑  收藏  举报