页面“Docker-compose教程”与“Redis批量删除key”之间的差异

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
docker>Evan
 
 
第1行: 第1行:
 +
[[category:ops]] [[category:redis]] 
  
 
+
[[Redis-bigkeys]]
可以 Dockerfile 创建容器,docker-compse实现部署 或者直接用官方的img 利用docker-compse实现部署
+
=redis cluster 批量删除key=
 
 
=introduce=
 
 
<pre>
 
<pre>
Compose项目来源于之前的fig项目,使用python语言编写,与docker/swarm配合度很高。
+
#June 19 2020  有空结果一下原来的版本再优化一下
 
+
cat redis_del_key.sh
Compose 是 Docker 容器进行编排的工具,定义和运行多容器的应用,可以一条命令启动多个容器,使用Docker Compose不再需要使用shell脚本来启动容器。
+
#!/bin/bash
 
+
# Usage
Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义,然后使用docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个容器进行开发的场景。
+
#redis_list=("host:post" "host:post")
 
 
docker-compose默认的模板文件是 docker-compose.yml,其中定义的每个服务都必须通过 image 指令指定镜像或 build 指令(需要 Dockerfile)来自动构建。
 
 
 
其它大部分指令都跟 docker run 中的类似。
 
如果使用 build 指令,在 Dockerfile 中设置的选项(例如:CMD, EXPOSE, VOLUME, ENV 等) 将会自动被获取,无需在 docker-compose.yml 中再次设置。
 
 
 
使用Compose 基本上分为三步:
 
1.Dockerfile 定义应用的运行环境
 
2.docker-compose.yml 定义组成应用的各服务
 
3.docker-compose up 启动整个应用
 
---------------------
 
 
 
Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。
 
 
 
在日常工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个 Web 项目,除了 Web服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。
 
 
 
Compose 恰好满足了这样的需求。它允许用户通过一个单独的 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目。
 
  
Compose 中有两个重要的概念:
+
redis_list=( "172.16.200.12:7003" "172.16.200.7:7004" "172.16.200.7:7005" "172.16.200.13:7000" "172.16.200.13:7001" "172.16.200.12:7002")
 +
#redis_list=( "172.16.200.13:7000" "172.16.200.13:7001" "172.16.200.12:7002")
 +
#redis_list=("172.16.200.7:7004")
 +
