ラベル Ubuntu の投稿を表示しています。 すべての投稿を表示
ラベル Ubuntu の投稿を表示しています。 すべての投稿を表示

2026年5月10日日曜日

Ubuntu にインストールした Ollama の設定変更を行う

Ubuntu にインストールした Ollama の設定変更手順をまとめた。

前提

  • OS: Ubuntu 24.04.4 LTS
  • Ollama: ollama version is 0.19.0

Ollama の設定変更手順

  1. sudo systemctl edit ollama コマンドを実行して、Ollama の systemd サービスのオーバーライドファイルを編集する

  2. あとは以下のような感じで、必要な設定を追加する

    ### Editing /etc/systemd/system/ollama.service.d/override.conf
    ### Anything between here and the comment below will become the contents of the drop-in file
    
    [Service]
    Environment="OLLAMA_HOST=0.0.0.0"
    Environment="OLLAMA_NUM_GPU=8"
    Environment="OLLAMA_MAX_LOADED_MODELS=2"
    Environment="OLLAMA_NUM_BATCH=2048"
    Environment="OLLAMA_NUM_THREAD=32"
    Environment="OLLAMA_KEEP_ALIVE=-1"
    Environment="CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7"
    
    ### Edits below this comment will be discarded
    
    
    ### /etc/systemd/system/ollama.service
    # [Unit]
    # Description=Ollama Service
    # After=network-online.target
    #
    # [Service]
    # ExecStart=/usr/local/bin/ollama serve
    # User=ollama
    # Group=ollama
    # Restart=always
    # RestartSec=3
  3. 編集が終わったら sudo systemctl restart ollama コマンドを実行して、Ollama サービスを再起動する

以上。

2025年8月15日金曜日

Ubuntu20.04 に Minikube をベアメタルインストールする

前提

  • Hyper-V を使用
  • OS: Ubuntu Server 20.04 LST
  • 標準インストール直後からの開始

ファイアウォールを OFF に

本当はまじめに穴あけしなきゃいけないのだけど、とりあえず動作確認という事で…

sudo ufw disable

Minikube の none ドライバ実行に必要なパッケージのインストール

conntrack

sudo apt install conntrack

containernetworking-plugins

CNI_PLUGIN_VERSION="v1.7.1"
CNI_PLUGIN_TAR="cni-plugins-linux-amd64-$CNI_PLUGIN_VERSION.tgz" # change arch if not on amd64
CNI_PLUGIN_INSTALL_DIR="/opt/cni/bin"

curl -LO "https://github.com/containernetworking/plugins/releases/download/$CNI_PLUGIN_VERSION/$CNI_PLUGIN_TAR"
sudo mkdir -p "$CNI_PLUGIN_INSTALL_DIR"
sudo tar -xf "$CNI_PLUGIN_TAR" -C "$CNI_PLUGIN_INSTALL_DIR"
rm "$CNI_PLUGIN_TAR"

crictl

DOWNLOAD_DIR="/usr/local/bin"
sudo mkdir -p "$DOWNLOAD_DIR"
CRICTL_VERSION="v1.33.0"
ARCH="amd64"
curl -L "https://github.com/kubernetes-sigs/cri-tools/releases/download/${CRICTL_VERSION}/crictl-${CRICTL_VERSION}-linux-${ARCH}.tar.gz" | sudo tar -C $DOWNLOAD_DIR -xz

kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Docker

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
           "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
             $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
               sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
               sudo apt-get update
 sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

cri-docker

curl -LO https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.18/cri-dockerd_0.3.18.3-0.ubuntu-focal_amd64.deb
sudo apt install -y ./cri-dockerd_0.3.18.3-0.ubuntu-focal_amd64.deb

Minikube

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
sudo dpkg -i minikube_latest_amd64.deb

OS 設定

何やっているか知らないけど、 minikube start --driver=none で怒られたので実行した。

sudo sysctl fs.protected_regular=0

Minikube クラスタの作成

sudo minikube start --driver=none

動作確認

mikoto@kubernetes:~$ sudo kubectl get nodes
NAME         STATUS   ROLES           AGE     VERSION
kubernetes   Ready    control-plane   7m51s   v1.33.1

