日常学习。。。。redis单机部署和高可用。通过docker部署,均未做数据持久化。
一,单机
dockerfile文件如下
from centos:7
RUN yum install vim net-tools tcl make gcc -y
ADD redis-5.0.7.tar.gz /opt/
RUN cd /opt/redis-5.0.7
&& make
&& make test
&& make install
RUN cp /opt/redis-5.0.7/redis.conf /etc/redis.conf
&& sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /etc/redis.conf
&& sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf
&& sed -i 's/appendonly no/appendonly yes/g' /etc/redis.conf
&& sed -i '$a
equirepass redis' /etc/redis.conf
RUN echo -e "#!/bin/bash
redis-server /etc/redis.conf
sh " >> /root/run.sh
RUN chmod a+x /root/run.sh
CMD ["/root/run.sh"]
创建镜像
[root@dou redis]# docker build -t redis:v1 . Sending build context to Docker daemon 202.4 MB Step 1/8 : FROM centos:7 ---> 08d05d1d5859 Step 2/8 : RUN yum install vim net-tools tcl make gcc -y ---> Using cache ---> ad582facf64d Step 3/8 : ADD redis-5.0.7.tar.gz /opt/ ---> Using cache ---> af8c2ddf726f Step 4/8 : RUN cd /opt/redis-5.0.7 && make && make test && make install ---> Using cache ---> 48b3bfd1d32a Step 5/8 : RUN cp /opt/redis-5.0.7/redis.conf /etc/redis.conf && sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /etc/redis.conf && sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf && sed -i '$a equirepass redis' /etc/redis.conf ---> Using cache ---> 8338a3cc7d2b Step 6/8 : RUN echo -e "#!/bin/bash redis-server /etc/redis.conf sh " >> /root/run.sh ---> Using cache ---> ecc27472d84f Step 7/8 : RUN chmod a+x /root/run.sh ---> Using cache ---> f177325dec1b Step 8/8 : CMD /root/run.sh ---> Using cache ---> d5ac3fc2210a Successfully built d5ac3fc2210a [root@dou redis]#
运行容器
docker run -itd --network doufy --ip 172.19.0.30 --name redis01 -h redis01 redis:v1
连接测试
[root@redis01 /]# redis-cli -h 172.19.0.30 -p 6379 -a redis Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 172.19.0.30:6379> ping PONG 172.19.0.30:6379>
二,主从复制
docker file文件如下
[root@dou redis]# more Dockerfile-master from centos:7 RUN yum install vim net-tools tcl make gcc -y ADD redis-5.0.7.tar.gz /opt/ RUN cd /opt/redis-5.0.7 && make && make test && make install RUN cp /opt/redis-5.0.7/redis.conf /etc/redis.conf && sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /etc/redis.conf && sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf && sed -i 's/appendonly no/appendonly yes/g' /etc/redis.conf && sed -i '$a equirepass redis' /etc/redis.conf COPY run.sh /root/run.sh RUN chmod a+x /root/run.sh CMD ["/root/run.sh"] [root@dou redis]# more Dockerfile-slave from centos:7 RUN yum install vim net-tools tcl make gcc -y ADD redis-5.0.7.tar.gz /opt/ RUN cd /opt/redis-5.0.7 && make && make test && make install RUN cp /opt/redis-5.0.7/redis.conf /etc/redis.conf && sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /etc/redis.conf && sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf && sed -i 's/appendonly no/appendonly yes/g' /etc/redis.conf && sed -i '$a equirepass redis' /etc/redis.conf && sed -i '$a eplicaof 172.19.0.51 6379' /etc/redis.conf && sed -i '$amasterauth redis' /etc/redis.conf COPY run.sh /root/run.sh RUN chmod a+x /root/run.sh CMD ["/root/run.sh"] [root@dou redis]# more docker-compose.yml version: '3.4' x-logging: &default-logging options: max-size: '12m' max-file: '5' driver: json-file services: master1: image: redis:m2 logging: *default-logging restart: "no" hostname: master networks: doufy: ipv4_address: 172.19.0.51 volumes: - /etc/localtime:/etc/localtime:ro #测试不持久化了 slave1: image: redis:s2 logging: *default-logging restart: "no" hostname: slave1 networks: doufy: ipv4_address: 172.19.0.52 volumes: - /etc/localtime:/etc/localtime:ro slave2: logging: *default-logging image: redis:s2 restart: "no" hostname: slave2 networks: doufy: ipv4_address: 172.19.0.53 volumes: - /etc/localtime:/etc/localtime:ro networks: doufy: external: true
创建镜像
docker build -t redis:m2 -f Dockerfile-master .
docker build -t redis:s2 -f Dockerfile-slave .
运行容器
docker-compose up -d
连接测试
[root@dou redis]# docker-compose up -d Creating redis_slave2_1 ... done Creating redis_slave1_1 ... done Creating redis_master1_1 ... done [root@dou redis]# docker exec -it redis_master1_1 bash -c "echo 'info replication'|redis-cli -a redis" # Replication role:master connected_slaves:2 slave0:ip=172.19.0.52,port=6379,state=online,offset=420,lag=1 slave1:ip=172.19.0.53,port=6379,state=online,offset=420,lag=1 master_replid:bbe4270c4f3f5e7e72923413ba5907dc9d5513c7 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:420 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:420 [root@dou redis]#
测试
在master节点设置key,slave节点获取key
master节点 [root@master /]# redis-cli -a redis 127.0.0.1:6379> set doufy caiji OK 127.0.0.1:6379> get doufy "caiji" 127.0.0.1:6379> slave节点 [root@slave1 /]# redis-cli -a redis 127.0.0.1:6379> get doufy "caiji" 127.0.0.1:6379> master删除,slave也没了,主从复制到此结束
三, 哨兵模式
docker file如下
[root@dou redis]# more Dockerfile-master from centos:7 RUN yum install vim net-tools tcl make gcc -y ADD redis-5.0.7.tar.gz /opt/ RUN cd /opt/redis-5.0.7 && make && make test && make install RUN cp /opt/redis-5.0.7/redis.conf /etc/redis.conf && sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /etc/redis.conf && sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf && sed -i 's/appendonly no/appendonly yes/g' /etc/redis.conf && sed -i '$a equirepass redis' /etc/redis.conf RUN cp /opt/redis-5.0.7/sentinel.conf /etc/sentinel.conf && sed -i '$aind 0.0.0.0' /etc/sentinel.conf && sed -i 's/daemonize no/daemonize yes/g' /etc/sentinel.conf && sed -i 's/logfile ""/logfile "/var/log/redis/sentine.log"/g' /etc/sentinel.conf && sed -i 's/30000/3000/g' /etc/sentinel.conf && sed -i 's/180000/18000/g' /etc/sentinel.conf && sed -i 's/mymaster 127.0.0.1/mymaster 172.19.0.71/g' /etc/sentinel.conf && sed -i '$asentinel auth-pass mymaster redis' /etc/sentinel.conf RUN mkdir /var/log/redis && touch /var/log/redis/sentine.log && chmod 755 /var/log/redis -R COPY run.sh /root/run.sh RUN chmod a+x /root/run.sh CMD ["/root/run.sh"] [root@dou redis]# more Dockerfile-slave from centos:7 RUN yum install vim net-tools tcl make gcc -y ADD redis-5.0.7.tar.gz /opt/ RUN cd /opt/redis-5.0.7 && make && make test && make install RUN cp /opt/redis-5.0.7/redis.conf /etc/redis.conf && sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/g' /etc/redis.conf && sed -i 's/daemonize no/daemonize yes/g' /etc/redis.conf && sed -i 's/appendonly no/appendonly yes/g' /etc/redis.conf && sed -i '$a equirepass redis' /etc/redis.conf && sed -i '$a eplicaof 172.19.0.71 6379' /etc/redis.conf && sed -i '$amasterauth redis' /etc/redis.conf RUN cp /opt/redis-5.0.7/sentinel.conf /etc/sentinel.conf && sed -i '$aind 0.0.0.0' /etc/sentinel.conf && sed -i 's/daemonize no/daemonize yes/g' /etc/sentinel.conf && sed -i 's/logfile ""/logfile "/var/log/redis/sentine.log"/g' /etc/sentinel.conf && sed -i 's/30000/3000/g' /etc/sentinel.conf && sed -i 's/180000/18000/g' /etc/sentinel.conf && sed -i 's/mymaster 127.0.0.1/mymaster 172.19.0.71/g' /etc/sentinel.conf && sed -i '$asentinel auth-pass mymaster redis' /etc/sentinel.conf RUN mkdir /var/log/redis && touch /var/log/redis/sentine.log && chmod 755 /var/log/redis -R COPY run.sh /root/run.sh RUN chmod a+x /root/run.sh CMD ["/root/run.sh"] [root@dou redis]# more run.sh #!/bin/bash echo "#######开启程序######" redis-server /etc/redis.conf echo "#######开启哨兵######" redis-sentinel /etc/sentinel.conf echo "#######查看端口######" netstat -ntulp echo "#######查看进程######" ps -aux while true do sleep 1h done [root@dou redis]# more docker-compose.yml version: '3.4' x-logging: &default-logging options: max-size: '12m' max-file: '5' driver: json-file services: master1: image: redis:m3 logging: *default-logging restart: "no" hostname: master networks: doufy: ipv4_address: 172.19.0.71 volumes: - /etc/localtime:/etc/localtime:ro #测试环境,不持久化了 slave1: image: redis:s3 logging: *default-logging restart: "no" hostname: slave1 networks: doufy: ipv4_address: 172.19.0.72 volumes: - /etc/localtime:/etc/localtime:ro slave2: logging: *default-logging image: redis:s3 restart: "no" hostname: slave2 networks: doufy: ipv4_address: 172.19.0.73 volumes: - /etc/localtime:/etc/localtime:ro networks: doufy: external: true
创建镜像
docker build -t redis:m3 -f Dockerfile-master .
docker build -t redis:s3 -f Dockerfile-slave .
运行容器
[root@dou redis]# docker-compose up -d Creating redis_master1_1 ... done Creating redis_slave1_1 ... done Creating redis_slave2_1 ... done
查看状态
#主节点,如下一切正常 [root@dou redis]# docker exec -it redis_master1_1 bash -c "echo 'info replication'|redis-cli -a redis" # Replication role:master connected_slaves:2 slave0:ip=172.19.0.72,port=6379,state=online,offset=3325,lag=1 slave1:ip=172.19.0.73,port=6379,state=online,offset=3325,lag=1 master_replid:49c58e94a1346ab8d59091690f55f0314e225198 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:3325 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:3325 #从节点1,如下一切正常 [root@dou redis]# docker exec -it redis_slave1_1 bash -c "echo 'info replication'|redis-cli -a redis" # Replication role:slave master_host:172.19.0.71 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:30222 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:49c58e94a1346ab8d59091690f55f0314e225198 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:30222 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:30222 #从节点2,如下一切正常 [root@dou redis]# docker exec -it redis_slave2_1 bash -c "echo 'info replication'|redis-cli -a redis" # Replication role:slave master_host:172.19.0.71 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:31469 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:49c58e94a1346ab8d59091690f55f0314e225198 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:31469 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:31469
测试
关闭主节点
docker exec -it redis_master1_1 bash -c "echo 'shutdown'|redis-cli -a redis"
查看从节点
#由此可见,从节点自动升级为主节点,哨兵配置完成 [root@dou redis]# docker exec -it redis_slave1_1 bash -c "echo 'info replication'|redis-cli -a redis" # Replication role:master connected_slaves:1 slave0:ip=172.19.0.73,port=6379,state=online,offset=90397,lag=1 master_replid:4a3e4ea435ccdf0be6f05c2072b5e55fd994ec44 master_replid2:49c58e94a1346ab8d59091690f55f0314e225198 master_repl_offset:90397 second_repl_offset:81962 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:90397
查看日志
四,Cluster
持续更新**********