NCS - Documents
Go to Portal
  • NIPA Cloud Space Overview
    • Welcome to NIPA Cloud Space documentation
    • NCS User Account
      • Create NIPA Cloud Space Account
      • Loging in to Nipa Cloud Space
      • Reset NCS Password
      • Activating Two-Factor Authentication
      • Deactivating Two-Factor Authentication
    • Co-working Projects
      • Create Co-working Project
      • Manage Project Member
      • Exporting Resource List
    • Billing & Wallet
      • Topup a Project Wallet
      • Redeem Voucher
      • Insufficient Wallet Balance
  • COMPUTE
    • Compute Instance
      • What is OS Status?
      • Launching Compute Instance
      • Managing Instance
        • Stop
        • Start
        • Restart
        • Resize (Change Machine Type)
        • Take Snapshot
        • Auto Backup
        • Reset Linux "root" Password
        • Reset Windows "Administrator" Password
      • SSH to Linux Compute Instance
        • Converting Key Pair for PuTTy
        • Windows Client using Key Pair
        • Windows Client using Password
        • MacOS/Linux using Key Pairs
        • MacOS/Linux using Password
      • Remote Desktop to Windows Instance
      • Setting Docker Image Caching
      • Renaming Instance
      • Exporting Instance List
      • How to change Compute Instance hostname
      • How to install QEMU Guest Agent
      • How to fix update kernel for RHEL
      • How to Update Rocky Linux 9
      • How to Upgrade Rocky Linux to 9.4
      • How to change RDP port on Windows
    • Compute Image
      • Create Image From a Bootable Volume
      • Importing Your Own Image
      • Export Image
      • Share Image to Between Projects
      • Exporting Image List
    • Key Pair
      • Managing Key Pair(s)
      • Creating a New Key Pair
      • Importing an Existing Key Pair
    • Deployment Script
      • Managing Deployment Script
      • Create a Deployment Script
      • Clone a Deployment Script
      • Edit a Deployment Script
      • Delete a Deployment Script
  • STORAGE
    • Block Storage
      • Managing Volume(s)
        • Create From Blank
        • Create From an Image
        • Create From a Volume
        • Create From a Snapshot
        • Transferring a Volume
        • Accepting a Tranferred Volume
        • Renaming a Volume
        • How to Change Volume Type
      • Managing Snapshot(s)
        • Create a Snapshot
        • Renaming a Volume Snapshot
      • Exporting Volume and Volume Snapshot List
    • Object Storage (S3)
      • Migrate file S3 AWS to S3 NIPA
      • Create an Object Storage Bucket
      • Delete an Object Storage Bucket
      • Create an Object Storage Sub-User
      • Regenerate Sub-User's Access Key
      • Revoke Sub-User's Access Key
      • Create Bucket Policy
      • Bucket Versioning
      • Access S3 Bucket with Cyberduck
        • Upload Files to a Bucket
        • Share File via Public Link
      • Access S3 Bucket with s3cmd
        • Basic command
        • การ set ACL สำหรับการเปิดใช้งาน Objects แบบ public
        • การสร้าง Presigned URL สำหรับการใช้งานชั่วคราว
      • Mount S3 Bucket on instances with s3fs-fuse
      • Mount the S3 bucket on the Windows
      • Access S3 buckets With AWS S3 Client SDK
        • S3Client Configuration
        • Basic Command
        • Multipart Upload
      • Access S3 buckets with internal network for NCS instance
      • Delete Lifecycle Policies
      • Move Objects Lifecycle Script
      • Configure a static website using S3 Bucket
    • NIPA Drive
      • Purchasing a Drive
  • NETWORKING
    • Networking
      • Managing VPC Network(s)
        • Create a Network
        • DHCP Setting
        • Create Port
        • Create Router
      • Managing Security Group(s)
        • Create a New Security Group
        • Create Security Group Presets
      • Managing External IP(s)
        • Create an External IP
        • Exporting External IP List
      • NAT Gateway with Ubuntu (VM)
      • NAT and VPN Gateway on NCS with Pfsense-2.6.0
  • LOAD BALANCING
    • Load Balancer as a Service
      • Create Load Balancer
      • Using Network Load Balancing
      • Using Application Load Balancing
      • Renaming a Load Balancer
      • Exporting Load Balancer List
      • Monitoring Load Balancer Using Prometheus
    • SSL Certificate
      • Import SSL Certificate
  • DATABASE AS A SERVICE
    • SQL Database
      • Create SQL Database Instance
        • Create MySQL Database Instance
      • Manage SQL Database Instance
        • Reboot Database Service
        • Delete Database Instance
        • Online Extend Storage Size
        • Edit Allowed CIDR
      • Auto-Scaling SQL Database Storage
        • Enable Auto-Scaling
        • Disable Auto-Scaling
        • Edit Auto-Scaling
      • Manage SQL Database Root User
        • Enable Root User
        • Reset Root User Password
      • Manage SQL Database Schema
        • Create Database Schema
        • Delete Database Schema
      • Manage SQL Database User
        • Create Database User
        • Delete Database User
        • Reset Password
        • Edit Access
      • Manage SQL Database Backup
        • Create Backup
        • Create A New SQL Database Instance From Backup
        • Delete Backup
      • Manage SQL Database Logs
        • Enable Logs
        • Disable Logs
        • Refresh Logs
        • Load More Logs
      • Manage Monitoring User
        • Create Monitoring User
        • Delete Monitoring User
      • Monitor SQL Database with Percona Monitoring and Management (PMM)
  • SCHEDULING
    • Schedules
      • Create Schedule
    • Jobs
  • Public API
    • What is NCS Public API ?
      • Download NCS Project RC File
      • Getting Start with NCS Public API
        • Using OpenStack Client Tool
        • Using REST API
        • Terraform with Openstack
        • Auto-scaling OpenStack Instances with Senlin and Prometheus
          • Installation Prometheus
          • Installation Alertmanager
  • MIGRATION
    • Migrating Linux VM from vSphere to NCS
    • Migrating Windows VM from vSphere to NCS
  • Customer Support
    • Having Problem Before Access a Project
    • Having Problem In a Project
  • Tutorial
    • My First Website
    • Access MySQL Database With MySQL Workbench
    • Pritunl for VPN server
    • Install Rancher Server with Docker Quick Start
      • Create RKE2 Cluster via Rancher Dashboard
    • Install odoo18 with external database
    • How to use LBaaS for mysql Load Balancing
    • How to use Cloudflare with Nipa Cloud Space
  • Release Notes
    • v5.0.X (v5.0.0-now)
      • v5.0.0
      • v5.1.0
      • v5.2.0
      • v5.2.1
      • v5.2.2
      • v5.2.3
      • v5.2.4
      • v.5.3.0
      • v5.4.0
    • v4.19.X (v4.19.0-v4.19.3)
      • v4.19.0
      • v4.19.1
      • v4.19.2
      • v4.19.3
    • v4.18.X (v4.18.0-v4.18.2)
      • v4.18.0
      • v4.18.1
      • v4.18.2
    • v4.17.X (v4.17.0-v4.17.3)
      • v4.17.0
      • v4.17.0.1
      • v4.17.1
      • v4.17.2
      • v4.17.3
    • v4.16.X (v4.16.0-v4.16.5)
      • v4.16.0
      • v4.16.1
      • v4.16.2
      • v4.16.3
      • v4.16.4
      • v4.16.5
    • v4.15.X (v4.15.0-v4.15.9)
      • v4.15.0
      • v4.15.1
      • v4.15.2
      • v4.15.3
      • v4.15.4
      • v4.15.5
      • v4.15.6
      • v4.15.7
      • v4.15.8
      • v4.15.9
    • v.4.14.X (v4.14.0-v4.14.2)
      • v.4.14.0
      • v4.14.1
      • 4.14.2
