1. 概述

1.1. 介绍

minIO是目前一个比较轻量级的分布式文件系统,遵循apache 2.0开源协议。我们可以把他视为数据中心版的S3(S3是AWS的一个云存储服务,相当于阿里云的oss),他是目前兼容性比较好的对象存储。他本身是由golang开发的,所以运行效率可以和ceph媲美。目前社区也非常活跃,git的代码提交者中,我们可以看到很多中国程序员的身影。

1.2. 功能与集成

既然称minIO是s3,那么s3的一些特性,minIO是完全具备的。并且,并不是只有商业版才有,而是开源版就具备了。比如:Bucket的版本控制,Bucket的生命周期管理,多租户,对外暴露API,支持Veeam备份,角色访问控制ARN,还可以发消息给中间件(redis,ES,kafka)。这些功能我们会在对象存储那一个专题中详细讲解。

2. 架构

由于是非常轻量级的软件,所以架构上也没有这么复杂,他使用操作系统的文件系统作为存储介质,我们在向任意节点写数据的时候,minIO会自动同步数据到另外的节点,而机制叫做erasure code(纠删码)来保证集群的稳定,保证数据可用,所以我们建议至少使用4个节点来构建集群。

如果一个N节点的分布式MinIO,只要有N/2节点在线,数据就是安全的。但是要保证至少有N/2+1个节点来创建新的对象。比如:我们的集群有4个节点,每个节点上一块盘,就算有2两个节点宕机,这个集群仍然是可读的,但是,我们需要3个节点才能让集群写数据。这就是为什么我们要有4个节点来构建集群。

早期版本中,每个租户至少有4个盘,最多有16个盘,这个是纠删码的限制,而新版本中是没有限制的。如果想要实现多租户,就需要借助于kubernetes来构建多个MinIO实例,或者启动多个实例来实现多租户。也就是说,一个进程对应一个实例,一个实例对应一个租户。

我们这次实验由下面这四台机器构成

3. 搭建minio集群

3.1. 挂载磁盘

机器资源挂载磁盘路径
192.168.159.137/data/minio_data
192.168.159.138/data/minio_data
192.168.159.139/data/minio_data
192.168.159.140/data/minio_data

       生产环境强烈建议至少四台机器,这也是官方的建议要求,这样的话就可以做到挂掉一台机器集群依然可以读写,挂掉两台机器集群依然可读,本文仅以四台机器为例子说明如何搭建集群 

创建挂载目录(所有节点,上面四台机器都创建相同路径的文件夹)

mkdir /data/minio_data/

注意:需要将新建的目录挂在到对应的磁盘下,磁盘不挂载好,集群启动会报错:找不到磁盘,如下:

  • [root@minio1 /]# df -h
    Filesystem               Size  Used Avail Use% Mounted on
    devtmpfs                 475M     0  475M   0% /dev
    tmpfs                    487M     0  487M   0% /dev/shm
    tmpfs                    487M  7.7M  479M   2% /run
    tmpfs                    487M     0  487M   0% /sys/fs/cgroup
    /dev/mapper/centos-root   17G  7.4G  9.6G  44% /
    /dev/sda1               1014M  137M  878M  14% /boot
    tmpfs                     98M     0   98M   0% /run/user/0
    [root@minio1 /]# mount /dev/sda1 /data/minio_data/
    mount: /dev/sda1 is already mounted or /data/minio_data busy
           /dev/sda1 is already mounted on /boot
           /dev/sda1 is already mounted on /data/minio_data
    [root@minio1 /]# lsblk
    NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda               8:0    0   20G  0 disk 
    ├─sda1            8:1    0    1G  0 part /data/minio_data
    └─sda2            8:2    0   19G  0 part 
      ├─centos-root 253:0    0   17G  0 lvm  /
      └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
    sr0              11:0    1  4.4G  0 rom  
    [root@minio1 /]#

3.2. 准备环境

0. 关闭防火墙

  • systemctl stop firewalld
    systemctl disable firewalld
    systemctl status firewalld

1. 修改主机名

  • 192.168.159.137,执行 hostnamectl set-hostname minio1;查看 cat /etc/hosts
    192.168.159.138,执行 hostnamectl set-hostname minio2;查看 cat /etc/hosts
    192.168.159.139,执行 hostnamectl set-hostname minio3;查看 cat /etc/hosts
    192.168.159.140,执行 hostnamectl set-hostname minio4;查看 cat /etc/hosts

2. 修改hosts文件,实现局域网互通

  • cat >> /etc/hosts << EOF
    192.168.159.137 minio1
    192.168.159.138 minio2 
    192.168.159.139 minio3 
    192.168.159.140 minio4
    EOF 

3. 修改系统最大文件数

  • ulimit -n #查看最大连接数
    ulimit -a
    echo "*   soft    nofile  65535" >> /etc/security/limits.conf
    echo "*   hard    nofile  65535" >> /etc/security/limits.conf
    sysctl -p
    reboot

4. 创建minio启动脚本和配置文件目录

  • mkdir -p /data/minio/run && mkdir -p /etc/minio

