一、关于

MinIO 是一个Go语言编写的,基于AGPL-3.0 License开源协议的对象存储服务,官网介绍有以下特性:

  • 高性能
  • 可扩展性
  • 云原生
  • 开放全部源代码 + 企业级支持
  • 简单

MinIO官方提供了、Java、Python、Go、.NET的SDK。

二、使用

2.1 安装

下载rpm包,然后安装,安装命令:

rpm -ivh 包名

下载地址:https://dl.min.io/server/minio/release/

2.2 配置系统服务

修改系统服务文件:

vim /etc/systemd/system/minio.service

修改用户以及组为root:

[Service]
WorkingDirectory=/usr/local

User=root
Group=root
2.3 配置参数

修改minio配置文件:

vim /etc/default/minio

配置参数,如下:

MINIO_VOLUMES="/opt/minio/data"

MINIO_OPTS="--address :9000"

MINIO_ACCESS_KEY=rabb

MINIO_SECRET_KEY=12345678

MINIO_REGION_NAME="test_region_name"

MINIO_ACCESS_KEY要大于三位,MINIO_SECRET_KEY要大于8位。

2.4 创建数据目录

创建data目录:

mkdir -p /opt/minio/data
2.5 启动

以服务形式启动:

systemctl daemon-reload
systemctl start minio

启动失败可以查看系统日志:

vim /var/log/messages
2.6 浏览器访问

访问地址:http://192.168.18.55:9000/

用户名密码为/etc/default/minio中配置的MINIO_ACCESS_KEY和MINIO_SECRET_KEY。

进入系统后,选择Buckets菜单点击Create Bucket按钮可以添加一个桶,添加桶后可以在桶的页面上传文件。

若设置桶的策略为public,则可以通过以下链接直接访问文件:

http://192.168.18.55:9000/BucketName/FileName

2.7 Java调用

添加依赖:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.minio</groupId>
      <artifactId>minio</artifactId>
      <version>8.3.6</version>
    </dependency>
    <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>4.9.3</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.11.0</version>
    </dependency>

application.properties添加配置:

minio.endpoint=http://192.168.18.55:9000
minio.accesskey=rabb
minio.secretKey=12345678

注册MinioClient:

@Data
@ConfigurationProperties(prefix = "minio")
@Component
public class MinIOConfig {
    private String endpoint;
    private String accesskey;
    private String secretKey;


    @Bean
    public MinioClient minioClient() {
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint(endpoint)
                        .credentials(accesskey, secretKey)
                        .build();
        return minioClient;
    }
}

工具类:

@Component
public class MinioUtils {
    /**
     * 创建bucket
     *
     * @param bucketName bucket名称
     */
    @SneakyThrows
    public void createBucket(String bucketName) {
        if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 上传文件
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @param stream     文件流
     */
    public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
        client.putObject(
                PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
                        stream, stream.available(), -1)
                        .build());
    }
}

测试:

@SpringBootTest
class MinioApplicationTests {

    @Autowired
    MinioClient minioClient;
    @Autowired
    MinioUtils minioUtils;

    @Test
    void contextLoads() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        List<Bucket> bucketList = minioClient.listBuckets();
        for (Bucket bucket : bucketList) {
            System.out.println(bucket.creationDate() + ", " + bucket.name());
        }
    }

    @Test
    void testUpload() throws Exception {
        String bucketName = "rabb";
        String filePath = "E:\\images\\th.jpg";
        File file = new File(filePath);
        minioUtils.createBucket(bucketName);
        minioUtils.putObject(bucketName,file.getName(),new FileInputStream(file));
    }
}
2.8 windows部署

下载minio windows程序,分别创建E:/minio/data1,E:/minio/data2,E:/minio/data3,E:/minio/data4四个文件夹,然后创建 start.bat:

set MINIO_ACCESS_KEY=rabb
set MINIO_SECRET_KEY=12345678
minio.exe server --address :20001 http://127.0.0.1/E:/download/minio/data1 ^
                 http://127.0.0.1/E:/download/minio/data2 ^
                 http://127.0.0.1/E:/download/minio/data3 ^
                 http://127.0.0.1/E:/download/minio/data4

最后执行该脚本就可以启动一个单节点四盘的minio实例。

2.9 docker部署

单机启动:

docker run -p 9000:9000  -p 10000:10000  --name minio   -v /opt/minio/data-single   -e "MINIO_ROOT_USER=rabb"   -e "MINIO_ROOT_PASSWORD=12345678"   minio/minio server  --console-address :10000 /data

集群启动,添加配置文件docker-compose.yaml,以及nginx.conf,之后执行docker-compose up命令。

docker-compose.yaml:

version: '3.7'

