Skip to content

06 · P2-2:对象存储与搜索(MinIO + Elasticsearch) ​

项目内容
所属阶段P2(第二天)
前置条件P2-1 完成;thin 池还有 ≥120G 空闲
预计耗时90 分钟
完成标志mc admin info 显示 3 节点 6 盘在线;ES 集群 status: green、3 个节点
下一步07-D2-P2-3蓝绿发布.md

这一步建 6 台:

VMID主机名角色规格
170 / 171 / 172minio-01/02/03对象存储(EC 4+2)2C/3G/30G
180 / 181 / 182es-01/02/03搜索 + 日志存储2C/3G/25G

实验简化:真机上 MinIO 每台挂 2 块独立盘,这里用同一块虚拟盘的 /data1、/data2 两个目录模拟。EC 逻辑一样,性能不一样。


本步需要的资料 ​

类型名称从哪来
下载MinIO server + mcdl.min.io
下载Elasticsearch / Kibana 7.17artifacts.elastic.co
前置thin 池 ≥120G 空闲01 建的 nvme-lvm

总表(含版本、下载地址、账号规则)见 00-资料清单.md。


步骤 0 · 开始前检查(3 分钟) ​

在哪台:PVE(root) 目的:确认内存和磁盘够 开始前:P2-1 验收通过

操作:

bash
# 在 PVE 上执行(root)
free -g | grep Mem
lvs -o lv_name,data_percent nvme-vg

应该看到:可用 ≥ 20G;data_percent < 50%。

失败怎么办:

  • 内存不足 → 关掉暂时不用的 VM;实在不够就分批建(先 MinIO,后 ES)

步骤 1 · 建 6 台并初始化(15 分钟) ​

在哪台:PVE(root)建机 → bastion(ubuntu)初始化 目的:拉起存储和搜索节点 开始前:步骤 0 通过

操作:

bash
# 在 PVE 上执行(root)
bash /root/create-lab-vm.sh 170 2 3072 30
bash /root/create-lab-vm.sh 171 2 3072 30
bash /root/create-lab-vm.sh 172 2 3072 30
bash /root/create-lab-vm.sh 180 2 3072 25
bash /root/create-lab-vm.sh 181 2 3072 25
bash /root/create-lab-vm.sh 182 2 3072 25
bash
# 在 bastion 上执行(ubuntu)
declare -A M=([170]=minio-01 [171]=minio-02 [172]=minio-03 [180]=es-01 [181]=es-02 [182]=es-03)
for id in "${!M[@]}"; do
  h=${M[$id]}
  scp -q -o StrictHostKeyChecking=no ~/p0-node-init.sh ubuntu@10.0.0.$id:/tmp/
  ssh -o StrictHostKeyChecking=no ubuntu@10.0.0.$id "sudo bash /tmp/p0-node-init.sh $h"
done

应该看到:6 段 初始化完成:...。

失败怎么办:

  • 建机报 no space → thin 池满了,先清理

步骤 2 · MinIO 三节点(25 分钟) ​

在哪台:minio-01/02/03 目的:组成一个 6 盘的对象存储池(EC 4+2,可坏 2 盘) 开始前:步骤 1 的三台 minio 能 ping 通

操作:

bash
# 在 bastion 上执行(ubuntu)—— 三台执行同样的命令
for id in 170 171 172; do
ssh -o StrictHostKeyChecking=no ubuntu@10.0.0.$id "bash -s" <<'EOS'
# 1) 两个"盘"目录
sudo mkdir -p /data1 /data2
sudo chown -R ubuntu:ubuntu /data1 /data2

# 2) 下载二进制
sudo curl -sSL https://dl.min.io/server/minio/release/linux-amd64/minio -o /usr/local/bin/minio
sudo chmod +x /usr/local/bin/minio
/usr/local/bin/minio --version | head -1

# 3) 环境变量
sudo tee /etc/default/minio >/dev/null <<'EOF'
MINIO_ROOT_USER=admin
MINIO_ROOT_PASSWORD=【实验密码】
MINIO_VOLUMES="http://10.0.0.17{0...2}/data{1...2}"
MINIO_OPTS="--console-address :9001"
MINIO_STORAGE_CLASS_STANDARD=EC:2
EOF

# 4) systemd
sudo tee /etc/systemd/system/minio.service >/dev/null <<'EOF'
[Unit]
Description=MinIO
After=network-online.target
Wants=network-online.target

[Service]
EnvironmentFile=/etc/default/minio
ExecStart=/usr/local/bin/minio server $MINIO_OPTS $MINIO_VOLUMES
Restart=always
RestartSec=3
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable --now minio
sleep 3
sudo systemctl is-active minio
EOS
done

应该看到:三台都输出 active。

EC:2 的意思是"2 份校验":6 块盘 = 4 数据 + 2 校验,坏 2 块盘数据不丢。

失败怎么办:

  • 下载卡住 → 换 https://dl.min.io/server/minio/release/linux-amd64/archive/minio.RELEASE.2024-06-13T22-53-53Z
  • active 但端口没起 → sudo journalctl -u minio -n 40 --no-pager
  • 三台启动时间差太大 → 先启 170,等 5 秒再启 171/172

步骤 3 · 验证 MinIO(10 分钟) ​

在哪台:minio-01 目的:确认 3 节点 6 盘都在线,能上传能下载 开始前:步骤 2 三台 active

操作:

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.170
bash
# 在 minio-01 上执行(ubuntu)
sudo curl -sSL https://dl.min.io/client/mc/release/linux-amd64/mc -o /usr/local/bin/mc
sudo chmod +x /usr/local/bin/mc

