docker-swarm-config-update

如何不停止 Docker swarm 服務修改 config

在使用 Docker swarm 部屬環境時,我們會在 compose file 中使用 configs 來讓服務可以動態調整參數。

1
2
3
4
# docker-compose.yml
configs:
http_config:
file: ./httpd.conf

但是當我們需要修改設定時會發現 docker stack deploy 並不會偵測到 config 檔案變更而幫我們更新服務設定,必須要停止所有服務重新啟動才會更新,停止所有服務只因為其中一個服務需要更新非常不合理,應該只有修改 config 的服務自己重啟就好。

為了解決這個問題我們必須要讓 docker 偵測到服務有變更需要更新,這邊我們可以使用 configs 中的 name 來幫 config 檔設定別名,當我們的 config 變更後別名跟著變動就可以讓 docker 偵測到服務需要重新啟動。

1
2
3
4
5
# docker-compose.yml
configs:
http_config:
file: ./httpd.conf
name: http_config_20220811 # config 修改後變更別名,例如在後面加入日期

雖然修改別名能夠解決問題,但是這樣每次改 config 就要同時修改 compose file 也是很不方便,所以我們用環境變數調整 name 的值讓他可以動態改變,在原本的 name 後面接上檔案的 hash,這樣只要內容有變更 hash 值就會一起變動,只需要在 config 修改後將 HTTP_CONFIG_HASH 設定成新的 hash 就好。

1
2
3
# update-hash.sh 
#!/bin/sh
export HTTP_CONFIG_HASH=$(shasum httpd.conf -a 512 | cut -c1-16)

部屬環境或是 config 修改完成要重新啟動服務時執行 source update-hash.sh

1
2
3
4
5
# docker-compose.yml
configs:
http_config:
file: ./httpd.conf
name: http_config_${HTTP_CONFIG_HASH}

參考

Updating Docker Swarm Configs and Secrets Without Downtime
update-hash.sh


git

Git 指令筆記


  • Git
    • 還原上一次 commit 並且刪除所有變更

      git reset --hard HEAD~1

    • 還原上一次 commit 並且保留所有變更

      git reset --soft HEAD~1

    • push 時遇到 ! [rejected] develop -> develop (would clobber existing tag) 修復方法

      git fetch --tags -f



linux

linux 筆記


複製 ssh key 時遇到權限問題先檢查以下設定有沒有問題

  • SSH Key 權限
    • .ssh 資料夾 700 (drwx------)
    • public key (id_rsa.pub) 644 (-rw-r--r--)
    • authorized_keys 644 (-rw-r--r--)
    • private key (id_rsa) 600 (-rw-------)

複製 public key 到 server

  1. ssh-copy-id -i <public key file> username@host


docker

Docker 指令筆記


  • Docker
    • 複製檔案到 container
      docker cp <src-path> <container>:<dest-path>
      範例:docker cp ./sample.txt 1cea5baea09b:/sample_folder

    • 連線到 container bash
      docker exec -it <container> /bin/bash
      範例:docker exec -it 1cea5baea09b /bin/bash



express-note

How to solve POST request req.body is empty?

1
2
3
4
5
6
7
8
9
10
11
12
13
const express = require('express');

const app = express();

app.post('/add', (req, res) =>{
const {a, b} = req.body; // TypeError: Cannot destructure property 'a' of 'req.body' as it is undefined.
const ret = parseInt(a) + parseInt(b);
res.send(`${a} + ${b} = ${ret}`);
});

app.listen(3000, () => {
console.log(`Example app listening at http://localhost:3000`);
});

Install bodyParser to parse body content

  1. npm instll body-parser
  2. Use bodyParser.json and bodyParser.urlencoded as middleware.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    const express = require('express');
    const bodyParser = require('body-parser')

    const app = express();
    app.use(bodyParser.urlencoded({ extended:true }));
    app.use(bodyParser.json());

    app.post('/add', (req, res) =>{
    const {a, b} = req.body; // now you can get your request data here
    const ret = parseInt(a) + parseInt(b);
    res.send(`${a} + ${b} = ${ret}`);
    });

    app.listen(3000, () => {
    console.log(`Example app listening at http://localhost:3000`);
    });