# starts 4 docker containers running minio server instances.
# using nginx reverse proxy, load balancing, you can access
# it through port 9000.
services:
  minio1:
    image: minio/minio:RELEASE.2020-11-10T21-02-24Z
    volumes:
      - data1-1:/data1
      - data1-2:/data2
    expose:
      - "9000"
    environment:
      MINIO_ACCESS_KEY: minio
      MINIO_SECRET_KEY: minio123
    command: server http://minio{1...4}/data{1...2}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3

  minio2:
    image: minio/minio:RELEASE.2020-11-10T21-02-24Z
    volumes:
      - data2-1:/data1
      - data2-2:/data2
    expose:
      - "9000"
    environment:
      MINIO_ACCESS_KEY: minio
      MINIO_SECRET_KEY: minio123
    command: server http://minio{1...4}/data{1...2}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3

  minio3:
    image: minio/minio:RELEASE.2020-11-10T21-02-24Z
    volumes:
      - data3-1:/data1
      - data3-2:/data2
    expose:
      - "9000"
    environment:
      MINIO_ACCESS_KEY: minio
      MINIO_SECRET_KEY: minio123
    command: server http://minio{1...4}/data{1...2}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3

  minio4:
    image: minio/minio:RELEASE.2020-11-10T21-02-24Z
    volumes:
      - data4-1:/data1
      - data4-2:/data2
    expose:
      - "9000"
    environment:
      MINIO_ACCESS_KEY: minio
      MINIO_SECRET_KEY: minio123
    command: server http://minio{1...4}/data{1...2}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3

  nginx:
    image: nginx:1.19.2-alpine
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    ports:
      - "9000:9000"
    depends_on:
      - minio1
      - minio2
      - minio3
      - minio4

## By default this config uses default local driver,

## For custom volumes replace with volume driver configuration.

volumes:
  data1-1:
  data1-2:
  data2-1:
  data2-2:
  data3-1:
  data3-2:
  data4-1:
  data4-2:

nginx.conf:

user  nginx;
worker_processes  auto;

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

events {
    worker_connections  1024;
}

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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

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

    upstream minio {
        server minio1:9000;
        server minio2:9000;
        server minio3:9000;
        server minio4:9000;
    }

    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;

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

docker-compose相关命令:

# 前台运行
docker-compose up
# 后台运行
docker-compose up -d
# 停止服务
docker-compose down
# 查看
docker-compose ps
2.10 遇到的问题

使用版本(RELEASE.2022-02-18T01-50-10Z)不允许使用一块硬盘启动集群模式,会报以下错误:

Disk will not be used. Please supply a separate disk and restart the server.

三、其他功能

3.1 压缩

MinIO服务器允许流式压缩以确保有效的磁盘空间使用。压缩是在飞行中发生的,即对象在写入磁盘之前已被压缩。MinIO klauspost/compress/s2由于其稳定性和性能而使用流式压缩。

该算法专门针对机器生成的内容进行了优化。每个CPU内核的写吞吐量通常至少为300MB / s。解压缩速度通常至少为1GB / s。 这意味着在原始IO低于这些数字的情况下,压缩不仅会减少磁盘使用量,而且有助于提高系统吞吐量。 通常,当可以压缩内容时,在旋转磁盘系统上启用压缩将提高速度。

3.2 纠删码

Minio使用纠删码erasure code和校验和checksum来保护数据免受硬件故障和无声数据损坏。 即便您丢失一半数量(N/2)的硬盘,您仍然可以恢复数据。

纠删码原理:https://www.jianshu.com/p/acf0f392bac9

3.3 分布式特性

1)数据保护

分布式Minio采用 纠删码来防范多个节点宕机和位衰减bit rot。

2)高可用

分布式Minio至少需要4个硬盘,使用分布式Minio自动引入了纠删码功能。单机Minio服务存在单点故障,相反,如果是一个有N块硬盘的分布式Minio,只要有N/2硬盘在线,你的数据就是安全的。不过你需要至少有N/2+1个硬盘来创建新的对象。

例如,一个16节点的Minio集群,每个节点16块硬盘,就算8台服務器宕机,这个集群仍然是可读的,不过你需要9台服務器才能写数据。

注意,只要遵守分布式Minio的限制,你可以组合不同的节点和每个节点几块硬盘。比如,你可以使用2个节点,每个节点4块硬盘,也可以使用4个节点,每个节点两块硬盘,诸如此类。

3)一致性

Minio在分布式和单机模式下,所有读写操作都严格遵守read-after-write一致性模型。

四、参考

官方中文文档集群部署:http://docs.minio.org.cn/docs/master/distributed-minio-quickstart-guide

docker部署教程:https://juejin.cn/post/7063999391152996383