2023年9月26日 星期二

[Linux] OpenSSL AES-128 加解密 @ macOS 13.2.1, OpenSSL 3.1.2 1 Aug 2023

總覺得我是不是以前也寫過這類筆記? 查了下快十年前 XD 用的是 openssl des3 

由於工作中太常接觸 AES-128 加解密了,想要筆記一下方便未來查詢,而透過 openssl command 也方便交叉驗證程式是否正確,這次就仿 openssl des3 筆記:

明文:

% cat /tmp/input.txt
Hello World
% md5 /tmp/input.txt
MD5 (/tmp/input.txt) = e59ff97941044f85df5297e1c302d260
% hexdump /tmp/input.txt 
0000000 6548 6c6c 206f 6f57 6c72 0a64          
000000c

產生加解密的 Key 值:

% dd if=/dev/urandom of=/tmp/password bs=1 count=32
32+0 records in
32+0 records out
32 bytes transferred in 0.000303 secs (105611 bytes/sec)
% md5 /tmp/password 
MD5 (/tmp/password) = 92eeb8e1bec70865650e1f96e5cd1819
% hexdump /tmp/password 
0000000 cf23 5006 70d0 bf1e 0e9e a70c 10f0 ecd6
0000010 dc01 e156 d818 bff2 2e3e f859 28c9 a91d
0000020
% hexdump -v -e '/1 "%02x"' -n 16 /tmp/password 
23cf0650d0701ebf9e0e0ca7f010d6ec

產生加解密的 IV 值(其實同 Key 值產生即可,目前改用另一招):

% date | md5
5d6476c85eca3ec56fda4913f5578b83

使用 OpenSSL AES-128 加密:

% openssl enc -e -aes-128-cbc -in /tmp/input.txt -out /tmp/encrypt.txt -K 23cf0650d0701ebf9e0e0ca7f010d6ec -iv 5d6476c85eca3ec56fda4913f5578b83
% hexdump /tmp/encrypt.txt
0000000 c3d4 cb0a d845 182c 319e afdf b29c c484
0000010

使用 OpenSSL AES-128 解密:

% openssl enc -d -aes-128-cbc -in /tmp/encrypt.txt -out /tmp/output.txt -K 23cf0650d0701ebf9e0e0ca7f010d6ec -iv 5d6476c85eca3ec56fda4913f5578b83
% md5 /tmp/output.txt 
MD5 (/tmp/output.txt) = e59ff97941044f85df5297e1c302d260
% cat /tmp/output.txt 
Hello World

工作上很容易 Key 值是一個 32 bytes 的 binary 檔案,且不加入輸出至檔案的方式,可以立即看解密的內容,連續動作如下:

% openssl enc -d -aes-128-cbc -in /tmp/encrypt.txt -K $(hexdump -v -e '/1 "%02x"' -n 16 /tmp/password) -iv 5d6476c85eca3ec56fda4913f5578b83
Hello World

收工

2023年9月20日 星期三

Windows 開發筆記 - 使用 Command Line / CMD / ssh 與 PHP 8.2 / Composer / Git / VIM 開發環境 @ Windows 11

幫同事看了一下 Windows 開發環境,要在此環境使用 PHP8.2 與 PHP Laravel v10 framework,由於之前採用 xampp 管理套件被環境變數卡住。這些問題描述,瞬間拉回到學生時代在那邊設置 Windows %PATH% 環境變數 XD 我也忘了那時在幹嘛?推論是配置 Java 環境吧

目前就把手邊的 Windows 11 筆電拿來遠端,但懶得打開它。並且實際在 command line 測試會碰到幾個問題。

1. 從 https://windows.php.net/download/ 下載 VS16 x64 Non Thread Safe ,並解壓在 C:\php 目錄中,必須在設置 php.ini 。想要知道自己的 php.ini 位置,可以用 php.exe --ini

C:\php>php.exe --ini 
Configuration File (php.ini) Path: 
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

接著把 C:\php\php.ini-development 複製到 C:\php\php.ini 使用:

C:\php>copy php.ini-development php.ini     
複製了         1 個檔案。