Powered by GitBook
On this page
  • Introduction
  • Prerequisite
  • Objective
  • Instructions
  • 1. Create MySQL user
  • 2. ssh & install docker, docker-compose
  • 3. create docker-compose file
  • 4. docker-compose up
  • 5. verify docker ทำงานถูกต้อง
  • 6. create Loadalance (ต้องสร้างภายใน VPC Networking เดียวกันกับ database)
  • 7. create backend group
  • 8. create listener
  • 9. verify via mysql login

Was this helpful?

  1. Tutorial

How to use LBaaS for mysql Load Balancing

ใช้ LBaaS กับ MySQL สร้าง Load Balancer เพื่อกระจายคำขออ่าน (read requests) ไปยัง MySQL read replicas หลายตัว วิธีนี้ช่วยเพิ่มประสิทธิภาพการอ่านข้อมูล และทำให้ระบบฐานข้อมูลรองรับการอ่านจำนวนมากได้

PreviousInstall odoo18 with external databaseNextHow to use Cloudflare with Nipa Cloud Space

Last updated 1 month ago

Was this helpful?

Introduction

เอกสารนี้จะแนะนำการตั้งค่า LBaaS เพื่อทำ Load Balancing สำหรับการอ่านข้อมูลจาก MySQL Replicas (Slaves) โดยมีเป้าหมายเพื่อเพิ่ม performance ในการรองรับการอ่านจำนวนมาก (Read Scaling) และเพิ่มความพร้อมใช้งาน (High Availability) ให้กับระบบฐานข้อมูล โดยจะต้องมีระบบ MySQL ที่ตั้งค่า Master-Slave Replication ไว้เรียบร้อยแล้ว (มี Master 1 ตัว และ Slave อย่างน้อย 1 ตัว)​ ซึ่งต้องติดตั้ง MySQL Health Check สำหรับตรวจสอบสถานะความพร้อมของ MySQL Server (โดยเฉพาะ Slave/Replica) เพื่อให้ LBaaS สามารถตรวจสอบได้ว่า server ไหนพร้อมใช้งาน และตั้งค่า LBaaS บน Nipa Cloud Space เพื่อทำหน้าที่กระจาย request ไปยัง backend group ที่สร้างไว้ ในส่วนของการเชื่อมต่อสำหรับการเขียน (Write) client จะเชื่อมต่อ MySQL Master ผ่าน Listener Port 3306 และสำหรับการอ่าน (Read) client จะเชื่อมต่อ MySQL Replica ผ่าน Listener Port 3307

