Updated Feb 2026

MikroTik Routing ขั้นสูง

Policy-Based Routing, ECMP Load Balancing และ Scripting

เรียนรู้เทคนิค Routing ขั้นสูงสำหรับ Network Engineers ครอบคลุม PBR, Failover, Dynamic Routing และ Scripts สำหรับ Automation

Routing
ECMP
OSPF/BGP
Scripts

1. บทนำ

ในยุคที่องค์กรต่างๆ ต้องการความน่าเชื่อถือของเครือข่ายสูง MikroTik RouterOS ถือเป็นตัวเลือกที่น่าสนใจด้วยราคาที่เข้าถึงได้และฟีเจอร์ที่ครบครัน บทความนี้จะพาคุณเจาะลึกเทคนิค Routing ขั้นสูงที่ใช้ในองค์กรจริง

เทคนิคที่เราจะครอบคลุมในบทความนี้ประกอบด้วย:

  • Policy-Based Routing (PBR) - ควบคุมเส้นทางการส่งข้อมูลตามนโยบาย
  • ECMP Load Balancing - กระจายภาระงานหลายเส้นทาง
  • Failover Automation - สลับเส้นทางอัตโนมัติเมื่อลิงก์ล้มเหลว
  • OSPF Dynamic Routing - โปรโตคอลหาเส้นทางอัตโนมัติ
  • BGP Configuration - เชื่อมต่อกับ ISP หรือ Data Center
  • Scripts & Automation - เขียนสคริปต์ควบคุม Router

สำหรับใคร?

บทความนี้เหมาะสำหรับ Network Engineers, System Administrators และผู้ที่สนใจเรียนรู้ MikroTik Routing ขั้นสูง ควรมีพื้นฐาน RouterOS ขั้นพื้นฐานมาก่อน

เปรียบเทียบ Routing Protocols บน MikroTik

MikroTik RouterOS รองรับ Routing Protocols หลายประเภท แต่ละประเภทมีข้อดีและข้อจำกัดต่างกัน ดังนี้:

Protocol ประเภท Use Case Scale Convergence
Static Routing Manual เครือข่ายเล็ก, Default Route < 100 routes N/A
PBR (Policy-Based) Manual + Rules Multi-WAN, Traffic Engineering < 1000 rules ทันที
ECMP Load Balance หลาย ISP, Bandwidth Aggregation Multiple Gateways ทันที
OSPF v2 Link-State IGP Internal Network IPv4, Campus ~10,000 routes วินาที (1-10s)
OSPF v3 Link-State IGP Internal Network IPv6 ~10,000 routes วินาที (1-10s)
BGP Path-Vector EGP ISP Peering, Multi-homing, DC Interconnect 900,000+ routes นาที (1-5 min)
RIP / RIPng Distance-Vector IGP เครือข่ายเล็ก (Legacy) < 500 routes ช้า (30-180s)
IS-IS Link-State IGP ISP Backbone, Large Networks ~50,000 routes วินาที (1-10s)

OSPF (Open Shortest Path First)

ข้อดี
  • Convergence เร็ว - ตรวจจับและปรับเส้นทางได้ภายในวินาที
  • รองรับ VLSM/CIDR - ใช้ IP Address ได้อย่างมีประสิทธิภาพ
  • มาตรฐานเปิด - ใช้กับอุปกรณ์ยี่ห้อต่างกันได้
  • Area-based Hierarchy - ลดขนาด Routing Table
  • Authentication - รองรับ MD5, SHA สำหรับความปลอดภัย
  • No Hop Count Limit - ไม่จำกัดจำนวน Router
ข้อจำกัด
  • ซับซ้อนกว่า RIP - ต้องวางแผน Area และ LSA Types
  • กิน Memory มาก - เก็บ Link-State Database ทั้งหมด
  • ไม่เหมาะกับ ISP - ไม่รองรับ Policy ที่ซับซ้อน
  • Single Autonomous System - ใช้ภายในองค์กรเท่านั้น
บทความละเอียด: MikroTik OSPF Deep Dive

BGP (Border Gateway Protocol)

