Friday, June 28, 2024

Quản lý thiết bị Datacenter sử dụng Netbox

-

NetBox là giải pháp hàng đầu để lập mô hình và lập tài liệu cho các mạng hiện đại. Bằng cách kết hợp các nguyên tắc truyền thống của quản lý địa chỉ IP (IPAM) và quản lý cơ sở hạ tầng trung tâm dữ liệu (DCIM) với các API và tiện ích mở rộng mạnh mẽ, NetBox cung cấp “nguồn chân lý” lý tưởng để cung cấp năng lượng tự động hóa mạng. Có sẵn dưới dạng phần mềm mã nguồn mở theo giấy phép Apache 2.0, NetBox được hàng nghìn tổ chức trên thế giới sử dụng.

1. Cài đặt mới, không import dữ liệu cũ

Đơn giản chỉ việc clone source code của Netbox trên Github về và chạy lênh docker-compose up để triển khai nhanh Netbox.

git clone -b release https://github.com/netbox-community/netbox-docker.git
cd netbox-docker
tee docker-compose.override.yml <<EOF
version: '3.4'
services:
  netbox:
    ports:
      - 8000:8080
EOF
docker-compose pull
docker-compose up -d

Sau khi triển khai xong, bạn sẽ có các conterner như dưới.

$ docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED         STATUS                 PORTS                                                                                                               NAMES
85baa47663d6   netboxcommunity/netbox:v3.3-2.3.0   "/usr/bin/tini -- /o…"   4 minutes ago   Up 3 minutes           0.0.0.0:8000->8080/tcp, :::8000->8080/tcp                                                                           netbox-docker-netbox-1
9e3c50693b06   netboxcommunity/netbox:v3.3-2.3.0   "/usr/bin/tini -- /o…"   4 minutes ago   Up 4 minutes                                                                                                                               netbox-docker-netbox-worker-1
9103dea361cc   netboxcommunity/netbox:v3.3-2.3.0   "/usr/bin/tini -- /o…"   4 minutes ago   Up 4 minutes                                                                                                                               netbox-docker-netbox-housekeeping-1
8d4a14cbbae8   redis:7-alpine                      "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes           6379/tcp                                                                                                            netbox-docker-redis-cache-1
ef8561db79c7   postgres:14-alpine                  "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes           5432/tcp                                                                                                            netbox-docker-postgres-1
790fe4b8aa97   redis:7-alpine                      "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes           6379/tcp                                                                                                            netbox-docker-redis-1

Sẽ mất ít phút để Netbox có thể sử dụng, bạn hãy show logs lên để theo dõi quá trình triển khai Netbox. Nếu bạn thấy nội dung Unit configuration loaded successfully thì bạn đã có thể sử dụng Netbox.

$ docker logs netbox-docker-netbox-1
🧬 loaded config '/etc/netbox/config/configuration.py'
🧬 loaded config '/etc/netbox/config/extra.py'
🧬 loaded config '/etc/netbox/config/logging.py'
🧬 loaded config '/etc/netbox/config/plugins.py'
💡 Superuser Username: admin, E-Mail: admin@example.com
⚠️⚠️⚠️ WARNING: The initializers have been moved to a plugin. See release notes.
⚠️⚠️⚠️ Set environment variable 'SKIP_STARTUP_SCRIPTS' to 'true' to remove this warning.
✅ Initialisation is done.
⏳ Waiting for control socket to be created... (1/10)
2022/11/14 06:12:15 [warn] 7#7 Unit is running unprivileged, then it cannot use arbitrary user and group.
2022/11/14 06:12:15 [info] 7#7 unit 1.27.0 started
2022/11/14 06:12:15 [info] 14#14 discovery started
2022/11/14 06:12:15 [notice] 14#14 module: python 3.10.4 "/usr/lib/unit/modules/python3.10.unit.so"
2022/11/14 06:12:15 [info] 7#7 controller started
2022/11/14 06:12:15 [notice] 7#7 process 14 exited with code 0
2022/11/14 06:12:15 [info] 16#16 router started
2022/11/14 06:12:15 [info] 16#16 OpenSSL 3.0.2 15 Mar 2022, 30000020
⚙️ Applying configuration from /etc/unit/nginx-unit.json
2022/11/14 06:12:16 [info] 20#20 "netbox" prototype started
2022/11/14 06:12:16 [info] 21#21 "netbox" application started
2022/11/14 06:12:20 [info] 38#38 "netbox" application started
✅ Unit configuration loaded successfully
2022/11/14 06:12:20 [notice] 7#7 process 12 exited with code 0

Toàn bộ ứng dụng sẽ có sẵn sau vài phút. Mở URL http://0.0.0.0:8000/trong trình duyệt web. Bạn sẽ thấy trang chủ NetBox. Ở góc trên bên phải, bạn có thể đăng nhập.

Thông tin đăng nhập mặc định là:

  • Tên người dùng: quản trị viên
  • Mật khẩu: admin
  • Mã thông báo API: 0123456789abcdef0123456789abcdef01234567

