ai

คู่มือฉบับสมบูรณ์ 2026: การตั้งค่า BGP Routing ในสภาพแวดล้อมการผลิตขั้นสูง

คู่มือฉบับสมบูรณ์ 2026: การตั้งค่า BGP Routing ในสภาพแวดล้อมการผลิตขั้นสูง
สรุปคำตอบ: BGP (Border Gateway Protocol) คือ routing protocol หลักของ internet ทำงานที่ Layer 3 ใช้ path-vector algorithm เชื่อม Autonomous Systems (AS) เข้าหากัน การ setup BGP ใน production ต้องคำนึงถึง security (GTSM, MD5 auth, prefix filtering), redundancy (dual-homing, ECMP), route policy (MED, LOCAL_PREF, AS-PATH prepending) และ monitoring (BGP session state, prefix count) เพื่อป้องกัน route leak และ hijacking

โดย อ.บอม กิตติทัศน์ | 25/03/2026 | SiamCafe.net Since 1997

BGP คืออะไร และทำไมถึงสำคัญ

BGP (Border Gateway Protocol) เป็น exterior gateway protocol (EGP) ที่ใช้เป็น routing protocol หลักของ internet ปัจจุบัน BGP version 4 (RFC 4271) เชื่อม Autonomous Systems (AS) หลายหมื่น AS เข้าหากัน ทำให้เกิดเป็น global internet routing table BGP ไม่ใช่แค่ protocol สำหรับ ISP ใหญ่ๆ เท่านั้น องค์กรที่มีหลาย ISP connections, multi-homed network, หรือต้องการ traffic engineering ล้วนจำเป็นต้องใช้ BGP

BGP ทำงานบน TCP port 179 ใช้ path-vector algorithm ในการตัดสินใจ route โดยแต่ละ route จะมี list ของ AS ที่ผ่าน (AS-PATH) BGP peers แบ่งเป็นสองประเภทหลัก: iBGP (internal BGP - peers ภายใน AS เดียวกัน) และ eBGP (external BGP - peers ระหว่าง AS ต่างกัน)

เนื้อหาเกี่ยวข้อง — Segment Routing Open Source Contribution

BGP Autonomous System และ AS Numbers

Autonomous System (AS) คือกลุ่มของ IP networks ที่อยู่ภายใต้การควบคุมของ entity เดียว มี routing policy เดียวกัน แต่ละ AS มี AS Number (ASN) ที่ไม่ซ้ำกัน 2-byte ASN (1-65535) หรือ 4-byte ASN (1-4294967295)

ASN แบ่งเป็น:

  • Public ASN ได้รับจาก Regional Internet Registry (RIR) เช่น APNIC, ARIN, RIPE สำหรับ connect กับ internet จริง
  • Private ASN 64512-65534 (2-byte) หรือ 4200000000-4294967294 (4-byte) ใช้ภายในองค์กร

ในการ lab หรือทดสอบใช้ private ASN เสมอ ห้าม advertise private ASN ออกสู่ internet

BGP Session States: ทำความเข้าใจ State Machine

BGP session ผ่าน states หลายขั้นตอนก่อนจะ Established:

State ความหมาย ปัญหาที่พบบ่อย
Idle เริ่มต้น รอ Start event Policy block, ACL
Connect พยายาม TCP connect Network unreachable, firewall
Active TCP connect fail รอ retry Peer IP ผิด, TCP port 179 blocked
OpenSent ส่ง OPEN message แล้ว ASN mismatch
OpenConfirm รอ KEEPALIVE confirm Holdtime mismatch
Established Session ทำงานปกติ แลก routes ได้

BGP Configuration บน Linux ด้วย FRRouting

FRRouting (FRR) เป็น open-source routing suite ที่นิยมใช้บน Linux รองรับ BGP, OSPF, ISIS, MPLS, SR-MPLS เป็นต้น ใช้ได้บน Ubuntu, Debian, RHEL