mikoto@kubernetes:~$ sudo kubectl run hello-world --image=hello-world --restart=Never
pod/hello-world created

mikoto@kubernetes:~$ sudo kubectl get pod
NAME          READY   STATUS      RESTARTS   AGE
hello-world   0/1     Completed   0          23s


mikoto@kubernetes:~$ sudo kubectl logs pod/hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

OK.

参考資料

2025年8月14日木曜日

Ubuntu20.04 に kubeadm で Kubernetes を構築する

コントロールプレーンに接続できたりできなかったりと、よくわからない状態になってしまったが、とりあえず作業記録として残す。

前提

  • OS: Ubuntu Server 20.04
  • 標準インストール直後からの開始

必須パッケージのインストール

環境構築中に必要になる雑多パッケージをインストール。

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gpg

swap の無効化

sudo swapoff -a
sudo sed -i '/\sswap\s/s/^/#/' /etc/fstab

containerd のインストール

sudo apt-get update
sudo apt-get install -y containerd

kubeadm のインストール

Kubernetes パッケージリポジトリの公開鍵をダウンロード

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.33/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Kubernetes パッケージのリポジトリを登録

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.33/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

インストール

kubelet, kubeadm, kubectl をインストールし、バージョンを固定する。

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

シングルノードクラスターの作成

sudo kubeadm init

root ユーザーでない自分んが kubectl を実行できるように設定

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

コントロールプレーンに Pod を配置できるように設定

シングルノード設定なので、コントロールプレーンに相乗りする。

デフォルトではセキュリティ上相乗りできないので、相乗りできるように設定する。

kubectl taint nodes --all node-role.kubernetes.io/control-plane-

動作確認

hello-world のイメージをデプロイしてみる。

mikoto@kubernetes:~$ sudo docker run -it --rm hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

良さそう。以上。

参考資料

2025年5月3日土曜日

Keycloak の Admin UI の開発環境を整える

やりたいこと

Keycloak の Admin Console に上手く動かない箇所があった ので、自分で直すために環境づくりをする。

前提

  • Docker インストール済み
  • docker exec コマンドが使えること

開発環境の起動

今回はすべての作業をコンテナ上で行う。

必要に応じてバインドマウントするなり、ベアメタルで環境構築するなりしてください。

docker run -it --rm -p "8080:8080" -p "5174:5174" ubuntu:24.04

必要なパッケージのインストール

Keycloak のビルド・実行には Node.js と Java が必要なのでインストール。

その他、開発に必要なものとして curl, git, vim をインストールする。

いろいろ

apt update
DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends curl git vim

OpenJDK

apt install -y openjdk-17-jdk-headless

NodeJS & pnpm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
\. "$HOME/.nvm/nvm.sh"
nvm install 22
npm install -g pnpm

Keycloak のクローン

cd ~/
git clone --depth 1 https://github.com/keycloak/keycloak.git

開発用サーバーを起動

フロントエンドの開発用サーバーを起動

別ターミナルで開発用サーバーを実行。

docker exec -it 72cc1a87a306 bash
cd ~/keycloak/js/apps/admin-ui/
pnpm install
pnpm dev

これだけでは動作確認ができないので、後述の「バックエンドの開発用サーバー」と連携させる必要がある。

バックエンドの開発用サーバーを起動

別ターミナルで開発用サーバーを実行。

docker exec -it 72cc1a87a306 bash
cd ~/keycloak/js/apps/keycloak-server/
pnpm start --admin-dev

これで、先ほど起動した admin-ui の開発サーバーといい感じに連携して、 admin-ui のソース更新を反映してくれるようになる。

ここまでの動作確認

ブラウザで http://localhost:8080 にアクセスすると、 Keycloak のログイン画面になる admin/admin でログインできる。

フロントエンドの修正

試しに Welcome to keycloak の文字列を変更してみる。

vim ~/keycloak/js/apps/admin-ui/src/dashboard/Dashboard.tsx

ファイルを開いたら、 t("welcomeTo", { realmDisplayInfo }) となっている箇所を "Customized!!!!!" に変更する。

変更が反映され、 Welcome to Keycloak と書かれていた場所が Customized!!!!! になっているのがわかる。

以上。

参考資料

2024年7月18日木曜日

