1. 簡介
kea是下一代的dhcp技術,也是ISC開發的。它支持ipv4和ipv6的地址分配,並且還可以支持動態dns更新。kea從2014年初開始研發,也算是比較新的技術了,並且現在也在更新中。

現在工作中遇到一些問題。新到貨的服務器要配置帶外ip,之前使用老的DHCP方法根據mac地址綁定IP,但是這樣做總感覺不太好。
第一、每個機房都有一個配置文件,管理起來不方便;
第二、當我需要查詢綁定的結果時,查詢起來也不方便;
第三、配置文件畢竟是文件格式,安全性不太好保障。
因為遇到了上面的種種問題,我就想,有沒有什么方法可以把mac與ip的記錄放到數據庫中。這樣更安全可靠好管理,然后在凱哥的指點下,學會了kea這個dhcp服務。
感覺非常好,唯一不足的就是有些很實用的功能需要花錢購買,還貴,還是dollar。
1.1 支持的平台
根據官方的說法,他支持的平台比較多,例如Red Hat Enterprise Linux, CentOS, Fedora 和 FreeBSD等。現在比較新的Kea版本是Kea 1.4.0 。在很多系統上測試都沒問題。並且centos上安裝epel源,可以直接yum安裝kea(yum install kea),只不過版本比較低。
2. 安裝
這里的安裝過程,我就按照我的方法來講了,有興趣多了解的同學可以去官網查看。
2.1 安裝數據庫
[root@test01 ~]# cat /etc/centos-release CentOS Linux release 7.4.1708 (Core) [root@test01 ~]# yum install mariadb mariadb-server -y [root@test01 ~]# systemctl start mariadb.service [root@test01 ~]# systemctl enable mariadb.service Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. [root@test01 ~]# mysql_secure_installation
2.2 安裝必要的依賴
[root@test01 ~]# yum install gcc gcc-c++ openssl-devel mariadb-devel libtool automake autoconf -y
2.3 編譯安裝必要的依賴
安裝Boost C++庫,要求版本在1.57以上,不然可能工作的不太好。
這個安裝可能比較慢,要有耐心。
[root@test01 ~]# wget https://dl.bintray.com/boostorg/release/1.65.1/source/boost_1_65_1.tar.gz [root@test01 ~]# tar -zxvf boost_1_65_1.tar.gz [root@test01 ~]# cd boost_1_65_1/ [root@test01 boost_1_65_1]# ./bootstrap.sh [root@test01 boost_1_65_1]# ./b2 install
安裝log4cplus,要求版本在1.0.3以上。
[root@test01 ~]# wget https://jaist.dl.sourceforge.net/project/log4cplus/log4cplus-stable/2.0.1/log4cplus-2.0.1.tar.gz [root@test01 ~]# tar -zxvf log4cplus-2.0.1.tar.gz [root@test01 ~]# cd log4cplus-2.0.1/ [root@test01 log4cplus-2.0.1]# ./configure [root@test01 log4cplus-2.0.1]# make [root@test01 log4cplus-2.0.1]# make install
2.4 安裝kea
這里也非常慢,要有耐心。
[root@test01 ~]# wget http://ftp.isc.org/isc/kea/1.4.0/kea-1.4.0.tar.gz [root@test01 ~]# tar -zxvf kea-1.4.0.tar.gz [root@test01 ~]# cd kea-1.4.0/ [root@test01 kea-1.4.0]# ./configure --with-mysql [root@test01 kea-1.4.0]# make [root@test01 kea-1.4.0]# make install
3. 配置
3.1 簡單的配置驗證
初始化數據庫
[root@test01 kea-1.4.0]# mysql -u root -p Enter password: ... MariaDB [(none)]> CREATE DATABASE kea; Query OK, 1 row affected (0.01 sec) MariaDB [(none)]> CREATE USER 'kea'@'%' IDENTIFIED BY 'kea'; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> GRANT ALL ON kea.* TO 'kea'@'%'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> quit Bye [root@test01 kea-1.4.0]# kea-admin lease-init mysql -u kea -p kea -n kea Checking if there is a database initialized already. Please ignore errors. Initializing database using script /usr/local/share/kea/scripts/mysql/dhcpdb_create.mysql mysql returned status code 0 Lease DB version reported after initialization: 6.0
因為我的測試環境沒有ipv6,所以去掉ipv6服務的啟動。
[root@test01 ~]# cd /usr/local/etc/kea/ [root@test01 kea]# vim keactrl.conf dhcp6=no
配置dhcpv4的網絡端口。
[root@test01 kea]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:9b:7b:dc brd ff:ff:ff:ff:ff:ff inet 192.168.1.30/24 brd 192.168.1.255 scope global ens33 valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:9b:7b:e6 brd ff:ff:ff:ff:ff:ff inet 192.168.212.1/24 brd 192.168.212.255 scope global ens37 valid_lft forever preferred_lft forever [root@test01 kea]# vim kea-dhcp4.conf "interfaces-config": { "interfaces": ["ens37"] },
配置IP地址段
[root@test01 kea]# vim kea-dhcp4.conf "subnet4": [ { "subnet": "192.168.212.0/24", "pools": [ { "pool": "192.168.212.100 - 192.168.212.200" } ], "option-data": [ { "name": "routers", "data": "192.168.212.201" } ] } ]
啟動kea服務並測試
[root@test01 kea]# keactrl start
INFO/keactrl: Starting /usr/local/sbin/kea-dhcp4 -c /usr/local/etc/kea/kea-dhcp4.conf
INFO/keactrl: kea-ctrl-agent appears to be running, see: PID 97955, PID file: /usr/local/var/kea/kea-ctrl-agent.kea-ctrl-agent.pid.
[root@test01 kea]# keactrl status
DHCPv4 server: active
DHCPv6 server: inactive
DHCP DDNS: inactive
Control Agent: active
Kea DHCPv4 configuration file: /usr/local/etc/kea/kea-dhcp4.conf
Kea DHCPv6 configuration file: /usr/local/etc/kea/kea-dhcp6.conf
Kea DHCP DDNS configuration file: /usr/local/etc/kea/kea-dhcp-ddns.conf
Kea Control Agent configuration file: /usr/local/etc/kea/kea-ctrl-agent.conf
keactrl configuration file: /usr/local/etc/kea/keactrl.conf
客戶端進行測試
[root@test01 ~]# dhclient -v ens33 Internet Systems Consortium DHCP Client 4.2.5 Copyright 2004-2013 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/ens33/00:0c:29:f9:21:91 Sending on LPF/ens33/00:0c:29:f9:21:91 Sending on Socket/fallback DHCPDISCOVER on ens33 to 255.255.255.255 port 67 interval 6 (xid=0x62beba25) DHCPREQUEST on ens33 to 255.255.255.255 port 67 (xid=0x62beba25) DHCPOFFER from 192.168.212.1 DHCPACK from 192.168.212.1 (xid=0x62beba25) bound to 192.168.212.100 -- renewal in 877 seconds. [root@test01 ~]# ip r default via 192.168.212.201 dev ens33 192.168.128.0/24 dev ens37 proto kernel scope link src 192.168.128.128 192.168.212.0/24 dev ens33 proto kernel scope link src 192.168.212.100 [root@test01 ~]#
查看kea的日志
[root@test01 kea]# tail -n2 /usr/local/var/log/kea-dhcp4.log 2018-07-07 00:37:53.599 INFO [kea-dhcp4.leases/97980] DHCP4_LEASE_ADVERT [hwtype=1 00:0c:29:f9:21:91], cid=[no info], tid=0x25babe62: lease 192.168.212.100 will be advertised 2018-07-07 00:37:53.600 INFO [kea-dhcp4.leases/97980] DHCP4_LEASE_ALLOC [hwtype=1 00:0c:29:f9:21:91], cid=[no info], tid=0x25babe62: lease 192.168.212.100 has been allocated
至此,基本的功能已經完成
3.2 數據庫綁定ip
配置數據庫的連接地址
[root@test01 kea]# pwd /usr/local/etc/kea [root@test01 kea]# vim kea-dhcp4.conf "hosts-database": { "type": "mysql", "name": "kea", "user": "kea", "password": "kea", "host": "192.168.1.30", "port": 3306 },
將mac和ip綁定的配置插到數據庫中
[root@test01 kea]# cat mac_ip.sql START TRANSACTION; ---要分配的IP地址 SET @ipv4_reservation='192.168.212.99'; ---要分配的主機名,有這個功能但是很少用 SET @hostname = 'myhost.example.org'; ---綁定的類型,一般情況是hw-address,代表mac地址 SET @identifier_type='hw-address'; ---客戶端的mac地址 SET @identifier_value='00:0C:29:F9:21:91'; ---網絡id,這個可以自己定義 SET @dhcp4_subnet_id=1; ---PXE啟動的時候配置的nextserver,一般不在這里配,但是也可以指定 SET @next_server='192.168.212.1'; ---nextserver的主機名 SET @server_hostname='server-name.example.org'; ---PXE啟動的文件,一般是pxelinux.0;如果是efi啟動模式的話,文件不一樣 SET @boot_file_name='bootfile.efi'; INSERT INTO hosts (dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, ipv4_address, hostname, dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name) VALUES (UNHEX(REPLACE(@identifier_value, ':', '')), (SELECT type FROM host_identifier_type WHERE name=@identifier_type), @dhcp4_subnet_id, INET_ATON(@ipv4_reservation), @hostname, INET_ATON(@next_server), @server_hostname, @boot_file_name); COMMIT;
[root@test01 kea]# mysql -u kea -h 192.168.212.1 -p Enter password: MariaDB [(none)]> use kea; MariaDB [kea]> source mac_ip.sql; MariaDB [kea]> SELECT -> HEX(h.dhcp_identifier) AS dhcp_identifier, -> i.name AS dhcp_identifier_name, -> h.dhcp4_subnet_id AS dhcp4_subnet_id, -> INET_NTOA(h.ipv4_address) AS ipv4_address, -> h.hostname AS hostname -> FROM -> hosts AS h -> INNER JOIN -> host_identifier_type AS i ON h.dhcp_identifier_type = i.type; +-----------------+----------------------+-----------------+----------------+--------------------+ | dhcp_identifier | dhcp_identifier_name | dhcp4_subnet_id | ipv4_address | hostname | +-----------------+----------------------+-----------------+----------------+--------------------+ | 000C29F92191 | hw-address | 1 | 192.168.212.99 | myhost.example.org | +-----------------+----------------------+-----------------+----------------+--------------