首页 > 其他 > 详细

docker-compose实现redis部署及键值添加

时间:2020-01-16 23:55:13      阅读:216      评论:0      收藏:0      [点我收藏+]
为了简化部署过程,减少手工操作,研究出使用docker-compose方式实现redis部署并往该redis中添加键值
以下为我编写的docker-compose.yml文件的内容

#vi /opt/docker-compose.yml
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添加键值信息

[root@192-83 ~]$ docker-compose -f /opt/docker-compose.yml 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.yml文件,使用shell脚本实现

#vi /opt/docker-compose.yml
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.yml 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.yml down
fi

docker-compose实现redis部署及键值添加

原文:https://blog.51cto.com/8355320/2467212

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!