後續就編輯 php.ini 開啟一些項目,在此就靠 C:\cygwin64\bin\vim.exe 當編輯器(若碰到滑鼠圈選文字難複製,記得關掉滑鼠模式 :set mouse-=a),主要打開一些 php.ini 註解:

; Directory in which the loadable extensions (modules) reside.
; https://php.net/extension-dir
;extension_dir = "./"
; On windows:
extension_dir = "ext"
; ...
extension=curl
extension=fileinfo
extension=gd
extension=intl
extension=mbstring
extension=exif
extension=mysqli
extension=openssl
extension=sqlite3

C:\php>php.exe --ini 
Configuration File (php.ini) Path: 
Loaded Configuration File:         C:\php\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

2. 下載 composer 後,預設會失敗:

C:\php>php.exe composer.phar self-update

In Factory.php line 648:
                                                                                                                          The openssl extension is required for SSL/TLS protection but is not available. If you can not enable the openssl extension, you can   
   disable this error, at your own risk, by setting the 'disable-tls' option to true.                          

self-update [-r|--rollback] [--clean-backups] [--no-progress] [--update-keys] [--stable] [--preview] [--snapshot] [--1] [--2] [--2.2] [--set-channel-only] [--] [<version>]

設置後:

C:\php>php.exe composer.phar self-update 
You are already using the latest available Composer version 2.6.3 (stable channel).

3. 在管理專案時,透過 C:\php\php.exe composer.phar install 時,會需要 GIT 指令,解法就是去官方安裝一下,安裝完的目錄位置在 C:\Program Files\Git 位置

C:\>"C:\Program Files\Git\bin\git.exe" --version
git version 2.42.0.windows.2

4. 回過頭來,更新環境變數 %PATH% 

C:\>git
'git' 不是內部或外部命令、可執行的程式或批次檔。
C:\>echo %PATH%
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Docker\Docker\resources\bin;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Users\user\AppData\Local\Microsoft\WindowsApps;
C:\>set PATH=%PATH%;C:\Program Files\Git\bin\
C:\>git --version
git version 2.42.0.windows.2

如此在 Windows 的 command line (PowerShell) 環境下,也可以靠純指令做一點事了

2023年9月14日 星期四

Docker 開發筆記 - 建立私有的 DockerHub 服務 / My Private Docker Registry

對於公司內的 Docker 使用,當然就不能把一堆程式碼都擺在外頭的 dockerhub 來管理,所幸的 Docker Registry 也可以透過 Docker 快速建立,並且把資料儲存那段跟指定的機器儲存結合,瞬間立馬建置完畢:

Docker Registry - docs.docker.com/registry/

操作:

% docker run -d -p 5000:5000 -v /tmp/registry-stoarge:/var/lib/registry --name registry registry:2 
Unable to find image 'registry:2' locally
2: Pulling from library/registry
...: Pull complete 

% docker container list
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                    NAMES
1ab332dd4bb5   registry:2   "/entrypoint.sh /etc…"   21 seconds ago   Up 20 seconds   0.0.0.0:5000->5000/tcp   registry

將 image 發佈到指定的 Docker Registry server:

% docker image list
REPOSITORY             TAG       IMAGE ID       CREATED        SIZE
my/test                1.0.0     9b400be021d8   25 hours ago   457MB
registry               2         0030ba3d620c   5 weeks ago    24.1MB

% docker tag my/test:1.0.0 localhost:5000/my/test
% docker push localhost:5000/my/test
Using default tag: latest
The push refers to repository [localhost:5000/my/test]
............: Pushed 
............: Pushed 
............: Pushed 
latest: digest: sha256:...........
 size: ...

列出指定的 Docker Registry server 上的 image list:

% curl localhost:5000/v2/_catalog
{"repositories":["my/test"]}

列出指定的 Docker Registry server 上的 image 上的 tag list:

% curl localhost:5000/v2/my/test/tags/list
{"name":"my/test","tags":["latest"]}

查看一下 /tmp/registry-stoarge 內已經儲存的資料結構:

% tree -L 7 /tmp/registry-stoarge
/tmp/registry-stoarge
└── docker
    └── registry
        └── v2
            ├── blobs
            │   └── sha256
            │       ├── ...
            │       │   └── ...
            │       └── ...
            │           └── ...
            └── repositories
                └── my
                    └── test
                        ├── _layers
                        ├── _manifests
                        └── _uploads