mc alias set lab http://10.0.0.170:9000 admin '【实验密码】'
mc admin info lab

应该看到:Servers: 3 online, 0 offline、Drives: 6 online, 0 offline。

再传一个文件验证读写:

bash
# 在 minio-01 上执行(ubuntu)
mc mb lab/demo
echo "hello minio $(date)" > /tmp/t.txt
mc cp /tmp/t.txt lab/demo/
mc ls lab/demo
mc cat lab/demo/t.txt

应该看到:文件列表和内容。

失败怎么办:

  • Connection refused → ss -lntp | grep 9000
  • 只有 1 个 server 在线 → 三台时间不同步(timedatectl 看)

步骤 4 · Elasticsearch 三节点(30 分钟) ​

在哪台:es-01/02/03 目的:组成三节点集群,给课程搜索和日志用 开始前:步骤 1 的三台 es 能 ping 通

操作:

bash
# 在 bastion 上执行(ubuntu)—— 三台依次执行,节点名自动排成 es-01/02/03
i=0
for id in 180 181 182; do
  i=$((i+1))
ssh -o StrictHostKeyChecking=no ubuntu@10.0.0.$id "bash -s" <<EOS
# 1) 内核参数
echo 'vm.max_map_count=262144' | sudo tee /etc/sysctl.d/99-es.conf >/dev/null
sudo sysctl --system >/dev/null

# 2) 装 ES 7.17
sudo apt install -y gnupg apt-transport-https
curl -s https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg
echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" \
  | sudo tee /etc/apt/sources.list.d/elastic-7.x.list >/dev/null
sudo apt update && sudo apt install -y elasticsearch

# 3) 堆内存 1G(每台 VM 只有 3G)
sudo mkdir -p /etc/elasticsearch/jvm.options.d
printf -- '-Xms1g\n-Xmx1g\n' | sudo tee /etc/elasticsearch/jvm.options.d/heap.options >/dev/null

# 4) 集群配置
sudo tee /etc/elasticsearch/elasticsearch.yml >/dev/null <<'EOF'
cluster.name: lab-es
node.name: es-0$i
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["10.0.0.180", "10.0.0.181", "10.0.0.182"]
cluster.initial_master_nodes: ["es-01", "es-02", "es-03"]
xpack.security.enabled: false
EOF

sudo systemctl enable --now elasticsearch
EOS
done

应该看到:无报错;等 30 秒。

失败怎么办:

  • systemctl start 后立刻退出 → sudo journalctl -u elasticsearch -n 50 --no-pager,多半是 max_map_count 或堆内存
  • initial_master_nodes 对不上 → 上面那条 sed 命令重跑

步骤 5 · 验证 ES 集群(5 分钟) ​

在哪台:bastion → es-01 目的:确认三节点、绿状态 开始前:步骤 4 的三台都启动了

操作:

bash
# 在 bastion 上执行(ubuntu)
curl -s http://10.0.0.180:9200/_cluster/health?pretty | grep -E '"status"|"number_of_nodes"'
curl -s http://10.0.0.180:9200/_cat/nodes?v

应该看到:"status": "green"、"number_of_nodes": 3,三行节点。

建一个索引试试:

bash
# 在 bastion 上执行(ubuntu)
curl -s -XPUT http://10.0.0.180:9200/course -H 'Content-Type: application/json' -d '
{"settings":{"number_of_shards":3,"number_of_replicas":1}}'
curl -s -XPOST http://10.0.0.180:9200/course/_doc/1 -H 'Content-Type: application/json' -d '
{"name":"Linux 运维","price":199}'
sleep 1
curl -s "http://10.0.0.180:9200/course/_search?q=Linux" | grep -o '"name":"[^"]*"'

应该看到:"name":"Linux 运维"。

失败怎么办:

  • status: yellow → 只有 1 个副本但节点没加入,_cat/nodes 看是不是只有 1 台
  • cluster_block_exception → 磁盘水位触发了只读,df -h 看是不是满了

步骤 6 · Kibana(10 分钟,可选) ​

在哪台:grafana-01(10.0.0.191) 目的:给 ES 一个网页界面 开始前:步骤 5 的 ES 是 green

操作:

bash
# 在 bastion 上执行(ubuntu)
ssh ubuntu@10.0.0.191
bash
# 在 grafana-01 上执行(ubuntu)
sudo apt install -y gnupg apt-transport-https
curl -s https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg
echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" \
  | sudo tee /etc/apt/sources.list.d/elastic-7.x.list >/dev/null
sudo apt update && sudo apt install -y kibana

sudo sed -i 's|^#server.host:.*|server.host: "0.0.0.0"|' /etc/kibana/kibana.yml
sudo sed -i 's|^#elasticsearch.hosts:.*|elasticsearch.hosts: ["http://10.0.0.180:9200"]|' /etc/kibana/kibana.yml
sudo systemctl enable --now kibana
sudo systemctl status kibana --no-pager | head -3

应该看到:Active: active (running);一分钟后浏览器打开 http://192.168.10.191:5601 能进。

失败怎么办:

  • Kibana server is not ready yet → 等 60 秒;仍不行看 sudo journalctl -u kibana -n 30
  • 5601 打不开 → ss -lntp | grep 5601

P2-2 验收清单 ​

  • [ ] 6 台 VM 全部 running
  • [ ] mc admin info lab 显示 3 servers / 6 drives 全在线
  • [ ] mc cp 上传下载正常
  • [ ] ES _cluster/health 是 green、3 节点
  • [ ] 写入 course 索引能搜到
  • [ ] Kibana 能打开(可选)

全部打勾后 → 进入 07-D2-P2-3蓝绿发布.md。