password="3636password"
  
     服务 (service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。
+
for info in ${redis_list[@]}
 +
     do
 +
        echo "开始执行:$info" 
 +
        ip=`echo $info | cut -d : -f 1`
 +
        port=`echo $info | cut -d : -f 2`
  
    项目 (project):由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。
+
      for  loop in  `cat key.txt`
 
+
        do
</pre>
+
        #echo $loop  |xargs -t -n1 redis-cli -c  -h $ip -p $port -a $password -c del
 +
        echo $loop  |xargs -t -n1 redis-cli -c  -h $ip -p $port -a $password -c keys
 +
        done
  
=Docker-compose常用命令=
+
    done
<pre>
+
    echo "success done完成"
docker-compose --help
 
  
docker-compose up -d nginx                    构建建启动nignx容器 # #这个是写在yml里面的server名  查看 文件, 不是容器名
+
# cat key.txt
 +
infnce:wonderfulCache
 +
ext_info
  
docker-compose exec nginx bash            登录到nginx容器中
 
  
docker-compose down                              删除所有nginx容器,镜像
 
  
docker-compose ps                                  显示所有容器
+
#原来的版本
  
docker-compose restart nginx                  重新启动nginx容器 #这个是写在yml里面的server名  不是容器名
+
del hello_user_*
  
docker-compose run --no-deps --rm php-fpm php -v  在php-fpm中不启动关联容器,并容器执行php -v 执行完成后删除容器
+
redis-cli keys
  
docker-compose build nginx                    构建镜像 。       
 
  
docker-compose build --no-cache nginx  不带缓存的构建。
+
redis-cli keys "hello_user_*" | xargs redis-cli del
  
docker-compose logs  nginx                    查看nginx的日志
 
  
docker-compose logs -f nginx                  查看nginx的实时日志
+
./redis-cli -h IP -p PORT -a PASSWORD keys 'key*' | xargs ./redis-cli -h IP  -p PORT -a PASSWORD del
   
 
  
docker-compose config -q                        验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。
+
redis-cli    -c  -h    172.31.22.218  -p 7000 -a pass keys 'hello_user_*'| xargs -i redis-cli    -c  -h    172.31.22.218  -p 7000  -a pass  del {}
 +
redis-cli    -c  -h    172.31.22.218  -p 7001  -a pass keys 'hello_user_*'| xargs -i redis-cli    -c  -h    172.31.22.218  -p 7001  -a pass  del {}
  
docker-compose events --json nginx      以json的形式输出nginx的docker日志
+
redis-cli    --h    172.31.16.135  -p 7002  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.16.135  -p 7002 -a pass  del {}
 +
redis-cli    -c  -h    172.31.16.135  -p 7003  -a pass keys 'hello_user_*'| xargs  -i redis-cli    -c  -h    172.31.16.135  -p 7003 -a pass  del {}
  
docker-compose pause nginx                暂停nignx容器
+
redis-cli    -c  -h    172.31.23.43  -p 7004  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.23.43  -p 7004 -a pass  del {}
 +
redis-cli    -c  -h    172.31.23.43  -p 7005  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.23.43  -p 7005 -a pass  del {}
  
docker-compose unpause nginx            恢复ningx容器
 
  
docker-compose rm nginx                      删除容器(删除前必须关闭容器)
 
  
docker-compose stop nginx                    停止nignx容器
+
#!/bin/bash
 +
redis_comm=/usr/local/redis-5.0.3/bin/redis-cli
 +
redis_ser01=192.168.50.175
 +
redis_ser02=192.168.50.174
 +
$redis_comm -c -h $redis_ser01  -p 7001 -a xxx  keys $1 | xargs -i $redis_comm -c -h $redis_ser01  -p 7001 -a xxx del {}
 +
$redis_comm -c -h $redis_ser02  -p 7001 -a xxx  keys $1 | xargs -i $redis_comm -c -h $redis_ser02  -p 7001 -a xxx del {}
  
docker-compose start nginx                    启动nignx容器
+
运行:
  
 +
./redis_del_key.sh  匹配的key*
  
note:
+
err 
docker-compose  知道补充
 
  
docker-compose ps
+
-i {} 都是一定要的
          Name                        Command              State                        Ports                     
 
--------------------------------------------------------------------------------------------------------------------
 
docker4jenkins_jenkins_1  /sbin/tini -- /usr/local/b ...  Up      0.0.0.0:50000->50000/tcp, 0.0.0.0:7099->8080/tcp
 
  
docker-compose  stop  jenkins #后面是加server名  服务名称
+
redis-cli -n 0 keys "test*" | xargs redis-cli -n 0 del
+
(error) CROSSSLOT Keys in request don't hash to the same slot
docker-compose.yml
 
 
services:
 
  jenkins:
 
    image: jenkins/jenkins:lts
 
  
所以
 
docker-compose  start  jenkins
 
 
</pre>
 
</pre>
 
=install=
 
<pre>
 
#方法1 如果是国内网络太慢 可以把变量换成对应的值 然后先在desktop 下载回来
 
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
 
 
#方法 2  有时axel最快了
 
#https://github.com/docker/compose/releases/download/1.24.0/docker-compose-linux-x86_64  -o /usr/local/bin/docker-compose
 
sudo chmod +x /usr/local/bin/docker-compose
 
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
 
$  docker-compose --version
 
docker-compose version 1.24.0, build 0aa59064</pre>
 
 
=Get started with Docker Compose=
 
==Step 1: Setup==
 
===1.Create a directory for the project:===
 
<pre> mkdir composetest &&  cd composetest</pre>
 
===Create a file called app.py in your project directory and paste this in:===
 
<pre>import time
 
 
import redis
 
from flask import Flask
 
 
app = Flask(__name__)
 
cache = redis.Redis(host='redis', port=6379)
 
 
 
def get_hit_count():
 
    retries = 5
 
    while True:
 
        try:
 
            return cache.incr('hits')
 
        except redis.exceptions.ConnectionError as exc:
 
            if retries == 0:
 
                raise exc
 
            retries -= 1
 
            time.sleep(0.5)
 
 
 
@app.route('/')
 
def hello():
 
    count = get_hit_count()
 
    return 'Hello World! I have been seen {} times.\n'.format(count)
 
 
if __name__ == "__main__":
 
    app.run(host="0.0.0.0", debug=True)  </pre>
 
===3.Create another file called requirements.txt in your project directory and paste this in===
 
<pre>flask
 
redis</pre>
 
==Step 2: Create a Dockerfile==
 
<pre>
 
FROM python:3.4-alpine
 
ADD . /code
 
WORKDIR /code
 
RUN pip install -r requirements.txt
 
CMD ["python", "app.py"]</pre>
 
==Step 3: Define services in a Compose file==
 
===Create a file called docker-compose.yml===
 
<pre>
 
version: '3'
 
services:
 
  web:
 
    build: .
 
    ports:
 
    - "5000:5000"
 
  redis:
 
    image: "redis:alpine"
 
</pre>
 
==Step 4: Build and run your app with Compose==
 
<pre>docker-compose up -d# (后台运行这些containers) 还是这个好
 
#马上访问就行了 哈哈 不用一直等
 
web_1    |    Use a production WSGI server instead.
 
web_1    |  * Debug mode: on
 
web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 
web_1    |  * Restarting with stat
 
 
Enter http://0.0.0.0:5000/ in a browser to see the application running.</pre>
 
 
==Step 5: Edit the Compose file to add a bind mount==
 
Edit docker-compose.yml in your project directory to add a bind mount for the web service:
 
<pre>
 
version: '3'
 
services:
 
  web:
 
    build: .
 
    ports:
 
    - "5000:5000"
 
    volumes:
 
    - .:/code
 
  redis:
 
    image: "redis:alpine"
 
#胶布的端口是机器的5000 后面的端口是容器的5000  volumes 也是这样顺序
 
</pre>
 
The new volumes key mounts the project directory (current directory) on the host to /code inside the container, allowing you to modify the code on the fly, without having to rebuild the image 把当前目录mounts 到容器的/code
 
 
  volumes:
 
    - ~/test/composetest:/code  #  将主机的当前目录映射为容器内部的 /code数据卷          把当前目录 ~/test/composetest mounts 到容器的/code
 
 
 
<pre>
 
eg:
 
 
#容器
 
evan@latop:~/test/composetest$ docker exec -it b4f882c81522 /bin/sh
 
/code # pwd
 
/code
 
/code # ls
 
Dockerfile          app.py              app.py~              docker-compose.yml  docker-compose.yml~  requirements.txt
 
 
#母机
 
evan@latop:~/test/composetest$ ls
 
app.py  app.py~  docker-compose.yml  docker-compose.yml~  Dockerfile  requirements.txt
 
 
 
</pre>
 
 
==Step 6: Re-build and run the app with Compose==
 
From your project directory, type docker-compose up to build the app with the updated Compose file, and run it.
 
==Step 7: Update the application==
 
Because the application code is now mounted into the container using a volume, you can make changes to its code and see the changes instantly, without having to rebuild the image. 可见 有mount 后 只改变母机的不用rebuild 了
 
 
Change the greeting in app.py and save it. For example, change the Hello World! message to Hello from Docker!:
 
 
return 'Hello from Docker! I have been seen {} times.\n'.format(count)
 
 
Refresh the app in your browser. The greeting should be updated, and the counter should still be incrementing.
 
 
=常用参数=
 
==links/external_links参数==
 
<pre>
 
-link  同一个物理机之前 
 
 
 
external_links
 
Docker平台的组合功能提供了一个叫“外部链接”(“external_links”)的设置选项,能用来接连那些在不同组合文件中定义的容器:
 
 
eg
 
    hostname: rabbitmq02
 
    extra_hosts:
 
      - "rabbitmq01:10.3.10.141" #前面是hostname 后面是ip
 
 
 
</pre>
 
 
[https://blog.csdn.net/kongxx/article/details/38676917 Docker使用Link在容器之间建立连接]
 
 
[https://sdk.cn/news/7643 Docker的组合功能:如何用external_links选项连接组合文件外部定义的容器]
 
 
[https://www.cnblogs.com/liyuanhong/articles/5851251.html 如何使不同主机上的docker容器互相通信]
 
 
[https://www.jianshu.com/p/aa984711df80 docker compose 中难缠的网络问题]
 
 
=trouble=
 
==Question ==
 
<pre>
 
docker-compose up
 
 
ERROR: Couldn't connect to Docker daemon at http+docker://localhost - is it running?
 
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable. </pre>
 
 
 
== solve==
 
<pre>
 
Use the command  sudo systemctl edit docker.service  to open an override file for docker.service in a text editor.
 
 
Add or modify the following lines, substituting your own values.
 
 
[Service]
 
ExecStart=
 
ExecStart=/usr/bin/dockerd -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock
 
Save the file.
 
 
Reload the systemctl configuration.
 
 
$ sudo systemctl daemon-reload
 
Restart Docker:
 
 
$ sudo systemctl restart docker.service
 
Check to see whether the change was honored by reviewing the output of netstat to confirm dockerd is listening on the configured port.
 
 
$ sudo netstat -lntp | grep dockerd
 
tcp        0      0 127.0.0.1:2375          0.0.0.0:*              LISTEN      3758/dockerd
 
 
vim ~/.bashrc
 
export DOCKER_HOST=tcp://localhost:2375
 
 
</pre>
 
 
https://stackoverflow.com/questions/27763340/docker-opts-do-not-work-in-config-file-etc-default-docker
 
 
https://stackoverflow.com/questions/44678725/cannot-connect-to-the-docker-daemon-at-unix-var-run-docker-sock-is-the-docker
 
 
其它办法 没过过 正确的是将当前用户加入docker组
 
jing@ubuntu:/tmp/docker$ sudo gpasswd -a ${USER} docker
 
  
 
=see also=
 
=see also=
官方文档永远是最帅的
 
 
https://docs.docker.com/compose/install/#install-compose
 
 
[https://www.jianshu.com/p/658911a8cff3 Docker:Docker Compose 详解]
 
 
[https://www.cnblogs.com/moxiaoan/p/9299404.html Docker-compose常用命令]
 
 
[https://blog.csdn.net/wanghailong041/article/details/52162293 Docker-compose命令详解]
 
 
https://docs.docker.com/compose/gettingstarted/
 
 
https://blog.csdn.net/zhugeaming2018/article/details/81518327
 
 
[https://www.cnblogs.com/regit/p/8309959.html docker-compose常用命令]
 
 
[https://www.jianshu.com/p/658911a8cff3 Docker:Docker Compose 详解]
 
 
 
[https://beginor.github.io/2017/06/08/use-compose-instead-of-run.html 使用 docker-compose 替代 docker run]
 
 
[https://www.cnblogs.com/ee900222/p/docker_5.html Docker系列之(五):使用Docker Compose编排容器]
 
 
 
[https://blog.csdn.net/u011781521/article/details/80464826 Docker(四)----Docker-Compose 详解]
 
 
[https://blog.csdn.net/u014799292/article/details/79094015 安装docker-compose并统一拉取镜像]
 
 
[https://blog.csdn.net/zhugeaming2018/article/details/81518327 『中级篇』Docker Compose的安装和基本使用(39)]
 
 
[https://blog.csdn.net/smartdt/article/details/78431618 Docker学习(6)Docker Compose介绍和编配]
 
 
[https://blog.csdn.net/hujyhfwfh2/article/details/83934415 Docker-Docker Compose]
 
 
[https://blog.csdn.net/yuzhongzi81/article/details/79295672 小白学Docker之Compose]
 
 
[https://blog.csdn.net/pushiqiang/article/details/78682323 docker-compose教程安装,使用, 快速入门]
 
  
  
[http://www.cnblogs.com/sammyliu/p/5932996.html 理解Docker(8):Docker 存储之卷(Volume)]
+
[https://blog.csdn.net/yangyangye/article/details/100523387  redis集群批量删除模糊key shell脚本]
  
[http://dockone.io/article/128 深入理解Docker Volume(一)]
+
[https://www.cnblogs.com/feng0520/p/11067025.html  redis cluster集群批量删除中的key]
  
[http://www.cnblogs.com/zhengran/p/4212519.html 深入理解Docker Volume(一)]
+
[https://my.oschina.net/u/1255588/blog/1563672 Redis集群批量删除key]
  
[[Docker-compose搭建nginx+php+mysql]]
+
[https://blog.csdn.net/zj20142213/article/details/80879744  redis集群批量删除指定的key]
  
[[category:容器]][[category: container]] [[category: compose]]
+
https://blog.csdn.net/qq_36090419/article/details/80537684

2020年6月22日 (一) 11:37的版本


Redis-bigkeys

redis cluster 批量删除key

#June 19 2020   有空结果一下原来的版本再优化一下 
cat redis_del_key.sh
#!/bin/bash
# Usage 
#redis_list=("host:post" "host:post")

redis_list=( "172.16.200.12:7003" "172.16.200.7:7004" "172.16.200.7:7005" "172.16.200.13:7000" "172.16.200.13:7001" "172.16.200.12:7002")
#redis_list=( "172.16.200.13:7000" "172.16.200.13:7001" "172.16.200.12:7002")
#redis_list=("172.16.200.7:7004")
password="3636password"

for info in ${redis_list[@]}
    do
        echo "开始执行:$info"  
        ip=`echo $info | cut -d : -f 1`
        port=`echo $info | cut -d : -f 2`

      for  loop in  `cat key.txt`
         do
         #echo $loop  |xargs -t -n1 redis-cli -c  -h $ip -p $port -a $password -c del
         echo $loop  |xargs -t -n1 redis-cli -c  -h $ip -p $port -a $password -c keys 
         done

    done
    echo "success done完成"

# cat key.txt 
infnce:wonderfulCache
ext_info



#原来的版本

del hello_user_* 

redis-cli keys


 redis-cli keys "hello_user_*" | xargs redis-cli del


./redis-cli -h IP -p PORT -a PASSWORD keys 'key*' | xargs  ./redis-cli -h IP  -p PORT -a PASSWORD del

redis-cli    -c  -h    172.31.22.218   -p 7000  -a pass keys 'hello_user_*'| xargs -i redis-cli    -c  -h    172.31.22.218   -p 7000  -a pass  del {}
redis-cli    -c  -h    172.31.22.218   -p 7001  -a pass keys 'hello_user_*'| xargs -i redis-cli    -c  -h    172.31.22.218   -p 7001  -a pass  del {}

redis-cli    -c  -h    172.31.16.135  -p 7002  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.16.135   -p 7002 -a pass  del {}
redis-cli    -c  -h    172.31.16.135  -p 7003  -a pass keys 'hello_user_*'| xargs  -i redis-cli    -c  -h    172.31.16.135   -p 7003 -a pass  del {}

redis-cli    -c  -h     172.31.23.43   -p 7004  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.23.43   -p 7004 -a pass  del {}
redis-cli    -c  -h     172.31.23.43   -p 7005  -a pass keys 'hello_user_*'| xargs -i  redis-cli    -c  -h    172.31.23.43   -p 7005 -a pass  del {}



#!/bin/bash
redis_comm=/usr/local/redis-5.0.3/bin/redis-cli
redis_ser01=192.168.50.175
redis_ser02=192.168.50.174
$redis_comm -c -h $redis_ser01  -p 7001 -a xxx  keys $1 | xargs -i $redis_comm -c -h $redis_ser01  -p 7001 -a xxx del {}
$redis_comm -c -h $redis_ser02  -p 7001 -a xxx  keys $1 | xargs -i $redis_comm -c -h $redis_ser02  -p 7001 -a xxx del {}

运行:

./redis_del_key.sh  匹配的key*

err  

-i  {} 都是一定要的 

redis-cli -n 0 keys "test*" | xargs redis-cli -n 0 del
(error) CROSSSLOT Keys in request don't hash to the same slot

see also

redis集群批量删除模糊key shell脚本

redis cluster集群批量删除中的key

Redis集群批量删除key

redis集群批量删除指定的key

https://blog.csdn.net/qq_36090419/article/details/80537684