30 directories, 0 files

最後,使用 Docker Registry server 上的 image 做事:

% docker run -it localhost:5000/my/test
root@.....:/# ls

2023年9月7日 星期四

Windows 開發筆記 - 開機自動啟動 SSH Reverse Tunnel / autossh @ Windows 11, cygwin

視窗鍵 + R 輸入 shell:startup

想說要善加利用 Windows 筆電,就想到把它規劃成算力單位後,以及思考如何自動化叫他做事。在資安角度上就可採用 SSH Reverse Tunnel 方案,讓筆電開機啟動後,建立一個連線到指定的機器候命。如此,在 Windows 筆上安裝了一些服務後,就可以用遠端 Port Forwarding 的方式存取到。

首先,要先找到 autossh 這種用法,不然單建立一個 ssh 掛了很麻煩的。雖然有一些 github 的可挑,但整體上要找一個夠信任的來源,最後選擇知名的 cygwin ,就順便安裝 autossh, tmux, vim, wget, lftp, git, zip, unzip 等,如此 autossh.exe 就搞定,位置在 C:\cygwin64\bin\autossh.exe

之前的文章已提到,我在 Windows 11 下已啟用 OpenSSH server 了,那我可以測試把 openssh server 服務建立反向的連線(須留意 cygwin 認定的使用者家目錄跟 Windows PowerShell 的不一樣,需要先建立好 keypair 等資料)

C:\cygwin64\bin\autossh.exe -M 0 -N -R 10022:localhost:22 ServerUser@RemotServer

如此,在指定的機器(RemoteServer)上,就可以測試連線:

$ telnet localhost 10022
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_for_Windows_8.6
^C^C
Connection closed by foreign host.

對於 cygwin 方面,目前採用 Windows 內建的 OpenSSH server 方案,連入後是 Windows Powershell 環境,若想切換到 cygwin ,可以多打一下:

Microsoft Windows [版本 10.0.22621.2215]

(c) Microsoft Corporation. 著作權所有,並保留一切權利。


user@WINDOWS-DESKTOP C:\Users\user>c:\cygwin64\Cygwin.bat


user@windows-desktop ~

$ 


對於 cygwin 用法,正規的安裝軟體還是透過原本的 setup.exe 去擴充,然而,可以去下載 github.com/transcode-open/apt-cyg 來使用,他可以提供一些便利的 command line 安裝套件的方式(但實務上不幸踩過失敗),當作一個備用方式:

user@windows-desktop ~

$ curl -s https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg


user@windows-desktop ~

$ chmod 700 ./apt-cyg 


user@windows-desktop ~

$ ./apt-cyg 

NAME

  apt-cyg - package manager utility


SYNOPSIS

  apt-cyg [operation] [options] [targets]


DESCRIPTION

  apt-cyg is a package management utility that tracks installed packages on a   

  Cygwin system. Invoking apt-cyg involves specifying an operation with any     

  potential options and targets to operate on. A target is usually a package    

  name, file name, URL, or a search string. Targets can be provided as command  

  line arguments.


OPERATIONS

  install

    Install package(s).


  remove

    Remove package(s) from the system.


  update

    Download a fresh copy of the master package list (setup.ini) from the       

    server defined in setup.rc.


  download

    Retrieve package(s) from the server, but do not install/upgrade anything.   


  show

    Display information on given package(s).


  depends

    Produce a dependency tree for a package.


  rdepends

    Produce a tree of packages that depend on the named package.


  list

    Search each locally-installed package for names that match regexp. If no    

    package names are provided in the command line, all installed packages will 

    be queried.


  listall

    This will search each package in the master package list (setup.ini) for    

    names that match regexp.


  category

    Display all packages that are members of a named category.


  listfiles

    List all files owned by a given package. Multiple packages can be specified 

    on the command line.


  search

    Search for downloaded packages that own the specified file(s). The path can 

    be relative or absolute, and one or more files can be specified.


  searchall

    Search cygwin.com to retrieve file information about packages. The provided 

    target is considered to be a filename and searchall will return the

    package(s) which contain this file.


  mirror

    Set the mirror; a full URL to a location where the database, packages, and  

    signatures for this repository can be found. If no URL is provided, display 

    current mirror.


  cache

    Set the package cache directory. If a file is not found in cache directory, 

    it will be downloaded. Unix and Windows forms are accepted, as well as      

    absolute or regular paths. If no directory is provided, display current     

    cache.


