1. 创建挂载目录
sudo mkdir -p /home/redis/conf
sudo mkdir -p /home/redis/data
sudo touch /home/redis/conf/redis.conf
cat > /home/redis/conf/redis.conf << EOF
appendonly yes
EOF
root@k8s-master:/home/redis# ls
conf data
root@k8s-master:/home/redis# tree /home/
/home/
└── redis
├── conf
│ └── redis.conf
└── data
3 directories, 1 file
root@k8s-master:/home/redis#
2. 运行容器
docker run -p 6379:6379 --name redis --restart=always \
-v /home/redis/data:/data \
-v /home/redis/conf/redis.conf:/etc/redis/redis.conf \
-d redis:5.0.5 redis-server /etc/redis/redis.conf
root@k8s-master:/home/redis# docker run -p 6379:6379 --name redis --restart=always \
> -v /home/redis/data:/data \
> -v /home/redis/conf/redis.conf:/etc/redis/redis.conf \
> -d redis:5.0.5 redis-server /etc/redis/redis.conf
904689cd1b093c967f93072b5a2ad1df4c481515acf5188db4345853bfd25db3
root@k8s-master:/home/redis# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
904689cd1b09 redis:5.0.5 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
root@k8s-master:/home/redis#
3. 进入容器
docker exec -it redis /bin/bash #进入容器
redis-cli #登录redis
set name mqq
get name
exit
root@k8s-master:/home/redis# docker exec -it redis /bin/bash #进入容器
root@904689cd1b09:/data# redis-cli
127.0.0.1:6379> set name mqq
OK
127.0.0.1:6379> get name
"mqq"
127.0.0.1:6379> exit
root@904689cd1b09:/data# exit
exit
root@k8s-master:/home/redis# cd /home/redis/data/
root@k8s-master:/home/redis/data# ls
appendonly.aof
root@k8s-master:/home/redis/data# cat appendonly.aof
*2
$6
SELECT
$1
0
*3
$3
set
$4
name
$3
mqq
root@k8s-master:/home/redis/data#
4. 重启容器查看数据还在
docker rm -f redis #删除容器
docker run -p 6379:6379 --name redis --restart=always -v /home/redis/data:/data -v /home/redis/conf/redis.conf:/etc/redis/redis.conf -d redis:5.0.5 redis-server /etc/redis/redis.conf #重新启动容器
docker exec -it redis /bin/bash #进入容器
redis-cli
get name
exit
root@k8s-master:/home/redis/data# docker rm -f redis
redis
root@k8s-master:/home/redis/data# docker run -p 6379:6379 --name redis --restart=always -v /home/redis/data:/data -v /home/redis/conf/redis.conf:/etc/redis/redis.conf -d redis:5.0.5 redis-server /etc/redis/redis.conf
81a02a81e76a6b9cb7e322cb3d1c8c94888cbebbdfb37dbbf44dcf49f3f6f93e
root@k8s-master:/home/redis/data# docker exec -it redis /bin/bash
root@81a02a81e76a:/data# redis-cli
127.0.0.1:6379> get name
"mqq"
127.0.0.1:6379> exit
root@81a02a81e76a:/data# exit
exit
root@k8s-master:/home/redis/data#
5.用redis远程工具测试
这里是没有任何的密码
6. 给redis设置登录密码
6.1 修改配置文件
#这个文件添加了requirepass Maqiang,密码是Maqiang
cat /home/redis/conf/redis.conf
appendonly yes
requirepass Maqiang
docker restart redis #重启容器
6.2 用redis远程工具无密码无法登录
6.3 用redis远程工具用密码登录
密码是刚才设置的Maqiang