ข้อดี
  • Scale มหาศาล - รองรับ 900,000+ routes (Full Internet Table)
  • Policy Control ละเอียด - ควบคุมเส้นทางด้วย Attributes มากมาย
  • Path Vector - ป้องกัน Routing Loops ข้าม AS
  • Inter-AS Routing - เชื่อมต่อองค์กร/ISP หลายแห่ง
  • Traffic Engineering - ควบคุม Inbound/Outbound Traffic
  • Multi-homing - เชื่อมต่อ ISP หลายเจ้าได้อัตโนมัติ
ข้อจำกัด
  • Convergence ช้า - ใช้เวลานาทีในการซิงค์ Routes
  • Configuration ซับซ้อน - ต้องเข้าใจ Communities, Attributes
  • ต้องมี Public ASN - สำหรับ Peering กับ ISP
  • กิน Memory มาก - ต้องเก็บ Full Internet Routing Table
บทความละเอียด: MikroTik BGP Deep Dive

Static & Policy-Based Routing (PBR)

ข้อดี
  • ง่ายต่อการทำความเข้าใจ - ไม่ต้องเรียนรู้ Protocol
  • Predictable - รู้เส้นทางที่ Traffic จะไปชัดเจน
  • ไม่กิน Resource - ไม่ต้องรัน Protocol Process
  • Security - ไม่มี Protocol Attack Surface
  • Flexible Rules - ควบคุมตาม IP, Port, Protocol
ข้อจำกัด
  • No Auto-Discovery - ต้องตั้งค่าทุกเส้นทางเอง
  • Scalability จำกัด - ยิ่งเครือข่ายใหญ่ยิ่งจัดการยาก
  • Manual Failover - ต้องเขียน Script เพิ่ม
  • Human Error - ผิดพลาดง่ายเมื่อมีหลาย Rules

ECMP (Equal-Cost Multi-Path)

ข้อดี
  • ใช้ Bandwidth เต็มที่ - รวมความเร็วหลาย Link
  • Automatic Failover - Link ขาดจะใช้อีกเส้นทางอัตโนมัติ
  • Configuration ง่าย - เพียงกำหนดหลาย Gateway
  • No Protocol Overhead - ไม่ต้องรัน Dynamic Protocol
  • Weight Support - กระจายตามอัตราส่วนได้
ข้อจำกัด
  • Asymmetric Routing - อาจทำให้ VoIP/HTTPS มีปัญหา
  • No Path Intelligence - ไม่รู้ว่าเส้นทางไหนดีกว่า
  • Session Issues - ต้องใช้ PCC สำหรับ Sticky Sessions
  • Equal Cost Only - ทุกเส้นทางต้องมี Cost เท่ากัน

RIP / RIPng (Legacy)

ข้อดี
  • Configuration ง่ายมาก - เพียง Enable และกำหนด Networks
  • กิน Resource น้อย - เหมาะกับ Router เล็ก
  • Wide Compatibility - รองรับทุกอุปกรณ์
  • ง่ายต่อ Debug - เข้าใจการทำงานได้ง่าย
ข้อจำกัด
  • Convergence ช้ามาก - 30-180 วินาที
  • Hop Limit 15 - ไม่เกิน 15 Router
  • Scale จำกัด - < 500 Routes
  • กิน Bandwidth - ส่ง Full Table ทุก 30 วินาที
  • ไม่แนะนำ - ควรใช้ OSPF แทน

IS-IS (Intermediate System to IS)

ข้อดี
  • Scale สูง - เหมาะกับ ISP Backbone
  • Convergence เร็ว - เทียบเท่า OSPF
  • Protocol Independent - ใช้ได้กับหลาย Protocol
  • Extension TLVs - ขยายได้ง่าย (IPv6, MPLS)
  • Less Overhead - ไม่ต้องการ IP to function
ข้อจำกัด
  • Configuration ซับซ้อน - ต้องเข้าใจ CLNS/CLNP
  • น้อยคนรู้จัก - หาผู้เชี่ยวชาญยาก
  • ส่วนใหญ่ใช้ใน ISP - ไม่ค่อยใช้ใน Enterprise
  • Documentation น้อย - เทียบกับ OSPF