OPTIONS

  --nodeps

    Specify this option to skip all dependency checks.


  --version

    Display version and exit.


下一步回歸到正題 - 開機自動執行,則是可以參考微軟官方文件 - 新增的應用程式以在 Windows 10 啟動時自動執行。此例在 Windows 11 上,透過 視窗鍵+R 執行 shell:startup 可快速開啟對應的目錄位置 ( %HOME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup ),只需在此目錄添加幾個 bat 檔案,開啟就會幫我運行這指令了,如:

00_ssh-reverse-tunnel_openssh-server.bat

內容:

C:\cygwin64\bin\autossh.exe -M 0 -N -R 10022:localhost:22 ServerUser@RemotServer

開啟後,他會佔一個小視窗。由於那台筆電本身就是閒置的,所以佔著小視窗反而更好觀測 autossh 運作是否正常

收工!

2023年9月5日 星期二

掰了 小米便攜相片印表機 白燈閃爍 電源燈閃爍


在 2021年夏天疫情期間買了這小玩意,當初想說拿來列印出貼紙給小孩把玩會是滿有趣的小品,殊不知印個不到10張就被冰封起來 XD 接著 2023年夏天,剛好想拍小孩的畫,在輸出成小貼紙時,發現這設備異常,想說可能被閒置一年了,起手式先充個電,只是充不到一小時後,看到充電孔旁的燈一直閃一直閃,查閱說明書的 "電量指示燈" 也沒這個狀況,查詢良久後,在某個商店的描述才看到「閃爍白燈---電量不足(建議至少3個月需充電一次)」... 最好這些東西不用的時候還會記得拿出來充...就這樣設備就殘了。


只能在上班時間致電小米電話客服(還找不太到寄信問),客服也只能像 NPC 一樣回已知的資訊,如回應保固內可換貨,保固後就沒了,晚上心一橫就用力拆機,總共有三顆螺絲,一開始只能拆一顆,拆完外殼才能在拆兩顆,最後蠻力拔起來才看到鋰電池的資訊等等


看來這產品因為鋰電池的狀態廢了 Orz 把電池接線拆掉後,直接接電源可以正常啟動、連上藍芽、在米家App可連上、升韌體等等,很可惜要列印照片時,挑選完照片準備列印,接著處理到一半就 GG 自動關機,不知是不是瞬間電力需求太大?

原本想說不插鋰電池還可以當個正常工具用,看來都得放棄了

發個文紀念它吧!這是一個完全不屬於我的設備,要價也超過一千五台幣...




2023年9月3日 星期日

Kubernetes/k8s 開發筆記 - 在 macOS 僅安裝 kubectl 、在 Windows 安裝 minikube v1.31.2 和 VirtualBox v7.0.10 @ macOS 13 , Windows 11

雖然 k8s 已夯很久,但工作事務上一直還沒用他,主因是現有的 Jenkins + Ansible 已經可以輕鬆管理數百台機器群,說穿了也滿像實體機房管理機制,有人(Linode/AWS/GCP/Azure)負責把實體機器上架插上電源網路線,接著設法自動化(Ansible)找到他們完成初始化,接著靠 Jenkions 完成 CI/CD。

年初就想要提升同事技能,刻意備好書推動,現在趁個颱風天自己也找點時間走一輪吧!我認為 k8s 的線上資源已經夠多了,已夯了五年,到處都有精美的簡中電子書和鐵人文,如:
整體上,我覺得可以 Kubernetes Handbook (Kubernetes指南)那為基準,先把基礎入門文都看一下,知道 k8s 源自於 Google Borg 的設計理念,往後只要看著 k8s 架構圖即可:


