minio对象存储docker-compose部署

参考:

https://juejin.cn/post/7128064094061461534

minio介绍:

  • 说得通俗易懂点,minio的作用就是用来存储文件的,比如图片、视频、音频等各种类型的文件。
    那么问题来了,java本身就可以直接把文件写到磁盘里面,为什么还要用minio呢?
  • minio有完善的文件管理功能,包括针对文件的上传,下载,删除等
    minio有强大的纠删功能,即便磁盘损坏,在一定程度上时可以避免丢失文件的
    minio有完善的权限管理功能,它可以针对不同的业务角色,分配不同的操作权限
    天然的分布式存储功能
    高性能;在标准硬件上,对象存储的读/写速度最高可以高达183 GB/s和171 GB/s
    文档全面,简单易用
  • 所以综上所述,相较于其他的文件存储方案,minio的竞争力还是很大的。下面附上官网链接

一. 单机版部署

镜像可选:

1
2
3
4
5
6
7
# 旧版
quay.io/minio/minio:RELEASE.2022-08-02T23-59-16Z
registry.cn-hangzhou.aliyuncs.com/zznn/mycentos:minio-RELEASE.2022-08-02T23-59-16Z

# 最新版
minio/minio
registry.cn-hangzhou.aliyuncs.com/zznn/mycentos:minio-latest

一般为了简单集成minio client,我们会自己部署一个单机版的先用用。现在我们开始,讲解一下如何快速部署一个单机版minio服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
version: "3.7"
services:
minio:
image: "quay.io/minio/minio:RELEASE.2022-08-02T23-59-16Z"
ports:
- "9000:9000"
- "9001:9001"
volumes:
- "./minio/data1:/data1"
- "./minio/data2:/data2"
command: server --console-address ":9001" http://minio/data{1...2}
environment:
- MINIO_ROOT_USER=admin
- MINIO_ROOT_PASSWORD=12345678
#- MINIO_ACCESS_KEY=AKIAIOSFODNN7EXAMPLE
#- MINIO_SECRET_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3

以上需要注意的几个点:

image-20230918154201819

image-20230918154219079

二. 纠删码模式部署(未测试)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
version: "3.7"
services:
minio:
image: "quay.io/minio/minio:RELEASE.2022-08-02T23-59-16Z"
ports:
- "9000:9000"
- "9001:9001"
volumes:
- "./minio/data1:/data1"
- "./minio/data2:/data2"
- "./minio/data3:/data3"
- "./minio/data4:/data4"
command: server --console-address ":9001" http://minio/data{1...4}
environment:
- MINIO_ROOT_USER=admin
- MINIO_ROOT_PASSWORD=12345678
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3

在单机模式部署的情况下,调整为纠删码模式,我们需要修改两个地方:

  • 挂载的磁盘增加到四个
  • 启动命令里面补上对应的挂载磁盘

该模式运行其中某个磁盘出现损坏的情况,在磁盘损坏后也能保证文件不会丢失。

三. 分布式部署

在单机上部署纠删码模式只能保证磁盘损坏的情况下,文件不丢失;并不能解决单点故障的问题,所以我们下面为了避免单点故障导致服务不可用,把minio服务改成分布式部署。
我们就部署四个机器,每个机器挂载四个磁盘,这样的话,我们就形成了分布式纠删码模式了,这样的话,我们文件存储服务的可靠性就大大地增强了。

  • 首先准备四台主机,ip地址分别为192.168.2.231,192.168.2.232,192.168.2.233,192.168.2.234;
  • 其次,我们分别在四台机器上部署一个minio服务实例,参考以下docker-compose.yml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
version: "3.7"
services:
minio:
image: "quay.io/minio/minio:RELEASE.2022-08-02T23-59-16Z"
ports:
- "9000:9000"
- "9001:9001"
volumes:
- "./minio/data1:/data1"
- "./minio/data2:/data2"
- "./minio/data3:/data3"
- "./minio/data4:/data4"
command: server --console-address ":9001" http://192.168.2.231:9000/data{1...4} http://192.168.2.232:9000/data{1...4} http://192.168.2.233:9000/data{1...4} http://192.168.2.234:9000/data{1...4}
environment:
- MINIO_ROOT_USER=admin
- MINIO_ROOT_PASSWORD=12345678
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3

在启动命令中,我们需要把集群中所有的服务ip都写进去,这样的话,minio分布式服务才能正常通信。

  • 部署负载均衡服务nginx
  • 当四台机器上的服务全部起来后,我们需要提供一个负载均衡服务作为访问minio分布式服务的统一的入口,首当其冲我们就想到了nginx,所以我们在使用docker compose部署一个nginx服务:
1
2
3
4
5
6
7
8
9
10
version: '3.7'
services:
nginx:
image: nginx:1.19.2-alpine
hostname: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "9000:9000"
- "9001:9001"

下面是nginx配置文件(未测试)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
user  nginx;
worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 4096;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;

# include /etc/nginx/conf.d/*.conf;

upstream minio {
server 192.168.2.231:9000;
server 192.168.2.232:9000;
server 192.168.2.233:9000;
server 192.168.2.234:9000;
}

upstream console {
ip_hash;
server 192.168.2.231:9001;
server 192.168.2.232:9001;
server 192.168.2.233:9001;
server 192.168.2.234:9001;
}

server {
listen 9000;
listen [::]:9000;
server_name localhost;

# To allow special characters in headers
ignore_invalid_headers off;
# Allow any size file to be uploaded.
# Set to a value such as 1000m; to restrict file size to a specific value
client_max_body_size 0;
# To disable buffering
proxy_buffering off;
proxy_request_buffering off;

location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

proxy_connect_timeout 300;
# Default is HTTP/1, keepalive is only enabled in HTTP/1.1
proxy_http_version 1.1;
proxy_set_header Connection "";
chunked_transfer_encoding off;

proxy_pass http://minio;
}
}

server {
listen 9001;
listen [::]:9001;
server_name localhost;

# To allow special characters in headers
ignore_invalid_headers off;
# Allow any size file to be uploaded.
# Set to a value such as 1000m; to restrict file size to a specific value
client_max_body_size 0;
# To disable buffering
proxy_buffering off;
proxy_request_buffering off;

location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;

# This is necessary to pass the correct IP to be hashed
real_ip_header X-Real-IP;

proxy_connect_timeout 300;

# To support websocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

chunked_transfer_encoding off;

proxy_pass http://console;
}
}
}

启动nginx服务,这样所有的请求都通过nginx负载均衡到minio服务上。

至此,我们就完成了minio服务的三种docker compose部署方式,逐次递进地讲解了每种方式的优势。如果我们只是为了集成minio到我们的应用程序中,只需要搭建一个单机版的服务即可。

转载自:

作者:梦想实现家_Z
链接:https://juejin.cn/post/7128064094061461534