# ติดตั้ง FRRouting บน Ubuntu
curl -s https://deb.frrouting.org/frr/keys.gpg | sudo tee /usr/share/keyrings/frrouting.gpg > /dev/null
FRRVER="frr-stable"
echo deb '[signed-by=/usr/share/keyrings/frrouting.gpg]' \
  https://deb.frrouting.org/frr $(lsb_release -s -c) $FRRVER | \
  sudo tee -a /etc/apt/sources.list.d/frr.list
sudo apt-get update && sudo apt-get install frr frr-pythontools

# Enable BGP daemon
sudo sed -i 's/bgpd=no/bgpd=yes/' /etc/frr/daemons
sudo systemctl restart frr
# /etc/frr/frr.conf - BGP basic config
frr version 9.1
frr defaults traditional
hostname router01
!
router bgp 65001
 bgp router-id 10.0.0.1
 bgp log-neighbor-changes
 !
 neighbor 192.168.1.2 remote-as 65002
 neighbor 192.168.1.2 description "Upstream ISP"
 neighbor 192.168.1.2 password BGPSecretKey123
 neighbor 192.168.1.2 timers 30 90
 !
 address-family ipv4 unicast
  network 203.0.113.0/24
  neighbor 192.168.1.2 activate
  neighbor 192.168.1.2 soft-reconfiguration inbound
  neighbor 192.168.1.2 prefix-list ALLOWED-IN in
  neighbor 192.168.1.2 prefix-list ALLOWED-OUT out
  neighbor 192.168.1.2 route-map SET-LOCAL-PREF in
 exit-address-family
!
ip prefix-list ALLOWED-IN seq 5 permit 0.0.0.0/0
ip prefix-list ALLOWED-IN seq 10 permit 0.0.0.0/0 le 24
ip prefix-list ALLOWED-IN seq 100 deny any
!
ip prefix-list ALLOWED-OUT seq 5 permit 203.0.113.0/24
ip prefix-list ALLOWED-OUT seq 100 deny any
!
route-map SET-LOCAL-PREF permit 10
 set local-preference 200

BGP Security: สิ่งที่ต้องทำใน Production

BGP security เป็นเรื่องสำคัญมาก ปัญหา BGP route leak และ hijacking เกิดขึ้นหลายครั้งและส่งผลกระทบต่อ internet ในวงกว้าง

1. Generalized TTL Security Mechanism (GTSM / TTL Security)

# FRRouting
neighbor 192.168.1.2 ttl-security hops 1

# Cisco IOS
neighbor 192.168.1.2 ttl-security hops 1

2. MD5 Authentication ป้องกัน TCP session hijacking

# FRRouting
neighbor 192.168.1.2 password StrongBGPPassword!2026

3. Prefix Filtering - RPKI ROV (Route Origin Validation)

# ติดตั้ง routinator RPKI validator
apt install routinator
routinator -v server --http 192.168.1.1:8080 --rtr 192.168.1.1:3323

# FRRouting - enable RPKI
rpki
 rpki cache 192.168.1.1 3323 preference 1
 exit
!
# ใน BGP neighbor config
neighbor 192.168.1.2 route-map RPKI-CHECK in

route-map RPKI-CHECK permit 10
 match rpki valid
route-map RPKI-CHECK permit 20
 match rpki notfound
route-map RPKI-CHECK deny 30
 match rpki invalid

4. Maximum Prefix Limit ป้องกัน route table overflow

neighbor 192.168.1.2 maximum-prefix 800000 80

5. BGPsec — RFC 8205 สำหรับ cryptographic path validation ยังไม่ deploy ในวงกว้าง แต่เป็น direction ของอนาคต

เนื้อหาเกี่ยวข้อง — Ubuntu Server 2026

BGP Path Selection Algorithm

