安裝 CoreOS – 設定 unit

Loading

現在來看看關於 units 設定方式.先來看一下 cloud-config.yaml 設定檔.其他設定請參考 https://benjr.tw/96511

#cloud-config
hostname: coreos1
# include one or more SSH public keys
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

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.

重開機後就可以透過 SSH 來連線, etcd2 服務也是啟動的.

units

CoreOS 採用 systemd 的方式來啟動服務,管理採用 systemctl .上面 units 的敘述表示 在開機的時候啟動 etcd2.service 這個服務.我們還可以看目前服務的狀態,主要可以分為兩區,服務狀態與服務狀態的訊息 (還可以透過參數 -l 或是 #journalctl -u etcd2 來查看所有的訊息)

core@coreos1 ~ $ sudo systemctl status etcd2
● etcd2.service - etcd2
   Loaded: loaded (/usr/lib/systemd/system/etcd2.service; enabled; vendor preset
   Active: active (running) since Thu 2017-01-05 03:10:05 UTC; 4s ago
 Main PID: 999 (etcd2)
    Tasks: 7
   Memory: 17.2M
      CPU: 164ms
   CGroup: /system.slice/etcd2.service
           └─999 /usr/bin/etcd2
Jan 05 03:10:05 coreos1 etcd2[999]: added member ce2a822cea30bfca [http://loca
Jan 05 03:10:05 coreos1 etcd2[999]: set the cluster version to 2.3 from store
Jan 05 03:10:05 coreos1 etcd2[999]: starting server... [version: 2.3.7, cluste
Jan 05 03:10:05 coreos1 systemd[1]: Started etcd2.
Jan 05 03:10:05 coreos1 etcd2[999]: ce2a822cea30bfca is starting a new electio
Jan 05 03:10:05 coreos1 etcd2[999]: ce2a822cea30bfca became candidate at term 
Jan 05 03:10:05 coreos1 etcd2[999]: ce2a822cea30bfca received vote from ce2a82
Jan 05 03:10:05 coreos1 etcd2[999]: ce2a822cea30bfca became leader at term 5
Jan 05 03:10:05 coreos1 etcd2[999]: raft.node: ce2a822cea30bfca elected leader
Jan 05 03:10:05 coreos1 etcd2[999]: published {Name:796f088d0abd4c3c8f4978f71a

關於已經安裝好的 CoreOS 可以用下面幾個指令來管理所有的服務狀態.

core@coreos1 ~ $ systemctl list-unit-files
UNIT FILE                              STATE
boot.automount                         static
.. 
session-1.scope                        transient
..
audit-rules.service                    disabled
..
lvm2-activation-early.service          generated
..  
systemd-journal-gatewayd.service       indirect
..

294 unit files listed.

最左邊的 UNIT FILE 是指查看 /usr/lib/systemd/system/ 內關於 systemd 的設定檔案 (因為我們用的參數是 list-unit-files)

右邊的是 STATE 有下面幾種狀態

  • enabled– 開機時自動被執行.
  • disabled– 開機時不被執行.
  • static– 無法 enable 需要由 enabled 的服務 daemon 來決定這一些相依性的服務 daemon 需要被執行.我們可以透過 #systemctl list-dependencies 指令來看相依性.
    core@coreos1 ~ $ systemctl list-dependencies
    default.target
    ● ├─audit-rules.service
    ● ├─dbus.service
    ..
    ├─basic.target
    ● │ ├─tmp.mount
    ● │ ├─sysinit.target
    ● │ │ ├─local-fs.target
    ● │ │ │ ├─boot.automount
    ..
    
  • generated
  • – ?

  • indirect
  • – ?

已經瞭解了目前服務開機時是否載入,如何透過指令的方式去修改,這時則要使用

  • #systemctl enable :設定開機時該 unit 會自動被啟動.
  • #systemctl disable :設定開機時該 unit 不會被啟動.
  • #systemctl is-enable :檢查該 unit 在開機時是否啟用
  • #systemctl status :檢查該 unit 目前的狀態,可以看到是否正在執行,開機時是否執行…等資訊.

雖然是 loaded 但卻是 inactive 的(下次開機才會生效).

core@coreos1 ~ $ sudo systemctl enable etcd2
Created symlink /etc/systemd/system/multi-user.target.wants/etcd2.service → /usr/lib/systemd/system/etcd2.service.
core@coreos1 ~ $ sudo systemctl status etcd2
● etcd2.service - etcd2
   Loaded: loaded (/usr/lib/systemd/system/etcd2.service; enabled; vendor preset
   Active: inactive (dead)

如果要立刻改變服務狀態則需要透過

  • #systemctl start :立刻啟動該 unit
  • #systemctl stop :立刻關閉該 unit
  • #systemctl restart :動作跟 stop 再 start 是同樣意思.
  • #systemctl reload :跟 restart 不同的是,只有重新載入設定檔.
  • #systemctl is-active :檢查該 unit 目前有沒有正在運作中.

狀態可以分為兩區,服務狀態與服務狀態的訊息 (還可以透過參數 -l 來查看所有的訊息)

core@coreos1 ~ $ sudo systemctl start etcd2
core@coreos1 ~ $ sudo systemctl status etcd2
● etcd2.service - etcd2
   Loaded: loaded (/usr/lib/systemd/system/etcd2.service; disabled; vendor prese
   Active: active (running) since Mon 2017-01-23 06:20:10 UTC; 10s ago
 Main PID: 874 (etcd2)
    Tasks: 7
   Memory: 16.8M
      CPU: 117ms
   CGroup: /system.slice/etcd2.service
           └─874 /usr/bin/etcd2
core@coreos1 ~ $ sudo systemctl stop etcd2
core@coreos1 ~ $ sudo systemctl status etcd2
● etcd2.service - etcd2
   Loaded: loaded (/usr/lib/systemd/system/etcd2.service; disabled; vendor prese
   Active: inactive (dead)

更多關於 systemctl 的使用可以參考 https://benjr.tw/94315

沒有解決問題,試試搜尋本站其他內容

4 thoughts on “安裝 CoreOS – 設定 unit

  1. 自動引用通知: CoreOS 設定檔 – Benjr.tw

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料