Và đây là kết quả sau khi login thành công

Nếu muốn xoá tất cả các container liên quan, hãy sử dụng lệnh

$ docker-compose down
[+] Running 7/7
 ⠿ Container netbox-docker-netbox-housekeeping-1  Removed                                                                                                                                                           0.4s
 ⠿ Container netbox-docker-netbox-1               Removed                                                                                                                                                           1.1s
 ⠿ Container netbox-docker-netbox-worker-1        Removed                                                                                                                                                           1.2s
 ⠿ Container netbox-docker-redis-cache-1          Removed                                                                                                                                                           0.4s
 ⠿ Container netbox-docker-redis-1                Removed                                                                                                                                                           0.4s
 ⠿ Container netbox-docker-postgres-1             Removed                                                                                                                                                           0.5s
 ⠿ Network netbox-docker_default                  Removed                                                                                                                                                           0.1s

2. Cài đặt mới và sử dụng dữ liệu Netbox cũ

Đầu tiên bạn hãy backup database ở server Netbox cũ bằng lệnh dưới và copy netbox.tar sang server Netbox mới.

pg_dump -h localhost -U netbox -W -F t netbox > /home/netbox/netbox.tar

Tạo thư mục chứa manifest

mkdir -p /home/netbox/

Trên server Netbox mưới hãy tạo file /home/netbox/netbox.yaml

---
version: '3.4'
services:
  netbox:
    image: lscr.io/linuxserver/netbox:latest
    container_name: netbox
    restart: always
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Ho_Chi_Minh
      - SUPERUSER_EMAILhoanghd164@gmail.com
      - SUPERUSER_PASSWORD=Hoanghd164
      - ALLOWED_HOST=*
      - DB_NAME=netbox
      - DB_USER=netbox
      - DB_PASSWORD=Hoanghd164
      - DB_HOST=192.168.13.205
      - DB_PORT=5432
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    ports:
      - 8000:8000
    restart: unless-stopped
    links:
      - "redis"
      - "postgres"

  redis:
    container_name: redis
    image: redis
    restart: always

  postgres:
    image: postgres
    container_name: postgres
    restart: always
    environment:
      - POSTGRES_USER=netbox
      - POSTGRES_DB=netbox
      - POSTGRES_PASSWORD=Hoanghd164
    ports:
      - 5432:5432

Tạo file /home/netbox/postgres.yaml

version: '3.4'
services:
  postgres:
    image: postgres
    container_name: postgres
    restart: always
    environment:
      - POSTGRES_USER=netbox
      - POSTGRES_DB=netbox
      - POSTGRES_PASSWORD=Hoanghd164
    ports:
      - 5432:5432

Sau khi tạo xong các thư mục và 2 file yaml, bạn sẽ có cây thư mục dạng như dưới

/home/
├── better-top
├── memory-top-script
├── netbox
    ├── netbox.yaml
    └── postgres.yaml

Đầu tiên hãy triển khai file postgres.yaml trước

$ docker-compose -f /home/netbox/postgres.yaml up -d
[+] Running 2/2
 ⠿ Network netbox_default  Created                                                                                                                                                                                  0.0s
 ⠿ Container postgres      Started 

Kết quả sau khi triển khai xong Postgres

$ docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS                 PORTS                                                                                                               NAMES
3b771128ad82   postgres                  "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes          0.0.0.0:5432->5432/tcp, :::5432->5432/tcp                                                                           postgres

Kiểm tra xem port 5432 của Postgres đã listen chưa

$ netstat -tlnp | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      8741/docker-proxy   
tcp6       0      0 :::5432                 :::*                    LISTEN      8748/docker-proxy

Để có thể import được database bạn cần cài đặt postgresql-client lên máy mà bạn đang chứa file backup database (ở ví dụ của mình là file netbox.tar). Lưu ý bạn phải cài bản postgresql-client giống với phiên bản Postgres ở server Netbox cũ, ở server Nexbox cũ của mình đang sử dụng Postgres version 12.

$ psql --version
psql (PostgreSQL) 12.12 (Ubuntu 12.12-0ubuntu0.20.04.1)

Hãy cài đặt postgresql-client 12 bằng lệnh dưới.

curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee  /etc/apt/sources.list.d/pgdg.list
sudo apt update
sudo apt -y install postgresql-client-12

Sau khi cài đặt xong postgresql-client 12 bạn có thể verify lại bằng lệnh dưới.

$ psql --version
psql (PostgreSQL) 12.13 (Ubuntu 12.13-1.pgdg18.04+1)

Hoặc

$ which pg_restore
/usr/bin/pg_restore

Nếu bạn sử dụng lệch phiên bản, khi chạy lệnh import bạn có thể sẽ gặp lỗi sau.

$ pg_restore -h localhost -U netbox -W -F t -d netbox /home/netbox/netbox.tar 
pg_restore: [archiver] unsupported version (1.14) in file header

Nếu 5432 đã listen, bạn hãy import database cũ vào bằng lệnh dưới.