BGP มี algorithm ในการเลือก best path ดังนี้ (เรียงตามลำดับความสำคัญ):

  1. Weight (Cisco proprietary) — สูงกว่าดีกว่า เป็น local to router
  2. LOCAL_PREF — สูงกว่าดีกว่า ใช้ภายใน AS เพื่อกำหนด preferred exit
  3. Locally originated — network command หรือ redistribute
  4. AS-PATH length — สั้นกว่าดีกว่า ใช้ AS-PATH prepending เพื่อทำให้ path ยาวขึ้น
  5. Origin type — IGP < EGP < Incomplete
  6. MED (Multi-Exit Discriminator) — ต่ำกว่าดีกว่า ใช้บอก peer ว่า prefer entry point ไหน
  7. eBGP over iBGP
  8. IGP metric ไปยัง next-hop
  9. Router ID — ต่ำกว่าดีกว่า (tie-breaker สุดท้าย)

BGP Multi-homing สำหรับ Redundancy

การ connect กับ ISP หลายราย (multi-homing) ทำให้ได้ redundancy และสามารถ load balance traffic ได้:

# Dual-homed setup: two ISPs
router bgp 65001
 bgp router-id 10.0.0.1
 !
 # ISP A - primary
 neighbor 192.0.2.1 remote-as 65100
 neighbor 192.0.2.1 description "ISP-A Primary"
 !
 # ISP B - backup
 neighbor 198.51.100.1 remote-as 65200
 neighbor 198.51.100.1 description "ISP-B Backup"
 !
 address-family ipv4 unicast
  network 203.0.113.0/24
  !
  # Prefer ISP-A for inbound (announce longer prefix to ISP-B)
  neighbor 192.0.2.1 route-map ISP-A-OUT out
  neighbor 198.51.100.1 route-map ISP-B-OUT out
  !
  # Prefer ISP-A for outbound
  neighbor 192.0.2.1 route-map SET-LP-HIGH in
  neighbor 198.51.100.1 route-map SET-LP-LOW in

route-map ISP-A-OUT permit 10
 match ip address prefix-list OWN-PREFIXES
 set community 65001:100

route-map ISP-B-OUT permit 10
 match ip address prefix-list OWN-PREFIXES
 set as-path prepend 65001 65001  # AS-PATH prepend ทำให้ path ยาวขึ้น = less preferred

route-map SET-LP-HIGH permit 10
 set local-preference 200

route-map SET-LP-LOW permit 10
 set local-preference 100

BGP Monitoring และ Troubleshooting

# FRRouting vtysh commands
show bgp summary                    # ดู session status ทั้งหมด
show bgp neighbors 192.168.1.2      # ดูรายละเอียด neighbor
show bgp ipv4 unicast               # ดู full BGP table
show ip route bgp                   # ดู routes ที่ install ใน RIB
show bgp ipv4 unicast statistics    # ดู prefix count
!
# Debug (ใช้ระวัง production!)
debug bgp neighbor-events
debug bgp updates
!
# Clear BGP session
clear bgp 192.168.1.2 soft         # soft reset ไม่ drop session
clear bgp 192.168.1.2              # hard reset drop session
# Monitoring BGP ด้วย Prometheus + Alertmanager
# prometheus.yml
scrape_configs:
  - job_name: 'frr_bgp'
    static_configs:
      - targets: ['localhost:9342']  # frr_exporter

# Alert rule
groups:
  - name: bgp_alerts
    rules:
      - alert: BGPSessionDown
        expr: frr_bgp_peer_state{state!="established"} == 1
        for: 2m
        annotations:
          summary: "BGP session down to {{ $labels.peer }}"

      - alert: BGPPrefixCountHigh
        expr: frr_bgp_prefixes_received > 900000
        annotations:
          summary: "BGP prefix count abnormally high"

เนื้อหาเกี่ยวข้อง — Debian Linux Server 2026

BGP Communities สำหรับ Traffic Engineering

BGP Communities เป็น attribute ที่ใช้แนบ metadata กับ BGP routes เพื่อกำหนด policy:

# Standard community format: AS:value
# ตัวอย่าง well-known communities
65001:100  = "prefer this route"
65001:200  = "backup route"
65001:666  = "blackhole this prefix"

# FRRouting
route-map TAG-ROUTES permit 10
 set community 65001:100

# Large communities (RFC 8092) - format: ASN:value1:value2
set large-community 65001:1:100  # ASN:policy:action

