zookeeper集群,kafka集群,redis集群的搭建
下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/
选择相应镜像地址,然后选择zookeeper版本,已3.4.10为例
下载成功后,执行命令,完成解压缩
tar -xvf zookeeper-3.4.10.tar.gz
Zookeeper推荐使用集群模式,且推荐使用奇数台实例部署(zookeeper特性:集群中只要有过半的机器是正常工作的,那么整个集群对外就是可用的,对于集群部署2n或2n-1个zookeeper实例,对实例宕机数量的容忍度是一样的,都是n-1。所以为了更加高效和节省资源,推荐奇数台实例部署模式)。在这里,采用3台实例部署为例(ip地址为172.21.133.38,172.21.133.39,172.21.133.40)。
首先配置第一个zookeeper实例,ip地址为172.21.133.38,进入zookeeper下conf目录,配置zoo.cfg文件。配置信息如下:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/xw/zookeeper-3.4.10/data/data
dataLogDir=/home/xw/zookeeper-3.4.10/data/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=172.21.133.38:2889:3889
server.2=172.21.133.39:2889:3889
server.3=172.21.133.40:2889:3889
由于采用了3台实例部署(ip地址为172.21.133.38,172.21.133.39,172.21.133.40),且zookeeper安装路径一致,且端口采用均一致,因此配置文件完全一样。只需在dataDir路径下,新建myid文件,输入对应的唯一标识号。在这里,172.21.133.38为1,172.21.133.39为2,172.21.133.40为3。
sh ./bin/zkServer.sh start
查看进程是否存在
ps -ef | grep $USER | grep zookeeper
查看主从关系
sh ./bin/zkServer.sh status
一台为leader,两台 follower
下载地址:http://kafka.apache.org/downloads
找到相应版本,进入之后选择下载镜像
下载成功后,执行命令,完成解压缩
tar -xvf kafka_2.11-2.0.0.tgz
kafka的启动依赖Java环境,所以先安装jdk,并且配置好Java环境变量
若是未配置java环境的执行以下步骤
vi ~/.profile
打开文件之后,在文件末尾加入:
export JAVA_HOME=/usr/share/jdk1.8.0_91
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
执行 source ~/.profile 让配置信息生效
查看Java环境是否配置成功,可以直接输入java回车,看是否支持java命令,若是支持,则表明配置成功
kafka是自带zookeeper,此处使用上面搭建的zookeeper集群
采用3台实例部署,ip地址为172.21.133.41,172.21.133.42,172.21.133.43。
配置第一台kafka实例,首先进入第一台 kafka 实例 config 目录,配置server.properties文件。其中几个最关键的参数如下:
broker.id=0
listeners=PLAINTEXT://172.21.133.41:9092
log.dirs=/home/xw/kafka-logs
zookeeper.connect=172.21.133.38:2181,172.21.133.39:2181,172.21.133.40:2181
auto.create.topics.enable=true
第二台,第三台 kafka实例同理配置,需指定各自的唯一broker.id,以及listeners。 若安装路径一直,log.dirs和zookeeper.connect配置,在三台实例中是相同的。
进入kafka目录,执行下列命令
sh ./bin/kafka-server-start.sh -daemon ./config/server.properties
查看进程是否正常
ps -ef | grep $USER | grep kafka
通过sentinel搭建三主三备,因服务器不够都是搭建在同一台服务上,所以只通过端口区分
主:172.19.151.60:6377-6379
备:172.19.151.60:6380-6381
下载地址:http://download.redis.io/releases/
选择相应版本,下载完成之后,解压
tar -xvf redis-5.0.3.tar.gz
编译并安装,进入redis目录,执行下列命令
make && make install
或者指定安装目录(建议另起目录安装)
make && make install PREFIX=/usr/local/workspaces/redis/redis1-6377/
安装另外两台redis服务器
make install PREFIX=/usr/local/workspaces/redis/redis2-6378/
make install PREFIX=/usr/local/workspaces/redis/redis3-6379/
进入redis的安装目录,新建log,data,etc三个目录
cd /usr/local/workspaces/redis/redis1-6377/
mkdir log
mkdir data
mkdir etc
将源码包目录下redis.conf和sentinel.conf复制到redis安装目录的etc文件夹下
主从配置相似,一般主redis数据不持久化,从redis进行数据持久化,主redis配置文件
#是否启动后台模式 yes/no
daemonize yes
pidfile "/usr/local/workspaces/redis/redis1-6377/data/redis_6377.pid"
#绑定IP地址
bind 172.19.151.60
#监听端口
port 6377
timeout 0
tcp-keepalive 0
#日志级别 支持debug notice warning verbose
loglevel notice
#日志文件名称
logfile "/usr/local/workspaces/redis/redis1-6377/log/redis_6377.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/usr/local/workspaces/redis/redis1-6377/data"
#如果做故障切换,不论主从节点都要填写密码且要保持一致 主节点授权密码
masterauth "000000"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 98
#当前redis密码
requirepass "000000"
appendonly yes
# appendfsync always
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
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配置文件,和主redis只是最后多了slaveof节点
#是否启动后台模式 yes/no
daemonize yes
pidfile "/usr/local/workspaces/redis/redis1-6380/data/redis_6380.pid"
#绑定IP地址
bind 172.19.151.60
#监听端口
port 6380
timeout 0
tcp-keepalive 0
#日志级别 支持debug notice warning verbose
loglevel notice
#日志文件名称
logfile "/usr/local/workspaces/redis/redis1-6380/log/redis_6380.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/usr/local/workspaces/redis/redis1-6380/data"
#如果做故障切换,不论主从节点都要填写密码且要保持一致 主节点授权密码
masterauth "000000"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 98
#当前redis密码
requirepass "000000"
appendonly yes
# appendfsync always
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
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
#配置主节点信息
slaveof 172.19.151.60 6377
./redis-server ./etc/redis.conf
进入log日志目录查看redis启动日志信息
用redis-cli连接redis查看信息
#172.19.151.60 redis绑定的IP 6377为端口 000000授权密码
./redis-cli -h 172.19.151.60 -p 6377 -a 000000
#连上redis之后用info命令查看信息
info
配置另外几台redis,并启动
修改三台主redis的sentinel就行,配置了三组master
sentinel1 172.19.151.60:26377
#监听端口
port 26377
#启动后台模式
daemonize yes
pidfile /usr/local/workspaces/redis/redis1-6377/data/redis-sentinel.pid
#指定工作目录
dir "/usr/local/workspaces/redis/redis1-6377/data"
protected-mode no
#日志文件
logfile "/usr/local/workspaces/redis/redis1-6377/log/sentinel.log"
sentinel current-epoch 839
######################### mymaster1 ################################
#sentinel monitor
# quorum表示sentinel集群至少需要多少个sentinel判决该master下线 slave是通过master获知的
sentinel monitor mymaster1 172.19.151.60 6377 2
# sentinel判决下线时间
sentinel down-after-milliseconds mymaster1 30000
#主从切换时间,默认180秒
sentinel failover-timeout mymaster1 180000
#redis主节点密码
sentinel auth-pass mymaster1 000000
sentinel config-epoch mymaster1 15
sentinel leader-epoch mymaster1 839
# #除了当前sentinel, 还有哪些在监控这个master的sentinel
sentinel known-sentinel mymaster1 172.19.151.60 26378
sentinel known-sentinel mymaster1 172.19.151.60 26379
######################### mymaster2 ################################
sentinel monitor mymaster2 172.19.151.60 6378 2
sentinel down-after-milliseconds mymaster2 30000
sentinel failover-timeout mymaster2 180000
sentinel auth-pass mymaster2 000000
sentinel config-epoch mymaster2 15
sentinel leader-epoch mymaster2 839
sentinel known-sentinel mymaster2 172.19.151.60 26378
sentinel known-sentinel mymaster2 172.19.151.60 26379
######################### mymaster3 ################################
sentinel monitor mymaster3 172.19.151.60 6379 2
sentinel down-after-milliseconds mymaster3 30000
sentinel failover-timeout mymaster3 180000
sentinel auth-pass mymaster3 000000
sentinel config-epoch mymaster3 15
sentinel leader-epoch mymaster3 839
sentinel known-sentinel mymaster3 172.19.151.60 26378
sentinel known-sentinel mymaster3 172.19.151.60 26379
sentinel2 172.19.151.60:26378
port 26378
#启动后台模式
daemonize yes
pidfile /usr/local/workspaces/redis/redis2-6378/data/redis-sentinel.pid
#指定工作目录
dir "/usr/local/workspaces/redis/redis2-6378/data"
protected-mode no
#日志文件
logfile "/usr/local/workspaces/redis/redis2-6378/log/sentinel.log"
sentinel current-epoch 839
#sentinel monitor
# quorum表示sentinel集群至少需要多少个sentinel判决该master下线 slave是通过master获知的
sentinel monitor mymaster1 172.19.151.60 6377 2
# sentinel判决下线时间
sentinel down-after-milliseconds mymaster1 30000
#主从切换时间,默认180秒
sentinel failover-timeout mymaster1 180000
#redis主节点密码
sentinel auth-pass mymaster1 000000
sentinel config-epoch mymaster1 15
sentinel leader-epoch mymaster1 839
# #除了当前sentinel, 还有哪些在监控这个master的sentinel
sentinel known-sentinel mymaster 172.19.151.60 26377
sentinel known-sentinel mymaster 172.19.151.60 26379
######################### mymaster2 ################################
sentinel monitor mymaster2 172.19.151.60 6378 2
sentinel down-after-milliseconds mymaster2 30000
sentinel failover-timeout mymaster2 180000
sentinel auth-pass mymaster2 000000
sentinel config-epoch mymaster2 15
sentinel leader-epoch mymaster2 839
sentinel known-sentinel mymaster2 172.19.151.60 26377
sentinel known-sentinel mymaster2 172.19.151.60 26379
######################### mymaster3 ################################
sentinel monitor mymaster3 172.19.151.60 6379 2
sentinel down-after-milliseconds mymaster3 30000
sentinel failover-timeout mymaster3 180000
sentinel auth-pass mymaster3 000000
sentinel config-epoch mymaster3 15
sentinel leader-epoch mymaster3 839
sentinel known-sentinel mymaster3 172.19.151.60 26377
sentinel known-sentinel mymaster3 172.19.151.60 26379
sentinel3 172.19.151.60:26379
port 26379
#启动后台模式
daemonize yes
pidfile /usr/local/workspaces/redis/redis3-6379/data/redis-sentinel.pid
#指定工作目录
dir "/usr/local/workspaces/redis/redis3-6379/data"
protected-mode no
#日志文件
logfile "/usr/local/workspaces/redis/redis3-6379/log/sentinel.log"
sentinel current-epoch 839
#sentinel monitor
# quorum表示sentinel集群至少需要多少个sentinel判决该master下线 slave是通过master获知的
sentinel monitor mymaster1 172.19.151.60 6377 2
# sentinel判决下线时间
sentinel down-after-milliseconds mymaster1 30000
#主从切换时间,默认180秒
sentinel failover-timeout mymaster1 180000
#redis主节点密码
sentinel auth-pass mymaster1 000000
sentinel config-epoch mymaster1 15
sentinel leader-epoch mymaster1 839
# #除了当前sentinel, 还有哪些在监控这个master的sentinel
sentinel known-sentinel mymaster 172.19.151.60 26377
sentinel known-sentinel mymaster 172.19.151.60 26378
######################### mymaster2 ################################
sentinel monitor mymaster2 172.19.151.60 6378 2
sentinel down-after-milliseconds mymaster2 30000
sentinel failover-timeout mymaster2 180000
sentinel auth-pass mymaster2 000000
sentinel config-epoch mymaster2 15
sentinel leader-epoch mymaster2 839
sentinel known-sentinel mymaster2 172.19.151.60 26377
sentinel known-sentinel mymaster2 172.19.151.60 26378
######################### mymaster3 ################################
sentinel monitor mymaster3 172.19.151.60 6379 2
sentinel down-after-milliseconds mymaster3 30000
sentinel failover-timeout mymaster3 180000
sentinel auth-pass mymaster3 000000
sentinel config-epoch mymaster3 15
sentinel leader-epoch mymaster3 839
sentinel known-sentinel mymaster3 172.19.151.60 26377
sentinel known-sentinel mymaster3 172.19.151.60 26378
1.通过redis-sentinel 启动,进入redis目录
./redis-sentinel ./etc/sentinel.conf
2.通过redis-server启动,进入redis目录
./redis-server ./etc/sentinel.conf --sentinel