เลือกใช้ Protocol ไหนดี?

Campus / Enterprise

หลาย Building, หลาย Floor

แนะนำ: OSPF v2

Data Center / ISP

Multi-homing, Peering

แนะนำ: BGP + OSPF

SME / Branch Office

2-3 Router, 1-2 ISP

แนะนำ: Static + PBR + ECMP

Load Balancing

ใช้หลาย ISP พร้อมกัน

แนะนำ: ECMP + PCC

High Availability

ต้องการ Uptime สูง

แนะนำ: OSPF + VRRP

Multi-Site

สำนักงานหลายที่, VPN

แนะนำ: OSPF over VPN

IPv6 Network

Dual Stack, Pure IPv6

แนะนำ: OSPF v3 / RIPng

ISP Backbone

MPLS, Large Scale

แนะนำ: IS-IS

Embedded / Low Resource

Router เล็ก, Limited RAM

แนะนำ: RIP (หรือ Static)

2. Network Architecture

Topology ที่ใช้ในบทความ

Internet ISP 1 100 Mbps ISP 2 50 Mbps MikroTik Router RouterOS v7 PBR + ECMP + OSPF LAN 1 192.168.1.0/24 LAN 2 192.168.2.0/24 Servers 10.10.10.0/24 ether1-WAN1 ether2-WAN2

อุปกรณ์ที่ใช้ในตัวอย่าง

Component Specification หมายเหตุ
MikroTik Router RB4011iGS+ หรือ RB5009 RouterOS v7.10+
WAN 1 ISP 1 - 100 Mbps Static IP / PPPoE
WAN 2 ISP 2 - 50 Mbps Static IP / DHCP
LAN 192.168.1.0/24, 192.168.2.0/24 VLAN Trunk

3. Policy-Based Routing (PBR)

Policy-Based Routing คือการกำหนดเส้นทางการส่งข้อมูลตามเงื่อนไขที่เราต้องการ เช่น ให้ LAN 1 ออกผ่าน ISP 1 และ LAN 2 ออกผ่าน ISP 2 โดยไม่ขึ้นกับ Routing Table ปกติ

ขั้นตอนการทำงานของ PBR

1. Packet เข้ามาจาก LAN 2. Mangle Mark Routing 3. Route Lookup by Mark 4. Forward ส่งออก Gateway 5. NAT Masquerade

Configuration สำหรับ RouterOS v7

ขั้นตอนที่ 1: สร้าง Address List

Address List Configuration
# สร้าง Address List สำหรับแต่ละ LAN
/ip/firewall/address-list
add address=192.168.1.0/24 list=LAN1-ISP1
add address=192.168.2.0/24 list=LAN2-ISP2
add address=10.10.10.0/24 list=SERVERS-ISP1

# สร้าง Address List สำหรับ Gateway Health Check
add address=8.8.8.8 list=GOOGLE_DNS
add address=1.1.1.1 list=CLOUDFLARE_DNS

ขั้นตอนที่ 2: Mangle Rules (Mark Routing)

Mangle Configuration
# Mark routing สำหรับ LAN1 ไป ISP1
/ip/firewall/mangle
add chain=prerouting src-address-list=LAN1-ISP1 \
    action=mark-routing new-routing-mark=to-ISP1 \
    passthrough=no comment="LAN1 -> ISP1"

# Mark routing สำหรับ LAN2 ไป ISP2  
add chain=prerouting src-address-list=LAN2-ISP2 \
    action=mark-routing new-routing-mark=to-ISP2 \
    passthrough=no comment="LAN2 -> ISP2"

# Mark routing สำหรับ Servers ไป ISP1 (Prioritize)
add chain=prerouting src-address-list=SERVERS-ISP1 \
    action=mark-routing new-routing-mark=to-ISP1 \
    passthrough=no comment="Servers -> ISP1"

ขั้นตอนที่ 3: Routing Table

Routing Configuration
# สร้าง Routing Table ใหม่สำหรับแต่ละ ISP
/routing/table
add name=ISP1_Table fib
add name=ISP2_Table fib

