所有文章

Centos7增加子IP

假设你的 Linux 主机上运行了多了 Docker 容器,而这些容器都需要映射同样的端口号到物理机,以便向外提供服务。这时你可能想到把这些容器的端口映给物理机的不同端口上,如下:

docker run \
-p 8080:8080 \
-p 8081:8081 \
-p 8082:8080 \
...

但这样很容易记不清每个端口对应的服务,本文将介绍一种更优雅的方式解决这个问题。

基本原理

通过修改 Linux 配置文件,为 Linux 主机创建多个子 IP ,为每个容器中的端口映射到不同的 IP 上即可。

实现步骤

查看当前网卡状态

[suer@ser0 ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp8s0f0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 70:e2:84:0e:5a:87 brd ff:ff:ff:ff:ff:ff
3: enp10s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 70:e2:84:0e:5a:86 brd ff:ff:ff:ff:ff:ff
    inet 10.100.124.231/24 brd 10.100.124.255 scope global enp10s0
       valid_lft forever preferred_lft forever
4: enp8s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 70:e2:84:0e:5a:88 brd ff:ff:ff:ff:ff:ff

我们看到第三个网卡 IP 地址为:10.100.124.231,也就是目前系统的主 IP 地址,它的名字为:enp10s0

修改网络配置文件,方法很简单,假设我要增加三个子 IP,如下:

[suer@ser0 ~]$ sudo vi /etc/sysconfig/network-scripts/ifcfg-enp10s0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=enp10s0
UUID=32fbf024-507c-4864-842d-36fec758cad7
DEVICE=enp10s0
ONBOOT=yes
IPADDR=10.100.124.231
NETMASK=255.255.255.0
GATEWAY=10.100.124.254
DNS1=10.96.1.18
DNS2=10.96.1.19
 
IPADDR1=10.100.124.111
PREFIX1=24
 
IPADDR2=10.100.124.112
PREFIX2=24
 
IPADDR3=10.100.124.113
PREFIX3=24

重启网络

sudo service network restart

查看结果

[suer@ser0 ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp8s0f0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 70:e2:84:0e:5a:87 brd ff:ff:ff:ff:ff:ff
3: enp10s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 70:e2:84:0e:5a:86 brd ff:ff:ff:ff:ff:ff
    inet 10.100.124.231/24 brd 10.100.124.255 scope global enp10s0
       valid_lft forever preferred_lft forever
    inet 10.100.124.111/24 brd 10.100.124.255 scope global secondary enp10s0
       valid_lft forever preferred_lft forever
    inet 10.100.124.112/24 brd 10.100.124.255 scope global secondary enp10s0
       valid_lft forever preferred_lft forever
    inet 10.100.124.113/24 brd 10.100.124.255 scope global secondary enp10s0
       valid_lft forever preferred_lft forever
4: enp8s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 70:e2:84:0e:5a:88 brd ff:ff:ff:ff:ff:ff

是的,就这么简单,现在不访试试从其它主机上 ping 一下这些子 IP,理论上只要能 ping 通主 IP,就能 ping 通子 IP。


编写日期:2017-02-05