新聞中心
NMState 是一個(gè)網(wǎng)絡(luò)管理器,允許你按照聲明式方法配置主機(jī)。這意味著你通過一個(gè)北向的聲明式 API 定義所需的配置狀態(tài),這個(gè)工具通過南向的提供者provider應(yīng)用配置。

創(chuàng)新互聯(lián)建站是一家專注于網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),沅陵網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:沅陵等地區(qū)。沅陵做網(wǎng)站價(jià)格咨詢:028-86922220
目前 NMState 支持的唯一的提供者是 NetworkManager,它是為 Fedora Linux 提供網(wǎng)絡(luò)功能的主要服務(wù)。不過,NMState 的開發(fā)計(jì)劃中將逐漸增加其他提供者。
關(guān)于 NMState 的進(jìn)一步信息,請?jiān)L問其項(xiàng)目 站點(diǎn) 或 GitHub 倉庫。
配置方法:命令式與聲明式
網(wǎng)絡(luò)管理有時(shí)候是一項(xiàng)非常復(fù)雜的任務(wù),這取決于環(huán)境的規(guī)模和多樣性。在 IT 的早期,網(wǎng)絡(luò)管理依賴于網(wǎng)絡(luò)管理員在網(wǎng)絡(luò)設(shè)備上手動(dòng)執(zhí)行命令。如今,基礎(chǔ)設(shè)施即代碼Infrastructure as Code(IaC)允許以不同的方式將這些任務(wù)自動(dòng)化。z這基本上有兩種方法:命令式或聲明式。
在命令式方法中,你會(huì)定義“如何”達(dá)到所需的配置狀態(tài)。而在聲明式范式里則定義了“什么”是所需的配置狀態(tài),所以它不確定哪些步驟是必需的,也不確定它們必須以何種順序執(zhí)行。這種方法目前正在聚集更多的人員參與,你可以在目前使用的大多數(shù)管理和編排工具上找到它。
NMState:一個(gè)聲明式的工具
NMState 是一個(gè)網(wǎng)絡(luò)管理器,允許你按照聲明式方法配置主機(jī)。這意味著你通過一個(gè)北向的聲明式 API 定義所需的配置狀態(tài),這個(gè)工具通過南向的提供者provider應(yīng)用配置。
目前 NMState 支持的唯一的提供者是 NetworkManager,它是為 Fedora Linux 提供網(wǎng)絡(luò)功能的主要服務(wù)。不過,NMState 的開發(fā)計(jì)劃中將逐漸增加其他提供者。
關(guān)于 NMState 的進(jìn)一步信息,請?jiān)L問其項(xiàng)目 站點(diǎn) 或 GitHub 倉庫。
安裝
NMState 在 Fedora Linux 29+ 上可用,需要在系統(tǒng)上安裝并運(yùn)行 NetworkManager 1.26 或更高版本。下面是在 Fedora Linux 34 上的安裝情況:
$ sudo dnf -y install nmstate
...
輸出節(jié)略
...
Installed:
NetworkManager-config-server-1:1.30.4-1.fc34.noarch gobject-introspection-1.68.0-3.fc34.x86_64 nispor-1.0.1-2.fc34.x86_64 nmstate-1.0.3-2.fc34.noarch
python3-gobject-base-3.40.1-1.fc34.x86_64 python3-libnmstate-1.0.3-2.fc34.noarch python3-nispor-1.0.1-2.fc34.noarch python3-varlink-30.3.1-2.fc34.noarch
Complete!
這樣,你可以使用 nmstatectl 作為 NMState 的命令行工具。請參考 nmstatectl -help 或 man nmstatectl 以了解關(guān)于這個(gè)工具的進(jìn)一步信息。
使用 NMstate
首先要檢查系統(tǒng)中安裝的 NMState 版本:
$ nmstatectl version1.0.3
檢查一個(gè)網(wǎng)絡(luò)接口的當(dāng)前配置,例如 eth0 的配置:
$ nmstatectl show eth0
2021-06-29 10:28:21,530 root DEBUG NetworkManager version 1.30.4
2021-06-29 10:28:21,531 root DEBUG Async action: Retrieve applied config: ethernet eth0 started
2021-06-29 10:28:21,531 root DEBUG Async action: Retrieve applied config: ethernet eth1 started
2021-06-29 10:28:21,532 root DEBUG Async action: Retrieve applied config: ethernet eth0 finished
2021-06-29 10:28:21,533 root DEBUG Async action: Retrieve applied config: ethernet eth1 finished
---
dns-resolver:
config: {}
running:
search: []
server:
- 192.168.122.1
route-rules:
config: []
routes:
config: []
running:
- destination: fe80::/64
metric: 100
next-hop-address: ''
next-hop-interface: eth0
table-id: 254
- destination: 0.0.0.0/0
metric: 100
next-hop-address: 192.168.122.1
next-hop-interface: eth0
table-id: 254
- destination: 192.168.122.0/24
metric: 100
next-hop-address: ''
next-hop-interface: eth0
table-id: 254
interfaces:
- name: eth0
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.238
prefix-length: 24
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
dhcp: true
ipv6:
enabled: true
address:
- ip: fe80::c3c9:c4f9:75b1:a570
prefix-length: 64
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
autoconf: true
dhcp: true
lldp:
enabled: false
mac-address: 52:54:00:91:E4:4E
mtu: 1500
正如你在上面看到的,這個(gè)網(wǎng)絡(luò)配置顯示了四個(gè)主要部分:
-
dns-resolver:這部分是這個(gè)接口的名字服務(wù)器配置。
-
route-rules:它說明了路由規(guī)則。
-
routes:它包括動(dòng)態(tài)和靜態(tài)路由。
-
interfaces:這部分描述了 ipv4 和 ipv6 設(shè)置。
修改配置
你可以在兩種模式下修改所需的配置狀態(tài):
-
交互式:通過
nmstatectl edit編輯接口配置。這個(gè)命令調(diào)用環(huán)境變量
EDITOR定義的文本編輯器,因此可以用 yaml 格式編輯網(wǎng)絡(luò)狀態(tài)。完成編輯后,NMState 將應(yīng)用新的網(wǎng)絡(luò)配置,除非有語法錯(cuò)誤。
-
基于文件的:使用
nmstatectl apply應(yīng)用接口配置,它從先前創(chuàng)建的 yaml 或 json 文件中導(dǎo)入一個(gè)所需的配置狀態(tài)。
下面幾節(jié)告訴你如何使用 NMState 來改變網(wǎng)絡(luò)配置。這些改變可能會(huì)對系統(tǒng)造成破壞,所以建議在測試系統(tǒng)或客戶虛擬機(jī)上執(zhí)行這些任務(wù),直到你對 NMState 有更好的理解。
這里使用的測試系統(tǒng)有兩個(gè)以太網(wǎng)接口,eth0 和 eth1:
$ ip -br -4 alo UNKNOWN 127.0.0.1/8eth0 UP 192.168.122.238/24eth1 UP 192.168.122.108/24
互動(dòng)配置模式的例子
使用 nmstatectl edit 命令將 eth0 接口的 MTU 改為 9000 字節(jié),如下所示:
$ sudo nmstatectl edit eth0
---
dns-resolver:
config: {}
running:
search: []
server:
- 192.168.122.1
route-rules:
config: []
routes:
config: []
running:
- destination: fe80::/64
metric: 100
next-hop-address: ''
next-hop-interface: eth0
table-id: 254
- destination: 0.0.0.0/0
metric: 100
next-hop-address: 192.168.122.1
next-hop-interface: eth0
table-id: 254
- destination: 192.168.122.0/24
metric: 100
next-hop-address: ''
next-hop-interface: eth0
table-id: 254
interfaces:
- name: eth0
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.123
prefix-length: 24
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
dhcp: true
ipv6:
enabled: true
address:
- ip: fe80::c3c9:c4f9:75b1:a570
prefix-length: 64
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
autoconf: true
dhcp: true
lldp:
enabled: false
mac-address: 52:54:00:91:E4:4E
mtu: 9000
在保存并退出編輯器后,NMState 應(yīng)用新的網(wǎng)絡(luò)期望狀態(tài):
2021-06-29 11:29:05,726 root DEBUG Nmstate version: 1.0.3
2021-06-29 11:29:05,726 root DEBUG Applying desire state: {'dns-resolver': {'config': {}, 'running': {'search': [], 'server': ['192.168.122.1']}}, 'route-rules': {'config': []}, 'routes': {'config': [], 'running': [{'destination': 'fe80::/64', 'metric': 102, 'next-hop-address': '', 'next-hop-interface': 'eth0', 'table-id': 254}, {'destination': '0.0.0.0/0', 'metric': 102, 'next-hop-address': '192.168.122.1', 'next-hop-interface': 'eth0', 'table-id': 254}, {'destination': '192.168.122.0/24', 'metric': 102, 'next-hop-address': '', 'next-hop-interface': 'eth0', 'table-id': 254}]}, 'interfaces': [{'name': 'eth0', 'type': 'ethernet', 'state': 'up', 'ipv4': {'enabled': True, 'address': [{'ip': '192.168.122.238', 'prefix-length': 24}], 'auto-dns': True, 'auto-gateway': True, 'auto-route-table-id': 0, 'auto-routes': True, 'dhcp': True}, 'ipv6': {'enabled': True, 'address': [{'ip': 'fe80::5054:ff:fe91:e44e', 'prefix-length': 64}], 'auto-dns': True, 'auto-gateway': True, 'auto-route-table-id': 0, 'auto-routes': True, 'autoconf': True, 'dhcp': True}, 'lldp': {'enabled': False}, 'mac-address': '52:54:00:91:E4:4E', 'mtu': 9000}]}
--- output omitted ---
2021-06-29 11:29:05,760 root DEBUG Async action: Update profile uuid:2bdee700-f62b-365a-bd1d-69d9c31a9f0c iface:eth0 type:ethernet started
2021-06-29 11:29:05,792 root DEBUG Async action: Update profile uuid:2bdee700-f62b-365a-bd1d-69d9c31a9f0c iface:eth0 type:ethernet finished
現(xiàn)在,使用 ip 命令和 eth0 的配置文件來檢查 eth0 的 MTU 是不是 9000 字節(jié)。
$ ip link show eth0
2: eth0:
mtu 9000 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 52:54:00:91:e4:4e brd ff:ff:ff:ff:ff:ff altname enp1s0 $ sudo cat /etc/NetworkManager/system-connections/eth0.nmconnection [sudo] password
for admin: [connection] id=eth0 uuid=2bdee700-f62b-365a-bd1d-69d9c31a9f0c
type=ethernet interface-name=eth0 lldp=0 permissions= [ethernet] cloned-mac-address=52:54:00:91:E4:4E mac-address-blacklist= mtu=9000 [ipv4] dhcp-client-id=mac dhcp-timeout=2147483647 dns-search= method=auto [ipv6] addr-gen-mode=eui64 dhcp-duid=ll dhcp-iaid=mac dhcp-timeout=2147483647 dns-search= method=auto ra-timeout=2147483647 [proxy]
基于文件的配置模式的例子
讓我們使用基于文件的方法來設(shè)置一個(gè)新的配置狀態(tài)。這里我們禁用 eth1 接口的 IPv6 配置。
首先,創(chuàng)建一個(gè) yaml 文件來定義 eth1 接口的期望狀態(tài)。使用 nmstatectl show 來保存當(dāng)前設(shè)置,然后使用 nmstatectl edit 來禁用 IPv6。
$ nmstatectl show eth1 > eth1.yaml
$ vi eth1.yaml
---
dns-resolver:
config: {}
running:
search: []
server:
- 192.168.122.1
route-rules:
config: []
routes:
config: []
running:
- destination: fe80::/64
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
- destination: 0.0.0.0/0
metric: 101
next-hop-address: 192.168.122.1
next-hop-interface: eth1
table-id: 254
- destination: 192.168.122.0/24
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
interfaces:
- name: eth1
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.108
prefix-length: 24
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
dhcp: true
ipv6:
enabled: false
address:
- ip: fe80::5054:ff:fe3c:9b04
prefix-length: 64
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
autoconf: true
dhcp: true
lldp:
enabled: false
mac-address: 52:54:00:3C:9B:04
mtu: 1500
保存新的配置后,用它來應(yīng)用新的狀態(tài):
$ vi eth1.yaml
---
dns-resolver:
config: {}
running:
search: []
server:
- 192.168.122.1
route-rules:
config: []
routes:
config: []
running:
- destination: fe80::/64
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
- destination: 0.0.0.0/0
metric: 101
next-hop-address: 192.168.122.1
next-hop-interface: eth1
table-id: 254
- destination: 192.168.122.0/24
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
interfaces:
- name: eth1
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.110
prefix-length: 24
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
dhcp: false
ipv6:
enabled: false
lldp:
enabled: false
mac-address: 52:54:00:3C:9B:04
mtu: 1500
你可以檢查看到 eth1 接口沒有配置任何 IPv6:
$ vi eth1.yaml
---
dns-resolver:
config: {}
running:
search: []
server:
- 192.168.122.1
route-rules:
config: []
routes:
config: []
running:
- destination: fe80::/64
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
- destination: 0.0.0.0/0
metric: 101
next-hop-address: 192.168.122.1
next-hop-interface: eth1
table-id: 254
- destination: 192.168.122.0/24
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
interfaces:
- name: eth1
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.110
prefix-length: 24
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
dhcp: false
ipv6:
enabled: false
lldp:
enabled: false
mac-address: 52:54:00:3C:9B:04
mtu: 1500
臨時(shí)應(yīng)用改變
NMState 的一個(gè)有趣的功能允許你臨時(shí)配置一個(gè)期望的網(wǎng)絡(luò)狀態(tài)。如果你對這個(gè)配置感到滿意,你可以事后提交。否則,當(dāng)超時(shí)(默認(rèn)為 60 秒)過后,它將回滾。
修改前面例子中的 eth1 配置,使它有一個(gè) IPv4 靜態(tài)地址,而不是通過 DHCP 動(dòng)態(tài)獲得。
$ vi eth1.yaml
---
dns-resolver:
config: {}
running:
search: []
server:
- 192.168.122.1
route-rules:
config: []
routes:
config: []
running:
- destination: fe80::/64
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
- destination: 0.0.0.0/0
metric: 101
next-hop-address: 192.168.122.1
next-hop-interface: eth1
table-id: 254
- destination: 192.168.122.0/24
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
interfaces:
- name: eth1
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.110
prefix-length: 24
auto-dns: true
auto-gateway: true
auto-route-table-id: 0
auto-routes: true
dhcp: false
ipv6:
enabled: false
lldp:
enabled: false
mac-address: 52:54:00:3C:9B:04
mtu: 1500
現(xiàn)在,使用選項(xiàng) no-commit 臨時(shí)應(yīng)用這個(gè)配置,讓它只在 30 秒內(nèi)有效。這可以通過添加選項(xiàng) timeout 來完成。同時(shí),我們將運(yùn)行 ip -br a 命令三次,看看配置在 eth1 接口的 IPv4 地址是如何變化的,然后配置就會(huì)回滾。
$ ip -br a && sudo nmstatectl apply --no-commit --timeout 30 eth1.yaml && sleep 10 && ip -br a && sleep 25 && ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0 UP 192.168.122.238/24 fe80::5054:ff:fe91:e44e/64
eth1 UP 192.168.122.108/24
2021-06-29 17:29:18,266 root DEBUG Nmstate version: 1.0.3
2021-06-29 17:29:18,267 root DEBUG Applying desire state: {'dns-resolver': {'config': {}, 'running': {'search': [], 'server': ['192.168.122.1']}}, 'route-rules': {'config': []}, 'routes': {'config': [], 'running': [{'destination': 'fe80::/64', 'metric': 101, 'next-hop-address': '', 'next-hop-interface': 'eth1', 'table-id': 254}, {'destination': '0.0.0.0/0', 'metric': 101, 'next-hop-address': '192.168.122.1', 'next-hop-interface': 'eth1', 'table-id': 254}, {'destination': '192.168.122.0/24', 'metric': 101, 'next-hop-address': '', 'next-hop-interface': 'eth1', 'table-id': 254}]}, 'interfaces': [{'name': 'eth1', 'type': 'ethernet', 'state': 'up', 'ipv4': {'enabled': True, 'address': [{'ip': '192.168.122.110', 'prefix-length': 24}], 'dhcp': False}, 'ipv6': {'enabled': False}, 'lldp': {'enabled': False}, 'mac-address': '52:54:00:3C:9B:04', 'mtu': 1500}]}
--- output omitted ---
Desired state applied:
---
dns-resolver:
config: {}
running:
search: []
server:
- 192.168.122.1
route-rules:
config: []
routes:
config: []
running:
- destination: fe80::/64
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
- destination: 0.0.0.0/0
metric: 101
next-hop-address: 192.168.122.1
next-hop-interface: eth1
table-id: 254
- destination: 192.168.122.0/24
metric: 101
next-hop-address: ''
next-hop-interface: eth1
table-id: 254
interfaces:
- name: eth1
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.110
prefix-length: 24
dhcp: false
ipv6:
enabled: false
lldp:
enabled: false
mac-address: 52:54:00:3C:9B:04
mtu: 1500
Checkpoint: NetworkManager|/org/freedesktop/NetworkManager/Checkpoint/7
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0 UP 192.168.122.238/24 fe80::5054:ff:fe91:e44e/64
eth1 UP 192.168.122.110/24
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0 UP 192.168.122.238/24 fe80::5054:ff:fe91:e44e/64
eth1 UP 192.168.122.108/24
從上面可以看到,eth1 的 IP 地址從 192.168.122.108 暫時(shí)變成了 192.168.122.110,然后在超時(shí)結(jié)束后又回到了 192.168.122.108。
名稱欄目:Linux下安裝并使用NMState網(wǎng)絡(luò)管理器
文章URL:http://www.5511xx.com/article/ccechdp.html


咨詢
建站咨詢
