GithubHelp home page GithubHelp logo

k8s-redis-cluster's Introduction

Cluster-NoSQL-Redis


Suitable Project

  • None

Version

Rev: 1.0.2


Status

pipeline status


Description

  • Base on kubernetes cluster ecosystem to build up the redis HA cluster.
  • The purpose to deployment redis-tool is used to sync data, backup data and restore data.
  • Use cronjob object of kubernetes to backup/dump data every hour.

Usage

  • Redis cluster instance.

    redis_pods = [
        'redis-app-0',
        'redis-app-1',
        'redis-app-2',
        'redis-app-3',
        'redis-app-4',
        'redis-app-5'
    ]
  • Execute the container from image of redis-tool.

    $ kubectl run -it redis-tool \
            --rm=true \
            --image="registry.ipt-gitlab:8081/sit-develop-tool/cluster-nosql-redis/redis-tool:$VERSION" \
            --overrides='{ "apiVersion": "v1", "spec": { "imagePullSecrets": [{"name": "gitlab-registry"}] } }' \
            --restart=Never bash -n kube-ops
  • Using python shell to list data keys.

    $ kubectl run -it redis-tool \
            --rm=true \
            --image="registry.ipt-gitlab:8081/sit-develop-tool/cluster-nosql-redis/redis-tool:$VERSION" \
            --overrides='{ "apiVersion": "v1", "spec": { "imagePullSecrets": [{"name": "gitlab-registry"}] } }' \
            --restart=Never bash -n kube-ops
    
    $ python
    
    
    import sys
    from rediscluster import RedisCluster
    def init_redis():
        startup_nodes = [
            {'host': 'redis-app-0.redis-headless.kube-ops.svc.cluster.local', 'port': 6379},
            {'host': 'redis-app-1.redis-headless.kube-ops.svc.cluster.local', 'port': 6379},
            {'host': 'redis-app-2.redis-headless.kube-ops.svc.cluster.local', 'port': 6379},
            {'host': 'redis-app-3.redis-headless.kube-ops.svc.cluster.local', 'port': 6379},
            {'host': 'redis-app-4.redis-headless.kube-ops.svc.cluster.local', 'port': 6379},
            {'host': 'redis-app-5.redis-headless.kube-ops.svc.cluster.local', 'port': 6379}
        ]
        try:
            conn = RedisCluster(startup_nodes=startup_nodes,
                                skip_full_coverage_check=True,
                                decode_responses=True)
            print('连接成功!!!!!1', conn)
            return conn
        except Exception as e:
            print("connect error ", str(e))
            sys.exit(1)
    
    rc = init_redis()
    rc.scan()
  • To dump the redis server data.

    # The first startup the container
    $ kubectl run -it redis-tool \
            --rm=true \
            --image="registry.ipt-gitlab:8081/sit-develop-tool/cluster-nosql-redis/redis-tool:$VERSION" \
            --overrides='{ "apiVersion": "v1", "spec": { "imagePullSecrets": [{"name": "gitlab-registry"}] } }' \
            --restart=Never bash -n kube-ops
    
    # When the redis-tool container was running
    $ kubectl exec -it redis-tool -n kube-ops bash
    
    
    # dump the cluster instance
    $ python redis-tool.py -n kube-ops -st cluster -m dump -p 6379
    
    # dump the standalone instance
    $ python redis-tool.py -n kube-ops -st standalone -s <instance_redis_ip> -m dump -p 6379
    
    # check dump data
    $ ls -al ./reports/$timestemp/
  • To restore the redis from specified data.

    # The first startup the container
    $ kubectl run -it redis-tool \
            --rm=true \
            --image="registry.ipt-gitlab:8081/sit-develop-tool/cluster-nosql-redis/redis-tool:$VERSION" \
            --overrides='{ "apiVersion": "v1", "spec": { "imagePullSecrets": [{"name": "gitlab-registry"}] } }' \
            --restart=Never bash -n kube-ops
    
    # When the redis-tool container was running
    $ kubectl exec -it redis-tool -n kube-ops bash
    
    # Restore the redis from specified data
    $ python redis-tool.py -n kube-ops -dt <standalone|cluster> -p 6379 -d <cluster_ignore|10.99.104.251> -m restore -rdir /usr/src/reports/$timestemp
    
    
    # check log showing as follow, press 'Ctrl + C'
    $ cat reports/redis-shake-restore.log
    2020/11/02 14:03:17 [INFO] restore from '[/usr/src/reports/20201102140026/dump-20201102140026.0 /usr/src/reports/20201102140026/dump-20201102140026.1 /usr/src/reports/20201102140026/dump-20201102140026.2]' to '[10.99.104.251:6379]' done
    2020/11/02 14:03:17 [INFO] Enabled http stats, set status (incr), and wait forever.
  • To synchronize the end-to-end redis instance

    # The first startup the container
    $ kubectl run -it redis-tool \
            --rm=true \
            --image="registry.ipt-gitlab:8081/sit-develop-tool/cluster-nosql-redis/redis-tool:$VERSION" \
            --overrides='{ "apiVersion": "v1", "spec": { "imagePullSecrets": [{"name": "gitlab-registry"}] } }' \
            --restart=Never bash -n kube-ops
    
    # When the redis-tool container was running
    $ kubectl exec -it redis-tool -n kube-ops bash
    
    
    # redis cluster to redis standalone(10.99.104.251)
    $ python redis-tool.py -p 6379 -n kube-ops -m sync
    
    # redis standalone instance(10.99.104.251) to cluster
    $ python redis-tool.py -p 6379 -n kube-ops -tt cluster  -s 10.99.104.251 -st standalone -m sync

Log

  • The dump log is reports/redis-shake-dump.log
  • The sync log is reports/redis-shake-sync.log
  • The restore log is reports/redis-shake-restore.log

Contact

Author: Chang.Jay

k8s-redis-cluster's People

Watchers

 avatar Jay Chang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.