# เพิ่ม Default Route สำหรับแต่ละ Table
/ip/route
add gateway=192.168.100.1 routing-table=ISP1_Table \
    check-gateway=ping comment="ISP1 Gateway"
add gateway=192.168.200.1 routing-table=ISP2_Table \
    check-gateway=ping comment="ISP2 Gateway"

# เพิ่ม Rule ให้ใช้ Routing Mark กับ Routing Table ที่สร้างไว้
/routing/rule
add src-address=192.168.1.0/24 action=lookup-only-in-table \
    table=ISP1_Table comment="LAN1 use ISP1"
add src-address=192.168.2.0/24 action=lookup-only-in-table \
    table=ISP2_Table comment="LAN2 use ISP2"

ขั้นตอนที่ 4: NAT Configuration

NAT Masquerade
# NAT สำหรับ ISP1
/ip/firewall/nat
add chain=srcnat out-interface=ether1-WAN1 \
    action=masquerade comment="NAT ISP1"

# NAT สำหรับ ISP2
add chain=srcnat out-interface=ether2-WAN2 \
    action=masquerade comment="NAT ISP2"

Use Case จริง

บริษัทในประเทศไทยหลายแห่งใช้ PBR เพื่อแยกเส้นทาง Internet ของแผนกต่างๆ เช่น แผนก IT ใช้ ISP หลักที่มี Bandwidth สูง ส่วนแผนกอื่นใช้ ISP รอง หรือแยกเส้นทาง Voice traffic (VoIP) ออกจาก Data traffic เพื่อลดความหน่วง

4. ECMP Load Balancing

ECMP (Equal-Cost Multi-Path) คือการกระจายภาระงานไปยังหลายเส้นทางที่มี Cost เท่ากัน ทำให้สามารถใช้ Bandwidth รวมกันได้เต็มที่

ECMP Load Balancing Flow

LAN Traffic All Networks ECMP Router Hash-based Distribution ISP 1 100 Mbps ISP 2 50 Mbps ISP 3 30 Mbps Internet Total: 180 Mbps 55% 28% 17%

ECMP Configuration (RouterOS v7)

ECMP Multi-Gateway
# วิธีที่ 1: ECMP แบบง่าย (Multi-Gateway)
/ip/route
add gateway=192.168.100.1,192.168.200.1 \
    check-gateway=ping comment="ECMP Default Route"

# วิธีที่ 2: ECMP พร้อม Weight (ถ้า Bandwidth ไม่เท่ากัน)
# Format: gateway@weight
/ip/route
add gateway="192.168.100.1@10,192.168.200.1@5" \
    check-gateway=ping comment="ECMP with Weight"

# วิธีที่ 3: ใช้ PCC (Per-Connection Classifier) สำหรับ Session Persistence
/ip/firewall/mangle
add chain=prerouting in-interface=bridge-LAN \
    connection-state=new \
    action=mark-connection new-connection-mark=ISP1_conn \
    passthrough=yes \
    per-connection-classifier=both-addresses:2/0 \
    comment="PCC ISP1"

add chain=prerouting in-interface=bridge-LAN \
    connection-state=new \
    action=mark-connection new-connection-mark=ISP2_conn \
    passthrough=yes \
    per-connection-classifier=both-addresses:2/1 \
    comment="PCC ISP2"

# Mark routing based on connection mark
add chain=prerouting connection-mark=ISP1_conn \
    action=mark-routing new-routing-mark=to-ISP1 \
    passthrough=no

add chain=prerouting connection-mark=ISP2_conn \
    action=mark-routing new-routing-mark=to-ISP2 \
    passthrough=no

ข้อควรระวัง!

ECMP พื้นฐานอาจทำให้เกิด Asymmetric Routing ซึ่งอาจทำให้บาง Application ทำงานผิดพลาด เช่น VoIP, HTTPS Banking ควรใช้ PCC สำหรับ Session Persistence

5. Failover Configuration

Automatic Failover ทำให้เครือข่ายสามารถสลับเส้นทางไปยัง ISP สำรองได้อัตโนมัติเมื่อ ISP หลักล้มเหลว โดยใช้ Recursive Routing หรือ Scripts

Failover Flow

