參考
一. 原理
目前市場上稍微有些實力的交換機廠商,均支持自動化的批量開局部署,雖然具體實現有些區別,但實現原理基本一致。如下圖:
- 1.交換機加電啟動,檢測到沒有配置文件則進入自動化配置模式,從dhcp服務器獲取地址;
- 2.交換機從dhcp的option信息中獲取文件服務器相關信息(文件服務器地址,需要下載的文件信息等);
- 3.交換機從文件服務器下載image,patch,配置文件等;
- 4.交換機加載配置(是否重啟根據廠商不同而不同),完成配置。
二. 設置DHCP & TFTP 服務器
2.1 DHCP服務器
2.1.1 iptables
# dhcp server: udp 67
# dhcp client: udp 68
vim /etc/sysconfig/iptables
-A INPUT -p udp -m state --state NEW -m udp --dport 67 -j ACCEPT
-A INPUT -p udp -m state --state NEW -m udp --dport 68 -j ACCEPT
# 重啟服務
service iptables restart
2.1.2 安裝配置
# 安裝
yum install dhcp -y
# 范例:/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
cat /etc/dhcp/dhcpd.conf
ddns-update-style none; # interim為DNS互動更新模式,ad-hoc為特殊DNS更新模式,none為不支持動態更新模式
ignore client-updates; # 忽略客戶端更新
# option domain-name "centos.org"; # 默認搜索域
# option domain-name-servers 100.64.191.11;
# option ntp-servers 100.64.191.25;
option time-offset -18000; # Eastern Standard Time
default-lease-time 21600; # The default is 43200 seconds
max-lease-time 43200; # If not defined, the default maximum lease time is 86400
#allow booting; # The booting flag is used to tell dhcpd whether or not to respond to queries from a particular client. This keyword only has meaning when it appears in a host declaration. By default, booting is allowed, but if it is disabled for a particular client, then that client will not be able to get an address from the DHCP server.
#allow bootp; # The bootp flag is used to tell dhcpd whether or not to respond to bootp queries. Bootp queries are allowed by default.
option tftp-server-address code 150 = ip-address; # isc-dhcp-server中並沒有"tftp-server-address"的option參數,需要提起指定
subnet 100.64.191.0 netmask 255.255.255.0 {
range 100.64.191.101 100.64.191.200;
# option subnet-mask 255.255.255.0;
option routers 100.64.191.1; # 如果在二層網絡下操作,是否設置網關並不重要
# next-server 100.64.191.10; # 一般操作系統的自動安裝使用option66指向tftp服務器
# filename "/pxelinux.0"; # tftp根目錄下的啟動引導文件
option tftp-server-address 100.64.191.10; # 一般交換機的自動配置使用option150指向tftp服務器,務必提前確認;另外centec交換機固定讀取 "smartdeploy.xml" 文件,不用特別指定並傳遞中間文件名
# we want the nameserver to appear at a fixed address
# host ns {
# hardware ethernet 00:1C:25:80:F4:58; # 特殊設備網卡mac
# fixed-address 100.64.191.11;
#}
}
# 啟動
systemctl enable dhcpd ; systemctl restart dhcpd
# client獲取地址后,可在server端查看租約
cat /var/lib/dhcpd/dhcpd.leases
2.2 TFTP服務器
2.2.1 iptables
# tcftp server: udp 69
vim /etc/sysconfig/iptables
-A INPUT -p udp -m state --state NEW -m udp --dport 69 -j ACCEPT
# 重啟服務
service iptables restart
2.2.2 安裝配置
# 安裝
yum install xinetd tftp-server tftp -y
# 創建tftp根目錄;
# 如果有權限問題,可執行"chmod 777 /tftp"
mkdir -p /tftp
# 配置,默認無tftp配置文件;
cat << EOF > /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer
# protocol. The tftp protocol is often used to boot diskless
# workstations, download configuration files to network-aware printers,
# and to start the installation process for some operating systems.
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -u nobody -s /tftp # -s指定tftp根目錄
disable = no # 默認yes
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
# 啟動服務
systemctl enable xinetd ; systemctl restart xinetd
三. Centec SmartConfig
Centec交換機的初始化配置特性為"SmartConfig"。
SmartConfig流程梳理:
- 1.在設置啟用SmartConfig功能后,交換機啟動時如果發現沒有startup-config文件,則進入"smart-config"模式;
- 2.管理網口從DHCP Server獲取IP地址與TFTP Server地址后,便會從獲取到的TFTP Server上讀取xml格式的中間文件smartdeploy.xml;
- 3.設備匹配smartdeploy.xml中的MAC\Product-id\SN等項,匹配任意一項,便下載匹配項對應的image文件,image文件下載成功后,再下載匹配到的配置文件;
- 4.image與配置文件都下載成功了,smart-config會自動指定設備下次啟動的image與配置文件為剛下載的兩個文件,然后自動重啟(只下載配置文件時不重啟);
- 5.啟動完成后,E580使用的image及配置即在smartdeploy.xml中指定的,smart-config流程結束。
SmartConfig具體實現步驟如下:
3.1 (option) 檢查smart-config是否處於使能狀態
# "initial-switch-deployment"顯示為"on"為使能狀態,"off"為關閉狀態;
# 如果為關閉狀態,在配置模式下使用"smart-config initial-switch-deployment"命令可打開smart-config功能,此命令重啟設備后生效;
# 默認情況下,smart-config處於使能狀態
E580# show smart-config config
Smart-Config config:
initial-switch-deployment: on
hostname-prefix: on
Send log message to console: on
3.2 編輯中間文件與配置文件
3.2.1 中間文件smartdeploy.xml范例
- SmartConfig特性中間文件為xml格式,固定命名"smartdeploy.xml";
- 以下xml文件范例對應1台交換機,如果有多台交換機,則需要在smartdeploy.xml文件中寫多段類似的控制信息;
cat ~/smartdeploy.xml
<SmartDeploy>
<ftype>init</ftype> # 不可修改
<hostprefix>E580</hostprefix> # 此處設置E580的hostname的前綴,完整的hastname為該前綴加上相應設備(非帶外管理口)的MAC地址的后六位,如E580的MAC地址為00:1e:08:a9:cc:6d,那么hostname為:"E580A9.CC6D"
# 此段編輯默認匹配項,就是如果有MAC\Productid\SN都不匹配的交換機,則使用這段條目里設置的image和配置文件
<defItem>
<option>disable</option> #"enable"啟用默認匹配,"disable"關閉默認匹配
<image>centecOS-e580-v6.2.26.r.bin</image> # 根據實際情況修改,如果已"disable"默認匹配,設置與否並不重要
<config>startup-config-default.conf</config> # 根據實際情況修改,如果已"disable"默認匹配,設置與否並不重要
</defItem>
# 三種匹配方式:設備MAC地址、設備Product-ID、以及設備序列號,只需任意匹配到其中一條,就可以用匹配到的條目下的image及配置啟動交換機;
# 此段為使用序列號匹配的范例,如果不需要換image,則將image字段的內容部分刪除,如"<image></image>";
# 注意事項:(1). 如果指定了更換image,下載image與配置文件后,設備會再次重啟;
# (2). 只指定配置文件,不指定更換image的情況下,為了避免設備再次重啟,可在配置文件中的管理網口的配置前面,添加“no management ip address dhcp"與"no interface vlan 1"兩句配置,具體可參考"配置文件示例"
<groups>
<Item>
<type>MAC</type> # 不用修改
<value></value> # 根據實際情況修改,如果不采用MAC匹配,建議不設置
<image></image> # 根據實際情況修改,如果不采用MAC匹配,建議不設置
<config></config> # 根據實際情況修改,如果不采用MAC匹配,建議不設置
</Item>
<Item>
<type>productid</type> # 不用修改
<value></value> # 根據實際情況修改,如果不采用Product-ID匹配,建議不設置
<image></image> # 根據實際情況修改,如果不采用Product-ID匹配,建議不設置
<config></config> # 根據實際情況修改,如果不采用Product-ID匹配,建議不設置
</Item>
<Item>
<type>SN</type> # 不用修改
<value>E163GD172018</value> # 根據實際情況修改
<image></image> # 根據實際情況修改
<config>startup-config-test.conf</config> # 根據實際情況修改
</Item>
</groups>
</SmartDeploy>
3.2.2 配置文件范例
注意事項: 只指定配置文件,不指定更換image的情況下,為了避免設備再次重啟,在配置文件中的管理網口的配置前面,添加“no management ip address dhcp"與"no interface vlan 1"兩句配置。
# 注意配置文件名與"smartdeploy.xml"中定義的config文件名一致
cat ~/startup-config-test.conf
no service password-encryption
!
username admin privilege 4 password 8 7834ed73c1ee2c999338cd31aaed88ce
!
no management ip address dhcp
no interface vlan 1
!
management ip address 100.64.191.201/24
management route add gateway 100.64.191.1
!
vlan database
vlan 10
!
interface eth-0-1
!
interface eth-0-2
!
interface eth-0-3
!
interface eth-0-4
!
interface eth-0-5
!
interface eth-0-6
!
interface eth-0-7
!
interface eth-0-8
!
interface eth-0-9
!
interface eth-0-10
!
interface eth-0-11
!
interface eth-0-12
!
interface eth-0-13
!
interface eth-0-14
!
interface eth-0-15
!
interface eth-0-16
!
interface eth-0-17
!
interface eth-0-18
!
interface eth-0-19
!
interface eth-0-20
!
interface eth-0-21
!
interface eth-0-22
!
interface eth-0-23
!
interface eth-0-24
!
interface eth-0-25
!
interface eth-0-26
!
interface eth-0-27
!
interface eth-0-28
!
interface eth-0-29
!
interface eth-0-30
!
interface eth-0-31
!
interface eth-0-32
!
interface eth-0-33
!
interface eth-0-34
!
interface eth-0-35
!
interface eth-0-36
!
interface eth-0-37
!
interface eth-0-38
!
interface eth-0-39
!
interface eth-0-40
!
interface eth-0-41
!
interface eth-0-42
!
interface eth-0-43
!
interface eth-0-44
!
interface eth-0-45
!
interface eth-0-46
!
interface eth-0-47
!
interface eth-0-48
!
interface eth-0-49
!
interface eth-0-50
!
interface eth-0-51
!
interface eth-0-52
!
line con 0
no line-password
no login
line vty 0 7
exec-timeout 35791 0
privilege level 4
transport input ssh
no line-password
login local
!
end
3.3 在TFTP Server上創建目錄
SmartConfig特性已定義從tftp服務獲取中間文件及配置文件,image等的路徑,嚴格按其預定義的路徑存放相關文件即可。
- 1.在TFTP服務器的根目錄上創建一個固定名為"smartconfig"的文件夾;
- 2.在smartconfig文件夾下級目錄再創建固定名為"conf"及"images"文件夾,將配置文件放置在conf文件夾中,將image放置在images文件夾中;
- 3.將編輯好的smartdeploy.xml放在smartconfig目錄。
# 在tftp根目錄下創建相關目錄
mkdir -p /tftp/smartconfig/{conf,image}
# 放置文件
mv ~/smartdeploy.xml /tftp/smartconfig/
mv ~/startup-config-test.conf /tftp/smartconfig/conf/
# 查看目錄結構
tree /tftp/
/tftp/
└── smartconfig
├── conf
│ └── startup-config-test.conf
├── images
└── smartdeploy.xml
3.4 配置DHCP Server
具體配置請見2.1.2 DHCP服務器安裝配置章節,注意事項:
- 1.DHCP Server需要能分發給E580帶外管理網口IP地址,務必使用帶外管理網口;
- 2.Centec交換機從DHCP Server的option 150獲取TFTP Server地址,而非一般操作系統自動部署的option 66,針對linux中開源的isc-dhcp-server,option 150需要提前定義;
- 3.保證E580的管理網口與TFTP Server能通信;
- 4.如果DHCP&TFTP Server與被部署的交換機在不同subnet,則中間網絡設備需要具備DHCP中繼功能與三層路由功能。
3.5 觸發smart-config工作
觸發SmartConfig工作的必要條件是:
- 1.flash下沒有startup-config.conf文件,默認未保存過配置的新機沒有配置文件;
# 如果測試或者已保存過配置的設備,可在特權模式下刪除startup-config.conf文件 E580# delete flash:/startup-config.conf
- 2.重啟,啟動完成后,SmartConfig功能開始工作。
3.6 Troubleshooting
如果smart-config未生效,可通過如下方式troubleshooting:
設備在沒有startup-config.conf文件重啟后,smart-config會自動加載一套默認配置給設備,此默認配置中,管理網口工作在DHCP模式下,可在特權模式下使用show dhcp client verbose指令查看管理網口是否已經從DHCP Server獲取到管理IP與TFTP Server IP。
# 重點關注"Allocated IP" 與 "TFTP server addresses" 兩個參數
Switch# show dhcp client verbose
DHCP client informations:
============================================================
Management interface DHCP client information:
Current state: BOUND
Allocated IP: 100.64.191.101 255.255.255.0
Lease/renewal/rebinding: 714/71/564 seconds
Lease from 2019-04-09 15:26:57 to 2019-04-09 15:46:57
Will Renewal in 0 days 0 hours 1 minutes 11 seconds
DHCP server: 100.64.191.10
Transaction ID: 0x16d35160
Default router: 100.64.191.1
TFTP server addresses: 100.64.191.10 # 如果未獲取到TFTP地址,則不顯示此行
Client ID: switch-001e:08a9:cc:6c-management # 管理網口MAC不同於設備板卡的MAC地址