# Community-based policy
route-map PROCESS-COMMUNITY permit 10
 match community ISP-A-PREFER
 set local-preference 200
!
ip community-list standard ISP-A-PREFER permit 65001:100

BGP Route Reflector สำหรับ iBGP Scalability

ใน large AS iBGP ต้องการ full-mesh (n*(n-1)/2 sessions) ซึ่ง scale ไม่ดี Route Reflector (RR) แก้ปัญหานี้โดยทำหน้าที่รับและ reflect routes จาก iBGP clients ไปยัง clients อื่น

router bgp 65001
 bgp cluster-id 10.0.0.1
 !
 neighbor 10.0.0.2 remote-as 65001
 neighbor 10.0.0.2 route-reflector-client
 !
 neighbor 10.0.0.3 remote-as 65001
 neighbor 10.0.0.3 route-reflector-client

FAQ: BGP Routing Advanced

Q: BGP ต่างจาก OSPF/ISIS อย่างไร?

A: OSPF/ISIS เป็น IGP (Interior Gateway Protocol) ใช้ภายใน AS เดียว เน้น convergence เร็ว BGP เป็น EGP ใช้ระหว่าง AS เน้น policy control และ scalability แต่ converge ช้ากว่า การใช้ OSPF หรือ ISIS เป็น underlay และ BGP เป็น overlay (BGP over MPLS/SR) เป็น design pattern ที่นิยมมากใน SP network

Q: Route leak และ Hijacking ต่างกันอย่างไร?

A: Route leak คือการ advertise prefix ที่ไม่ควร advertise โดยไม่ตั้งใจ hijacking คือการ advertise prefix ของคนอื่นโดยตั้งใจหรือไม่ก็ตาม ทั้งสองทำให้ traffic route ไปผิดทาง RPKI ROV ช่วยป้องกัน hijacking ได้ดี แต่ route leak ยังต้องป้องกันด้วย prefix filtering policy

Q: ECMP ใน BGP ทำงานอย่างไร?

A: Equal-Cost Multi-Path ใน BGP ต้องการ routes หลาย paths ที่มี AS-PATH length เท่ากัน ต้อง enable ด้วย maximum-paths FRRouting: bgp bestpath as-path multipath-relax และ maximum-paths 4

BGP ใน Production: Best Practices และ Design Patterns

การ deploy BGP ใน production environment ที่ stable ต้องใช้ best practices หลายอย่างร่วมกัน เริ่มจาก router ID ควรใช้ loopback address ที่ stable ไม่เปลี่ยนแปลงเมื่อ physical interface down จากนั้น iBGP peers ควร peer กัน loopback-to-loopback เพื่อ resilience

การตั้งค่า timers เป็นเรื่องสำคัญ default BGP holdtime คือ 90 วินาที keepalive 30 วินาที ใน datacenter ที่ต้องการ convergence เร็วสามารถปรับเป็น holdtime 9 วินาที keepalive 3 วินาที หรือแม้แต่ BFD (Bidirectional Forwarding Detection) สำหรับ sub-second failure detection

สำหรับ production network ที่ต้องการ high availability ควรออกแบบ redundancy ตั้งแต่ต้น route reflector ควรมีอย่างน้อย 2 ตัวในต่าง physical location eBGP peers ควรมีอย่างน้อย 2 connections ไปยัง upstream ISP แต่ละราย การ monitor BGP session state, prefix count, memory usage ใน router ควรทำผ่าน Prometheus + Alertmanager และ alert ทันทีเมื่อ session down หรือ prefix count เปลี่ยนแปลงผิดปกติ

BGP ใน Cloud Environment (AWS, GCP, Azure)

ใน cloud environment BGP ใช้ใน Direct Connect (AWS), Cloud Interconnect (GCP), ExpressRoute (Azure) เพื่อ connect on-premise network กับ cloud VPC ผ่าน dedicated fiber BGP ยังใช้ใน Virtual Private Gateway (VGW) บน AWS และ Cloud Router บน GCP สำหรับ Site-to-Site VPN