Normal State Router ISP1 ✓ ISP2 Failover State Router ISP1 ✗ ISP2 ✓ Failover

วิธีที่ 1: Recursive Routing

Recursive Routing Failover
# กำหนด Target สำหรับ Health Check (Google DNS)
/ip/route
add dst-address=8.8.8.8 gateway=192.168.100.1 \
    check-gateway=ping distance=1 comment="Google via ISP1"
add dst-address=8.8.8.8 gateway=192.168.200.1 \
    check-gateway=ping distance=2 comment="Google via ISP2"

# Recursive Route ที่ใช้ 8.8.8.8 เป็น Gateway
add gateway=8.8.8.8 distance=1 \
    comment="Main route via recursive"

# ถ้า 8.8.8.8 ผ่าน ISP1 ไม่ได้ Router จะใช้ ISP2 อัตโนมัติ
# เพราะ route ไป 8.8.8.8 จะเปลี่ยนไปใช้ ISP2

วิธีที่ 2: Script-based Failover

Failover Script
# สร้าง Script สำหรับตรวจสอบและสลับ Gateway
/system/script
add name="check-failover" source={
    # กำหนดตัวแปร
    :local isp1Gateway "192.168.100.1"
    :local isp2Gateway "192.168.200.1"
    :local checkHost "8.8.8.8"
    :local currentGateway [/ip/route/get [find comment="main-default"] gateway]
    
    # ทดสอบ ISP1
    :local isp1Up false
    if ([/ping $checkHost interface=ether1-WAN1 count=3] > 0) do={
        set isp1Up true
    }
    
    # ตรรกะสลับ Gateway
    if ($isp1Up && $currentGateway != $isp1Gateway) do={
        /ip/route set [find comment="main-default"] gateway=$isp1Gateway
        :log info "Switched to ISP1"
    }
    
    if (!$isp1Up && $currentGateway = $isp1Gateway) do={
        /ip/route set [find comment="main-default"] gateway=$isp2Gateway
        :log info "Switched to ISP2 (Failover)"
    }
}

# สร้าง Schedule เรียก Script ทุก 30 วินาที
/system/scheduler
add name="failover-check" interval=30s \
    on-event="check-failover" comment="Auto failover check"

6. OSPF Dynamic Routing

OSPF (Open Shortest Path First) เป็นโปรโตคอลหาเส้นทางแบบ Link-State ที่เหมาะสำหรับเครือข่ายภายในองค์กรขนาดกลางถึงใหญ่ รองรับการแบ่ง Area และ Convergence เร็ว

OSPF Multi-Area Topology

Area 0 (Backbone) Core ABR Area 1 Router 1 Area 2 Router 2 10.0.0.0/30 10.0.0.4/30 192.168.1.0/24 192.168.2.0/24 Router ID: 1.1.1.1 | OSPF Version 2 | Cost: 10

OSPF Configuration (RouterOS v7)

OSPF v2 Configuration
# สร้าง OSPF Instance
/routing/ospf/instance
add name=ospf-internal router-id=1.1.1.1 \
    distribute-default=if-installed-as-type-1 \
    redistribute-connected=as-type-1

# สร้าง OSPF Area
/routing/ospf/area
add instance=ospf-internal name=backbone area-id=0.0.0.0
add instance=ospf-internal name=area1 area-id=0.0.0.1
add instance=ospf-internal name=area2 area-id=0.0.0.2

# กำหนด Interface Template
/routing/ospf/interface-template
add interfaces=ether3-Backbone area=backbone \
    networks=10.0.0.0/30 cost=10 priority=1 \
    hello-interval=10s dead-interval=40s

add interfaces=ether4-Branch1 area=area1 \
    networks=10.0.1.0/30 cost=20

add interfaces=ether5-Branch2 area=area2 \
    networks=10.0.2.0/30 cost=20

# ตรวจสอบ OSPF Neighbors
/routing/ospf/neighbor/print

# ตรวจสอบ OSPF Routes
/routing/ospf/route/print