Prerequisite

  • มีการติดตั้ง mysql master-slave

  • มีการ create และ attach security group port 9200

สามารถดูวิธีการสร้าง security group ได้ที่ Create a New Security Group

Objective

เพื่อให้ mysql มี healthcheck ให้ตรวจสอบได้ว่าพร้อมรับ request จาก loadbalancer จีงจำเป็นต้องมี tool ในการเช็ค โดยจะเลือกใช้การติดตั้งผ่าน docker และ run container mysqlcheck ตรวจสอบสถานะ MySQL Slave ได้อย่างแม่นยำ สำหรับรัน Healthcheck script เพื่อตรวจสอบว่า replication ยังทำงาน (Slave_IO_Running และ Slave_SQL_Running เป็น Yes) และ slave ไม่ lag จาก master ด้วย port 9200 ใช้เป็น port สำหรับ healthcheck จาก loadbalancer วิธีนี้ช่วยให้มั่นใจว่า client อ่านข้อมูลจาก slave ที่ sync อยู่เสมอ (HTTP respone 200) และยังสามารถทำ self-healing เมื่อมี connection timeout จากที่ตั้งค่าไว้ จะมีการ restart service MySQL แบบ automate ได้อีกด้วย

โดย Service mysqlcheck จะมี HTTP Response ดังนี้

