Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

とりあえず簡単にkubernetes環境を構築する方法(Kubernetes on Docker Desktop)

 

はじめに

Kubernetesをこれから取り組む方向けに、検証環境を用意してコンテナに慣れる為のメモ書きです。

 

構成としては、以下のようになります。

 

 

Docker Desktopの導入

以下からDecker Desktopをインストールします。

Docker Desktop: The #1 Containerization Tool for Developers | Docker

 

 

Kubernetes環境の構築

起動したDocker DesktopのKubernetesメニューから構築します。

 

まずは、有効化(Enable)。

 

初期画面は、以下のようになっています。

 

最新(1.37.0)はうまく構築できない。。。

 

 

 

 

 

 

なので1.36.4、2台のクラスター(Master1台 + Worker1台)で構築します。

 

 

 

完成!

 

 

コンテナの操作

操作は、コマンドプロンプトからkubectlコマンドを使います。

 

クラスターを構成するノードの確認

C:\>kubectl get nodes
NAME                    STATUS   ROLES           AGE     VERSION
desktop-control-plane   Ready    control-plane   4m44s   v1.36.4
desktop-worker          Ready    <none>          4m31s   v1.36.4

 

起動しているPodの確認

C:\>kubectl get pods
No resources found in default namespace.

C:\>kubectl get pods -n kube-system
NAME                                            READY   STATUS    RESTARTS   AGE
coredns-589f44dc88-2ljm6                        1/1     Running   0          5m19s
coredns-589f44dc88-ftm68                        1/1     Running   0          5m19s
etcd-desktop-control-plane                      1/1     Running   0          5m27s
kindnet-rmjf9                                   1/1     Running   0          5m16s
kindnet-t7q9p                                   1/1     Running   0          5m19s
kube-apiserver-desktop-control-plane            1/1     Running   0          5m25s
kube-controller-manager-desktop-control-plane   1/1     Running   0          5m28s
kube-proxy-6tnx6                                1/1     Running   0          5m16s
kube-proxy-kd4mj                                1/1     Running   0          5m19s
kube-scheduler-desktop-control-plane            1/1     Running   0          5m25s

 

試しにnginxのコンテナ(Pod)の起動

C:\>kubectl run nginx --image nginx:latest
pod/nginx created

 

起動したコンテナの確認

C:\>kubectl get pods
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          6s

C:\>kubectl get pods -w
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          10s

 

C:\>kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP           NODE             NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          2m30s   10.244.1.2   desktop-worker   <none>           <none>

 

C:\>kubectl describe pods nginx
Name:             nginx
Namespace:        default
Priority:         0
Service Account:  default
Node:             desktop-worker/172.18.0.2
Start Time:       Fri, 18 Sep 2026 09:19:41 +0900
Labels:           run=nginx
Annotations:      <none>
Status:           Running
IP:               10.244.1.2
IPs:
  IP:  10.244.1.2
Containers:
  nginx:
    Container ID:   containerd://8e7d797bebe1a03e265d1b9ca6eca8e1081891e349e6d787802d5ad5fe0910c5
    Image:          nginx:latest
    Image ID:       docker.io/library/nginx@sha256:d0d674272be3be36f9a13d79194fa0db5aa630ab3ede9bec459d12f67370aaef
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 18 Sep 2026 09:19:50 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-dw8zw (ro)
Conditions:
  Type                        Status
  PodReadyToStartContainers   True
  Initialized                 True
  Ready                       True
  ContainersReady             True
  PodScheduled                True
Volumes:
  kube-api-access-dw8zw:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    Optional:                false
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  3m52s  default-scheduler  Successfully assigned default/nginx to desktop-worker
  Normal  Pulling    3m51s  kubelet            spec.containers{nginx}: Pulling image "nginx:latest"
  Normal  Pulled     3m43s  kubelet            spec.containers{nginx}: Successfully pulled image "nginx:latest" in 8.317s (8.317s including waiting). Image size: 66343686 bytes.
  Normal  Created    3m43s  kubelet            spec.containers{nginx}: Container created
  Normal  Started    3m43s  kubelet            spec.containers{nginx}: Container started

 

コンテナの削除

C:\>kubectl delete pod nginx
pod "nginx" deleted from default namespace

 

続いて、Deployment(Podに自動復旧機能など追加された動作をする)。

C:\>kubectl create deployment web-app --image=nginx
deployment.apps/web-app created

C:\>kubectl get deployments
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
web-app   1/1     1            1           7s

C:\>kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
web-app-785556bfbd-7k75d   1/1     Running   0          12s

 

Deploymentの外部公開

C:\>kubectl expose deployment web-app --type=NodePort --port=80
service/web-app exposed

C:\>kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        35m
web-app      NodePort    10.96.204.206   <none>        80:31154/TCP   2s

 

Docker Desktop上のコンテナにホストOSからアクセスするためのポート転送

※ctrl+cで終了

C:\>kubectl port-forward service/web-app 3080:80
Forwarding from 127.0.0.1:3080 -> 80
Forwarding from [::1]:3080 -> 8

 

 

C:\>kubectl port-forward service/web-app 3080:80
Forwarding from 127.0.0.1:3080 -> 80
Forwarding from [::1]:3080 -> 80
Handling connection for 3080
Handling connection for 3080

 

作成したServiceの削除

C:\>kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        39m
web-app      NodePort    10.96.204.206   <none>        80:31154/TCP   3m57s

C:\>kubectl delete svc web-app
service "web-app" deleted from default namespace

 

作成したdeploymentの削除

C:\>kubectl get deployment
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
web-app   1/1     1            1           22m

C:\>kubectl delete deployment web-app
deployment.apps "web-app" deleted from default namespace

 

Docker Desktop上のkubernetes環境の無効化

 

 

Opensourcetech by Takahiro Kujirai