gitlab備份與還原(使用docker)

我家裡的 gitlab 之前是用 docker-gitlab 架設的,前一陣子因為誤刪了某些檔案,導致有問題。現在就改用 gitlab 官方的 docker image 來架設,目前一切安好,為了之後的長長久久,必須看一下怎麼備份跟回存。

主要參考資料:

備份方法

docker-compose exec -it gitlab-ce gitlab-backup create

還原方法

還原時,需要先把容器裡的某些服務停掉,所以步驟比較多。

docker-compose exec -it gitlab-ce gitlab-ctl stop puma
docker-compose exec -it gitlab-ce gitlab-ctl stop sidekiq
docker-compose exec -it gitlab-ce gitlab-ctl status
docker-compose exec -it gitlab-ce gitlab-backup restore BACKUP=<backup_name>
docker-compose exec -it gitlab-ce gitlab-ctl restart
docker-compose exec -it gitlab-rake gitlab:check SANITIZE=true

結語

備份、還原蠻簡單的,很容易放到 crontab 裡去執行,之後再把備份出來的複製到遠端就可以了。

PowerShell網路

最近幫朋友寫腳本,需要變更網路,這首選當然是用 PowerShell ,再來是 netsh 指令。

所以就查了這部份,以下這些腳本都需要管理者權限。

從固定IP改為DHCP

Set-NetIPInterface -InterfaceAlias 'Ethernet 2' -Dhcp Enabled
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute

從 DHCP 改為固定IP

Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress  IpAddress 192.168.1.10 -PrefixLength 24 -DefaultGateway 192.168.1.1

變更固定IP

Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetIpAddress
Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress ‑IpAddress 192.168.1.11 -PrefixLength 24 -DefaultGateway 192.168.1.1

重設DNS

Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ResetServerAddresses
Set-DnsClientServerAddress -InterfaceAlias "Wi-fi" -ResetServerAddresses

設定DNS

Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses ("8.8.8.8","8.8.4.4")
Set-DnsClientServerAddress -InterfaceAlias "Wi-fi" -ServerAddresses ("8.8.8.8","8.8.4.4")

參考資料

避免 ephemeral storage 空間用完

來源:Avoid running out of ephemeral storage space on your Kubernetes worker Nodes

Kubernetes 裡用 emptyDir 的 volume 的話,就是用 ephemeral storage,這塊空間就是 node 上的磁碟空間。所以用完的話,表示 node 上也沒空間了,其他 pod 在使用上可能就會有狀況。

node 上的位置可能會因發行版而有差異,文章裡提供的位置是:/var/lib/kubelet/pods//volumes/kubernetes.io~empty-dir//…

避免的方法有兩種。

第1種是在掛載 volume 時,設定 sizeLimit ,例如

volumes:
- name: www-content
  emptyDir:
    sizeLimit: 2Mi

缺點是當超過這限制時,kubernetes 會把這個 pod evict 掉。

第二種是設定 ResourceQuotas/LimitRange,先新增以下 YAML,然後 apply

apiVersion: v1
kind: ResourceQuota
metadata:
  name: default-resource-quotas
  namespace: my-application-namespace
spec:
  hard:
    limits.cpu: "2"
    limits.memory: 8Gi
    limits.ephemeral-storage: 2Gi
    requests.cpu: "1"
    requests.memory: 4Gi
    requests.ephemeral-storage: 1Gi
---
apiVersion: v1
kind: LimitRange
metadata:
  name: default-limit-ranges
  namespace: my-application-namespace
spec:
  limits:
  - default:
      cpu: 100m
      memory: 128Mi
      ephemeral-storage: "2Mi"
    defaultRequest:
      cpu: 25m
      memory: 64Mi
      ephemeral-storage: "1Mi" 
   type: Container

再來在 deployment/statefulset 裡增加 resource 指定,例如

apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-deployment-3
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: helloworld-deployment-3
  replicas: 2
  template:
    metadata:
      labels:
        app.kubernetes.io/name: helloworld-deployment-3
    spec:
      volumes:
      - name: www-content
        emptyDir: {}
      containers:
        - name: hello-world
          image: helloworld:1.0
          volumeMounts:
          - mountPath: /www
            name: www-content
          resources:
            requests:
              ephemeral-storage: "1Mi"
            limits:
              ephemeral-storage: "2Mi"


在 Kubernetes 裡雖然可以很容易擴充,但不表示資源無限,反而更應該去管控好,避免浪費。

瀏覽器的Unsafe port

之前很方便,自己寫個應用程式,去 listen 指定 port ,用瀏覽器存取,都沒什麼問題。

但現在瀏覽器守備範圍變寬了,怕你危險,所以當使用奇怪的 port 時,就會出現 unsafe port 的訊息。

每種瀏覽器有不同的解除限制方式。

Chrome

啟動 google chrome 時,加入參數

--explicitly-allowed-ports=xxx

例如

C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --explicitly-allowed-ports=6666

我想使用 Chrome 引擎的幾個瀏覽器應該也都適用,例如 Brave、Edge …

Firefox

firefox 處理方式比較簡單,開個新分頁,網址列輸入

about:config

然後輸入

network.security.ports.banned.override

然後填入

87,88,89,1050

或是範圍

10000-19999

完成以後,會立刻生效。

參考資料

結語

開發上,還是會需要使用到這些不安全的 port 來開發,所以知道怎麼繞過去會蠻有用的。

Hash Buster

Hackin9 看到這篇:Hash Buster – Why crack hashes when you can bust them?

這篇主要是在說 md5/sha1 是不安全的,透過目前電腦的強大能力,已經是可逆的了。用 Hash-Buster 這工具可以很快逆向取得雜湊前的結果。

