一、背景
公司内部自建k8s集群有一台节点挂了导致部分容器异常,redis未作数据持久化导致数据丢失,经查看原因为redis容器未作数据持久化,现对redis进行数据持久化以及安装redis-dump、redis-load导出原redis已有数据。
二、步骤
1.拉取redis容器。
docker pull redis
docker tag redis:latest 10.0.16.250/fuliao-library/redis:latest
docker push 10.0.16.250/fuliao-library/redis:latest
2.编写k8s yaml配置。
####configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-conf-new
  namespace: k8s-gold-gourd-test-01
data:
  redis.conf: |+
    requirepass ytflredis
    protected-mode no
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /data
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    slave-lazy-flush no
    appendonly yes
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble no
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events Ex
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes
####redis-pv-pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv
  namespace: k8s-gold-gourd-test-01
spec:
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: redis-nfs
  nfs:
    path: /k8s-data/test01-redis-data
    server: 10.0.16.167
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pvc
  namespace: k8s-gold-gourd-test-01
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: redis-nfs
####redis-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: redis-svc
  namespace: k8s-gold-gourd-test-01
spec:
  selector:
    name: redis
  ports:
  - protocol: TCP
    port: 6379
    targetPort: 6379
####deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: k8s-gold-gourd-test-01
  labels:
    name: redis
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      name: redis
  template:
    metadata:
      labels:
        name: redis
      annotations:
        version/date: "20210310"
        version/author: "lc"
    spec:
      containers:
      - name: redis
        image: 10.0.16.250/fuliao-library/redis:latest
        imagePullPolicy: IfNotPresent
        command: ["redis-server","/etc/redis/redis.conf"]
        ports:
        - containerPort: 6379
        volumeMounts:
        - name: redis-config
          mountPath: /etc/redis/redis.conf
          subPath: redis.conf
        - name: redis-persistent-storage
          mountPath: /data
      volumes:
      - name: redis-config
        configMap:
          name: redis-conf-new
          items:
          - key: redis.conf
            path: redis.conf
      - name: redis-persistent-storage
        persistentVolumeClaim:
          claimName: redis-pvc
3.创建容器
kubectl create -f configmap.yaml  deployment.yaml  redis-pv-pvc.yaml  redis-svc.yaml
4.查看容器状态以及进入容器添加测试数据查看对应宿主机目录是否有文件生成。
5.下载redis-dump和redis-load。
yum -y install curl

gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3  
curl -L get.rvm.io | bash -s stable   
source /usr/local/rvm/scripts/rvm
rvm list known
rvm install 2.3.3
rvm use 2.3.3
rvm use 2.3.3 --default
ruby --version

yum install ruby rubygems ruby-devel -y
gem install redis-dump -V
gem install redis-dump -V

redis-dump -u 10.244.1.156:6379 -a ytflredis > redis-data.json
< redis-data.json redis-load -u 10.244.2.241:6379 -a ytflredis
三、问题总结
1.gem install redis-dump -V,报错redis requires Ruby version >= 2.3.0.需要安装ruby2.3.0版本
具体可以查看上面第5步
2.使用rvm 2.1.3 --default设置默认的ruby的命令时出现RVM is not a function, selecting rubies with ‘rvm use …’ will not work.
先执行source /usr/local/rvm/scripts/rvm,并且将 /usr/local/rvm/bin添加到/etc/profile PATH中,使用./rvm 也会有问题。
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