Best Practices สำหรับ OSPF

  • • กำหนด Router ID แบบ Manual เพื่อป้องกันปัญหา ID เปลี่ยน
  • • ใช้ Area 0 เป็น Backbone เท่านั้น ไม่ควรมี Area อื่นติดต่อกันโดยตรง
  • • กำหนด Cost ตาม Bandwidth จริงของ Link
  • • ใช้ Authentication (MD5 หรือ HMAC-SHA) เพื่อความปลอดภัย
  • • กำหนด Stub Area สำหรับ Branch Offices ที่ไม่ต้องการ Full Routing Table

7. BGP Configuration

BGP (Border Gateway Protocol) เป็นโปรโตคอลหาเส้นทางหลักของ Internet ใช้สำหรับเชื่อมต่อกับ ISP หรือ Data Center ที่มีหลาย Uplink

BGP Configuration พื้นฐาน

BGP Configuration
# กำหนด BGP Instance
/routing/bgp/connection
add name=isp1-peer remote.address=203.0.113.1 \
    remote.as=65001 local.address=192.168.100.2 \
    local.as=65002 as-override=yes \
    output.procname=isp1-in

add name=isp2-peer remote.address=198.51.100.1 \
    remote.as=65003 local.address=192.168.200.2 \
    local.as=65002 as-override=yes \
    output.procname=isp2-in

# ประกาศ Networks ของเรา
/routing/bgp/network
add network=203.0.113.0/24 synchronize=no
add network=198.51.100.0/24 synchronize=no

# กำหนด Route Filter (Preferring ISP1)
/routing/filter/rule
add chain=isp1-in action=accept \
    set-bgp-local-pref=200 comment="Prefer ISP1"

add chain=isp2-in action=accept \
    set-bgp-local-pref=100 comment="ISP2 Backup"

# ตรวจสอบ BGP Status
/routing/bgp/connection/print
/routing/bgp/advertisements/print
/routing/bgp/routes/print

BGP vs OSPF: เลือกใช้อะไร?

Feature BGP OSPF
ใช้งาน เชื่อมต่อ ISP / Multi-homing Internal Network / Campus
Scale 900,000+ Routes 10,000 Routes
Convergence ช้า (นาที) เร็ว (วินาที)
Configuration ซับซ้อน ง่ายกว่า
Policy Control ละเอียดมาก จำกัด

8. Automation Scripts

MikroTik RouterOS มี Scripting Engine ที่ทรงพลัง สามารถเขียนสคริปต์เพื่อ Automation ต่างๆ เช่น Backup, Monitoring, Failover, และ Configuration Management

Script 1: Auto Backup ส่ง Email

Auto Backup Script
# ตั้งค่า Email ก่อน
/tool/e-mail
set address=smtp.gmail.com port=587 \
    from=admin@company.com \
    user=your-email@gmail.com \
    password=your-app-password

# Script สำหรับ Auto Backup
/system/script
add name="auto-backup" source={
    :local backupName ("backup-" . \
        [:pick [/system/clock/get date] 0 4] . \
        [:pick [/system/clock/get date] 5 7] . \
        [:pick [/system/clock/get date] 8 10])
    
    # สร้าง Backup File
    /system/backup/save name=$backupName
    
    # ส่ง Email
    /tool/e-mail/send to="admin@company.com" \
        subject=("Router Backup - " . $backupName) \
        body="Backup file attached" \
        file=($backupName . ".backup")
    
    :log info ("Backup sent: " . $backupName)
}

# Schedule ทุกวันเวลา 02:00
/system/scheduler
add name="daily-backup" start-time=02:00:00 \
    interval=1d on-event="auto-backup"

Script 2: Bandwidth Monitoring & Alert