例如操作 k8s 可以透過 kubectl 指令,也可以透過 api 呼叫,更可以透過 Web UI 來操作。如果硬要對比的話,早年 AWS 2009 前後,一開始也是先提供 API 操控方式,接著進展到 Firefox plugin,後續提供 Web UI 並且越來越豐富,對應的還有 Azure / GCP 一推出就有 Web UI 可操作,接著為了自動化操控,我們會使用 API level 的方式,透過基本的權限掌控後,得知機器的 IP 跟默認的登入方式,也可以簡化成單純在 AWS/GCP Web UI 開好機器及設定好標籤後,後續 ansible-playbook 就能全盤接手處理。

至於要認識 k8s ,目前看到 minukube 是最佳的下手,他支援跨平台。實務上只需做一兩件事:
  1. 下載 minukube ,可以架設出本地 k8s cluster (預設是單一節點)
  2. 下載 kubectl 工具(雖然 minukube kubectl 也能呼喚出)
這邊做一點有趣的情境:在 macos 上僅安裝 kubectl 而已,在 windows 11 安裝 minukube v1.31.2 + VirtualBox v7.0.10,讓 Windows 資源拿來運作 k8s 環境,未來 macos 就縮減成 thin client 用來遠端操作。

實作方式 - macOS (也可靠 MacPorts 或 Homebrew 安裝):

% curl -L https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/darwin/amd64/kubectl > /tmp/kubectl
% chmod 700 /tmp/kubectl
% /tmp/kubectl version
Client Version: v1.28.1
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
The connection to the server localhost:8080 was refused - did you specify the right host or port?

這邊就可以看到 kubectl 他本身預設會去跟 localhost:8080 溝通,那代表其可以設定在遠方。

實作方式 - Windows 11: 

PS C:\Users\user\Downloads> .\minikube-windows-amd64.exe start
😄  minikube v1.31.2 on Microsoft Windows 11 Pro 10.0.22621.2215 Build 22621.2215
✨  Using the virtualbox driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🔄  Restarting existing virtualbox VM for "minikube" ...
❗  This VM is having trouble accessing https://registry.k8s.io
💡  To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
🐳  Preparing Kubernetes v1.27.4 on Docker 24.0.4 ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
    ▪ Using image docker.io/kubernetesui/dashboard:v2.7.0
    ▪ Using image docker.io/kubernetesui/metrics-scraper:v1.0.8
🔎  Verifying Kubernetes components...
💡  Some dashboard features require the metrics-server addon. To enable all features please run:

        minikube addons enable metrics-server

🌟  Enabled addons: default-storageclass, storage-provisioner, dashboard
💡  kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A'
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

PS C:\Users\user\Downloads> .\minikube-windows-amd64.exe stop
✋  Stopping node "minikube"  ...
🛑  1 node stopped.

PS C:\Users\user\Downloads> .\minikube-windows-amd64.exe delete
🔥  Deleting "minikube" in virtualbox ...
💀  Removed all traces of the "minikube" cluster.

以上就是很粗淺的....我起來了,我停掉,我砍掉了。其中比較麻煩之處應該是 Virutalbox 7 的使用,剛安裝完後要重新啟動電腦,接著運行 minikube-windows-amd64.exe start 時,看能不能順利跑起來,常見卡住的地方是 Host-Only 網路卡等等。

此外,一直切換筆電很煩,就來把 Windows 11 Desktop 安裝 OpenSSH ,讓 macOS 遠端登入:[macOS] 從 Macbook 遠端登入 Windows 筆電並使用 PowerShell 工作環境

接著就回到 macOS 遠端登入 windows minikube 的用法:

% ssh user@windows-minikube-ip

Microsoft Windows [版本 10.0.22621.2215]
(c) Microsoft Corporation. 著作權所有,並保留一切權利。

user@WINDOWS-DESKTOP C:\Users\user>cd Downloads

user@WINDOWS-DESKTOP C:\Users\user\Downloads>minikube-windows-amd64.exe start
😄  minikube v1.31.2 on Microsoft Windows 11 Pro 10.0.22621.2215 Build 22621.2215
✨  Automatically selected the virtualbox driver
👍  Starting control plane node minikube in cluster minikube
🔥  Creating virtualbox VM (CPUs=2, Memory=4000MB, Disk=20000MB) ...
❗  This VM is having trouble accessing https://registry.k8s.io
💡  To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
🐳  Preparing Kubernetes v1.27.4 on Docker 24.0.4 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🔎  Verifying Kubernetes components...
🌟  Enabled addons: default-storageclass, storage-provisioner
💡  kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A'
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

