microk8s升級

使用過 OpenShift 跟 Kubernetes ,對於升級這件事情,就會比較小心。關於升級這件事情,可以參考 Kubernetes 升級 FAQ

microk8s 這部份蠻簡單的,官方的文件很清楚:Upgrading MicroK8s

單個節點

用 snap 更新就可以

sudo snap refresh microk8s --channel=1.21/stable

多個節點

Kubernetes 本身允許某些節點的版本較低 (service skew),這邊就一個節點、一個節點處理。

文件沒有提到順序,我的想法是先升級 master 節點,再升級 worker 節點,步驟都一樣。

先把 pod 都趕出來

microk8s kubectl drain <node> --ignore-daemonsets

用以下指令確認除了 daemonset 以外的 pod 都已經被終止。

microk8s kubectl get po -A -o wide

然後更新

sudo snap refresh microk8s --channel=1.21/stable

再用以下指令去確認 node 都升級完成

microk8s.kubectl get node

最後再將 node 設置為可以調度

microk8s kubectl uncordon <node>

這邊有篇實例可以參考:https://microk8s.io/docs/upgrade-cluster

git-delta

git-delta 是帶有語法高亮的比對工具。

你知道的,git 預設是使用 diff,diff 沒有語法高亮,而且在比對的顯示需要一點時間習慣。

git-delta :delta

安裝

安裝蠻簡單的,提供各種作業系統的安裝方法:Installation – delta

以 Debian/Ubuntu 來說,從下載網頁下載 debian package以後,用 dpkg -i 安裝就可以。

wget https://github.com/dandavison/delta/releases/download/0.15.1/git-delta_0.15.1_amd64.deb
sudo dpkg -i git-delta_0.15.1_amd64.deb

設定

編輯 ~/.gitconfig ,加上以下設定

[core]
    pager = delta

[interactive]
    diffFilter = delta --color-only --features=interactive

[delta]
    features = decorations

[delta "interactive"]
    keep-plus-minus-markers = false

[delta "decorations"]
    commit-decoration-style = blue ol
    commit-style = raw
    file-style = omit
    hunk-header-decoration-style = blue box
    hunk-header-file-style = red
    hunk-header-line-number-style = "#067a00"
    hunk-header-style = file line-number syntax

使用

設定加好以後,在使用以下 git 指令時,就會看到比對了

  • git diff
  • git show
  • git log -p
  • git stash show -p
  • git reflog -p
  • git add -p

也可以直接拿來替代 diff 使用

delta a.txt b.txt

其他還有很多設定可以調整,可以依照自己的需求來調整:Usage – delta

結語

這種可以提升開發速度的工具,要列到建立開發環境的步驟裡,這樣在開發上可以提升效率。

Windows Server 空間清理

用 Microsoft Windows Server 提供的工具比較保險,查到有兩種方式,一種是需要安裝桌面體驗,一種是下指令。

安裝桌面體驗會要求額外安裝其他的元件,但空間就已經不太夠了,就不選擇這個方案。

下指令的方式是使用 dism 指令,先用以下指令分析

dism.exe /online /Cleanup-Image /AnalyzeComponentStore

分析完畢會有狀態報告,接下來就可以進行清理

dism.exe /online /Cleanup-Image /StartComponentCleanup

看起來是有排程進行清理,所以應該也不需要特別使用這指令。

參考資料

s3fs

Ubuntu 可以用 apt 安裝,安裝以後,不需要 root 權限就可以掛載。可以用這個掛載 AWS S3, minio 這類有相同 API 的 object storage。

首先把 ACCESS_KEY_ID 跟 SECRET_ACCESS_KEY 放到 ~/.passwd-s3fs 或 /etc/passwd-s3fs 檔案裡。

<ACCESS_KEY_ID>:<SECRET_ACCESS_KEY>

掛載時就用

s3fs <bucket_name> /mnt -o passwd_file=/home/<user>/.passwd-s3fs,use_path_request_style,url=<minio_url>

使用上蠻簡單的。

參考資料

vim基本款設定與常用指令、按鍵整理

去客戶那邊的時候,vim 設定一定都要重來過的,如何在最快的時間設定成自己想要的樣子?

目前自己的基本設定

set et
set ts=2
set sw=2
set autoindent
set nu
colorscheme desert

這樣就足夠了,太多也記不住。

再來是常用指令