安裝 Hash-Buster

方法1

git clone https://github.com/s0md3v/Hash-Buster.git
cd Hash-Buster
make install

方法2

wget https://raw.githubusercontent.com/s0md3v/Hash-Buster/master/hash.py -O /usr/local/bin/buster && chmod +x /usr/local/bin/buster

使用

把雜湊值當參數

buster -s "5e8ff9bf55ba3508199d22e984129be6"

把內有雜湊值的檔案當參數

buster -f with_hashed.txt

找目錄下所有內含有雜湊值的檔案

buster -d /somedir

Python 3 產生 md5 雜湊

import hashlib

print(hashlib.md5('sample'.encode('utf-8')).hexdigest())

結語

這工具蠻方便的,而且很小,但使用時必須要注意的一件事情,裡面的程式主要是使用外部的服務,所以是把雜湊值上傳到外部網站,然後取得結果。若是因為不小心遺忘密碼,那麼在取得結果後,要趕緊進行變更,以防萬一。

Powershell 找早於15天的檔案

這個需求在 linux 用 find 寫,很方便

find /tmp/location -type f -mtime +15 -delete

在 Windows Powershell 的話,可以參考這篇 Delete files older than 15 days using PowerShell

$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

這是使用 Get-ChildItem -Path $path ,搭配 Where-Object ,Where-Object 裏面是做判斷, $_ 是每個項目,用每個項目的 CreationTime 去判斷,然後用 Remove-Item 去刪除。

hadolint

名字裡有 lint 的,通常表示這是個用來檢查語法的工具。所以 hadolint 是個檢查用的工具,主要是用來檢查 Dockerfile 。

安裝方法

hadolint 的原始碼放在 github 上,可以直接透過這個網址去找最新的編譯好的版本來下載:https://github.com/hadolint/hadolint/tags

下載以後,變更權限,然後搬到 /usr/local/bin ,就可以使用了。

chmod +x hadolint-Linux-x86_64
mv hadolint-Linux-x86_64 /usr/local/bin/hadolint

使用

使用很簡單,帶入 Dockerfile 即可。

hadolint Dockerfile

執行以後,會出現 hadolint 覺得有問題的地方,這時候就可以參考 hadolint rules (要往下捲,找到 Rules) 的說明去修改 Dockerfile。修改完可以再重新執行檢查。

其他使用方式

除了可以下載執行檔以外,也可以透過 container 方式來運行,簡單的說就是 hadolint 工具被包在 Container image 裡,那用 docker run 或 podman run 就可以檢查。

docker pull hadolint/hadolint
docker pull hadolint/hadolint:v2.12.0-debian
docker pull hadolint/hadolint:v2.12.0-alpine

docker run --rm -i hadolint/hadolint < Dockerfile
docker run --rm -i hadolint/hadolint:v2.12.0-debian < Dockerfile
docker run --rm -i hadolint/hadolint:v2.12.0-alpine < Dockerfile

最後一種方式,是搭配 Visual Studio Code (vscode) 的 extension,如果你剛好是把 Visual Studio Code 當作主力的話,這很方便。從 Visual Studio Code 的 marketplace 上找 hadolint ,就可以安裝。安裝以後,在編輯 Dockerfile 時,下方就會出現檢查結果,邊修改邊檢查。

結論

Dockerfile 的檢查可以導入 CI 的流程裡,一方面是可以讓 Dockerfile 標準化,一方面透過這些規則,也可以縮小 Container image 的大小,加快 docker pull 的速度。

參考資料

在IIS佈署asp.net應用程式小技巧

在 IIS 佈署 asp.net 應用程式基本上只要把檔案複製過去就可以,但如果沒有停止網站的話,檔案是可能會複製失敗的,這會導致佈署失敗。

第一個小技巧是使用 App_offline.htm ,在佈署位置新增 App_offline.htm 檔案以後,IIS 會自動讓網站進入維護模式,對來訪問網站的人會看到 App_offline.htm 檔案的內容。這時候 .dll 也會被卸載,就不會造成複製失敗。

第二個小技巧是 sleep,上面有提到可以新增 App_offline.htm 來讓 IIS 進入維護模式,但這需要一點點時間讓 IIS 去做出反應,那就會需要 sleep 。可是Windows 並沒有 sleep 這類的指令,所以沒辦法 sleep ,那麼可以怎麼做呢?這裡可以參考 如何在批次檔(Batch)中實現 sleep 命令讓任務暫停執行 n 秒 ,我後來是使用 ping 這個指令來作,ping 可以指定次數,也可以指定秒數,透過 ping 就可以達到 sleep 的目的。

ping -c 4 -w 1000 127.0.0.1

參考資料

k8s.gcr.io改為registry.k8s.io

新聞:k8s.gcr.io Redirect to registry.k8s.io – What You Need to Know | Kubernetes

簡單的說,k8s.gcr.io 要被關掉了,所以從 k8s.gcr.io 拉取的 image ,未來會有影響,例如砍掉 pod 以後,有可能拉不到 image 了。

所以要找出所有使用 k8s.gcr.io 的 deployment/statefulset/pod … 資源,方法有兩種。

第1種是用 kubectl 來找

kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c

簡單的說,就是列出所有 pod 的 YAML,取出裡面的 image ,整理過以後,列出來。

第二種是用 kubectl krew 安裝 community-images plugin

kubectl krew install community-images
kubectl community-images

使用以上其中一種方法找到以後,該怎麼辦?

接下來就是用 kubectl edit 去修改 image 位址。

若是用 gitops 或 helm 的方法,作法也相似,就是去修改 manifest yaml ,然後重新佈署就可以了。