user@WINDOWS-DESKTOP C:\Users\user\Downloads>

user@WINDOWS-DESKTOP C:\Users\user\Downloads>minikube-windows-amd64.exe status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

user@WINDOWS-DESKTOP C:\Users\user\Downloads>minikube-windows-amd64.exe kubectl -- config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: C:\Users\user\.minikube\ca.crt
    extensions:
    - extension:
        provider: minikube.sigs.k8s.io
        version: v1.31.2
      name: cluster_info
    server: https://192.168.59.101:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    extensions:
    - extension:
        provider: minikube.sigs.k8s.io
        version: v1.31.2
      name: context_info
    namespace: default
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: C:\Users\user\.minikube\profiles\minikube\client.crt
    client-key: C:\Users\user\.minikube\profiles\minikube\client.key

user@WINDOWS-DESKTOP C:\Users\user\Downloads>minikube-windows-amd64.exe kubectl -- version --output=json     
{
  "clientVersion": {
    "major": "1",
    "minor": "27",
    "gitVersion": "v1.27.4",
    "gitCommit": "fa3d7990104d7c1f16943a67f11b154b71f6a132",
    "gitTreeState": "clean",
    "buildDate": "2023-07-19T12:20:54Z",
    "goVersion": "go1.20.6",
    "compiler": "gc",
    "platform": "windows/amd64"
  },
  "kustomizeVersion": "v5.0.1",
  "serverVersion": {
    "major": "1",
    "minor": "27",
    "gitVersion": "v1.27.4",
    "gitCommit": "fa3d7990104d7c1f16943a67f11b154b71f6a132",
    "gitTreeState": "clean",
    "buildDate": "2023-07-19T12:14:49Z",
    "goVersion": "go1.20.6",
    "compiler": "gc",
    "platform": "linux/amd64"
  }
}


user@WINDOWS-DESKTOP C:\Users\user\Downloads>ipconfig

Windows IP 設定


乙太網路卡 乙太網路:

   媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線
   連線特定 DNS 尾碼 . . . . . . . . :

乙太網路卡 乙太網路 2:

   連線特定 DNS 尾碼 . . . . . . . . :
   IPv4 位址 . . . . . . . . . . . . : 192.168.56.1
   子網路遮罩 . . . . . . . . . . . .: 255.255.255.0
   預設閘道 . . . . . . . . . . . . .:

乙太網路卡 乙太網路 3:

   連線特定 DNS 尾碼 . . . . . . . . :
   IPv4 位址 . . . . . . . . . . . . : 192.168.59.1
   子網路遮罩 . . . . . . . . . . . .: 255.255.255.0
   預設閘道 . . . . . . . . . . . . .:

...

如此,若要用 macOS kubectl 去控制 Windows 11 上的 minikube ,先挑個土法煉鋼模式:

1. 把 minikube-windows-amd64.exe kubectl -- config view 存起來(可以透 ssh remote command)

% ssh user@windows-minikube-ip '%HOME%\Downloads\minikube-windows-amd64.exe kubectl -- config view' > /tmp/kubectl.config.yaml

2. 將上面列到的 certificate-authority, client-certificate 和 client-key 也都靠 scp 下載回來

% scp user@windows-minikube-ip:"/C:/Users/user/.minikube/ca.crt" /tmp/kubectl.ca.crt
% scp user@windows-minikube-ip:"/C:/Users/user/.minikube/profiles/minikube/client.crt" /tmp/kubectl.client.crt
% scp user@windows-minikube-ip:"/C:/Users/user/.minikube/profiles/minikube/client.key" /tmp/kubectl.client.key

3. 修改 /tmp/kubectl.config.yaml 上 certificate-authority, client-certificate 和 client-key 對應位置

