codis集群部署实战
一、概要1、折腾codis集群已经快两个月了,感谢一直以来codis的作者刘奇和黄东旭的耐心支持,在这里给你们点个赞,现在我司已经有一个业务跑在了codis集群上,目
一、概要
1、折腾codis集群已经快两个月了,感谢一直以来codis的作者刘奇和黄东旭的耐心支持,在这里给你们点个赞,现在我司已经有一个业务跑在了codis集群上,目前只是切了整个业务的10%的量,预计下周会全量切到codis上,这个时候大家肯定特别想知道codis稳定吗?有没有什么bug啊,你想知道的也是我想知道的,搞起吧,用了才知道,反正目前我们这没发现啥问题,一些小的问题已经及时联系作者改掉了,好吧,不扯淡了,写这篇文章的目的是帮助想了解codis的初学者快速部署(官方的部署文档对应运维知识弱一点的童鞋看来还是有点费力)还有就是给自己做一个备录以便后期集群的部署。
2、Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 (有一些命令不支持), 上层应用可以像使用单机的 Redis 一样使用, Codis 底层会处理请求的转发, 不停机的数据迁移等工作, 所有后边的一切事情, 对于前面的客户端来说是透明的, 可以简单的认为后边连接的是一个内存无限大的 Redis 服务,,当然,前段时间redis官方的3.0出了稳定版,3.0支持集群功能,codis的实现原理和3.0的集群功能差不多,我了解的现在美团、阿里已经用了3.0的集群功能了,我们这边的业务主要是php,3.0集群的sdk目前貌似还没有支持php语言的,大家谁的php应用上了3.0集群,请联系我,我去取经,有关redis常见的集群技术,请移步到 @萧田国 萧老师的infoq专栏Redis集群技术及Codis实践
二、架构
三、角色分批
zookeeper集群: 10.10.0.47 10.10.0.48 10.10.1.76 codis-config、codis-ha: 10.10.32.10:18087 codis-proxy: 10.10.32.10:19000 10.10.32.49:19000 codis-server: 10.10.32.42:6379、10.10.32.43:6380(主、从) 10.10.32.43:6379、10.10.32.44:6380(主、从) 10.10.32.44:6379、10.10.32.42:6380(主、从)四、部署
1、安装zookeeper
yum -y install zookeeper jdk ##安装服务vim /etc/hosts ##添加host 10.10.0.47 ZooKeeper-node1 10.10.0.48 ZooKeeper-node2 10.10.1.76 ZooKeeper-node3vim /etc/zookeeper/conf/zoo.cfg ##撰写zk的配置文件 maxClientCnxns=50 tickTime=2000 initLimit=10 syncLimit=5 dataDir=/data/zookeeper/ clientPort=2181 server.1=ZooKeeper-node1:2888:3888 server.2=ZooKeeper-node2:2888:3888 server.3=ZooKeeper-node3:2888:3888mkdir /data/zookeeper/ ##创建zk的datadir目录 echo "2" >/data/zookeeper/myid ##生成ID,这里需要注意, myid对应的zoo.cfg的server.ID,比如ZooKeeper-node2对应的myid应该是2 /usr/lib/zookeeper/bin/zkServer.sh start ## 服务启动2、go安装(codis是go语言写的,所以那些机器需要安装你懂得)
wget https://storage.googleapis.com/golang/go1.4.1.linux-amd64.tar.gz tar -zxvf go1.4.1.linux-amd64.tar.gz mv go /usr/local/ cd /usr/local/go/src/ bash all.bash cat >> ~/.bashrc 3、下载并编译codis(codis-config、codis-proxy、codis-server所在的机器)mkdir /data/go export GOPATH=/data/go /usr/local/go/bin/go get github.com/wandoulabs/codis cd /data/go/src/github.com/wandoulabs/codis/ ./bootstrap.sh make gotest五、服务启动及初始化集群
1、启动 dashboard(codis-config上操作)
cat /etc/codis/config_10.ini ##撰写配置文件 zk=10.10.0.47:2181,10.10.0.48:2181,10.10.1.76:2181 product=zh_news proxy_id=codis-proxy_10 net_timeout=5000 proto=tcp4 dashboard_addr=10.10.32.10:18087cd /data/go/src/github.com/wandoulabs/codis/ && ./bin/codis-config -c /etc/codis/config_10.ini dashboard &cd /data/go/src/github.com/wandoulabs/codis/ ./bin/codis-config -c /etc/codis/config_10.ini server add 1 10.10.32.42:6379 master ./bin/codis-config -c /etc/codis/config_10.ini server add 1 10.10.32.43:6380 slave ./bin/codis-config -c /etc/codis/config_10.ini server add 2 10.10.32.43:6379 master ./bin/codis-config -c /etc/codis/config_10.ini server add 2 10.10.32.44:6380 slave ./bin/codis-config -c /etc/codis/config_10.ini server add 3 10.10.32.44:6379 master ./bin/codis-config -c /etc/codis/config_10.ini server add 3 10.10.32.42:6380 slave5、设置 server group 服务的 slot 范围 Codis 采用 Pre-sharding 的技术来实现数据的分片, 默认分成 1024 个 slots (0-1023), 对于每个key来说, 通过以下公式确定所属的 Slot Id : SlotId = crc32(key) % 1024 每一个 slot 都会有一个特定的 server group id 来表示这个 slot 的数据由哪个 server group 来提供.(codis-config上操作)
cd /data/go/src/github.com/wandoulabs/codis/ ./bin/codis-config -c /etc/codis/config_10.ini slot range-set 0 300 1 online ./bin/codis-config -c /etc/codis/config_10.ini slot range-set 301 700 2 online ./bin/codis-config -c /etc/codis/config_10.ini slot range-set 701 1023 3 online
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.
