RKE安裝

因公司某產品說在 RKE 平台上有問題,所以就找了資料,裝一套起來試試看,想不到意外的簡單。

參考資料

準備 VM 與配置 VM 環境

建立兩台 VM,可以使用 RHEL8

接下來動作,兩台都要作

移除 podman

sudo yum remove podman runc docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

安裝 docker-ce

sudo ls /etc/yum.repos.d wget https://download.docker.com/linux/centos/docker-ce.repo -O - | sudo tee /etc/yum.repos.d/docker.repo sudo yum install -y docker-ce-cli-20.10.24-3.el8 docker-ce-20.10.24-3.el8 sudo systemctl enable --now docker

關閉 swap

sudo swapoff -a

編輯 /etc/fstab ,註解掉 swap 該行

關閉 selinux

sudo setenforce 0 sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

關閉防火牆

sudo systemctl stop firewalld sudo systemctl disable firewalld

新增 sysctl 設定

新增 /etc/sysctl.d/99-k8s.conf

net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1

增加使用者 apadmin

sudo useradd apadmin sudo passwd apadmin sudo usermod -aG docker apadmin

安裝

在 bastion 上作業

下載 rke

因這邊有需求要裝 kubernetes 1.24,所以要下載對應的 rke 版本 1.43-rc3

這是在 bastion 主機上作

wget https://github.com/rancher/rke/releases/download/v1.4.3-rc3/rke_linux-amd64 -O /tmp/rke sudo mv /tmp/rke /usr/local/bin/rke sudo chmod +x /usr/local/bin/rke

配置 ssh 免密碼登入

ssh-keygen ssh-copy-id apadmin@<master> ssh-copy-id apadmin@<worker>

rke設定檔

先列出此版 rke 可安裝的 kubernetes 版本

rke config --list-version -all

輸出結果是

v1.24.8-rancher1-1 v1.25.5-rancher1-1 v1.23.14-rancher1-1

新增 cluster.yml ,裡面的 kubernetes_version 指定的 kubernetes 版本

cluster_name: ithome-rancher kubernetes_version: "v1.24.8-rancher1-1" nodes: - address: 10.11.23.81 user: apadmin role: [controlplane,worker,etcd] - address: 10.11.23.82 user: apadmin role: [worker,etcd] services: etcd: backup_config: enabled: true interval_hours: 6 retention: 60 network: plugin: flannel

開始安裝

執行以下指令就可以安裝

rke up

安裝後配置

安裝完成後,目錄裡會有 kube_config_cluster.yml 的檔案

這是給 kubectl 使用的檔案,先下載 kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" sudo mv kubectl /usr/local/bin sudo chmod +x /usr/local/bin/kubectl

把 kube_config_cluster.yml 複製為 ~/.kube/config

mkdir -p ~/.kube cp kube_config_cluster.yml ~/.kube/config

接下來就可以用 kubectl 指令查看

kubectl get nodes kubectl get pods -A

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 裡雖然可以很容易擴充,但不表示資源無限,反而更應該去管控好,避免浪費。

如何檢查RHEL有受到特定CVE影響?

因為好奇怎麼去查,就找到這篇:How to check if an RHEL system is vulnerable to a CVE ,文章裡面提到兩個方法。

第一個方法是用 rpm -q --changelog <套件> | grep <CVE_number>

舉個例子

rpm -q --changelog openssl | grep CVE-2021-3450

第二個方法是用 yum updateinfo info --cve <CVE_number>

舉個例子

yum updateinfo info --cve CVE-2021-3445

最後,想調查這台 RHEL 有受到哪些 Errata 影響,可以用

yum updateinfo info --summary
yum updateinfo info --list

若要帶入 CVE 資訊,可以用這兩個指令

yum updateinfo info --summary --with-cve
yum updateinfo info --list --with-cve

Red Hat 也有一篇 KB ,方法相似:https://access.redhat.com/solutions/3628301

不太一樣的地方是,指令是查看 rpm 的,所以是用

rpm -qp kernel-3.10.0-862.11.6.el7.x86_64.rpm --changelog | grep CVE-2017-12190

或者是用 yum list –cve <CVE_number>

yum list --cve CVE-2017-12190 | grep kernel.x86_64

瀏覽器的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 去刪除。

找指定位置下的目錄

想找到指定位置下的目錄,但不要再深入。指定位置下的目錄是這樣的

/tmp/location
  +-- dir1
    + dir11
  +-- dir2
    + dir21
  foo.txt

我預期是只看到 dir1 跟 dir2,所以我用 find

find /tmp/location -type d -print

結果我找到的是

/tmp/location
/tmp/location/dir2
/tmp/location/dir2/dir21
/tmp/location/dir1
/tmp/location/dir1/dir11

find 的輸出跟我預期的不相符啊,我又不想用 ls 處理,後來找到 -maxdepth -mindepth 這兩個參數,搭配起來使用就可以了。

find /tmp/location -maxdepth 1 -mindepth 1 -type d -print

這樣就會是我需要的 dir1 跟 dir2

/tmp/location/dir2
/tmp/location/dir1

又學到一課,謝謝你 find