指令說明
:set wrap超過螢幕寬度就換行
:set nowrap超過螢幕寬度不換行
/\ccopyright
:set ignorecase
搜尋不分大小寫,可參閱 :help ignorecase:help smartcase
:set nu顯示行號
:set nu!隱藏行號
:tabedit filename在新頁籤開啟指定檔案
:tabnew開新頁籤
:ls列出所有開啟的檔案 (buffer)
:b1切到第一個檔案 (buffer)
:set paste設定為貼上,不要管 auto indent
:set nopaste不要設定為貼上,這樣若有 autoindent 時,會自動縮排
:wq最重要的指令,存檔然後離開。若加上 ! 表示強制存檔後離開。
:set mouse= 或 :set mouse=r 或 :set mouse=v 設定不要為 a 就可以使用標準系統的選取、複製模式

常用按鍵

Mode按鍵說明
Normalctrl+w s分割畫面-橫切
Normalctrl+w v分割畫面-直切
Normalctrl+w c關閉當前窗格
Normalctrl+w j / ctrl+w k / ctrl+w h / ctrl+w l移到下面/上面/左邊/右邊的窗格
Normalgt到下個頁籤
NormalgT到上個頁籤
Insertctrl+x ctrl+p表示往前找有輸入過的來作自動完成
Insertctrl+x ctrl+f自動完成檔名

參考資料

atuin

atuin 可以把指令歷史改放到 sqlite db 裡,提供了 fancy 的畫面跟功能,還蠻酷的。它在雲端有提供 sync 伺服器,可以把指令同步上去,我本來在想這會有安全疑慮吧,畢竟有些時候指令就會帶密碼。不過,atuin 已經有做加密了,放上去是安全的。

atuin sync 伺服器也可以自行架設,除了可以同步之外, sync 伺服器還有提供一個類似 github graph 的功能,可以看到活動圖,蠻有趣的。

安裝

