MinIO 分布式集群搭建

背景

阿里云迁移 IDC,选型 MinIO 来替代阿里云 OSS,并为 K8S 提供对象存储服务。

简介

MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。
MinIO是一个非常轻量的服务,可以很简单的和其他应用的结合,类似 NodeJS, Redis 或者 MySQL。

单机 Docker 安装(用于测试)

# 下载minio镜像
$ docker pull minio/minio

# 创建目录
$ mkdir /data/minio

# 单机启动
$ docker run -d -p 9000:9000 \
-e "MINIO_ROOT_USER=admin" \
-e "MINIO_ROOT_PASSWORD=bPxRfiCYEXAMPLEKEY" \
--name minio minio/minio server /data/minio

# 安装 mc 命令
$ cd /usr/local/bin/
$ wget https://dl.min.io/client/mc/release/linux-amd64/mc
$ chmod +x mc
$ ./mc --help

# 配置 mc 命令自动提示
$ sudo wget https://raw.githubusercontent.com/minio/mc/master/autocomplete/bash_autocomplete -O /etc/bash_completion.d/mc
$ source /etc/bash_completion.d/mc

分布式集群搭建

http://docs.minio.org.cn/docs/master/distributed-minio-quickstart-guide

服务器

3 台服务器,每台服务器 2 块磁盘。

# 3 台服务器列表
12.3.35.31 minio1
12.3.35.32 minio2
12.3.35.33 minio3

# 每个服务器 2 块磁盘
$ mkdir -p /minio/data1
$ mkdir -p /minio/data2

手动启动

# 3 个节点都执行如下命令
$ export MINIO_ACCESS_KEY=admin
$ export MINIO_SECRET_KEY=Minio@1234
$ minio server --address "0.0.0.0:9090" http://node{1...3}/minio/data{1...2}

minio 启停脚本(3台服务器)

# ,启停脚本 minio.sh
$ cat /home/minio/minio.sh
#!/usr/bin/env bash
# minio 启停脚本

# 使用方法
usage() {
   echo "Usage: sh $0 {start|stop|status|restart}"
   exit 1
}


# 判断参数
if [ $# -lt 1 ]; then
    usage
    exit 1;
fi

# 认证信息
export MINIO_ACCESS_KEY=admin
export MINIO_SECRET_KEY=Minio@1234

start() {
    echo "Starting minio..."
    # 启动
    nohup minio server --address "0.0.0.0:9090" http://data{1...3}/minio/data{1...2} >/dev/null 2>&1 &
    # 启动失败
    if [ $? -ne 0 ]; then
      echo "Failed to stop minio."
      exit 1
    fi

    echo "Started minio."
}

stop() {
    echo "Stopping minio..."
    PID=$(ps -ef | grep 'minio server' | grep -v grep | awk '{print $2}')
    if [ "X${PID}" != "X" ]; then
        kill -9 ${PID}
        if [ $? -ne 0 ]; then
          echo "Failed to kill minio $pid."
          exit 1
        fi
    fi

    echo "stopped minio."
}

status() {
    PID=$(ps -ef | grep 'minio server' | grep -v grep | awk '{print $2}')
    if [ "X${PID}" = "X" ]; then
        echo "minio is not running."
        exit 1
    else
        echo "minio is running PID: (${PID})."
        exit 0
    fi
}

restart() {
    stop
    start
    sleep 5
    status
}


case "$1" in
        'start')
            start
            ;;

        'stop')
            stop
            ;;

        'status')
            status
            ;;

        'restart')
            restart
            ;;

        *)
            usage
            exit 1
            ;;
esac
exit 0

# 设置权限
$ chmod +x minio.sh

# 管理 minio 服务
$ ./minio.sh {start|stop|status|restart}

配置 systemd

# 配置 minio.service
$ cat << EOF > /usr/lib/systemd/system/minio.service
[Unit]
Description=Minio Server
After=network.target

[Service]
Type=forking
User=minio
Group=minio
ExecStart=/home/minio/minio.sh start
ExecReload=/home/minio/minio.sh restart
ExecStop=/home/minio/minio.sh stop
PrivateTmp=true

# Restart 配置可以在进程被 kill 掉之后,让 systemctl 产生新的进程,避免服务挂掉
Restart=on-failure
RestartSec=30

[Install]
WantedBy=multi-user.target
EOF

# 设置开机启动,并立即启动
$ systemctl enable --now minio.service

# 使用 systemd 管理 minio 服务
$ systemctl [start|stop|restart|status] minio.service

Nginx 高可用代理

# nginx 配置文件示例
$ cat nginx.conf
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;

    upstream minio {
        server 12.3.35.31:9090;
        server 12.3.35.32:9090;
        server 12.3.35.33:9090;
    }

    server {
        listen       9090;
        listen  [::]:9090;
        server_name  minio.daodaotest.com;

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

使用对象存储

# mc 子命令自动补全设置
$ ./mc --autocompletion

# 3 台服务器上分别执行,创建一个存储空间 test
$ mc config host add test http://12.3.35.xx:9090 admin Minio@1234
  
# 查看存储空间
$ mc admin info test

# 查看所有存储空间
$ mc config host ls

# 创建一个 bucket
$ mc mb test/default
  
# 创建文件并查看
$ echo "hello world" | mc pipe test/default/test.txt
$ mc ls test/default/
$ mc cat test/default/test.txt

微信公众号:daodaotest

你可能感兴趣的:(minio,minio,对象存储)