Để chạy được câu lệnh trên cần cài đặt Docker. Nếu đã cài đặt rồi thì bỏ qua. Nếu chưa biết cách cài đặt Docker, hãy chạy câu lệnh sau
source /etc/os-release
NAME=docker
GPG=https://download.docker.com/linux/$ID/gpg
REPO="https://download.docker.com/linux/$ID $VERSION_CODENAME stable"
curl -fsSL $GPG | sudo gpg --dearmor -o /etc/apt/keyrings/$NAME.gpg
cat << EOF | sudo tee /etc/apt/sources.list.d/$NAME.list
deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/$NAME.gpg] $REPO
EOF
sudo apt update
sudo apt install -y \
docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker --version
Cài đặt Docker compose, bỏ qua bước này nếu bạn đã cài đặt Docker compose
export VER=2.29.6
sudo curl -SL \
-o /usr/bin/docker-compose \
"https://github.com/docker/compose/releases/download/v${VER}/docker-compose-linux-x86_64"
sudo chmod +x /usr/bin/docker-compose
docker-compose --version
Tạo các folder để lưu trữ dữ liệu lâu dài, đổ dữ liệu từ container ra máy host
sudo mkdir -p /opt/mongo
sudo /usr/bin/docker-compose -f /opt/mongo/mongo.yml down
for i in mongo_proxy mongo_conf1 mongo_conf2 mongo_a1 mongo_a2 mongo_b1 mongo_b2
do
sudo rm -rf /data/$i
sudo rm -rf /dump/log/$i
sudo mkdir -p /data/$i
sudo mkdir -p /dump/log/$i
sudo chmod -R 777 /data/$i
sudo chmod -R 777 /dump/log/$i
done
sudo rm -rf /data/mongo_proxy
cat << EOF | sudo tee /opt/mongo/mongo.yml
networks:
mongo_net:
driver: bridge
x-common: &common
networks:
- mongo_net
deploy:
resources:
limits:
memory: 1G
logging:
driver: json-file
options:
max-size: 10m
max-file: 3
image: mongodb/mongodb-community-server:latest
extra_hosts:
- 'host.docker.internal:host-gateway'
x-rs_check: &rs_check
healthcheck:
test: mongosh --eval "rs.status()" || exit 1
interval: 10s
timeout: 5s
retries: 3
start_period: 30s
services:
mongo_proxy:
<<: *common
container_name: mongo_proxy
hostname: mongo_proxy
volumes:
- /dump/log/mongo_proxy:/var/log/mongodb:rw
command: mongos --configdb confSet/host.docker.internal:27001,host.docker.internal:27002 --port 27017 --bind_ip_all
ports:
- '27017:27017'
healthcheck:
test: mongosh --eval "show dbs" || exit 1
interval: 10s
timeout: 5s
retries: 3
start_period: 30s
mongo_conf1:
<<: [*common, *rs_check]
container_name: mongo_conf1
hostname: mongo_conf1
volumes:
- /data/mongo_conf1:/data/db:rw
- /dump/log/mongo_conf1:/var/log/mongodb:rw
command: --configsvr --replSet confSet --dbpath /data/db --port 27017
ports:
- '27001:27017'
mongo_conf2:
<<: [*common, *rs_check]
container_name: mongo_conf2
hostname: mongo_conf2
volumes:
- /data/mongo_conf2:/data/db:rw
- /dump/log/mongo_conf2:/var/log/mongodb:rw
command: --configsvr --replSet confSet --dbpath /data/db --port 27017
ports:
- '27002:27017'
mongo_a1:
<<: [*common, *rs_check]
container_name: mongo_a1
hostname: mongo_a1
volumes:
- /data/mongo_a1:/data/db:rw
- /dump/log/mongo_a1:/var/log/mongodb:rw
command: --shardsvr --replSet shardA --dbpath /data/db --port 27017
ports:
- '27101:27017'
mongo_a2:
<<: [*common, *rs_check]
container_name: mongo_a2
hostname: mongo_a2
volumes:
- /data/mongo_a2:/data/db:rw
- /dump/log/mongo_a2:/var/log/mongodb:rw
command: --shardsvr --replSet shardA --dbpath /data/db --port 27017
ports:
- '27102:27017'
mongo_b1:
<<: [*common, *rs_check]
container_name: mongo_b1
hostname: mongo_b1
volumes:
- /data/mongo_b1:/data/db:rw
- /dump/log/mongo_b1:/var/log/mongodb:rw
command: --shardsvr --replSet shardB --dbpath /data/db --port 27017
ports:
- '27201:27017'
mongo_b2:
<<: [*common, *rs_check]
container_name: mongo_b2
hostname: mongo_b2
volumes:
- /data/mongo_b2:/data/db:rw
- /dump/log/mongo_b2:/var/log/mongodb:rw
command: --shardsvr --replSet shardB --dbpath /data/db --port 27017
ports:
- '27202:27017'
EOF
/usr/bin/docker-compose -f /opt/mongo/mongo.yml up -d
Sau khi hoàn thành bước Triển khai container, các container được tạo nhưng chúng chưa kết nối với nhau thành Cluster. Chạy câu lệnh sau đây để khởi tạo Cluster
sleep 5
docker exec -it mongo_conf1 \
mongosh --eval 'rs.initiate({
_id: "confSet",
configsvr: true,
members: [
{ _id: 0, host: "host.docker.internal:27001" },
{ _id: 1, host: "host.docker.internal:27002" }
]})'
docker exec -it mongo_a1 \
mongosh --eval 'rs.initiate({
_id: "shardA",
members: [
{ _id: 0, host: "host.docker.internal:27101" },
{ _id: 1, host: "host.docker.internal:27102" }
]})'
docker exec -it mongo_b1 \
mongosh --eval 'rs.initiate({
_id: "shardB",
members: [
{ _id: 0, host: "host.docker.internal:27201" },
{ _id: 1, host: "host.docker.internal:27202" }
]})'
sleep 15
sudo docker exec -it mongo_proxy \
mongosh --eval 'sh.addShard("shardA/host.docker.internal:27101,host.docker.internal:27102")'
sudo docker exec -it mongo_proxy \
mongosh --eval 'sh.addShard("shardB/host.docker.internal:27201,host.docker.internal:27202")'
Chúc mừng bạn đã cài đặt thành công Cluster MongoDB, bài học đến đây là kết thúc !
Cluster Database MongoDB gồm có 3 thành phần:
Client chỉ cần làm việc với mongo_proxy qua port 27017. Chú ý cơ chế của proxy này là redirect chứ không phải forward. Vì vậy tất cả các thành phần cần phải được expose ra internet, mỗi thành phần chiếm lấy 1 port của máy host.
Dùng systemd để giúp MongoDB Cluster tự động khởi động cùng với hệ điều hành
cat << EOF | sudo tee /etc/systemd/system/mongo_shard.service
[Unit]
Description=MongoDB Cluster Sharding Docker Compose Service
After=docker.service
Requires=docker.service
[Service]
Type=simple
User=root
WorkingDirectory=/opt/mongo
ExecStop=/usr/bin/docker-compose -f /opt/mongo/mongo.yml down
ExecStart=/usr/bin/docker-compose -f /opt/mongo/mongo.yml up -d
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now mongo_shard
Cài đặt Cluster Database MongoDB kiến trúc Replica Set bằng Docker Compose
quangnn - November 5, 2024
Tran Phuc Hau - April 23, 2023
quangnn - October 27, 2024
Tran Phuc Hau - April 23, 2023
Regional Content Hub - August 29, 2024
Regional Content Hub - November 4, 2024
Leverage cloud-native database solutions dedicated for FinTech.
Learn MoreMigrate your legacy Oracle databases to Alibaba Cloud to save on long-term costs and take advantage of improved scalability, reliability, robust security, high performance, and cloud-native features.
Learn MoreMigrating to fully managed cloud databases brings a host of benefits including scalability, reliability, and cost efficiency.
Learn MoreDBStack is an all-in-one database management platform provided by Alibaba Cloud.
Learn MoreMore Posts by quangnn