Bandwidth Monitor Script
# Script ตรวจสอบ Bandwidth และแจ้งเตือน
/system/script
add name="bandwidth-monitor" source={
    :local interface "ether1-WAN1"
    :local threshold 80000000  # 80 Mbps in bits
    :local telegramToken "YOUR_BOT_TOKEN"
    :local chatId "YOUR_CHAT_ID"
    
    # ดึงค่า Traffic
    :local rxCurrent [/interface/get $interface rx-bits-per-second]
    :local txCurrent [/interface/get $interface tx-bits-per-second]
    :local totalTraffic ($rxCurrent + $txCurrent)
    
    # แปลงเป็น Mbps
    :local rxMbps ($rxCurrent / 1000000)
    :local txMbps ($txCurrent / 1000000)
    
    :log info ("RX: " . $rxMbps . " Mbps, TX: " . $txMbps . " Mbps")
    
    # ตรวจสอบ Threshold
    if ($totalTraffic > $threshold) do={
        :local message ("⚠️ High Traffic Alert%0A" . \
            "Interface: " . $interface . "%0A" . \
            "RX: " . $rxMbps . " Mbps%0A" . \
            "TX: " . $txMbps . " Mbps")
        
        /tool/fetch url=("https://api.telegram.org/bot" . \
            $telegramToken . "/sendMessage?chat_id=" . \
            $chatId . "&text=" . $message) \
            keep-result=no
    }
}

# Schedule ทุก 5 นาที
/system/scheduler
add name="bw-monitor" interval=5m on-event="bandwidth-monitor"

Script 3: Configuration Sync to Git

Export Config Script
# Script สำหรับ Export Configuration และส่งไป Git Server
/system/script
add name="config-export" source={
    :local gitServer "git.company.com"
    :local projectName "network-configs"
    :local routerName [/system/identity/get name]
    :local timestamp [/system/clock/get date]
    
    # Export Configuration
    /export file=("config-" . $routerName . ".rsc")
    
    # ส่งไปยัง API Server (Node.js/Python)
    /tool/fetch url=("https://" . $gitServer . "/api/config/upload") \
        http-method=post \
        http-data=({ \
            "router": $routerName; \
            "timestamp": $timestamp; \
            "config": [/file/get [find name=("config-" . $routerName . ".rsc")] contents] \
        }) \
        keep-result=no
    
    :log info ("Config exported and synced: " . $timestamp)
}

# Schedule ทุกชั่วโมง
/system/scheduler
add name="config-sync" interval=1h on-event="config-export"

9. Open Source Monitoring Integration

การ Monitor MikroTik Router ด้วย Open Source Tools ช่วยให้เห็นภาพรวมของเครือข่ายและช่วยแก้ไขปัญหาได้เร็วขึ้น

Monitoring Stack Architecture

MikroTik SNMP Agent MikroTik SNMP Agent Prometheus Time Series DB SNMP Exporter Grafana Visualization Alerting Alertmanager Notifications Zabbix Full Monitoring Telegram SNMP Query Alerts

SNMP Configuration บน MikroTik

SNMP v3 Setup
# Enable SNMP และตั้งค่า Community
/snmp
set enabled=yes contact="admin@company.com" \
    location="Data Center 1"

# สร้าง Community (สำหรับ SNMP v1/v2c)
/snmp/community
add name=monitoring addresses=192.168.1.100/32 \
    read-access=yes write-access=no

# สำหรับ SNMP v3 (แนะนำสำหรับ Security)
/snmp/community
add name=secure-monitoring \
    security=private \
    authentication-protocol=SHA \
    authentication-password=your-auth-password \
    encryption-protocol=AES \
    encryption-password=your-enc-password \
    read-access=yes

# ตรวจสอบ
/snmp/print

Prometheus SNMP Exporter Config

prometheus.yml
# prometheus.yml
scrape_configs:
  - job_name: 'mikrotik-routers'
    static_configs:
      - targets:
          - 192.168.1.1  # Core Router
          - 192.168.1.2  # Branch Router 1
          - 192.168.1.3  # Branch Router 2
    metrics_path: /snmp
    params:
      module: [mikrotik]
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: snmp-exporter:9116

# snmp.yml (Module สำหรับ MikroTik)
modules:
  mikrotik:
    walk:
      - 1.3.6.1.2.1.1  # System
      - 1.3.6.1.2.1.2  # Interfaces
      - 1.3.6.1.4.1.14988.1  # MikroTik MIB

Grafana Dashboard Recommendations

10. Troubleshooting ปัญหาที่พบบ่อย

1

ปัญหา: Asymmetric Routing ทำให้ VoIP ขาดหาย

