docker部署redis程序数据库
############单节点redis
mkdir -p /opt/redis/conf
touch /opt/redis/conf/redis.conf
###启动redis容器
docker run -d --name redis -p 6379:6379 \
> -v /opt/redis/data:/data \
> -v /opt/redis/conf:/etc/redis \
> redis redis-server /etc/redis/redis.conf
###访问redis
第一种方式:容器访问
docker run -it redis:latest redis-cli -h 172.17.0.5
##第二种方式访问
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install redis
redis-cli -h 192.168.3.201 -p 6379
#############redis集群
准备脚本:
[root@docker-ui opt]# cat redis-cluster.sh
#!/bin/bash
for port in $(seq 8001 8006); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port ${port}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.3.201
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
appendonly yes
EOF
docker run -p ${port}:${port} -p1${port}:1${port} --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d redis:5.0.7 redis-server /etc/redis/redis.conf; \
done
###执行脚本
[root@docker-ui opt]# ./redis-cluster.sh
Unable to find image 'redis:5.0.7' locally
5.0.7: Pulling from library/redis
68ced04f60ab: Pull complete
7ecc253967df: Pull complete
765957bf98d4: Pull complete
52f16772e1ca: Pull complete
2e43ba99c3f3: Pull complete
d95576c71392: Pull complete
Digest: sha256:938ee5bfba605cc85f9f52ff95024e9a24cf5511ba6f1cbc68ec9d91a0432125
Status: Downloaded newer image for redis:5.0.7
b6a9fa492002fe50bdb9f8250571d0982ecbef6e90173d8e2ddd0c710b7a2db0
2221c2209b60769e5e159e3a18bf95f7fabfd104cacd0d0cd96df25d486a5604
5109eaa69e2c0cd11086a53c3ca05ece60b3e511d891d6f1fd024f3ea92750a4
768fc84ad330774652dcecf4cd2755f2e52aca099683a515c30df219b6a05a28
cfb82a664b00e77e83af6a34413a4dc7adf55dc40272b86afa6b689c5c714f16
eef0b43fb595cea5af06681317ec832745ad4e2f7ac1c6a27d2454b35f9fa8f8
##############准备创建集群
docker exec -it redis-8001 /bin/bash
redis-cli --cluster create 192.168.3.201:8001 192.168.3.201:8002 192.168.3.201:8003 192.168.3.201:8004 192.168.3.201:8005 192.168.3.201:8006 --cluster-replicas 1;
#####测试数据:
redis-cli -h 192.168.3.201 -p 8001 -c
都能看到数据