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 --restart=always -d bitnami/mysql:9.4.0-debian-12-r1

MYSQL_DATABASE环境变量用于指定自动创建的数据库,只能指定一个,多个数据库名用逗号分隔识别不出来。见https://hub.docker.com/r/bitnami/mysql

2、postgresql

docker run --name postgresql -h postgresql -e TZ=Asia/Shanghai -e POSTGRESQL_PASSWORD=sBL2y7Uuxqyi -p 5432:5432 --restart=always -d bitnami/postgresql:17.6.0-debian-12-r4

3、redis

docker run --name redis -h redis -e TZ=Asia/Shanghai -e REDIS_PASSWORD=sBL2y7Uuxqyi -u root -v /root/redis/data/:/bitnami/redis/data/ -p 6379:6379 --restart=always -d bitnami/redis:8.2.1-debian-12-r0

配置文件在/opt/bitnami/redis/etc目录。

可以在run时指定环境变量来修改配置,在/opt/bitnami/redis/etc目录中会生成一份自定义的redis.conf文件。还有很多其他的环境变量可以指定,见https://hub.docker.com/r/bitnami/redis

redis8.0开始原生支持布隆过滤器,不用再额外安装RedisBloom插件了。https://hub.docker.com/_/redis

docker run --name redis -h redis -e TZ=Asia/Shanghai -p 6379:6379 --restart=always -d redis:8.2.1-bookworm

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

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

4、kafka

docker run --name kafka -h kafka -e TZ=Asia/Shanghai -e KAFKA_CFG_NODE_ID=0 -e KAFKA_CFG_PROCESS_ROLES=controller,broker -e KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093 -e KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT -e KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093 -e KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER -p 9092:9092 --restart=always -d bitnami/kafka:4.0.0-debian-12-r9

5、pulsar

docker run --name pulsar -h pulsar -e TZ=Asia/Shanghai -p 6650:6650 -p 8080:8080 --restart=always -d apachepulsar/pulsar:4.0.6 bin/pulsar standalone

6、elasticsearch

docker run --name elasticsearch -h elasticsearch -e TZ=Asia/Shanghai -e ALLOW_EMPTY_PASSWORD=yes -p 9200:9200 --restart=always -d bitnami/elasticsearch:9.1.2-debian-12-r0

7、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 -d elastic/filebeat:9.0.3 filebeat --strict.perms=false

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

8、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 -d bitnami/kibana:9.1.2-debian-12-r0

9、mongodb

docker run --name mongodb -h mongodb -e TZ=Asia/Shanghai -p 27017:27017 --restart=always -d bitnami/mongodb:8.0.13-debian-12-r0

10、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环境:

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 --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 --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;
    }
}

11、clickhouse
docker run --name clickhouse -h clickhouse -e TZ=Asia/Shanghai -e CLICKHOUSE_ADMIN_PASSWORD=sBL2y7Uuxqyi --log-driver=none -v /path/to/config.xml:/opt/bitnami/clickhouse/etc/config.xml:ro -p 8123:8123 -p 9000:9000 --restart=always -d bitnami/clickhouse:25.6.3-debian-12-r0
使用--log-driver=none关闭了docker log,不然刷盘太严重,很快磁盘就满了。config.xml从clickhouse容器(把上面命令去掉-v映射后,启动,创建一个clickhouse容器,进入容器执行ps命令查看启动clickhouse进程的命令)拷贝而来,在里面搜索"<ttl>",把其中的"INTERVAL 30 DAY DELETE"都改成"INTERVAL 1 DAY DELETE",即让日志1天就过期。否则,日志也会占用大量磁盘空间。
12、influxdb
docker run --name influxdb -h influxdb -e TZ=Asia/Shanghai -e INFLUXDB_ADMIN_USER_PASSWORD=sBL2y7Uuxqyi -p 8086:8086 --restart=always -d bitnami/influxdb:3.2.0-debian-12-r0

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

导航