Role
Status
Response

slave

healthy

200

slave

not healthy (not sync, error)

503

master

healthy

200

other

-

200

Instructions

วิธีการติดตั้งให้ดำเนินการ ดังนี้

1. Create MySQL user

เพื่อให้ LoadBalancer สามารถ healthcheck ผ่าน command select ได้

CREATE USER 'lb_heathcheck'@'%' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';
GRANT SELECT ON performance_schema.* TO 'lb_heathcheck'@'%';
FLUSH PRIVILEGES;

2. ssh & install docker, docker-compose

ให้ ssh เข้าไปที่ mysql host และ install docker เพื่อจะทำเป็น healthcheck

sudo apt update && sudo apt install docker.io docker-compose -y

3. create docker-compose file

สร้าง directory mysqlcheck แล้วสร้าง docker-compose.yaml file

version: "3"
services:
    mysqlcheck:
        container_name: mysqlcheck
        network_mode: host
        restart: unless-stopped
        volumes:
        - /var/run/mysqld/:/var/run/mysqld/
        image: registry.nipa.cloud/nipa-public-services/mysqlcheck:v1
        environment:
          MYSQL_USERNAME: lb_heathcheck        ## mysql healthcheck user 
          MYSQL_PASSWORD: xxxxxx               ## mysql healthcheck password 
          RESTART_ON_TIMEOUT: 'TRUE'           ## enable/disable auto restart mysql
          RESTART_MYSQL_TIMEOUT: '1800         ## timeout for call restart in second 
          CACHE_EXPIRE: '5'                    ## caching timeout second
          HEALTHCHECK_PORT: '9200'             ## binding port for script

4. docker-compose up

ทำการ docker-compose up เพื่อสร้าง container environment

docker-compose up -d --build

5. verify docker ทำงานถูกต้อง

ตรวจสอบ service ว่าทำงานถูกต้องด้วย command

curl 0.0.0.0:9200

Slave Node:

expect response: (status code 200)

{"message":"[Slave] MySQL Server Master-Slave is synced."}

Master Node:

expect response: (status code 200)

{"message":"[Master] MySQL Server Master-Slave is available."}

6. create Loadalance (ต้องสร้างภายใน VPC Networking เดียวกันกับ database)

สามารถดูวิธีการติดตั้ง LoadBalancer ได้ที่ LOAD BALANCING

7. create backend group

  • สร้าง backend group สำหรับ role writer แล้วใช้ healthcheck แบบ http โดย check ไปที่ port 9200

  • สร้าง backend group สำหรับ role reader แล้วใช้ healthcheck แบบ http โดย check ไปที่ port 9200 โดย role reader จะมี Algorithm แบบ round robin และเพิ่ม Member List ของ slave host

เมื่อสร้างทั้งหมดจะได้ทั้ง role-writer และ role-reader

8. create listener

  • ให้ไปที่ tab Listener แล้วดำเนินการสร้าง role-writer โดยจะใช้ TCP port 3306

  • จากนั้นสร้าง role-reader โดยใช้ TCP port 3307

เมื่อสร้างเสร็จแล้วจะได้ทั้ง role-writer และ role-reader

9. verify via mysql login

ทดสอบเข้าใช้งานด้วยการใช้ mysql cli

mysql -h${LoadBalancer_IP} -P${writer_port} -u${username} -p'${password} -e'select @@hostname'
mysql -h${LoadBalancer_IP} -P${reader_port} -u${username} -p'${password} -e'select @@hostname'
Diagram แสดง flow การใช้งาน LBaaS กับ MySQL databases
การเข้าใช้งานด้วย LoadBalancer IP และ Port สำหรับ Writer
การเข้าใช้งานด้วย LoadBalancer IP และ Port สำหรับ Reader