5. 下载minio到/data/minio/run目录下

  • (cd /data/minio/run && wget https://dl.min.io/server/minio/release/linux-amd64/minio)

3.3. 编写集群启动脚本(所有节点配置文件相同)

  • 启动脚本/data/minio/run/run.sh

  • #!/bin/bash
    #export MINIO_ACCESS_KEY=minio
    #export MINIO_SECRET_KEY=miniostorage
    
    export MINIO_ROOT_USER=minio
    export MINIO_ROOT_PASSWORD=miniostorage
    
    /data/minio/run/minio server  --config-dir /etc/minio --address ":9000" --console-address ":9001" \
    http://192.168.159.137/data/minio_data/data1 http://192.168.159.137/data/minio_data/data2 \
    http://192.168.159.138/data/minio_data/data1 http://192.168.159.138/data/minio_data/data2 \
    http://192.168.159.139/data/minio_data/data1 http://192.168.159.139/data/minio_data/data2 \
    http://192.168.159.140/data/minio_data/data1 http://192.168.159.140/data/minio_data/data2
  • 其中,“MINIO_ACCESS_KEY”为用户名,“MINIO_SECRET_KEY”为密码,密码不能设置过于简单,不然minio会启动失败

  • 创建好的/data/minio/run/目录结构如下:

  • [root@minio1 run]# pwd
    /data/minio/run
    [root@minio1 run]# ll
    总用量 106824
    -rwxr-xr-x. 1 root root 109383680 2月  13 14:35 minio
    -rwxr-xr-x. 1 root root       620 2月  15 06:25 run.sh
    [root@minio1 run]# 
    
  • systemd配置文件minio.service

    cat > /usr/lib/systemd/system/minio.service <<EOF
    [Unit]
    Description=Minio service
    Documentation=https://docs.minio.io/
    
    [Service]
    WorkingDirectory=/data/minio/run/
    ExecStart=/data/minio/run/run.sh
    
    Restart=on-failure
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target
    EOF

3.4. 启动测试

  • 修改权限

    chmod +x /usr/lib/systemd/system/minio.service && chmod +x /data/minio/run/minio && chmod +x /data/minio/run/run.sh
  • 依次启动每个服务器的minio

  • [root@minio1 run]# systemctl daemon-reload
    [root@minio1 run]# systemctl enable minio && systemctl start minio
    [root@minio1 run]# systemctl status minio
  • 浏览器输入集群任意节点地址+9000端口,即可访问minio,可创建“bucket”并上传文件测试
  • http://192.168.159.137:9000
  • http://192.168.159.138:9000
  • http://192.168.159.139:9000
  • http://192.168.159.140:9000
  • 用户名和密码 cat /data/minio/run/run.sh中
  • MINIO_ROOT_USER=minio
  • MINIO_ROOT_PASSWORD=miniostorage

3.5. 使用nginx做代理

  • 资源准备
  • # 服务器IP地址 192.168.159.141
# IP地址 192.168.159.141
hostnamectl set-hostname nginx #登录192.168.159.141
reboot
  • 关闭防火墙
systemctl stop firewalld
systemctl status firewalld
systemctl disable firewalld
  • 安装nginx
# 一、安装编译工具及库文件
        yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

# 二、安装 PCRE让 Nginx 支持 Rewrite 功能
        cd /usr/local/src/
        wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

    # 解压安装包
        tar -zxvf pcre-8.35.tar.gz

    # 进入安装包目录
        cd pcre-8.35

    # 编译安装
        ./configure
        make && make install

    # 查看pcre版本
        pcre-config --version

# 三、安装 Nginx
        cd /usr/local/src/
        wget http://nginx.org/download/nginx-1.20.2.tar.gz

    # 解压安装包
        tar -zxvf nginx-1.20.2.tar.gz

    # 进入安装包目录
        cd nginx-1.20.2

    # 编译安装
        ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
        make
        make install

# 到此,nginx安装完成
  • nginx安装结束。目录结构
  • 配置文件:/usr/local/webserver/nginx/conf/nginx.conf
  • 启动目录:/usr/local/webserver/nginx/sbin/nginx
[root@nginx nginx]# pwd
/usr/local/webserver/nginx

[root@nginx nginx]# ll
总用量 4
drwx------. 2 nobody root    6 2月  15 21:54 client_body_temp
drwxr-xr-x. 2 root   root 4096 2月  15 21:51 conf
drwx------. 2 nobody root    6 2月  13 21:39 fastcgi_temp
drwxr-xr-x. 2 root   root   40 2月  13 17:18 html
drwxr-xr-x. 2 root   root   58 2月  14 08:49 logs
drwx------. 2 nobody root    6 2月  13 21:39 proxy_temp
drwxr-xr-x. 2 root   root   36 2月  15 21:31 sbin
drwx------. 2 nobody root    6 2月  13 21:39 scgi_temp
drwx------. 2 nobody root    6 2月  13 21:39 uwsgi_temp
  • 修改配置文件:/usr/local/webserver/nginx/conf/nginx.conf
upstream minio{
        server 192.168.159.137:9001;
        server 192.168.159.138:9001;
        server 192.168.159.139:9001;
        server 192.168.159.140:9001;
}
server {
        listen 80;
        server_name localhost;
        location / {
                proxy_pass http://minio;
                proxy_set_header Host $http_host;
               #client_max_body_size 1000m;
        }
}
  • 启动nginx:
cd /usr/local/webserver/nginx
./sbin/nginx  -c ./conf/nginx.conf #指定配置文件,启动方式
./sbin/nginx  -c ./conf/nginx.conf -s reload #修改配置文件,重新加载
  • 验收成果:

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