为了简化部署过程, 减少手工操作, 研究出使用 docker-compose 方式实现 Redis 部署并往该 Redis 中添加键值
以下为我编写的 docker-compose.YAML 文件的内容
- #vi /opt/docker-compose.YAML
- version: '3'
- services:
- Redis:
- image: Redis:4.0
- ports:
- - "16379:6379"
- environment:
- - TZ="Asia/Shanghai"
- volumes:
- - /opt/Redis/conf/Redis.conf:/usr/local/etc/Redis/Redis.conf
- - /opt/Redis/data:/data
- rediscli:
- image: Redis:4.0
- command: Redis-cli -h Redis -p 6379 -c hset REDIS_USER:admin REDIS_USER admin
- depends_on:
- - Redis
- networks:
- redis_net:
- ipam:
- driver: default
以下为操作过程执行的 docker-compose 的 log 输出:
倒数第二行的内容 rediscli_1 | 1 即是往 Redis 容器中操作 Redis-cli 添加键值信息
- [[email protected] ~]$ docker-compose -f /opt/docker-compose.YAML up
- WARNING: Some networks were defined but are not used by any service: redis_net
- Creating network "deploy_default" with the default driver
- Creating deploy_redis_1 ... done
- Creating deploy_rediscli_1 ... done
- Attaching to deploy_redis_1, deploy_rediscli_1
- redis_1 | 1:C 16 Jan 03:30:23.404 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
- redis_1 | 1:C 16 Jan 03:30:23.405 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
- redis_1 | 1:C 16 Jan 03:30:23.405 # Warning: no config file specified, using the default config. In order to specify a config file use Redis-server /path/to/Redis.conf
- redis_1 | 1:M 16 Jan 03:30:23.407 * Running mode=standalone, port=6379.
- redis_1 | 1:M 16 Jan 03:30:23.407 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys.NET/core/somaxconn is set to the lower value of 128.
- redis_1 | 1:M 16 Jan 03:30:23.407 # Server initialized
- redis_1 | 1:M 16 Jan 03:30:23.407 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
- redis_1 | 1:M 16 Jan 03:30:23.408 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will
- create latency and memory usage issues with Redis. To fix this issue run the command 'echo never> /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
- redis_1 | 1:M 16 Jan 03:30:23.408 * Ready to accept connections
- rediscli_1 | 1
- deploy_rediscli_1 exited with code 0
其中 deploy_rediscli_1 这个容器的作用就是往 deploy_redis_1 容器中添加键值, 完成任务即退出, 通过 Redis 客户端工具, 也可以看到添加的键值成功
方法二: 修改 docker-compose.YAML 文件, 使用 shell 脚本实现
- #vi /opt/docker-compose.YAML
- version: '3'
- services:
- Redis:
- image: Redis:4.0
- ports:
- - "16379:6379"
- environment:
- - TZ="Asia/Shanghai"
- volumes:
- - /opt/Redis/conf/Redis.conf:/usr/local/etc/Redis/Redis.conf
- - /opt/Redis/data:/data
- command: /usr/local/bin/Redis-server /usr/local/etc/Redis/Redis.conf
- networks:
- redis_net:
- ipam:
- driver: default
- #vi /opt/setkey.sh
- #!/bin/sh
- docker-compose -f /opt/docker-compose.YAML up -d
- if [ $? -eq 0 ]; then
- docker exec -i Redis Redis-cli -a foobared -c hset REDIS_USER:admin REDIS_USER admin
- if [ $? -eq 0 ];then
- echo "insert success"
- else
- echo "failed"
- fi
- else
- echo "comopose up failed"
- docker-compose -f /opt/docker-compose.YAML down
- fi
来源: http://www.bubuko.com/infodetail-3384139.html