---
title: "Minio 笔记"
date: 2021-10-17T00:48:00+08:00
lastmod: 2021-10-17T00:48:00+08:00
keywords: []
tags: ["minio"]
categories: ["storage"]
---

## 部署单节点分布式存储
### 环境
- 服务器 IP: 192.168.1.10
- 自定义域名: X.X.X

### 安装 minio
- 下载
    ```bash
    curl -L -o /usr/local/bin/minio https://dl.min.io/server/minio/release/linux-amd64/minio
    curl -L -o /usr/local/bin/mc https://dl.min.io/client/mc/release/linux-amd64/mc
    chmod 0755 /usr/local/bin/{minio,mc}
    ```

- 修改 hosts，增加自定义域名解析
    ```
    192.168.1.10 X.X.X
    ```

- 创建四个目录，用于存储 minio 数据
    ```bash
    mkdir -p /minio/{1,2,3,4}
    ```

- 创建 /etc/systemd/system/minio.service，内容如下
    ```ini
    [Unit]
    Description=MinIO
    After=network.service
    Wants=network.service

    [Service]
    Environment=MINIO_ROOT_USER=XXX
    Environment=MINIO_ROOT_PASSWORD=XXXXXXXX
    ExecStart=/usr/local/bin/minio server \
        --console-address ":9001" \
        http://X.X.X:9000/minio/{1...4}
    #Restart=on-failure
    RestartSec=10s

    [Install]
    WantedBy=multi-user.target
    ```

### 启动 minio
- 配置开机自动启动
    ```bash
    systemctl daemon-reload
    systemctl enable minio
    ```

- 启动 minio
    ```bash
    systemctl start monio
    ```

## 部署多节点分布式存储
### 环境
自定义域名 | 网卡 IP                                    | 硬盘槽 | 数据分区
----       | ----                                       | ----   | ----
X1.X.X     | eth0: 192.168.1.11<br>eth1:10.0.0.11 | 8 个   | /dev/sdb1, /dev/sdc1<br>/dev/sdd1, /dev/sde1
X2.X.X     | eth0: 192.168.1.12<br>eth1:10.0.0.12 | 8 个   | /dev/sdb1, /dev/sdc1<br>/dev/sdd1, /dev/sde1
X3.X.X     | eth0: 192.168.1.13<br>eth1:10.0.0.13 | 8 个   | /dev/sdb1, /dev/sdc1<br>/dev/sdd1, /dev/sde1
X4.X.X     | eth0: 192.168.1.14<br>eth1:10.0.0.14 | 8 个   | /dev/sdb1, /dev/sdc1<br>/dev/sdd1, /dev/sde1

- eth0 用于接收 api 请求
- eth1 用于集群内部通信
- 每台服务器有 8 个硬盘槽，这里先各用 4 个组成 server pool，运行 minio 集群
- 每台服务器剩下的 4 个硬盘槽用于后面的扩容操作

### 挂载数据分区
- 在全部服务器上执行如下操作
- 挂载硬盘
    ```bash
    mkdir -p /mnt/minio{1,2,3,4}
    mount /dev/sdb1 /mnt/minio1
    mount /dev/sdc1 /mnt/minio2
    mount /dev/sdd1 /mnt/minio3
    mount /dev/sde1 /mnt/minio4
    ```

- 建议在 fstab 中配置开机自动挂载

### 安装 minio
- 在全部服务器上执行如下操作
- 下载
    ```bash
    curl -L -o /usr/local/bin/minio https://dl.min.io/server/minio/release/linux-amd64/minio
    curl -L -o /usr/local/bin/mc https://dl.min.io/client/mc/release/linux-amd64/mc
    chmod 0755 /usr/local/bin/{minio,mc}
    ```

- 修改 hosts，增加自定义域名解析，**注意这里是集群通信，要用 eth1 网卡 ip**
    ```
    10.0.0.11 X1.X.X
    10.0.0.12 X2.X.X
    10.0.0.13 X3.X.X
    10.0.0.14 X4.X.X
    ```

- 创建 /etc/systemd/system/minio.service，内容如下
    ```ini
    [Unit]
    Description=MinIO
    After=network.service
    Wants=network.service

    [Service]
    Environment=MINIO_ROOT_USER=XXX
    Environment=MINIO_ROOT_PASSWORD=XXXXXXXX
    ExecStart=/usr/local/bin/minio server \
        --console-address ":9001" \
        http://X{1...4}.X.X:9000/mnt/minio{1...4}
    #Restart=on-failure
    RestartSec=10s

    [Install]
    WantedBy=multi-user.target
    ```

### 启动 minio
- 在全部服务器上执行如下操作
- 配置开机自动启动
    ```bash
    systemctl daemon-reload
    systemctl enable minio
    ```

- 启动 minio
    ```bash
    systemctl start monio
    ```

## 扩容多节点分布式存储
### 加装硬盘
- 在全部服务器上执行如下操作
- 在每台服务器上个增加四个新硬盘，每个硬盘创建一个分区，格式化
- 挂载新增的硬盘分区
    ```bash
    mkdir -p /mnt/minio{5,6,7,8}
    mount /dev/sdf1 /mnt/minio5
    mount /dev/sdg1 /mnt/minio6
    mount /dev/sdh1 /mnt/minio7
    mount /dev/sdi1 /mnt/minio8
    ```

- 建议在 fstab 中配置开机自动挂载

### 扩容
- 在全部服务器上执行如下操作
- 停止 minio 服务
    ```bash
    systemctl stop minio
    ```

- 修改 /etc/systemd/system/minio.service，增加新的 server pool，内容如下
    ```ini
    [Unit]
    Description=MinIO
    After=network.service
    Wants=network.service

    [Service]
    Environment=MINIO_ROOT_USER=XXX
    Environment=MINIO_ROOT_PASSWORD=XXXXXXXX
    ExecStart=/usr/local/bin/minio server \
        --console-address ":9001" \
        http://X{1...4}.X.X:9000/mnt/minio{1...4} \
        http://X{1...4}.X.X:9000/mnt/minio{5...8}
    #Restart=on-failure
    RestartSec=10s

    [Install]
    WantedBy=multi-user.target
    ```

- 启动 minio 服务
    ```bash
    systemctl start minio
    ```

## 客户端常用操作
- 创建 alias，**注意这里是 api 调用，要用 eth0 网卡 ip**
    ```bash
    mc alias set XXX http://192.168.1.X:9000 XXX XXXXXXXX
    ```

- 创建 bucket
    ```bash
    mc mb XXX/XXX
    ```

- 创建用户
    ```bash
    mc admin user add XXX XXXXXXXX
    ```

- 给新增用户授权
    ```bash
    mc admin policy set XXX readwrite user=XXX
    ```

