docker安装

1、下载镜像

  1. docker pull redis

2、运行容器

  1. docker run -d --name redis -p 6379:6379 \
  2. -v /data/docker/redis/data:/data \
  3. -v /data/docker/redis/conf/redis.conf:/etc/redis/redis.conf \
  4. --privileged=true redis:latest --appendonly yes --requirepass "123456"

注意:/etc/redis/redis.conf 是个目录

创建目录:

  1. mkdir /data/docker/redis/data
  2. mkdir /data/docker/redis/conf

创建 /data/docker/redis/conf/redis.conf

  1. bind 0.0.0.0
  2. protected-mode no
  3. port 6379
  4. tcp-backlog 511
  5. timeout 0
  6. tcp-keepalive 300

3、查看容器

  1. docker ps

4、开发端口

  1. firewall-cmd --zone=public --add-port=6379/tcp --permanent

5、进入容器

  1. docker exec -it redis redis-cli -h 192.168.56.100 -p 6379 -a '123456'

docker-compose安装

单节点安装:

  1. version: '3.1'
  2. services:
  3. db:
  4. image: redis
  5. container_name: redis
  6. restart: always
  7. command: redis-server /etc/redis/redis.conf --requirepass 123456
  8. ports:
  9. - 6379:6379
  10. volumes:
  11. - /data/docker/redis/data:/data
  12. - /data/docker/redis/conf/redis.conf:/etc/redis/redis.conf

集群安装:

  1. version: '3.1'
  2. services:
  3. master:
  4. image: redis
  5. container_name: redis-master
  6. ports:
  7. - 6379:6379
  8. slave1:
  9. image: redis
  10. container_name: redis-slave-1
  11. ports:
  12. - 6380:6379
  13. command: redis-server --slaveof redis-master 6379
  14. slave2:
  15. image: redis
  16. container_name: redis-slave-2
  17. ports:
  18. - 6381:6379
  19. command: redis-server --slaveof redis-master 6379
  20. sentinel1:
  21. image: redis
  22. container_name: redis-sentinel-1
  23. ports:
  24. - 26379:26379
  25. command: redis-sentinel /data/docker/redis/conf/sentinel.conf
  26. volumes:
  27. - /data/docker/redis/conf/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
  28. sentinel2:
  29. image: redis
  30. container_name: redis-sentinel-2
  31. ports:
  32. - 26380:26379
  33. command: redis-sentinel /usr/local/etc/redis/sentinel.conf
  34. volumes:
  35. - /data/docker/redis/conf/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
  36. sentinel3:
  37. image: redis
  38. container_name: redis-sentinel-3
  39. ports:
  40. - 26381:26379
  41. command: redis-sentinel /usr/local/etc/redis/sentinel.conf
  42. volumes:
  43. - /data/docker/redis/conf/sentinel3.conf:/usr/local/etc/redis/sentinel.conf

sentinel.conf

  1. port 26379
  2. dir /tmp
  3. # 自定义集群名,其中 127.0.0.1 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
  4. sentinel monitor mymaster 127.0.0.1 6379 2
  5. sentinel down-after-milliseconds mymaster 30000
  6. sentinel parallel-syncs mymaster 1
  7. sentinel failover-timeout mymaster 180000
  8. sentinel deny-scripts-reconfig yes