
NetBox คืออะไร?
NetBox เป็น Open Source Web Application สำหรับจัดการ Network Infrastructure ที่พัฒนาโดย DigitalOcean ในปี 2016 และปัจจุบันดูแลโดย NetBox Labs ทำหน้าที่เป็น Source of Truth สำหรับข้อมูลโครงสร้างพื้นฐานเครือข่ายทั้งหมดขององค์กร
NetBox รวม 2 ฟังก์ชันหลักไว้ในที่เดียว:
- IPAM (IP Address Management): จัดการ IP Addresses, Prefixes, VLANs, VRFs ทั้งหมดในองค์กร
- DCIM (Data Center Infrastructure Management): จัดการ Sites, Racks, Devices, Cables, Power ทั้ง Data Center
แนวคิดสำคัญคือ NetBox เป็น “Source of Truth” หมายความว่าข้อมูลใน NetBox คือ ความจริงที่ถูกต้อง ที่ทุกระบบ Automation ควรอ้างอิง ไม่ใช่ข้อมูลจาก Spreadsheet, Email หรือหัวของ Network Engineer คนใดคนหนึ่ง
NetBox vs phpIPAM vs Nautobot
| Feature | NetBox | phpIPAM | Nautobot |
|---|---|---|---|
| IPAM | ครบถ้วน | ดีมาก (จุดเด่นหลัก) | ครบถ้วน (Fork จาก NetBox) |
| DCIM | ดีมาก | จำกัด | ดีมาก |
| API | REST + GraphQL | REST (จำกัด) | REST + GraphQL |
| Automation Integration | Ansible, Terraform | จำกัด | Ansible, Terraform (ดีกว่า) |
| Plugin System | มี (Django-based) | จำกัด | มี (Apps framework) |
| Community | ใหญ่ที่สุด | ปานกลาง | กำลังเติบโต |
| Learning Curve | ปานกลาง | ง่าย | สูง |
| Language | Python/Django | PHP | Python/Django |
| Best For | Network + DC ทั่วไป | IPAM อย่างเดียว | Enterprise Automation |
สรุป: ถ้าต้องการทั้ง IPAM + DCIM + Automation → NetBox ถ้าต้องการ IPAM อย่างเดียวแบบง่ายๆ → phpIPAM ถ้าเป็น Enterprise ที่ต้องการ Customization สูง → Nautobot
การติดตั้ง NetBox ด้วย Docker
วิธีที่ง่ายที่สุดในการติดตั้ง NetBox คือใช้ Docker Compose ผ่าน Official netbox-docker repository:
ขั้นตอนที่ 1: Clone Repository
# Clone official netbox-docker
git clone -b release https://github.com/netbox-community/netbox-docker.git
cd netbox-docker
# สร้าง docker-compose.override.yml สำหรับ Custom config
cat > docker-compose.override.yml << 'EOF'
services:
netbox:
ports:
- "8000:8080"
environment:
SUPERUSER_API_TOKEN: "your-api-token-here"
SUPERUSER_EMAIL: "[email protected]"
SUPERUSER_NAME: "admin"
SUPERUSER_PASSWORD: "admin123"
ALLOWED_HOSTS: "*"
EOF
ขั้นตอนที่ 2: Start NetBox
# Pull images and start
docker compose pull
docker compose up -d
# ตรวจสอบสถานะ
docker compose ps
# ดู logs
docker compose logs -f netbox
# เปิด http://localhost:8000
# Login: admin / admin123
ขั้นตอนที่ 3: การตั้งค่าเพิ่มเติม
# แก้ไข configuration.py สำหรับ Production
# ใน docker-compose.override.yml:
services:
netbox:
environment:
# Database
DB_HOST: postgres
DB_NAME: netbox
DB_PASSWORD: "strong-password"
DB_USER: netbox
# Redis
REDIS_HOST: redis
REDIS_PASSWORD: "redis-password"
# Security
SECRET_KEY: "your-very-long-random-secret-key"
ALLOWED_HOSTS: "netbox.example.com"
# LDAP (optional)
# REMOTE_AUTH_ENABLED: "true"
Data Model ของ NetBox
NetBox มี Data Model ที่ครอบคลุมทุกมิติของ Network Infrastructure:
1. Organization
| Object | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| Sites | สถานที่ทางกายภาพ | DC-BKK-01, Office-CNX |
| Regions | กลุ่มของ Sites | Asia Pacific, Thailand |
| Site Groups | กลุ่ม Sites ตามหน้าที่ | Production DCs, Branch Offices |
| Tenants | ลูกค้า/หน่วยงาน | Customer-A, Department-IT |
2. Racks
| Object | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| Racks | ตู้ Rack (กำหนด U height) | Rack-A01 (42U) |
| Rack Roles | ประเภทของ Rack | Network, Server, Storage |
| Rack Reservations | จอง U ใน Rack | U1-U4 reserved for UPS |
3. Devices
| Object | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| Device Types | รุ่นของอุปกรณ์ | Cisco C9300-48P |
| Manufacturers | ผู้ผลิต | Cisco, Arista, Juniper |
| Device Roles | หน้าที่ของอุปกรณ์ | Core Switch, Access Switch, Firewall |
| Platforms | OS ของอุปกรณ์ | IOS-XE, NX-OS, Junos |
4. Interfaces & Cables
# ตัวอย่าง Data Model ความสัมพันธ์:
#
# Site: DC-BKK-01
# └── Rack: A01
# ├── Device: core-sw-01 (Cisco C9500-48Y4C)
# │ ├── Interface: GigabitEthernet1/0/1 → Cable → access-sw-01 Gi1/0/49
# │ ├── Interface: GigabitEthernet1/0/2 → Cable → access-sw-02 Gi1/0/49
# │ ├── Interface: TenGigabitEthernet1/1/1 → Cable → core-sw-02 Te1/1/1
# │ └── Management0 → IP: 10.0.1.1/24
# │
# └── Device: access-sw-01 (Cisco C9300-48P)
# ├── Interface: GigabitEthernet1/0/1 → VLAN 10 (Users)
# ├── Interface: GigabitEthernet1/0/2 → VLAN 20 (VoIP)
# └── Management0 → IP: 10.0.1.11/24
5. IP Address Management (IPAM)
| Object | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| IP Addresses | IP แต่ละตัว | 10.0.1.1/24, 2001:db8::1/64 |
| Prefixes | Subnet/Network | 10.0.0.0/16, 172.16.0.0/12 |
| VLANs | Virtual LAN | VLAN 10 (Users), VLAN 20 (VoIP) |
| VRFs | Virtual Routing & Forwarding | VRF-CUSTOMER-A, VRF-MGMT |
| Aggregates | IP Block ที่ได้รับจัดสรร | 10.0.0.0/8 (RFC 1918) |
| ASNs | Autonomous System Numbers | AS65001 |
6. Prefix Hierarchy
# NetBox จัดระเบียบ IP เป็น Hierarchy:
#
# Aggregate: 10.0.0.0/8 (Private - RFC1918)
# └── Prefix: 10.0.0.0/16 (DC-BKK)
# ├── Prefix: 10.0.1.0/24 (Management) - VLAN 1
# │ ├── IP: 10.0.1.1/24 → core-sw-01
# │ ├── IP: 10.0.1.2/24 → core-sw-02
# │ └── IP: 10.0.1.11/24 → access-sw-01
# │
# ├── Prefix: 10.0.10.0/24 (Users) - VLAN 10
# │ ├── IP: 10.0.10.1/24 → Gateway
# │ └── IP: 10.0.10.100-200 → DHCP Pool
# │
# └── Prefix: 10.0.20.0/24 (VoIP) - VLAN 20
# └── IP: 10.0.20.1/24 → Gateway
#
# แต่ละ Prefix มี:
# - Status: Active, Reserved, Deprecated
# - Role: Production, Development, Management
# - Utilization: กี่ % ถูกใช้แล้ว
7. Circuits & Virtual Machines
| Object | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| Circuits | วงจรสื่อสาร (WAN links) | MPLS-BKK-CNX-001 |
| Providers | ผู้ให้บริการ | TRUE, AIS, CAT Telecom |
| Circuit Types | ประเภทวงจร | MPLS, Internet, Dark Fiber |
| Virtual Machines | VM ในระบบ | web-server-01, db-server-01 |
| Clusters | กลุ่ม VM Host | VMware-Cluster-01 |
API-First Design (REST + GraphQL)
NetBox ถูกออกแบบให้เป็น API-First หมายความว่าทุกอย่างที่ทำได้ผ่าน Web UI สามารถทำผ่าน API ได้ทั้งหมด:
REST API
# สร้าง API Token ใน Admin > API Tokens
# หรือใช้ Token ที่ตั้งไว้ตอนติดตั้ง
# ดู Sites ทั้งหมด
curl -H "Authorization: Token your-api-token" http://netbox.example.com/api/dcim/sites/
# สร้าง Site ใหม่
curl -X POST -H "Authorization: Token your-api-token" -H "Content-Type: application/json" -d '{"name": "DC-BKK-02", "slug": "dc-bkk-02", "status": "active"}' http://netbox.example.com/api/dcim/sites/
# ดู IP Addresses ทั้งหมด
curl -H "Authorization: Token your-api-token" http://netbox.example.com/api/ipam/ip-addresses/
# ค้นหา IP ที่ว่างใน Prefix
curl -H "Authorization: Token your-api-token" http://netbox.example.com/api/ipam/prefixes/1/available-ips/
# สร้าง Device
curl -X POST -H "Authorization: Token your-api-token" -H "Content-Type: application/json" -d '{
"name": "access-sw-03",
"device_type": 1,
"role": 2,
"site": 1,
"rack": 1,
"position": 10,
"face": "front",
"status": "active"
}' http://netbox.example.com/api/dcim/devices/
GraphQL API
# NetBox 3.x+ รองรับ GraphQL
# เข้า http://netbox.example.com/graphql/
# Query: ดู Devices พร้อม Interfaces และ IP
query {
device_list(site: "dc-bkk-01") {
name
device_role { name }
platform { name }
interfaces {
name
type
ip_addresses {
address
status
}
connected_endpoints {
... on InterfaceType {
name
device { name }
}
}
}
}
}
# Query: ดู Prefix Utilization
query {
prefix_list(site: "dc-bkk-01") {
prefix
vlan { vid name }
status
role { name }
description
}
}
NetBox สำหรับ Automation
Ansible Dynamic Inventory
# ansible.cfg
[defaults]
inventory = netbox_inventory.yml
# netbox_inventory.yml
plugin: netbox.netbox.nb_inventory
api_endpoint: http://netbox.example.com
token: your-api-token
validate_certs: false
# จัดกลุ่มตาม
group_by:
- device_roles
- sites
- platforms
- tags
# เลือกเฉพาะ Active devices
query_filters:
- status: active
# Map variables
compose:
ansible_host: primary_ip4.address | split('/') | first
ansible_network_os: platform.slug
# ใช้ Ansible Playbook กับ NetBox Inventory
# devices จะถูกจัดกลุ่มอัตโนมัติ
# playbook.yml
---
- name: Backup all switches
hosts: device_roles_access_switch
gather_facts: false
tasks:
- name: Backup running config
cisco.ios.ios_config:
backup: yes
backup_options:
dir_path: /backups/{{ inventory_hostname }}/
# รัน:
ansible-playbook playbook.yml
# Ansible จะ query NetBox API โดยอัตโนมัติ
# หา devices ที่มี role = access_switch
# ใช้ primary_ip เป็น ansible_host
Terraform Integration
# Terraform สามารถอ่านข้อมูลจาก NetBox
# เพื่อสร้าง Cloud resources ตาม Source of Truth
# provider.tf
terraform {
required_providers {
netbox = {
source = "e-breuninger/netbox"
version = "~> 3.0"
}
}
}
provider "netbox" {
server_url = "http://netbox.example.com"
api_token = var.netbox_api_token
}
# data.tf - ดึงข้อมูลจาก NetBox
data "netbox_virtual_machines" "web_servers" {
filter {
name = "role"
value = "web-server"
}
}
# main.tf - สร้าง VM ตามที่กำหนดใน NetBox
resource "vsphere_virtual_machine" "web" {
for_each = { for vm in data.netbox_virtual_machines.web_servers.vms : vm.name => vm }
name = each.value.name
vcpu = each.value.vcpus
memory = each.value.memory
}
Custom Fields และ Tags
Custom Fields
NetBox อนุญาตให้เพิ่ม Custom Fields ในทุก Object เพื่อเก็บข้อมูลเฉพาะขององค์กร:
# ตัวอย่าง Custom Fields ที่มีประโยชน์:
#
# Devices:
# - purchase_date (Date): วันที่ซื้อ
# - warranty_end (Date): วันหมดประกัน
# - asset_tag (Text): รหัส Asset
# - responsible_team (Selection): ทีมที่ดูแล
# - monitoring_enabled (Boolean): เปิด Monitoring หรือไม่
#
# IP Addresses:
# - dns_name (Text): ชื่อ DNS
# - scan_date (Date): วันที่ Scan ล่าสุด
#
# สร้าง Custom Field:
# Admin > Customization > Custom Fields > Add
# กำหนด: Name, Type, Object Type, Required/Optional
Tags
# Tags ช่วย Label objects ข้าม Object types
# ตัวอย่าง Tags:
# - production: อุปกรณ์ Production
# - maintenance-window-sun: Maintenance วันอาทิตย์
# - pci-scope: อยู่ใน PCI DSS scope
# - eol-2026: End of Life ปี 2026
# - backup-daily: Backup ทุกวัน
#
# ใช้ Tags ใน API filter:
curl -H "Authorization: Token your-token" "http://netbox.example.com/api/dcim/devices/?tag=production"
#
# ใช้ Tags ใน Ansible Inventory:
# group_by:
# - tags
# → สร้าง group: tag_production, tag_pci_scope, etc.
NetBox Plugins
NetBox รองรับ Plugin System (Django Apps) สำหรับเพิ่มฟังก์ชันใหม่:
| Plugin | หน้าที่ |
|---|---|
| netbox-topology-views | แสดง Network Topology Diagram จาก NetBox data |
| netbox-bgp | จัดการ BGP Sessions, ASNs, Communities |
| netbox-access-lists | จัดการ ACLs สำหรับ Firewall/Router |
| netbox-dns | จัดการ DNS Zones และ Records |
| netbox-config-diff | เปรียบเทียบ Config จริงกับ Intended |
| netbox-documents | แนบเอกสารกับ Objects |
| netbox-secrets | เก็บ Secrets (passwords, keys) แบบเข้ารหัส |
การติดตั้ง Plugin
# สำหรับ Docker deployment:
# 1. สร้าง Dockerfile-Plugins
FROM netboxcommunity/netbox:latest
COPY ./plugin_requirements.txt /opt/netbox/
RUN /opt/netbox/venv/bin/pip install -r /opt/netbox/plugin_requirements.txt
# 2. plugin_requirements.txt
netbox-topology-views
netbox-bgp
# 3. เพิ่มใน configuration.py
PLUGINS = [
'netbox_topology_views',
'netbox_bgp',
]
# 4. docker compose up --build -d
Device Role & Platform
Device Roles ที่แนะนำ
| Role | Color | ตัวอย่าง Devices |
|---|---|---|
| Core Switch | Red | Cisco C9500, Arista 7280 |
| Distribution Switch | Orange | Cisco C9300, Arista 7050 |
| Access Switch | Green | Cisco C9200, Aruba 6300 |
| Router | Blue | Cisco ISR 4000, Juniper MX |
| Firewall | Dark Red | Palo Alto, Fortinet |
| Wireless AP | Cyan | Cisco C9120, Aruba AP-635 |
| Server | Gray | Dell PowerEdge, HPE ProLiant |
| PDU | Yellow | APC, Raritan |
Platform Definitions
# Platforms กำหนด OS/Software ของ Device
# ใช้สำหรับ Ansible automation (ansible_network_os)
#
# Slug | Name | Manufacturer | NAPALM Driver
# cisco-ios-xe | Cisco IOS-XE | Cisco | ios
# cisco-nxos | Cisco NX-OS | Cisco | nxos
# juniper-junos | Juniper Junos | Juniper | junos
# arista-eos | Arista EOS | Arista | eos
# paloalto-panos | PAN-OS | Palo Alto | panos
# linux | Linux | - | -
Cable Management
NetBox จัดการ Cable ได้อย่างละเอียด:
# Cable Properties:
# - Type: CAT5e, CAT6, CAT6A, SMF, MMF, DAC, Power
# - Color: ใช้สีตาม Standard (Blue=data, Yellow=SM fiber, etc.)
# - Length: ความยาว (m/ft)
# - Label: ป้ายชื่อสาย
# - Status: Connected, Planned, Decommissioning
#
# Cable Trace:
# NetBox สามารถ Trace สาย End-to-End:
# Server-01 eth0 → Patch-Panel-A Port-1 → Cable → Patch-Panel-B Port-1 → Switch Gi1/0/1
#
# ทำผ่าน API:
curl -H "Authorization: Token your-token" "http://netbox.example.com/api/dcim/cables/?device=server-01"
NetBox สำหรับ Cloud Resources
แม้ NetBox จะออกแบบมาสำหรับ Physical infrastructure แต่สามารถจัดการ Cloud resources ได้:
- Virtual Machines: ใช้ Cluster + VM objects สำหรับ AWS EC2, Azure VM, GCP Compute
- Cloud Prefixes: จัดการ VPC CIDR, Subnet ใน Cloud ผ่าน IPAM
- Tags: ใช้ Tags แบ่ง cloud-aws, cloud-azure, on-prem
- Custom Fields: เก็บ AWS Account ID, Azure Subscription, GCP Project
# ตัวอย่าง: จัดการ AWS VPC ใน NetBox
#
# Site: AWS-AP-SOUTHEAST-1
# Cluster: EKS-Production
# VM: web-app-01 (2 vCPU, 4GB RAM)
# VM: api-server-01 (4 vCPU, 8GB RAM)
#
# Prefix: 10.100.0.0/16 (VPC: prod-vpc)
# ├── 10.100.1.0/24 (Public Subnet AZ-a)
# ├── 10.100.2.0/24 (Public Subnet AZ-b)
# ├── 10.100.10.0/24 (Private Subnet AZ-a)
# └── 10.100.20.0/24 (Private Subnet AZ-b)
Webhooks สำหรับ Automation Triggers
NetBox สามารถส่ง Webhooks เมื่อมีการเปลี่ยนแปลงข้อมูล:
# ตัวอย่าง Webhook Configurations:
#
# 1. Webhook: Notify Slack when new device is created
# - Object Type: Device
# - Event: Created
# - URL: https://hooks.slack.com/services/xxx
# - Body Template:
# {"text": "New device created: {{ data.name }} at {{ data.site.name }}"}
#
# 2. Webhook: Trigger Ansible when IP assigned
# - Object Type: IP Address
# - Event: Created, Updated
# - URL: https://ansible-tower.example.com/api/v2/job_templates/1/launch/
# - Headers: {"Authorization": "Bearer tower-token"}
#
# 3. Webhook: Update DNS when IP changes
# - Object Type: IP Address
# - Event: Updated
# - URL: https://dns-api.example.com/update
# - Conditions: {"status": "active"}
#
# สร้างที่: Admin > Integrations > Webhooks
Backup และ Migration
Database Backup
# NetBox ใช้ PostgreSQL เป็น Database
# Backup database
docker compose exec postgres pg_dump -U netbox netbox > netbox_backup.sql
# Backup media files (uploaded images, etc.)
docker compose cp netbox:/opt/netbox/netbox/media ./media_backup/
# Automated daily backup script
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR=/backups/netbox
# Database
docker compose exec -T postgres pg_dump -U netbox netbox | gzip > ${BACKUP_DIR}/db_${DATE}.sql.gz
# Media
tar czf ${BACKUP_DIR}/media_${DATE}.tar.gz $(docker compose exec -T netbox ls /opt/netbox/netbox/media/)
# Keep last 30 days
find ${BACKUP_DIR} -type f -mtime +30 -delete
echo "Backup completed: ${DATE}"
Migration
# ย้าย NetBox ไปเซิร์ฟเวอร์ใหม่:
# 1. Export จากเซิร์ฟเวอร์เก่า
docker compose exec -T postgres pg_dump -U netbox netbox > netbox_full.sql
docker compose cp netbox:/opt/netbox/netbox/media ./media_export/
# 2. ใน Server ใหม่: ติดตั้ง NetBox Docker ตามปกติ
# 3. Import database
docker compose exec -T postgres psql -U netbox netbox < netbox_full.sql
# 4. Copy media files
docker compose cp ./media_export/ netbox:/opt/netbox/netbox/media/
# 5. Run migrations (ถ้า version ต่างกัน)
docker compose exec netbox python manage.py migrate
# 6. Restart
docker compose restart
Best Practices สำหรับ NetBox
- Data Governance: กำหนดว่าใครมีสิทธิ์เพิ่ม/แก้ไขข้อมูลอะไร ใช้ RBAC ของ NetBox
- Naming Convention: กำหนด Standard สำหรับ Device names, Rack names, Prefix descriptions
- Automation First: ทุกการเปลี่ยนแปลง Infrastructure ควรเริ่มจาก NetBox แล้ว Automation จัดการ
- Regular Audits: ตรวจสอบข้อมูลใน NetBox กับอุปกรณ์จริงเป็นประจำ
- Change Management: ใช้ NetBox Journal/Changelog เพื่อบันทึกเหตุผลของการเปลี่ยนแปลง
- Start Small: เริ่มจาก Sites + Devices + IP แล้วค่อยๆ เพิ่มข้อมูลอื่น
- Device Type Library: ใช้ Community Device Type Library (github.com/netbox-community/devicetype-library) แทนการสร้างเอง
สรุป
NetBox เป็นเครื่องมือที่ Network Engineer ทุกคนควรรู้จักและใช้งานในปี 2026 ไม่ว่าจะเป็นองค์กรเล็กหรือใหญ่ การมี Source of Truth สำหรับ Network Infrastructure ช่วยลดข้อผิดพลาดจากการจดบันทึกใน Spreadsheet ทำให้ Automation ทำงานได้ถูกต้อง และทำให้ทีมทุกคนเห็นภาพรวมของ Infrastructure ได้อย่างชัดเจน
เริ่มต้นด้วยการ Docker Compose ติดตั้ง NetBox เพิ่ม Sites, Racks, Devices ขององค์กร แล้วค่อยๆ เพิ่ม IPAM data จากนั้นเชื่อมต่อกับ Ansible เพื่อใช้ NetBox เป็น Dynamic Inventory คุณจะเห็นทันทีว่า NetBox เปลี่ยนวิธีจัดการ Network Infrastructure ของคุณไปอย่างสิ้นเชิง