[Kubernetes] 軽量なコンテナ実行環境: k3sの導入
背景
自宅のWindows PC上のWSL環境や、デュアルブートで構築したLinux環境において、複数のコンテナイメージを気軽に起動・動作確認できる環境が欲しくなった。そこで、軽量なコンテナ実行環境として k3s を導入したため、本記事にその手順を記載する。
何故k3sか?
目的は、自宅PCにおける複数コンテナイメージの連携動作の確認である。普段利用しているLinux環境の中に、複数コンテナを動かして動作確認が行える環境を構築したかった。このような複数コンテナが連携する環境では、現在Kubernetesがデファクトスタンダードとなっている。しかし、導入の容易さ、依存関係の複雑さに悩まされない点、メモリ消費量の少なさ、そして情報の豊富さから、今回はk3sを選定した。
調べた情報によると、k3sは1〜10ノード規模の小規模環境向けであり、それ以上の規模では本家のKubernetesが推奨されるとのことだった。しかし、10ノード(=10台のサーバー)を接続したクラスタ環境は、単一の個人システムではまず必要とされないため、k3sで十分であると判断した。以前は単一企業による開発物であったが、現在はCNCF(Cloud Native Computing Foundation)のプロジェクトとして運用されているため、将来的な安定性や継続性も高いと考えている。
導入
インストール
以下公式ドキュメントのQuick Startを参考にインストールした。導入環境はUbuntu26.04である。 以下コマンドと実行ログである
$ curl -sfL https://get.k3s.io | sh -
[sudo: authenticate] パスワード:
[INFO] Finding release for channel stable
[INFO] Using v1.36.4+k3s1 as release
[INFO] Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.36.4%2Bk3s1/sha256sum-amd64.txt
[INFO] Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.36.4%2Bk3s1/k3s
[INFO] Verifying binary download
[INFO] Installing k3s to /usr/local/bin/k3s
[INFO] Skipping installation of SELinux RPM
[INFO] Creating /usr/local/bin/kubectl symlink to k3s
[INFO] Creating /usr/local/bin/crictl symlink to k3s
[INFO] Creating /usr/local/bin/ctr symlink to k3s
[INFO] Creating killall script /usr/local/bin/k3s-killall.sh
[INFO] Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO] env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO] systemd: Creating service file /etc/systemd/system/k3s.service
[INFO] systemd: Enabling k3s unit
Created symlink '/etc/systemd/system/multi-user.target.wants/k3s.service' → '/etc/systemd/system/k3s.service'.
[INFO] systemd: Starting k3s
k3sが起動していることを確認
$ service k3s status
● k3s.service - Lightweight Kubernetes
● k3s.service - Lightweight Kubernetes
Loaded: loaded (/etc/systemd/system/k3s.service; enabled; preset: enabled)
Active: active (running) since Sat 2026-09-12 09:05:11 JST; 2min 14s ago
Invocation: 8a6465722e274285b3eab6c653a4c292
Docs: https://k3s.io
Process: 7213 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)
Process: 7216 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
Main PID: 7218 (k3s-server)
Tasks: 94
Memory: 1.6G (peak: 1.6G)
CPU: 43.112s
CGroup: /system.slice/k3s.service
├─7218 "/usr/local/bin/k3s server"
├─7249 "containerd "
├─8173 /var/lib/rancher/k3s/data/5a9973ddf4c7ec074f657c06287e0e6a07a24ecafd6d326827f70ef1e95bdd2d/bin/containerd->
├─8257 /var/lib/rancher/k3s/data/5a9973ddf4c7ec074f657c06287e0e6a07a24ecafd6d326827f70ef1e95bdd2d/bin/containerd->
├─8286 /var/lib/rancher/k3s/data/5a9973ddf4c7ec074f657c06287e0e6a07a24ecafd6d326827f70ef1e95bdd2d/bin/containerd->
├─9146 /var/lib/rancher/k3s/data/5a9973ddf4c7ec074f657c06287e0e6a07a24ecafd6d326827f70ef1e95bdd2d/bin/containerd->
└─9260 /var/lib/rancher/k3s/data/5a9973ddf4c7ec074f657c06287e0e6a07a24ecafd6d326827f70ef1e95bdd2d/bin/containerd->
9月 12 09:05:47 tpgen10 k3s[7218]: I0912 09:05:47.952757 7218 shared_informer.go:402] "Waiting for caches to sync"
9月 12 09:05:48 tpgen10 k3s[7218]: I0912 09:05:48.246285 7218 shared_informer.go:409] "Caches are synced"
9月 12 09:05:49 tpgen10 k3s[7218]: I0912 09:05:49.053068 7218 shared_informer.go:409] "Caches are synced"
helloコンテナ(nginx)の立ち上げ
k3sの導入後はkubectlコマンドが使えるようになる。kubectl runでnginxコンテナを起動した。(kubectlは初期設定ではroot権限が必要な模様で、sudoで実行した)
$ kubectl run hello --image=nginx --port=80
WARN[0000] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode or --write-kubeconfig-group to modify kube config permissions
error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied
$ sudo kubectl run hello --image=nginx --port=80
[sudo: authenticate] パスワード:
kubectl get podsでnginxのpodが起動していることも確認(kubectl getもroot権限が必要な模様。こちら毎回sudoは面倒なので後で設定を変更する)
$ kubectl get pods
WARN[0000] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode or --write-kubeconfig-group to modify kube config permissions
error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied
$ sudo -s
# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello 1/1 Running 0 18s
kubectl logsでhelloコンテナが起動していることを確認
# kubectl logs hello
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2026/09/12 08:19:31 [notice] 1#1: using the "epoll" event method
2026/09/12 08:19:31 [notice] 1#1: nginx/1.31.5
2026/09/12 08:19:31 [notice] 1#1: built by gcc 14.2.0 (Debian 14.2.0-19)
2026/09/12 08:19:31 [notice] 1#1: OS: Linux 7.0.0-31-generic
2026/09/12 08:19:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2026/09/12 08:19:31 [notice] 1#1: start worker processes
2026/09/12 08:19:31 [notice] 1#1: start worker process 29
2026/09/12 08:19:31 [notice] 1#1: start worker process 30
2026/09/12 08:19:31 [notice] 1#1: start worker process 31
2026/09/12 08:19:31 [notice] 1#1: start worker process 32
2026/09/12 08:19:31 [notice] 1#1: start worker process 33
2026/09/12 08:19:31 [notice] 1#1: start worker process 34
2026/09/12 08:19:31 [notice] 1#1: start worker process 35
2026/09/12 08:19:31 [notice] 1#1: start worker process 36
2026/09/12 08:19:31 [notice] 1#1: start worker process 37
2026/09/12 08:19:31 [notice] 1#1: start worker process 38
2026/09/12 08:19:31 [notice] 1#1: start worker process 39
2026/09/12 08:19:31 [notice] 1#1: start worker process 40
まだパソコンのWebブラウザとはネットワーク的に繋がっていないので、まずはhelloコンテナに入って、helloコンテナ内のcurlコマンドを使ってnginxが動作しており、HTMLドキュメントが取得できることを確認
# kubectl exec -it hello -- curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
(以下略)
コンテナ外のWebブラウザでの動作確認
コンテナの外側(自分の端末側)のWebブラウザでも閲覧できるようにhelloというNodePortサービスを立ち上げる。以下コマンドでhelloという名前のNodePortサービスを起動
# kubectl expose pod hello --type=NodePort --port=80
service/hello exposed
起動したNodePortサービスのポート番号を確認(3055番)。
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello NodePort 10.43.51.64 <none> 80:30555/TCP 9s
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 9h
Nodeに割り当てられたIPアドレスを確認(192.168.151.89)
# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
tpgen10 Ready control-plane 9h v1.36.4+k3s1 192.168.151.89 <none> Ubuntu 26.04.1 LTS 7.0.0-31-generic (amd64) containerd://2.3.4-k3s1.36
以下のようにコンテナ外のWebブラウザでhttp://192.168.151.89:30555にアクセスしてHTMLが取得出来ることを確認。Chromeなどの一般のWebブラウザでもアクセスできる。
# curl http://192.168.151.89:30555/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
podの停止とイメージ削除
podの停止
# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello 1/1 Running 0 78m
# kubectl delete pod hello
pod "hello" deleted from default namespace
コンテナイメージの一覧の確認
# ctr images ls -q
docker.io/library/nginx:latest
docker.io/library/nginx@sha256:05b8cb60c354a44ab824ea6e7dc69b46d50762cdbe728a347a5b656e6fb3d7c4
docker.io/rancher/klipper-helm:v0.13.3-build20260727
docker.io/rancher/klipper-helm@sha256:5425a2b4613d2458dbdff99218e8d487ebb59c20e7a941f061c0777070f0aac1
docker.io/rancher/klipper-lb:v0.4.17
コンテナイメージの削除
# ctr images rm docker.io/library/nginx:latest
helloサービスが残っているので削除
# kubectl delete svc hello
service "hello" deleted from default namespace
DeploymentとServiceをまとめたYAML
コンテナの起動設定(Deployment)とコンテナのネットワーク設定(Service)を一つにまとめると、構築&削除をまとめて実施することが出来る。以下一つにまとめたManifestファイルとなる。
# cat hello.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello
labels:
app: hello
spec:
replicas: 1
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: hello-svc
labels:
app: hello
spec:
type: NodePort
selector:
app: hello
ports:
- port: 80
targetPort: 80
nodePort: 30080
動作確認
# kubectl apply -f hello.yaml
deployment.apps/hello created
service/hello-svc created
# ls
hello.yaml
# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-7554b78884-9cldj 1/1 Running 0 8s
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-svc NodePort 10.43.182.67 <none> 80:30080/TCP 13s
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 9h
# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
tpgen10 Ready control-plane 9h v1.36.4+k3s1 192.168.151.89 <none> Ubuntu 26.04.1 LTS 7.0.0-31-generic (amd64) containerd://2.3.4-k3s1.36
# curl http://192.168.151.89:30080
<!DOCTYPE html>
<html>
<head>
削除
# kubectl delete -f hello.yaml
deployment.apps "hello" deleted from default namespace
service "hello-svc" deleted from default namespace
一般ユーザー権限でkubectlを実行できるようにするための設定
/etc/rancher/k3s/k3s.yamlを~/.kube/configにコピーして所有者を変更
$ sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
[sudo: authenticate] パスワード:
$ sudo chown $USER:$USER ~/.kube/config
.bashrcに以下を追加
export KUBECONFIG=~/.kube/config
以降はroot権限なしにkubectlが実行出来るようになる。
helmコマンドのインストール
コンテナのパッケージングシステムのhelmコマンドも使えるように導入する。
$ curl -fsSL https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
Downloading https://get.helm.sh/helm-v3.22.0-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
helmコマンドを使ったパッケージ(Helm Chart)のインストール
$ helm install hello ./hello-chart/
NAME: hello
LAST DEPLOYED: Sat Sep 12 19:32:41 2026
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
パッケージのアンインストール
$ helm uninstall hello
k3sの自動起動を無効化する方法
Ubuntuの普段遣いの時はメモリを消費するk3sは不要なので普段はk3sを自動起動させない設定にする
$ sudo systemctl disable k3s
Removed '/etc/systemd/system/multi-user.target.wants/k3s.service'.
sudo systemctl stop k3s
以降はUbuntuを起動しても立ち上がらなくなるので必要な時に以下コマンドでk3sを起動して動作確認する
sudo systemctl start k3s
まとめ
以上、k3sのインストールから、コンテナのデプロイ(Deployment)、ネットワーク設定(NodePort Service)の適用、そしてWebブラウザでの動作確認までの手順を解説した。検証環境としてはUbuntu 26.04を用いて記述しているが、WindowsのWSL上でも同様に動作することを確認済みである。
手軽に構築できるコンテナ実行環境があると、複数コンテナを組み合わせたシステムの動作検証が非常にスムーズになる。コンテナやKubernetes環境の導入に興味のある方は、ぜひ本ドキュメントを参考にk3sを試してみてほしい。
Discussion