บน AWS ค่า BGP ASN สำหรับ Virtual Private Gateway สามารถเลือกได้ตั้งแต่ 64512-65534 (private) Direct Connect ใช้ ASN จริงที่ AWS จัดสรรให้หรือ customer ASN ที่มีอยู่แล้ว บน GCP Cloud Router ต้องการ Google ASN (16550) สำหรับ Dedicated Interconnect หรือ customer ASN สำหรับ Partner Interconnect

ข้อควรระวังสำหรับ cloud BGP: ECMP ใน cloud มีข้อจำกัดที่ต่างจาก on-premise router, latency ของ BGP session ผ่าน Internet VPN สูงกว่า Direct Connect มาก, และ cloud BGP มักไม่รองรับ configuration options ทั้งหมดที่มีใน hardware router เช่น certain community manipulation หรือ advanced route policy

เนื้อหาเกี่ยวข้อง — Docker Compose V2 Team Productivity

BGP Labs: ฝึกด้วย GNS3 และ ContainerLab

การเรียนรู้ BGP ต้องอาศัยการฝึกจริงในสภาพแวดล้อม lab ContainerLab เป็นตัวเลือกที่ดีมากสำหรับ build BGP lab บน Linux ใช้ Docker containers รัน network OSes เช่น FRRouting, Nokia SR Linux, Arista cEOS

# containerlab topology สำหรับ BGP lab
name: bgp-lab

topology:
  nodes:
    router1:
      kind: linux
      image: frrouting/frr:v9.1.0
      binds:
        - ./configs/router1/frr.conf:/etc/frr/frr.conf
        - ./configs/router1/daemons:/etc/frr/daemons

    router2:
      kind: linux
      image: frrouting/frr:v9.1.0
      binds:
        - ./configs/router2/frr.conf:/etc/frr/frr.conf
        - ./configs/router2/daemons:/etc/frr/daemons

    router3:
      kind: linux
      image: frrouting/frr:v9.1.0
      binds:
        - ./configs/router3/frr.conf:/etc/frr/frr.conf
        - ./configs/router3/daemons:/etc/frr/daemons

  links:
    - endpoints: ["router1:eth1", "router2:eth1"]
    - endpoints: ["router2:eth2", "router3:eth1"]
    - endpoints: ["router1:eth2", "router3:eth2"]
# deploy lab
containerlab deploy -t bgp-lab.yml

# เข้าไปใน router
docker exec -it clab-bgp-lab-router1 vtysh

# cleanup
containerlab destroy -t bgp-lab.yml

GNS3 เป็นอีกตัวเลือกที่รองรับ network OS หลากหลายมากกว่า ทั้ง Cisco, Juniper, Palo Alto สำหรับคนที่ต้องการ vendor-specific BGP behavior GNS3 เหมาะกว่า แต่ resource requirement สูงกว่า ContainerLab มาก

สรุป BGP ใน Production

BGP เป็น protocol ที่ซับซ้อนและต้องใช้เวลาศึกษาจริงจัง การเริ่มต้นจาก lab environment แล้วค่อยนำความรู้ไปใช้ใน staging ก่อน production เป็นแนวทางที่ปลอดภัยที่สุด สิ่งที่ต้องเข้าใจให้ชัดเจนก่อน deploy BGP ใน production ได้แก่ BGP path selection algorithm, security best practices (RPKI, prefix filtering, GTSM), redundancy design, และ monitoring/alerting ที่ครอบคลุม การ simulate failure scenarios บน lab ก่อน production ช่วยให้ทีม network มั่นใจในการ operate ระบบจริง

บทความที่เกี่ยวข้อง

XM Legend · เทรดเดอร์ & ผู้สอน Forex 13 ปี

ผู้ก่อตั้ง SiamCafe ตั้งแต่ปี 1997 · เทรดเดอร์สาย Forex มากกว่า 13 ปี ได้รับการยกย่องเป็น XM Legend · แบ่งปันความรู้ Forex, ไอที, AI และการเทรด จากประสบการณ์จริงในตลาดจริง