bash <(curl https://raw.githubusercontent.com/ellie/atuin/main/install.sh)

如果想試試預設的 sync 伺服器,就先用 atuin register 來註冊

atuin register -u <USERNAME> -e <EMAIL> -p <PASSWORD>

若不需要,就匯入目前的 history

atuin import auto

設定

如果是使用 bash ,就用以下指令,把設定放到 .bashrc

curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc
echo 'eval "$(atuin init bash)"' >> ~/.bashrc

若是 zsh ,就改用以下指令,把設定放到 .zshrc 裡

echo 'eval "$(atuin init zsh)"' >> ~/.zshrc

設定好,就關掉終端機,開新的終端機,或者是登出,重新再登入,設定就生效了。

使用

使用很簡單,按下 ctrl+r 或是按上,就會看到 atuin 的畫面,輸入字母,上方就會篩選出相關的指令。

那也可以打指令去找,例如找昨天下午3點以前的 ls 指令

atuin search --exit 0 --after "yesterday 3pm" ls

相當簡單。

架設 sync 伺服器的部份有空再來試好了,目前就先都以本機使用為主。

想法

其實就現在來說,使用 ctrl+r 就已經很方便,而且也不需要安裝,直接就能使用。未來會有需要集中管理指令歷史功能的強烈需求嗎?我想可能是沒有,就先用一陣子試試看好了,一周以後再來評估是否要繼續使用。

HAProxy

去年因為需要建置 OpenShift 容器平台而接觸到了 HAProxy,才知道為什麼 AWS 上會有分 Application Load balancer 等不同類型的負載平衡器。

簡單的說,HAProxy 的負載平衡模式分為兩種,一種是 HTTP mode,一種是 TCP mode;也就是 Layer 7 跟 Layer 4。建置 OpenShift 容器平台時,是使用 TCP mode ,Layer 4 的模式,當 HAProxy 收到連線請求時,是直接看哪一個 backend node 可以用,就直接左手給右手,連線到該台 backend node。那 HAProxy 也可以設定為 HTTP mode ,在這種模式下,就像是 nginx/apache 的 reverse proxy ,可以在 frontend 加上憑證跟做 VirtualHost。

安裝很簡單,Ubuntu 跟 RHEL 都有提供

sudo apt install -y haproxy
sudo yum install -y haproxy

安裝以後,設定檔位置是在 /etc/haproxy.cfg

主要需要調整的是 frontend 跟 backend

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 10s
        timeout client  60s
        timeout server  60s
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

frontend http_bind
        bind *:80
        mode tcp
        option tcplog
        default_backend router_http


frontend https_bind
        bind *:443
        mode tcp
        option tcplog
        default_backend router_https

backend router_http
        mode tcp
        server router_server router:80

backend router_https
        mode tcp
        server router_server router:443

frontend 決定 haproxy 要 listen 哪一個 port,backend 決定是由哪個 node 來處理連線,很容易理解。(聽同事說,這跟 F5 的設定也很接近。)

在 RHEL 會比較容易遇到 HAProxy 有問題,主因是 SELinux ,在啟動 HAProxy 前,需要先使用 setsebool 來設定:

getsebool -a | grep haproxy_connect_any
setsebool haproxy_connect_any on

跟 HAProxy 相關的 SELinux 設定可以用 man 8 haproxy_selinux 來查詢。

參考資料

讓 curl 在狀態碼非2XX時,回傳錯誤

一般在使用 curl 去存取某個網路資源時,只要可以連上並存取,curl 的 exit code 就會是 0。

可是很常見的情境是會想要加上 HTTP 狀態碼的判斷,例如 404 表示 not found,這時候就只能用 -I(即 -X HEAD) 再加上 -v 去取得 HTTP 請求回應標頭裡的狀態碼,再來判斷。

curlf() {
  OUTPUT_FILE=$(mktemp)
  HTTP_CODE=$(curl --silent --output $OUTPUT_FILE --write-out "%{http_code}" "$@")
  if [[ ${HTTP_CODE} -lt 200 || ${HTTP_CODE} -gt 299 ]] ; then
    >&2 cat $OUTPUT_FILE
    return 22
  fi
  cat $OUTPUT_FILE
  rm $OUTPUT_FILE
}

還好,在 curl 7.76 以後的版本加入了 --fail-with-body/–fail ,使用這個參數就可以不用那麼複雜的判斷,當 HTTP 狀態碼不是 2XX ,exit code 就不會是 0 了,而會是 22,22 表示 。

curl -sI --fail-with-body http://example.com/not-found/
echo $?
22

這樣以後就不需要解析請求回應標頭裡的狀態碼了。

參考資料

從 JFrog Artifactory 下載 artifact

登入 JFrog Artifactory 以後,進入自己的 Profile,然後點 Generate API Key,取得 API Key。

接下來,就可以在 curl 裡指定 X-JFrog-Art-Api: <token> 這個 Header,這樣就可以下載 artifact 了。

例如

curl -H "X-JFrog-Art-Api: <toen>" -o commons-collections-3.1.jar http://localhost:8081/artifactory/jcenter/apache-collections/commons-collections/3.1/commons-collections-3.1.jar

參考資料

PowerShell-取得磁碟剩餘空間

朋友主機的作業系統是 Windows Server 2012,之後會定期去做維護,那連線因為懶得用滑鼠按鍵去一個一個看,所以參考網路文章,寫了個簡單的腳本來一次印出來。

這腳本我命名為 du.ps1

function Get-FreeSpace {
    param ([string]$path);
    $space = (Get-Volume -FilePath $path).SizeRemaining;
    return [int64]($space / (1024 * 1024)); # this would otherwise be a float
}

$freeSpace = Get-FreeSpace "C:\"
Write-Host $freeSpace, "MB" -Separator " "
$freeSpace = Get-FreeSpace "D:\"
Write-Host $freeSpace, "MB" -Separator " "
$freeSpace = Get-FreeSpace "E:\"
Write-Host $freeSpace, "MB" -Separator " "
Read-Host "Press ENTER to exit"

簡單說明如下:

  1. Get-Volume -FilePath "<path>" 是取得指定路徑的物件,然後取物件的 SizeRemaining 屬性。
  2. 因為單位是 bytes,所以除以 1024 變成 K,再除以 1024 變成 MB
  3. 用 Write-Host 印出來,因為找不到類似 printf 或 print 的函式。Write-Host 的 -Separator 參數是指定分隔符號,這邊我用 ” ” 當作分隔,所以印出時,就會是 “100 MB” 這樣子的字串。
  4. 最後用 Read-Host 等按鍵,因為之後我會用滑鼠點一點來執行,若不加這行,執行完,視窗就會消失。

開啟 PowerShell 終端機視窗來執行 .\du.ps1 時,會有錯誤訊息,說不允許執行。這時候需要先執行下面這行,才能執行

Set-ExecutionPolicy RemoteSigned

參考文章