1,550 瀏覽數

安裝 CoreOS – 設定 Static IP

先來看一下 cloud-config.yaml 關於 設定 Static IP 設定檔.其他設定請參考 http://benjr.tw/96511

#cloud-config
hostname: coreos1
ssh_authorized_keys:
  - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5wZYPD/mBs+9O9CrUxdg9kpOus24VrMuNncdt4BRc4iF5npV90HYe5j/y3IG6+2MRbAb2edyf/FUcaJHN/V+i123456yuqyAT2rv9T0eB2+wpmYCUQzqZscJP2uLK8jMhezKWS0l7X5CgJf+d17VooS6CADR9MyTbku3upKp5yEnsCfB+pBLGdrqCUTnGHPfJcLTBIvuMriz/kae0azxcderfbw7YWR8oKdWjKYKlznnBmH6VYFcgv/jSXbRbdZjKNSXIm2xIj6TIIJmo6sWhptcGohi467ODyrzCDioXD1MsYx6ImTMcY5mzL2RDePAW7CM4gWIMaIxDeL5e10SX ben@appledeAir

coreos:
  units:
    - name: etcd2.service
      command: start
    - name: systemd-networkd.service
      command: stop
    - name: 00-eth0.network
      runtime: true
      content: |
        [Match]
        Name=ens32

        [Network]
        Address=172.16.15.21/24
        Gateway=172.16.15.2
        DNS=168.95.1.1
    - name: systemd-networkd.service
      command: start

hostname , ssh_authorized_keys 與 units 前面連結都介紹過了,這次把重點放在如何設定固定 IP 上.

在 00-eth0.network 前後有多增加關於 systemd-networkd.service stop , start 的設定,之前沒有增加這設定時 , CoreOS 的 Static IP 有時會跑掉變成 Global secondary .

name: 00-eth0.network

名稱可以自訂,如果有多個自訂網路裝置設定,會依據名稱的先後來決定設定.

[Match]

  • Name=ens32
    這邊就是實際的網路裝置名稱,現在網路裝置名稱是依據 net.ifnames – Predictable Network Interface 的命名規則.

    裝置名稱由 裝置類型 + 裝置位置+數字 組成:
    裝置類型

    1. en for Ethernet,
    2. wl for wireless LAN (WLAN),
    3. ww for wireless wide area network (WWAN).

    裝置位置+數字

    • o
      on-board device index number
    • s[f][d]
      hotplug slot index number
    • x
      MAC address
    • ps[f][d]
      PCI geographical location
    • ps[f][u][..][i]
      USB port number chain

    ens32 就代表 Ethernet ,hotplug slot index number 32

[Network]

這邊就可以設定關於 IP 的相關參數.

  • Address=172.16.15.31/24
  • 除了固定 IP 外還可以設定其他 Gateway, DNS 等 IP .

  • Gateway=172.16.15.2
  • DNS=168.95.1.1

CoreOS 光碟開機後就進入文字模式,直接透過指令 #coreos-install 來安裝.這次有透過 -c 來指定 cloud-init config .

core@localhost ~ $ # sudo coreos-install -d /dev/sda -C stable -c ~/cloud-config.yaml
2016/12/21 09:41:12 Checking availability of "local-file"
2016/12/21 09:41:12 Fetching user-data from datasource of type "local-file"
Downloading the signature for https://stable.release.core-os.net/amd64-usr/1185.3.0/coreos_production_image.bin.bz2...
2016-12-21 09:41:14 URL:https://stable.release.core-os.net/amd64-usr/1185.3.0/coreos_production_image.bin.bz2.sig [543/543] -> "/tmp/coreos-install.fmCj9mKD5k/coreos_production_image.bin.bz2.sig" [1]
Downloading, writing and verifying coreos_production_image.bin.bz2...
...
Success! CoreOS stable 1185.3.0 is installed on /dev/sda
core@localhost ~ $ sudo reboot

使用的參數:
-d ( DEVICE ) – Install CoreOS to the given device.
-C ( CHANNEL ) – Release channel to use (e.g. stable, beta)
-c ( CLOUD ) – Insert a cloud-init config to be executed on boot.

安裝完的 CoreOS 可以透過下面的檔案檢查目前設定.

core@coreos1 ~ $ cat /run/systemd/network/00-eth0.network
[Match]
Name=ens32

[Network]
Address=172.16.15.31/24

或是指令來觀看.

core@coreos1 ~ $ ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.15.31  netmask 255.255.255.0  broadcast 172.16.15.255
        inet6 fe80::20c:29ff:fe3b:642e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:3b:64:2e  txqueuelen 1000  (Ethernet)
        RX packets 82  bytes 9119 (8.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 74  bytes 10807 (10.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 405  bytes 32658 (31.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 405  bytes 32658 (31.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
core@coreos1 ~ $ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:3b:64:2e brd ff:ff:ff:ff:ff:ff
    inet 172.16.15.31/24 brd 172.16.15.255 scope global ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe3b:642e/64 scope link 
       valid_lft forever preferred_lft forever

如果要修改設定檔,剛剛 /run/systemd/network/00-eth0.network 是不能修改的,就算是修改了,下次開機還是會恢復成原本的設定.我們需要在 /etc/systemd/network/ 建立新的設定檔.

core@coreos1 ~ $ sudo vi /etc/systemd/network/10-static.network
[Match]
Name=ens32

[Network]
Address=172.16.15.21/24

core@coreos1 ~ $ sudo systemctl restart systemd-networkd

我透過 #systemctl restart systemd-networkd 但沒有生效,後來是重新開機才生效.

core@coreos1 ~ $ sudo reboot

如果要改成 DHCP 也是可以的 (目前也不知道為什麼這樣的設定不會生效).

core@coreos1 ~ $ sudo vi /etc/systemd/network/10-static.network
[Match]
Name=ens32

[Network]
DHCP=yes

core@coreos1 ~ $ sudo systemctl restart systemd-networkd

事後要修改 DNS 的設定需要改 /etc/systemd/resolved.conf 檔案

core@coreos1 ~ $ sudo vi /etc/systemd/resolved.conf
[Resolve]
DNS=168.95.1.1
#FallbackDNS=
#Domains=
#LLMNR=yes
#DNSSEC=allow-downgrade
#Cache=yes

關於其他網路設定的檔案位置.

  • The system network directory /usr/lib/systemd/network
  • The volatile runtime network directory /run/systemd/network
  • The local administration network directory /etc/systemd/network
  • /etc/nsswitch.conf

更多關於 CoreOS Network 設定請參考官方網頁說明 https://coreos.com/os/docs/latest/network-config-with-networkd.html

2 Replies to “安裝 CoreOS – 設定 Static IP”

  1. 自動參照通知: CoreOS 設定檔 – Benjr.tw

  2. 自動參照通知: 安裝 CoreOS – 設定 etcd2 – Benjr.tw

發表迴響