อาการ: โทรศัพท์ IP มีเสียงขาดหายเป็นช่วงๆ เมื่อใช้ ECMP

วิธีแก้:

ใช้ PCC (Per-Connection Classifier) เพื่อให้การเชื่อมต่อเดียวกันออกทาง Gateway เดียวกันเสมอ

# ใช้ both-addresses-and-ports สำหรับ VoIP
/ip/firewall/mangle
add chain=prerouting protocol=udp dst-port=5060,10000-20000 \
    connection-state=new \
    per-connection-classifier=both-addresses-and-ports:2/0 \
    action=mark-connection new-connection-mark=voip-isp1 \
    passthrough=yes
2

ปัญหา: OSPF Neighbor ไม่ขึ้น

อาการ: Router ไม่เห็น Neighbor แม้ว่า Cable ต่อแล้ว

ตรวจสอบ:

# 1. ตรวจสอบว่า OSPF ทำงาน
/routing/ospf/instance/print

# 2. ตรวจสอบ Interface
/routing/ospf/interface/print

# 3. ดู Neighbors
/routing/ospf/neighbor/print

# 4. ตรวจสอบ Firewall ไม่บล็อก OSPF (Protocol 89)
/ip/firewall/filter/print

# 5. ตรวจสอบ MTU (ต้องเท่ากัน)
/interface/print
3

ปัญหา: Failover ไม่ทำงานอัตโนมัติ

อาการ: ISP ล้มแต่ Traffic ไม่สลับไป ISP สำรอง

วิธีแก้:

# ตรวจสอบ check-gateway
/ip/route/print detail
# ต้องเห็น check-gateway=ping

# ทดสอบ Ping Gateway ด้วย Source Interface
/ping 8.8.8.8 interface=ether1-WAN1

# ถ้าใช้ Recursive ตรวจสอบว่า Route ไป Target ยังอยู่
/ip/route/print where dst-address=8.8.8.8/32

# Debug Script Failover
/system/script/run check-failover
/log/print where topics~"failover"
4

ปัญหา: PBR ไม่ทำงาน - Traffic ออกผิด Gateway

อาการ: ตั้งค่า PBR แล้วแต่ Traffic ยังออกทาง Default Route

วิธีแก้:

# 1. ตรวจสอบว่า Mangle ทำงาน
/ip/firewall/mangle/print stats
# ดูว่ามี Packet Count เพิ่มขึ้นไหม

# 2. ตรวจสอบ Routing Mark
/ip/firewall/mangle/print where new-routing-mark!=""

# 3. ตรวจสอบ Routing Rule
/routing/rule/print

# 4. ตรวจสอบว่า Route มี routing-mark ถูกต้อง
/ip/route/print detail

# 5. Debug ด้วย Torch
/tool/torch interface=bridge-LAN port=any

# 6. ตรวจสอบ FastTrack (อาจข้าม Mangle)
/ip/firewall/filter/print where action=fasttrack-connection

11. สรุป

ในบทความนี้เราได้เรียนรู้เทคนิค MikroTik Routing ขั้นสูงที่ใช้ใน Production Environment จริง สรุปสิ่งที่ได้เรียนรู้:

Policy-Based Routing

ควบคุมเส้นทางตาม Source IP, Protocol หรือ Application

ECMP Load Balancing

กระจายภาระงานหลายเส้นทางพร้อม Session Persistence

Automatic Failover

สลับเส้นทางอัตโนมัติด้วย Recursive Routing หรือ Scripts

Dynamic Routing

OSPF สำหรับ Internal, BGP สำหรับ ISP Peering

Automation Scripts

Auto Backup, Monitoring, Configuration Sync

Open Source Monitoring

Prometheus + Grafana สำหรับ Visualization

ขั้นตอนต่อไป

  • 1. ทดลองใน Lab - ใช้ GNS3 หรือ EVE-NG จำลอง Environment ก่อนนำไปใช้จริง
  • 2. Documentation - บันทึก Configuration และ Diagram ไว้อ้างอิง
  • 3. Monitoring - ตั้งค่า Alerting ก่อนนำไป Production
  • 4. Testing - ทดสอบ Failover และ Load Balancing ทุก Scenario