$ pg_restore -h localhost -U netbox -W -F t -d netbox /home/netbox/netbox.tar 
Password:

Sau khi import database xong, hãy triển khai file netbox.yaml

$ docker-compose -f /home/netbox/netbox.yaml up -d
[+] Running 3/3
 ⠿ Container redis     Started                                                                                                                                                                                      1.2s
 ⠿ Container postgres  Running                                                                                                                                                                                      0.0s
 ⠿ Container netbox    Started                                                                                                                                                                                      1.8s

Kiểm tra lại trạng thái của các container.

$ docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED          STATUS                 PORTS                                                                                                               NAMES
b8eb50a4648d   lscr.io/linuxserver/netbox:latest   "/init"                  29 minutes ago   Up 29 minutes          0.0.0.0:8000->8000/tcp, :::8000->8000/tcp                                                                           netbox
9fa4f66a38ab   redis                               "docker-entrypoint.s…"   3 minutes ago    Up 3 minutes           6379/tcp                                                                                                            redis
77ba3e7cf341   postgres                            "docker-entrypoint.s…"   3 minutes ago    Up 3 minutes           0.0.0.0:5432->5432/tcp, :::5432->5432/tcp                                                                           postgres

Bạn có thể theo dõi logs triển khai bằng lệnh dưới

$ docker logs netbox
[custom-init] No custom services found, skipping...
[migrations] started
[migrations] no migrations found

-------------------------------------
          _         ()
         | |  ___   _    __
         | | / __| | |  /  \
         | | \__ \ | | | () |
         |_| |___/ |_|  \__/


Brought to you by linuxserver.io
-------------------------------------

To support LSIO projects visit:
https://www.linuxserver.io/donate/
-------------------------------------
GID/UID
-------------------------------------

User uid:    1000
User gid:    1000
-------------------------------------

Operations to perform:
  Apply all migrations: admin, auth, circuits, contenttypes, dcim, django_rq, extras, ipam, sessions, social_django, taggit, tenancy, users, virtualization, wireless
Running migrations:
  Applying tenancy.0002_tenant_ordering... OK
  Applying extras.0060_customlink_button_class... OK
  Applying extras.0061_extras_change_logging... OK
  Applying extras.0062_clear_secrets_changelog... OK
  Applying tenancy.0003_contacts... OK
  Applying tenancy.0004_extend_tag_support... OK
  Applying ipam.0049_prefix_mark_utilized... OK
  Applying ipam.0050_iprange... OK
  Applying ipam.0051_extend_tag_support... OK
  Applying extras.0063_webhook_conditions... OK
  . . . .
  Applying virtualization.0032_virtualmachine_update_sites... OK
  Applying wireless.0002_standardize_id_fields... OK
  Applying wireless.0003_created_datetimefield... OK
  Applying wireless.0004_wireless_tenancy... OK
  Applying wireless.0005_wirelesslink_interface_types... OK
[custom-init] No custom files found, skipping...
[uWSGI] getting INI configuration from uwsgi.ini
[uwsgi-static] added mapping for /static => static
*** Starting uWSGI 2.0.20 (64bit) on [Mon Nov 14 11:51:09 2022] ***
compiled with version: 11.2.1 20220219 on 21 December 2021 13:20:05
os: Linux-4.15.0-194-generic #205-Ubuntu SMP Fri Sep 16 19:49:27 UTC 2022
nodename: 9dbc247e9026
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 16
current working directory: /app/netbox/netbox
detected binary path: /usr/sbin/uwsgi
your memory page size is 4096 bytes
detected max file descriptor number: 1048576
building mime-types dictionary from file /etc/mime.types...1323 entry found
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
Python version: 3.10.8 (main, Nov 10 2022, 12:59:17) [GCC 11.2.1 20220219]
[ls.io-init] done.
Python main interpreter initialized at 0x7feac3d6ab40
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 203184 bytes (198 KB) for 1 cores
*** Operational MODE: single process ***
running "exec:/usr/bin/python3 ./manage.py collectstatic --noinput" (pre app)...

256 static files copied to '/app/netbox/netbox/static'.
running "exec:/usr/bin/python3 ./manage.py remove_stale_contenttypes --no-input" (pre app)...
running "exec:/usr/bin/python3 ./manage.py clearsessions" (pre app)...
WSGI app 0 (mountpoint='') ready in 3 seconds on interpreter 0x7feac3d6ab40 pid: 160 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 160)
spawned uWSGI worker 1 (pid: 184, cores: 1)
[uwsgi-daemons] spawning "/usr/bin/python3 ./manage.py rqworker" (uid: 1000 gid: 1000)

Toàn bộ ứng dụng sẽ có sẵn sau vài phút. Mở URL http://0.0.0.0:8000/trong trình duyệt web. Bạn sẽ thấy trang chủ NetBox. Ở góc trên bên phải, bạn có thể đăng nhập. Kết quả cho thấy dữ liệu bên Nexbox cũ đã được import vào đầy đủ.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,956FansLike
256FollowersFollow
223SubscribersSubscribe
spot_img

Related Stories