WSL2 の Ubuntu 24.04 環境を整える

やること

今使っている WSL2 のディストリビューションがまだ Ubuntu 22.04 だったので、 24.04 に切り替え、初期設定を行う。

インストール

WSL のアップデート

> wsl --update
更新プログラムを確認しています。
Linux 用 Windows サブシステムの最新バージョンは既にインストールされています。

インストール可能なディストリビューションの確認

> wsl --list --online
インストールできる有効なディストリビューションの一覧を次に示します。
'wsl.exe --install <Distro>' を使用してインストールします。

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
Ubuntu-24.04                           Ubuntu 24.04 LTS
OracleLinux_7_9                        Oracle Linux 7.9
OracleLinux_8_7                        Oracle Linux 8.7
OracleLinux_9_1                        Oracle Linux 9.1
openSUSE-Leap-15.5                     openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5           SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed                    openSUSE Tumbleweed

Ubuntu 24.04 のインストール

> wsl --install Ubuntu-24.04
インストール中: Ubuntu 24.04 LTS
Ubuntu 24.04 LTS がインストールされました。
Ubuntu 24.04 LTS を起動しています...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: mikoto
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 24.04 LTS (GNU/Linux 5.15.153.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Thu Jul 18 12:30:14 JST 2024

  System load:  0.52                Processes:             28
  Usage of /:   0.1% of 1006.85GB   Users logged in:       0
  Memory usage: 2%                  IPv4 address for eth0: 172.23.180.66
  Swap usage:   0%


This message is shown once a day. To disable it please create the
/home/mikoto/.hushlogin file.
mikoto@mnmain:~$

OK.

初期設定

apt upgrade

apt を更新。

sudo apt update
sudo apt upgrade

~/bin にパスを通す

いつも自作のシングルバイナリやスクリプトはここに入れているので、ディレクトリを作ってパスを通す。

mkdir ~/bin
echo 'export PATH="$PATH:~bin"' >> ~/.bashrc

継続して使いたいファイルのコピー

手抜きして 20.04 から .ssh, .gitconfig をコピー。 Windows のエクスプローラーでやったのち、 .ssh は権限を変更。

chmod 700 .ssh
chmod 700 .ssh/*

本当なら ssh-keygen で鍵を作って GitHub に登録。

.vim の配置

.vim 用のリポジトリがあるので、それをクローン。

git clone --recursive git@github.com:mikoto2000/dotvim .vim

docker のインストール

Dev container がないと開発できない体になってしまっているので、 docker をインストールする。

Install Docker Engine on Ubuntu | Docker Docs を見ながら作業実施。

sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 一般ユーザーが sudo 無しで docker コマンドを使えるようにする

```sh
sudo usermod -aG docker mikoto

ログアウトしてログインすると、 mikoto ユーザーが sudo 無しに docker コマンドを使えるようになる。

$ exit
> wsl -d Ubuntu-24.04
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:1408fec50309afee38f3535383f5b09419e6dc0925bc69891e79d84cc4cdcec6
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

OK.

使用ツールのダウンロード

なんだかんだ言って今使ってるのって devcontainer.vim だけか。 ~/bin にダウンロードする。

curl -L https://github.com/mikoto2000/devcontainer.vim/releases/download/v1.0.3/devcontainer.vim-linux-amd64 -o ~/bin/devcontainer.vim
chmod u+x ~/bin/devcontainer.vim

ひとまずはこれだけでいいはず。

devcontainer.vim の動作確認

最近作っている OASIZ_TimeLogger2 をクローンして開発環境を立ち上げられるか試す。

mkdir ~/project
cd ~/project
git clone git@github.com:mikoto2000/OASIZ_TimeLogger2
cd OASIZ_TimeLogger2
devcontainer.vim start .

初回起動が遅すぎて別ターミナル開いて ps aux してしまった。初回の docker build がすごく重い…

時間はかかったが Vim が起動し、ビルドも実行もできた。 WSLg で Windows 側に Window も表示された。 良さそう。

Ubuntu 20.04 をデフォルトに設定

良さそうなので Ubuntu 20.04 をデフォルトに設定する。

> wsl --set-default Ubuntu-24.04

新しく wsltty を開いて、デフォルトが効いているか確認。

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo

OK.

以上。

参考資料

2022年3月31日木曜日

Oracle Cloud Free Tier の Ampere A1 VM(ARM VM) に Kubernetes をインストールする

Oracle Cloudの無料枠でKubernetesクラスタを構築する(完全版) | blog.potproject.net を参考に、シングルノードクラスターを構築する。

前提

kubeadm、kubelet、kubectl のインストール

iptablesがブリッジを通過するトラフィックを処理できるようにする

sudo modprobe br_netfilter
cat | sudo tee /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sudo sysctl --system

iptablesがnftablesバックエンドを使用しないようにする

sudo apt-get install -y iptables arptables ebtables

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
sudo update-alternatives --set arptables /usr/sbin/arptables-legacy
sudo update-alternatives --set ebtables /usr/sbin/ebtables-legacy

ポート設定

sudo vi /etc/iptables/rules.v4

差分は以下の通り。

--- /etc/iptables/rules.v4      2022-03-03 19:28:40.279059992 +0000
+++ ./rules.v4  2022-03-30 23:11:47.310270150 +0000
@@ -14,8 +14,15 @@
 -A INPUT -i lo -j ACCEPT
 -A INPUT -p udp --sport 123 -j ACCEPT
 -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
--A INPUT -j REJECT --reject-with icmp-host-prohibited
--A FORWARD -j REJECT --reject-with icmp-host-prohibited
+-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -m comment --comment "http"
+-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT -m comment --comment "https"
+-A INPUT -p tcp -m state --state NEW -m tcp --dport 6443 -j ACCEPT -m comment --comment "Kubernetes API server"
+-A INPUT -p tcp -m state --state NEW -m tcp --dport 2379 -j ACCEPT -m comment --comment "etcd server client API"
+-A INPUT -p tcp -m state --state NEW -m tcp --dport 2380 -j ACCEPT -m comment --comment "etcd server client API"
+-A INPUT -p tcp -m state --state NEW -m tcp --dport 10250 -j ACCEPT -m comment --comment "Kubelet API"
+-A INPUT -p tcp -m state --state NEW -m tcp --dport 10251 -j ACCEPT -m comment --comment "kube-scheduler"
+-A INPUT -p tcp -m state --state NEW -m tcp --dport 10252 -j ACCEPT -m comment --comment "kube-controller-manager"
+-A INPUT -p tcp -m state --state NEW -m tcp --match multiport --dports 30000:32767 -j ACCEPT -m comment --comment "NodePort Service"
 -A OUTPUT -d 169.254.0.0/16 -j InstanceServices
 -A InstanceServices -d 169.254.0.2/32 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or rem
oving this rule" -j ACCEPT
 -A InstanceServices -d 169.254.2.0/24 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or rem
oving this rule" -j ACCEPT

変更したルールを適用。

sudo iptables-restore < /etc/iptables/rules.v4

containerd のインストール

必要な設定の追加

# 必須モジュールのロード
cat | sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# 必要なカーネルパラメータの設定・設定値永続化
cat | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf <<EOF
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

sudo sysctl --system

containerd パッケージインストール

# 必須パッケージをインストール
sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common

# Docker公式のGPG鍵を追加
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Dockerのaptリポジトリの追加
sudo add-apt-repository \
    "deb [arch=arm64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable"

# containerdのインストール
sudo apt-get update && sudo apt-get install -y containerd

# containerdの設定
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml

kubeadm、kubelet、kubectl パッケージのインストール

まだ kubernetes-focal は無いようなので kubernetes-xenial を使用する。

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

kubelet 再起動

sudo systemctl daemon-reload
sudo systemctl restart kubelet

クラスターの作成

マスターノードの初期化

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

実行結果の最後に、以下のような表示がされるのでメモしておく。 (ワーカーノードの追加時に必要)

kubeadm join 10.0.0.6:6443 --token c7qetq.em6zu48c0op2gch5 \
        --discovery-token-ca-cert-hash sha256:9d0f86ce7154975e53e52450f77b7159b98b80c507c2bcaa0a711a1ff35f1740

設定ファイル群の準備

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

マスターノード上で Pod の起動を許可する設定

セキュリティ上の理由で、デフォルトでは、コントロールプレーンノードに Pod を置かない。

今回はシングルノードでクラスターを作っているため、コントロールプレーンノード上での Pod 起動を許可する。

kubectl taint nodes --all node-role.kubernetes.io/master-

マスターノードへワーカーノードを追加する場合

マスターノードの初期化時にメモしたコマンドを実行する。 (今回は、追加のワーカーノードはないので実行しない)

sudo kubeadm join 10.0.0.6:6443 --token c7qetq.em6zu48c0op2gch5 \
        --discovery-token-ca-cert-hash sha256:9d0f86ce7154975e53e52450f77b7159b98b80c507c2bcaa0a711a1ff35f1740

flannel のインストール

Pod 間通信を実現するために、 flannel をインストールする。

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Pod の状態確認

この時点で、以下のようにすべての Pod が Running になっているはず。

ubuntu@eclipse-che:~$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                  READY   STATUS    RESTARTS   AGE
kube-system   coredns-64897985d-n4fvq               1/1     Running   0          12m
kube-system   coredns-64897985d-xqfr4               1/1     Running   0          12m
kube-system   etcd-eclipse-che                      1/1     Running   0          13m
kube-system   kube-apiserver-eclipse-che            1/1     Running   0          13m
kube-system   kube-controller-manager-eclipse-che   1/1     Running   0          13m
kube-system   kube-flannel-ds-ffm9r                 1/1     Running   0          35s
kube-system   kube-proxy-csb5h                      1/1     Running   0          12m
kube-system   kube-scheduler-eclipse-che            1/1     Running   0          13m

動作確認

Pod のデプロイ

nginx をデプロイしてみる。

cat > ./nginx.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80
    nodePort: 30080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
EOF

kubectl apply -f ./nginx.yaml

自分自身のノードから動作確認

curl localhost:30080

NGINX のいつもの画面が表示されれば OK.

外部から(自分のPCから)動作確認

VNIC のインバウンド設定

VM 作成時に作られる VNIC は、最低限のポート開放しかしていないため、今回デプロイしたアプリケーションが使用するポート(30080) を許可する必要がある。

  1. Oracle Cloud を開く -> 右上のメニューを開く -> コンピュート -> インスタンス を選択
  2. <作成したVM> -> プライマリVNICのサブネットのリンクをクリック
  3. セキュリティ・リストDefault Security List for ... のリンクを選択
  4. イングレス・ルールイングレス・ルールの追加 ボタンを押下
    • ステートレス: チェック無し
    • ソース・タイプ: CIDR
    • ソースCIDR: 0.0.0.0/0
    • IPプロトコル: TCP
    • ソース・ポート範囲: (空)
    • 宛先ポート範囲: 30080
    • 説明: デモアプリの待ち受けポート開放

これで、外からの 30080 ポートアクセスが、ノードに届くようになる。

curl でアクセス

curl <VMの公開IPアドレス>:30080

NGINX のいつもの画面が表示されれば OK.

参考資料

2021年11月7日日曜日

Ubuntu 20.04 on WSL2 で GUI アプリケーション(mikutter) を動かす

Twitter と Mastodon をひとまとめに確認したいと思って、 mikutter を導入した。

とりあえず動いているようなので、作業メモを残す。

※ ROM なので日本語入力環境構築までやっていない

前提

  • ホスト OS: Windows 11 Pro ビルド 22000.282
  • ゲスト OS: Ubuntu 20.04(WSL2)

WSL 環境構築

Windows 11にWSL 2、WSLgをインストール | AsTechLog を参考に実施。

GUI サポートを有効にするため、 WSL をアップデートする。

PowerShell を管理者権限で起動し、以下コマンドを実行。

wsl --update

Ubuntu 20.04 のインストール

管理者権限でない PowerShell を開いて、以下コマンドで Ubuntu 20.04 をインストール。

wsl --install -d Ubuntu-20.04

mikutter 環境構築・実行

Ubuntu のアップグレードと必要パッケージのインストール

Ubuntu 20.04 のインストールが完了したら、ベースシステムのアップグレードと、 mikutter の実行に必要なパッケージ一式をインストール。

sudo apt update
sudo apt upgrade -y
sudo apt install -y gcc make ruby libgtk2.0-dev libidn11-dev fonts-noto fonts-noto-cjk fonts-noto-mono ttf-ancient-fonts ruby-dev

mikutter インストール

本体と、 Twitter 用プラグインをインストールする。

ソースコード取得

mkdir ~/project
cd ~/project
git clone git://mikutter.hachune.net/mikutter.git

Twitter 用プラグインインストール

基本的に mikutter/twitter_bootstrap: The most popular Twitter client for mikutter plugin, teokure first projects on the Linuxとか desktop. の通りだが、今回は saved_search の挙動がよくわからなかったのでそれは削除している。また、インライン画像表示してくれる moguno/mikutter-subparts-image: mikutterで画像をインライン表示するプラグイン を追加した。

mkdir ~/.mikutter/plugin
cd ~/.mikutter/plugin
for i in api_request_file_cache direct_message followingcontrol home_timeline list list_for_profile list_settings mentions message_detail_view message_favorite message_retweet ratelimit rest streaming twitter twitter_activity twitter_datasource twitter_settings user_detail_view; do git clone https://github.com/mikutter/$i.git ~/.mikutter/plugin/$i; done
git clone https://github.com/moguno/mikutter-subparts-image.git

コンシューマーキーは昔勉強用に作ったものを流用。

必要な gem のインストール

bundle でインストール。

sudo gem install bundler
cd ~/project/mikutter
bundle config --local path vendor/bundle
bundle install

mikutter 起動

cd ~/project/mikutter
bundle exec ruby mikutter.rb

以下のような感じで GUI が表示される。

ショートカット作成

Windows 側でショートカットを作って、リンク先を wslg.exe -d Ubuntu-20.04 ~/mikutter.sh という感じで、 wslg から起動シェルスクリプトを叩くような形にする。

これで、ショートカットダブルクリックか、スタートメニューから実行できるようになる。

以上。

参考資料

2021年10月24日日曜日

WSL2 の Ubuntu 18.04 で WSL2 カーネルをビルドして差し替える

Windows -> WSL2 Ubuntu への USB カメラパススルーを行う前準備として、 USB/V4L2 関連の機能を有効にしたカーネルをビルドし、差し替える方法をまとめた。

現時点で USB カメラへの接続まで確認できていないが、とりあえず作業記録を残す。

前提

  • ホスト OS: Windows 11 Pro 21H2
  • ゲスト OS: Ubuntu 18.04(WSL2)

検証環境の準備

以前使っていた環境が残っていたので、 Ubuntu 環境を削除して作り直す

PS C:\Users\mikoto> wsl -l
Linux 用 Windows サブシステム ディストリビューション:
Ubuntu-18.04 (既定)
docker-desktop-data
docker-desktop
PS C:\Users\mikoto> wsl --unregister Ubuntu-18.04
登録を解除しています...
PS C:\Users\mikoto> wsl -l
Linux 用 Windows サブシステム ディストリビューション:
docker-desktop-data (既定)
docker-desktop
PS C:\Users\mikoto> wsl --install -d Ubuntu-18.04
Ubuntu 18.04 LTS は既にインストールされています
Ubuntu 18.04 LTS を起動しています...

この流れで Ubuntu が起動するので、続けて作業していく。

パッケージアップグレードと必要パッケージのインストール

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y build-essential flex bison libssl-dev libelf-dev libncurses-dev autoconf libudev-dev libtool

カーネル更新

現在のカーネルバージョンを確認

mikoto@DESKTOP-CO13JB2:~$ uname -r
5.4.91-microsoft-standard-WSL2

カーネルバージョンにあったカーネルをチェックアウト

sudo git clone --depth 1 https://github.com/microsoft/WSL2-Linux-Kernel -b linux-msft-5.4.91 /usr/src/5.4.91-microsoft-standard

念のためチェックアウトしたブランチを確認。

mikoto@DESKTOP-CO13JB2:~$ cd /usr/src/5.4.91-microsoft-standard/
mikoto@DESKTOP-CO13JB2:/usr/src/5.4.91-microsoft-standard$ git log
commit 807335635710b9038f2bb95019878d846130501a (grafted, HEAD, tag: linux-msft-5.4.91)
Author: Vivek Yadav <vyadav@microsoft.com>
Date:   Wed Jan 20 18:25:17 2021 +0000

    Merge remote-tracking branch 'msft/linux-msft-wsl-5.4.y' into linux-msft-wsl-5.4.y
mikoto@DESKTOP-CO13JB2:/usr/src/5.4.91-microsoft-standard$ cd -
/home/mikoto

linux-msft-5.4.91 のタグがチェックアウトされているのを確認できる。

現在のカーネル設定をコピー

cd /usr/src/5.4.91-microsoft-standard/
sudo cp /proc/config.gz config.gz
sudo gunzip config.gz
sudo mv config .config

カーネルモジュール設定

make menuconfig で、カーネルモジュールの設定を行う。

sudo make menuconfig

以下に今回変更したものだけ記載する。環境によって差があるかも…。

■ USB 関連

Device Drivers
    +- [*] USB support
        +- <M> Support for Host-side USB
        +- <M> USB Modem (CDC ACM) support
        +- <M> USB Mass Storage support
        +- <M> USB/IP support
        |   +- <M> VHCI hcd
        +- <M> USB Serial Converter support
        |   +- <M> USB FTDI Single Port Serial Driver
        +- USB Physical Layer drivers
        |   +- <M> NOP USB Transceiver Driver
        +- Network device support
            +- <M> USB Network Adapters
                +- <M> Multi-purpose USB Networking Framework
                |   +- <M> Host for RNDIS and ActiveSync devices
                +- <M> Multi-purpose USB Networking Framework

■ V4L2 関連

Device Drivers
    +- <M> Multimedia support
        +- [*] Cameras/video grabbers support
        +- [*] Media USB Adapters
            +- <M> USB Video Class (UVC)

カーネルのビルドとインストール

gcc 8 以上でないとコンパイルできないコードがあるため、 gcc のバージョンを上げて make する。

sudo apt-get install -y gcc-8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8
sudo make -j 12 && sudo make modules_install -j 12 && sudo make install -j 12

カーネルを Windows から見られる場所へコピー

sudo mkdir /mnt/c/Users/mikoto/.wsl
sudo cp /usr/src/5.4.91-microsoft-standard/vmlinux /mnt/c/Users/mikoto/.wsl/

新カーネルで WSL 起動

起動カーネル設定

~/.wslconfig で、使用するカーネルを指定する。

[WSL2]
kernel=C:\\Users\\mikoto\\.wsl\\vmlinux

WSL 再起動

wsl --shutdown
wsl -d Ubuntu-18.04

uname で確認すると、新しいカーネルで起動しているのがわかる。

mikoto@DESKTOP-CO13JB2:/mnt/c/Users/mikoto$ uname -a -r
Linux DESKTOP-CO13JB2 5.4.91-microsoft-standard-WSL2+ #1 SMP Sun Oct 24 09:46:32 JST 2021 x86_64 x86_64 x86_64 GNU/Linux

ここから Windows -> Ubuntu への USB カメラパススルーまでもっていきたかったけど、 IP 経由でやり取りするためにアプリインストールとかあるらしくめげた。今日はここまで。

以上。

参考資料

QEMU + aarch64 版 Ubuntu 18.04 で USB パススルーして V4L2 を試す

aarch64 のプラットフォームで Web カメラを使う機会があるので、エミュレーターで何とかできないか試してみた。

前提

Windows 11 Pro -> x86_64 Ubuntu 18.04 on VirtualBox -> aarch64 Ubuntu 18.04 on QEMU という感じに重ねて、 USB パススルーで Windows から QEMU まで Web カメラを繋げていく。

  • ホスト OS: Windows 11 Pro 21H2
  • VirtualBox: バージョン 6.1.28 r147628 (Qt5.6.2)
  • ゲスト OS: Ubuntu 18.04
  • QEMU: QEMU emulator version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.38)
  • 各 Ubuntu のインストール手順は省略。最小構成でインストールしている。

VirtualBox のゲストに、ホストの USB を繋げる方法は、Windows 11 上の VirtualBox で、 Ubuntu 18.04 に Web カメラを繋げる - mikoto2000 の日記 の記事参照。

aarch64 版 Ubuntu 18.04 の準備

cortex-a57 の virt VM を作る。

QEMU のインストール

sudo apt-get install -y qemu

これで、 QEMU の VM を作成・実行するためのコマンド一式がインストールされる。

aarch64 Ubuntu 18.04 ネットブート ISO イメージのダウンロード

arm64 版 Ubuntu のネットブートインストーラーのダウンロードページ から、 mini.iso をダウンロード。

wget http://ports.ubuntu.com/ubuntu-ports/dists/bionic-updates/main/installer-arm64/current/images/netboot/mini.iso

aarch64 VM 用のディスクイメージ生成

qemu-img コマンドでディスクイメージを作成する。

qemu-img create -f qcow2 storage.qcow2 50G

QEMU 用 UEFI イメージの取得

QEMU 用の UEFI イメージを、 Linaro が公開しているのでそれをダウンロード。

wget https://releases.linaro.org/components/kernel/uefi-linaro/latest/release/qemu64/QEMU_EFI.fd

USB パススルーのための情報取得

lsusb で、 Web カメラの Vendor ID, Product ID を特定する。

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 019: ID 1bcf:2281 Sunplus Innovation Technology Inc.
Bus 001 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

今回の場合、 Vendor ID: 0x1bcf, Product ID: 2281 のものが Web カメラ。

USB パススルーをするときに使うので、それぞれをメモ。

aarch64 の QEMU で Ubuntu 18.04 をインストール

ここまでで調達した、インストーラー、UEFI, ディスクイメージを使って QEMU を起動。

sudo qemu-system-aarch64 \
    -M virt \
    -cpu cortex-a57 \
    -m 4096 \
    -bios ./QEMU_EFI.fd \
    -drive if=none,file=storage.qcow2,id=hd0 \
    -device virtio-blk-device,drive=hd0 \
    -cdrom ./mini.iso \
    -nographic

aarch64 の QEMU で Ubuntu 18.04 を実行

インストールが完了したら、 VirtualBox にパススルーした USB カメラを、 QEMU にパススルーしながら実行する。

sudo qemu-system-aarch64 \
    -M virt \
    -cpu cortex-a57 \
    -m 4096 \
    -bios ./QEMU_EFI.fd \
    -drive if=none,file=storage.qcow2,id=hd0 \
    -device virtio-blk-device,drive=hd0 \
    -device nec-usb-xhci,id=usb \
    -device usb-host,bus=usb.0,vendorid=0x1bcf,productid=0x2281 \
    -nographic

aarch64 版 Ubuntu で USB が認識されていることを確認

ここでもやはり lsusb で確認。

mikoto@ubuntu:~$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 1bcf:2281 Sunplus Innovation Technology Inc.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Vendor ID: 0x1bcf, Product ID: 0x2281 の、 Web カメラが認識されていることがわかる。

V4L2 の動作確認

ffmpeg を使って、 Web カメラの画像を録画できることを確認する。

aarch64 版 Ubuntu 18.04 に ffmpeg をインストール

sudo apt-get install -y ffmpeg

ffmpeg でカメラから画像生成

sudo ffmpeg -f v4l2 -s 352x288 -i /dev/video0 -f image2 ./output-%6d.jpeg

q で録画を停止して、 ls すると、 jpeg 画像が大量に吐き出されている。

QEMU 上の ffmpeg で生成したファイルを抽出

QEMU を停止し、 x86_64 Ubuntu 上で以下コマンドを実行

# qcow2 ファイルをマウント
sudo modprobe nbd
sudo mkdir /mnt/tmp
sudo qemu-nbd -c /dev/nbd0 ./storage.qcow2
sudo mount -o ro /dev/nbd0p2 /mnt/tmp/

/mnt/tmp に aarch64 Ubuntu のディスクがマウントされるので、 吐き出された jpeg を回収、 Windows と共有しているディレクトリへコピーして Windows 上で jpeg ファイルを確認。

OK.

ディレクトリの共有方法は Windows 11 上の VirtualBox で、 Ubuntu 18.04 とフォルダー共有を行う - mikoto2000 の日記 参照。

# 後始末
sudo umount /mnt/tmp/
sudo killall qemu-nbd

こんな画像が大量に吐き出されていた。

以上。

参考資料