% /tmp/kubectl --kubeconfig ./kubectl.config.yaml config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: kubectl.ca.crt
    extensions:
    - extension:
        provider: minikube.sigs.k8s.io
        version: v1.31.2
      name: cluster_info
    server: https://127.0.0.1:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    extensions:
    - extension:
        provider: minikube.sigs.k8s.io
        version: v1.31.2
      name: context_info
    namespace: default
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: kubectl.client.crt
    client-key: kubectl.client.key

其中上述的 clusters.cluster.server 數值我已換成 https://127.0.0.1:8443 ,這是為了彈性自行靠 ssh tunnel 

4. 最後,我再透過 SSH tunnel 自建一條連到 192.168.56.101:8443 (Windows 11 上的 minikube api 入口點)

% ssh -N -L 8443:192.168.59.101:8443 user@windows-minikube-ip

5. 如此,未來想從 macOS 的 kubectl 遠端連去控制 Windows 11 上的 minikube 時,就是先建立一條 SSH tunnel ,接著運行 kubectl 要指定 config 位置,就能黑皮遠端操控

% ./kubectl --kubeconfig ./kubectl.config.yaml version
Client Version: v1.28.1
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.27.4

最後一提,其實 Windows 11 Desktop 安裝完 OpenSSH server 後,已經可以自行遠端進去用 minikube-windows-amd64.exe kubectl 做事,只剩熟不熟悉 PowerShell 指令環境,所以,上述有點脫褲子放屁 XD 好處是練一輪後,未來有很多 k8s cluster 可以用 --kubeconfig 切換吧!

其他資訊:

Kubernetes/k8s 開發筆記 - 在 Macbook M1 Pro 安裝 minikube, qemu, socket_vmnet @ macOS 13.5.1


之前略知 Apple M1 架構,對於使用一些軟體服務會卡卡,終於來體驗一下。原先在考慮依照官網的簡介:


後來決定都靠 Homebrew 來管理,安裝流程:

% sw_vers
ProductName: macOS
ProductVersion: 13.5.1
BuildVersion: 22G90

% brew --version
Homebrew 4.1.7

% brew install qemu socket_vmnet minikube
Error: Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)!
Please create a new installation in /opt/homebrew using one of the
"Alternative Installs" from:
  https://docs.brew.sh/Installation
You can migrate your previously installed formula list with:
  brew bundle dump

% eval "$(/opt/homebrew/bin/brew shellenv)"

% brew install qemu socket_vmnet  minikube

...
socket_vmnet requires root privileges so you will need to run
  `sudo /opt/homebrew/opt/socket_vmnet/socket_vmnet` or `sudo brew services start socket_vmnet`.
You should be certain that you trust any software you grant root privileges.

socket_vmnet is keg-only, which means it was not symlinked into /opt/homebrew,
because Homebrew's bin directory is often writable by a non-admin user.

If you need to have socket_vmnet first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/socket_vmnet/bin:$PATH"' >> ~/.zshrc

To start socket_vmnet now and restart at startup:
  sudo brew services start socket_vmnet
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/socket_vmnet/bin/socket_vmnet --vmnet-gateway\=192.168.105.1 /opt/homebrew/var/run/socket_vmnet
...

% sudo brew services start socket_vmnet
Service `socket_vmnet` already started, use `brew services restart socket_vmnet` to restart.

% minikube start --driver=qemu --network socket_vmnet
😄  minikube v1.31.2 on Darwin 13.5.1 (arm64)
✨  Using the qemu2 driver based on user configuration
👍  Starting control plane node minikube in cluster minikube
🔥  Creating qemu2 VM (CPUs=2, Memory=6000MB, Disk=20000MB) ...
🐳  Preparing Kubernetes v1.27.4 on Docker 24.0.4 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🔎  Verifying Kubernetes components...
🌟  Enabled addons: default-storageclass, storage-provisioner
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

% minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

% minikube stop  
✋  Stopping node "minikube"  ...
🛑  1 node stopped.

% minikube status
minikube
type: Control Plane
host: Stopped
kubelet: Stopped
apiserver: Stopped
kubeconfig: Stopped

然後重開機後,進入 zsh 環境時,需要重新做一點設定,就看個人的習慣要不要安置自動化了:

% minikube status
zsh: command not found: minikube
% eval "$(/opt/homebrew/bin/brew shellenv)"
% minikube status
🤷  Profile "minikube" not found. Run "minikube profile list" to view all profiles.
👉  To start a cluster, run: "minikube start"

% minikube start --driver=qemu --network socket_vmnet
😄  minikube v1.31.2 on Darwin 13.5.1 (arm64)
✨  Using the qemu2 driver based on user configuration
👍  Starting control plane node minikube in cluster minikube
🔥  Creating qemu2 VM (CPUs=2, Memory=6000MB, Disk=20000MB) ...\ OUTPUT: 
ERROR: Failed to connect to "/opt/homebrew/var/run/socket_vmnet": Connection refused


🔥  Deleting "minikube" in qemu2 ...
🤦  StartHost failed, but will try again: creating host: create: creating: Failed to connect to "/opt/homebrew/var/run/socket_vmnet": Connection refused: exit status 1
🔥  Creating qemu2 VM (CPUs=2, Memory=6000MB, Disk=20000MB) ...\ OUTPUT: 
ERROR: Failed to connect to "/opt/homebrew/var/run/socket_vmnet": Connection refused


😿  Failed to start qemu2 VM. Running "minikube delete" may fix it: creating host: create: creating: Failed to connect to "/opt/homebrew/var/run/socket_vmnet": Connection refused: exit status 1

❌  Exiting due to GUEST_PROVISION: error provisioning guest: Failed to start host: creating host: create: creating: Failed to connect to "/opt/homebrew/var/run/socket_vmnet": Connection refused: exit status 1

╭───────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                           │
│    😿  If the above advice does not help, please let us know:                             │
│    👉  https://github.com/kubernetes/minikube/issues/new/choose                           │
│                                                                                           │
│    Please run `minikube logs --file=logs.txt` and attach logs.txt to the GitHub issue.    │
│                                                                                           │
╰───────────────────────────────────────────────────────────────────────────────────────────╯

% sudo brew services restart socket_vmnet
Password:
Stopping `socket_vmnet`... (might take a while)
==> Successfully stopped `socket_vmnet` (label: homebrew.mxcl.socket_vmnet)
Warning: Taking root:admin ownership of some socket_vmnet paths:
  /opt/homebrew/Cellar/socket_vmnet/1.1.2/bin
  /opt/homebrew/Cellar/socket_vmnet/1.1.2/bin/socket_vmnet
  /opt/homebrew/opt/socket_vmnet
  /opt/homebrew/opt/socket_vmnet/bin
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
==> Successfully started `socket_vmnet` (label: homebrew.mxcl.socket_vmnet)

% minikube delete

% minikube start --driver=qemu --network socket_vmnet
😄  minikube v1.31.2 on Darwin 13.5.1 (arm64)
✨  Using the qemu2 driver based on user configuration
👍  Starting control plane node minikube in cluster minikube
🔥  Creating qemu2 VM (CPUs=2, Memory=6000MB, Disk=20000MB) ...
🐳  Preparing Kubernetes v1.27.4 on Docker 24.0.4 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🔎  Verifying Kubernetes components...
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

% minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

上述是因為筆電重啟後時,碰到 socket_vmnet 問題,最後就先試著把整套環境刪除 (minikube delete) 而重來,而後續常規的用法:

% eval "$(/opt/homebrew/bin/brew shellenv)"

% sudo brew services restart socket_vmnet  
Warning: Taking root:admin ownership of some socket_vmnet paths:
  /opt/homebrew/Cellar/socket_vmnet/1.1.2/bin
  /opt/homebrew/Cellar/socket_vmnet/1.1.2/bin/socket_vmnet
  /opt/homebrew/opt/socket_vmnet
  /opt/homebrew/opt/socket_vmnet/bin
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
==> Successfully started `socket_vmnet` (label: homebrew.mxcl.socket_vmnet)

% minikube start --driver=qemu --network socket_vmnet
😄  minikube v1.31.2 on Darwin 13.5.1 (arm64)
✨  Using the qemu2 driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🔄  Restarting existing qemu2 VM for "minikube" ...
🐳  Preparing Kubernetes v1.27.4 on Docker 24.0.4 ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

% minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured