docker第二篇:docker安装常用中间件

1、mysql

docker run --name mysql -h mysql -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=sBL2y7Uuxqyi -e MYSQL_DATABASE=test -p 3306:3306 --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d mysql:8.4.8

MYSQL_DATABASE环境变量用于指定能自动创建的一个数据库。

2、redis

2.1、单机模式:

docker run -v /root/redis/conf/:/usr/local/etc/redis/ -v /root/redis/data/:/data/ --name redis -h redis -e TZ=Asia/Shanghai -p 6379:6379 --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d redis:8.4.0-bookworm redis-server /usr/local/etc/redis/redis.conf

redis.conf文件在/root/redis/conf目录中,内容如下:

bind 0.0.0.0
port 6379
protected-mode yes
requirepass sBL2y7Uuxqyi
appendonly yes
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync always
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
save ""

windows环境,则需要在PowerShell上执行

docker run -v D:\redis\conf/:/usr/local/etc/redis/ -v D:\redis\data/:/data/ --name redis -h redis -e TZ=Asia/Shanghai -p 6379:6379 --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d redis:8.4.0-bookworm redis-server /usr/local/etc/redis/redis.conf

redis.conf文件在D:\redis\conf目录。

save "" 的作用是禁用RDB。

appendfsync 可选值有always、everysec、no。

操作布隆过滤器的命令的关键字是bf,见https://redis.io/docs/latest/commands/?name=bf

如bf.reserve bf 0.0001 100000000,创建一个容量是1亿,错误率是万分之一的布隆过滤器。布隆过滤器内存使用量可以通过https://hur.st/bloomfilte评估。

2.2、cluster模式:

利用docker compose部署,up后,执行redis-cli --cluster create命令。

docker-compose.yaml文件内容如下:

version: '3.4'
services:
  node1:
    image: redis:8.4.0-bookworm
    container_name: redis-node1
    restart: always
    ports:
      - 7001:7001
      - 17001:17001
    volumes:
      - /data/redis-cluster/7001/data:/data
      - /data/redis-cluster/7001:/usr/local/etc/redis
    command:
      redis-server /usr/local/etc/redis/redis.conf
    privileged: true
 
  node2:
    image: redis:8.4.0-bookworm
    container_name: redis-node2
    restart: always
    ports:
      - 7002:7002
      - 17002:17002
    volumes:
      - /data/redis-cluster/7002/data:/data
      - /data/redis-cluster/7002:/usr/local/etc/redis
    command:
      redis-server /usr/local/etc/redis/redis.conf
    privileged: true

  node3:
    image: redis:8.4.0-bookworm
    container_name: redis-node3
    restart: always
    ports:
      - 7003:7003
      - 17003:17003
    volumes:
      - /data/redis-cluster/7003/data:/data
      - /data/redis-cluster/7003:/usr/local/etc/redis
    command:
      redis-server /usr/local/etc/redis/redis.conf
    privileged: true
 
  node4:
    image: redis:8.4.0-bookworm
    container_name: redis-node4
    restart: always
    ports:
      - 7004:7004
      - 17004:17004
    volumes:
      - /data/redis-cluster/7004/data:/data
      - /data/redis-cluster/7004:/usr/local/etc/redis
    command:
      redis-server /usr/local/etc/redis/redis.conf
    privileged: true
 
  node5:
    image: redis:8.4.0-bookworm
    container_name: redis-node5
    restart: always
    ports:
      - 7005:7005
      - 17005:17005
    volumes:
      - /data/redis-cluster/7005/data:/data
      - /data/redis-cluster/7005:/usr/local/etc/redis
    command:
      redis-server /usr/local/etc/redis/redis.conf
    privileged: true
 
  node6:
    image: redis:8.4.0-bookworm
    container_name: redis-node6
    restart: always
    ports:
      - 7006:7006
      - 17006:17006
    volumes:
      - /data/redis-cluster/7006/data:/data
      - /data/redis-cluster/7006:/usr/local/etc/redis
    command:
      redis-server /usr/local/etc/redis/redis.conf
    privileged: true

node1做了容器7001端口到宿主机7001端口的映射,所以redis-node1容器内部要兼容7001端口,而不是6379端口。其他容器同理。也就是说要根据实际情况修改redis.conf文件中的port值。

redis.conf文件内容,除了上面内容外,还需添加:

masterauth sBL2y7Uuxqyi
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.189.128
cluster-announce-port 7001
cluster-announce-bus-port 17001

其中,masterauth是slave连接master时要使用的密码,如果master设置了密码,则masterauth必需。cluster-announce-ip是宿主机ip,假如node1、node2在宿主机192.168.189.128上,node3、node4在宿主机192.168.189.129上,node5、node6在宿主机192.168.189.130上,那么node1、node2的cluster-announce-ip值为192.168.189.128,node3、node4的cluster-announce-ip值为192.168.189.129,node5、node6的cluster-announce-ip值为192.168.189.130。

每个节点的cluster-announce-ip、cluster-announce-port、cluster-announce-bus-port可能不一样,要根据实际情况修改。如node1的是cluster-announce-port是7001,cluster-announce-bus-port是17001,node2的cluster-announce-port是7002,cluster-announce-bus-port是17002。。。以此类推。

执行docker-compose -f redis-docker-compose.yaml up -d命令启动。

之后,进到某一个节点里,执行redis-cli --cluster create命令:

redis-cli -a sBL2y7Uuxqyi --cluster create 192.168.189.128:7001 192.168.189.128:7002 192.168.189.128:7003 192.168.189.128:7004 192.168.189.128:7005  192.168.189.128:7006 --cluster-replicas 1

命令执行后,会有一些日志,能看到slot的分配情况,以及master、slave的情况,比如哪些节点是master,哪些节点是slave,master的slave有哪些,slave的master是哪个。

此时,redis集群就部署好了。 见https://redis.io/docs/latest/operate/oss_and_stack/management/scaling/

可以在集群外用redis客户端连接,也可进到某一个节点里,执行redis-cli -p xxx -c连接,加-c的话,如果操作的key不在当前节点,则会自动重定向到对应节点,否则不会自动重定向,如果操作的key不在当前节点,会报错。连接上后,可以执行cluster命令,如cluster nodes可以查看有哪些master,哪些slave。具体有哪些cluster命令,可以执行cluster help查看。

3、kafka

https://hub.docker.com/r/apache/kafka

4、pulsar

docker run --name pulsar -h pulsar -e TZ=Asia/Shanghai -p 6650:6650 -p 8080:8080 --cpus=2 -m 4g --memory-swap 8g --restart=always -d apachepulsar/pulsar:4.0.6 bin/pulsar standalone

5、elasticsearch

docker run --name elasticsearch -h elasticsearch -e TZ=Asia/Shanghai -e ALLOW_EMPTY_PASSWORD=yes -p 9200:9200 --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d bitnami/elasticsearch:9.1.2-debian-12-r0

6、filebeat

filebeat配置文件:

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /path/to/log1/*.log
    - /path/to/log2/*.log
  exclude_lines: ['^$']
  multiline:
    type: pattern
    pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
    negate: true
    match: after
output.elasticsearch:
  enabled: true
  hosts: ["http://host.docker.internal:9200"]
  indices:
    - index: "%{[index]}-%{+yyyy.MM.dd}"
      when.equals:
        level: "info"
    - index: "error-%{[detail.name]}-%{+yyyy.MM.dd}"
      when.contains:
        status: "ERR"
    - index: "default-%{+yyyy.MM.dd}"

如上,可以同时多个文件。

docker run -d --name filebeat -h filebeat -u root -v /etc/localtime:/etc/localtime:ro -v /home/koushengrui/blackbox/ELK/filebeat.yml:/usr/share/filebeat/filebeat.yml:ro --add-host=host.docker.internal:host-gateway --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d elastic/filebeat:9.0.3 filebeat --strict.perms=false

还有,别忘了把日志文件映射到容器的指定目录。

7、kibana

docker run --name kibana -h kibana -e TZ=Asia/Shanghai -e KIBANA_ELASTICSEARCH_URL=host.docker.internal:9200 --add-host=host.docker.internal:host-gateway -p 5601:5601 --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d bitnami/kibana:9.1.2-debian-12-r0

8、mongodb

docker run --name mongodb -h mongodb -e TZ=Asia/Shanghai -p 27017:27017 --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d bitnami/mongodb:8.0.13-debian-12-r0

9、openresty

利用openresty官方提供的openresty镜像。openresty自带的配置文件nginx.conf在/usr/local/openresty/nginx/conf目录,其include了/etc/nginx/conf.d目录中所有以conf结尾的文件,所以我们只需要把宿主机中的自定义配置文件所在的目录通过-v映射到/etc/nginx/conf.d目录即可。nginx.conf默认开启了access_log,并且把access.log、error.log放在了/usr/local/openresty/nginx/logs目录中,我们可以把日志文件目录映射出来。

windows环境:

在PowerShell执行

docker run --name openresty -h openresty -v D:\openresty\conf.d:/etc/nginx/conf.d -v D:\openresty\logs:/usr/local/openresty/nginx/logs -p 80:80 --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d openresty/openresty:1.25.3.2-5-bookworm

mac/linux环境:

docker run --name openresty -h openresty --add-host=host.docker.internal:host-gateway -v /etc/localtime:/etc/localtime:ro -v /root/openresty/conf.d:/etc/nginx/conf.d -v /root/openresty/logs:/usr/local/openresty/nginx/logs -p 80:80 --cpus=0.5 -m 1g --memory-swap 2g --restart=always -d openresty/openresty:1.25.3.2-5-bookworm

如果要使用https,则要指定TLS证书,如果宿主机的TLS证书的用户是root,则必须要用-u指定用户是root。

-v /etc/letsencrypt:/etc/letsencrypt -u root -p 443:443
如果要启用http/2,则参考https://nginx.org/en/docs/http/ngx_http_v2_module.html配置。
自定义配置文件default.conf内容如下:
upstream user_service {
    server host.docker.internal:9000;
}

upstream feed_service {
    server host.docker.internal:9001;
}

server {
    listen 80;
    location / {
       root   /usr/local/openresty/nginx/html;
       index  index.html index.htm;
    }
}

server { 
    listen 443 ssl; 
    http2 on; 

    ssl_certificate     /etc/letsencrypt/live/www.koushr.online/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.koushr.online/privkey.pem;

    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    location /user/ {
        proxy_pass http://user_service;
    }

    location /feed/ {
        proxy_pass_header Server;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_pass http://feed_service;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }
}

10

posted on 2024-07-22 19:31  koushr  阅读(274)  评论(0)    收藏  举报

导航