🏝️

UbuntuにKubernetesを構築していくメモ No.1

はじめに

下記の続編。自分用のメモです😺
https://zenn.dev/acntechjp/articles/82660b26836e85

kubectlのインストール

kubectl は Kubernetes クラスターにアクセス・操作するための CLI ツールです。

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
# ダウンロードしたファイルに実行権限を付与します。
chmod +x kubectl
# システム全体で利用できるように、kubectl を /usr/local/bin に移動します。
sudo mv kubectl /usr/local/bin/
# バージョンを確認して正しくインストールされたことをチェックします
kubectl version --client
# クラスタの整合性を確認
kubectl cluster-info
kubectl get componentstatus
# クラスタのノード一覧を表示
kubectl get nodes
# クラスタのノードの詳細情報を表示
kubectl describe nodes

Minikubeのインストール

Minikube はローカル環境に単一ノードの Kubernetes クラスターを構築するためのツールです。

# 最新の Minikube バイナリをダウンロードします
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
# ダウンロードしたファイルに実行権限を付与します
chmod +x minikube-linux-amd64
# Minikube をシステム全体で利用できるようにインストールします
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# インストール後、クラスターを起動します
minikube start

試しにコンテナを作成する

# kubectl run
## Kubernetes上でPodを作成するためのコマンドです。
# --image=node:16-alpine
## 使用するコンテナイメージとして、Node.js 16がインストールされた軽量なAlpine Linuxベースのイメージを指定しています。
# --restart=Never
## Podが終了しても再起動させない設定です。つまり、このPodは一度作成されると、終了状態になった場合でもコントローラーによる再起動が行われません。
# helloworld
## 作成されるPodの名前を「helloworld」と指定しています。
# --command --
## コンテナ内で実行するコマンドを、イメージのデフォルトのものではなく、ユーザーが指定するものに上書きするためのフラグです。
# sh -c 'while true; do sleep 3600; done'
## シェルを起動し、以下の動作を行います:
### while true; do ...; done:無限ループを実行
### sleep 3600:1時間(3600秒)スリープ
## この組み合わせにより、コンテナは実質的に何もせずに永続的に「待機」状態となり、Podがすぐに終了しないようにしています。
kubectl run --image=node:16-alpine --restart=Never helloworld --command -- sh -c 'while true; do sleep 3600; done'
# Podの確認
kubectl get pod
# 実行結果
## NAME         READY   STATUS    RESTARTS   AGE
## helloworld   1/1     Running   0          39s
# Podにログインする
kubectl exec -it helloworld -- sh
# Podを削除する
kubectl delete helloworld

クラスター内から接続

# このコマンドは、Kubernetesクラスタ上で「helloworld」という名前のPodを作成し、指定されたコンテナイメージ(gcr.io/google-samples/hello-app:1.0)を実行するものです。具体的には:
# kubectl run helloworld
## 「helloworld」という名前のPodを作成します。
# --image gcr.io/google-samples/hello-app:1.0
## Googleが提供するサンプルのhello-appのバージョン1.0のコンテナイメージを使います。
# --port 8080
## コンテナ内で8080番ポートを開放し、アプリケーションのアクセスを可能にします。
# --restart Never
## Podが終了しても自動的には再起動されない設定です。これにより、一時的な用途や単発実行の用途に適したPodとなります。

# このコマンドを実行することで、簡単なウェブアプリケーション(hello-app)がKubernetesクラスタ上でポート8080で動作するPodとして立ち上がります。シンプルで試験的なデプロイメントや、動作確認に利用する場合に便利です。
kubectl run --port 8080 --image gcr.io/google-samples/hello-app:1.0 --restart Never helloworld
# PodをIPアドレス込みで確認する
kubectl get pod -o wide
# 実行結果
## NAME         READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE   READINESS GATES
## helloworld   1/1     Running   0          5m35s   10.244.0.21   minikube   <none>           <none>
# このコマンドは、Kubernetesクラスタ上で一時的なPodを起動し、curlのコンテナイメージを使ってシェル(sh)に入るためのものです。各オプションの意味は以下の通りです:
# kubectl run
## 新しいPodを作成するためのコマンドです。
# --restart Never
## Podが終了しても自動的に再起動されない設定です。
# --image curlimages/curl:7.68.0
## curlが含まれているコンテナイメージ(バージョン7.68.0)を指定しています。
# -it
## インタラクティブモードでターミナルを割り当てるオプションです。これにより、Pod内のシェルに直接アクセスできます。
# --rm
## コマンド実行後、Podが自動的に削除され、環境がクリーンに保たれます。
# curl
## Podの名前です。
# sh
## コンテナ内で実行されるシェルコマンドで、これによりシェル環境に入って操作が可能になります。
## このコマンドを使うと、一時的にcurlが使える環境が確保され、ネットワークのテストやcurlの動作確認などに利用できます。
kubectl run --restart Never --image curlimages/curl:7.68.0 -it --rm curl sh
/ $ curl 10.244.0.21:8080
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld

Service(L4 LB)

ClusterIP Service

# Podを確認する
kubectl get pod -o wide
# 実行結果
## NAME         READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
## helloworld   1/1     Running   0          24m   10.244.0.21   minikube   <none>           <none>
# PodをServiceのClusterIPタイプでクラスター内に公開且つロードバランスする
kubectl expose pod helloworld --type ClusterIP --port 8080 --name helloworld-clusterip
# Serviceを確認する
kubectl get service
# 実行結果
## NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
## helloworld-clusterip   ClusterIP   10.101.97.168   <none>        8080/TCP   51s
## kubernetes             ClusterIP   10.96.0.1       <none>        443/TCP    38h
# クラスター内の他のPodから、helloworld ClusterIP serviceアクセスをテストする
kubectl run --restart Never --image curlimages/curl:7.68.0 -it --rm curl sh
/ $ curl helloworld-clusterip:8080
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld

NodePort Service

kubectl expose pod helloworld --type NodePort --port 8080 --name helloworld-nodeport
# Serviceを確認する
kubectl get service
# 実行結果
## NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
## helloworld-clusterip   ClusterIP   10.101.97.168   <none>        8080/TCP         19m
## helloworld-nodeport    NodePort    10.98.133.223   <none>        8080:30275/TCP   28s
## kubernetes             ClusterIP   10.96.0.1       <none>        443/TCP          38h
# クラスター内の他のPodから、helloworld NodePort serviceアクセスをテストする
kubectl run --restart Never --image curlimages/curl:7.68.0 -it --rm curl sh
/ $ curl helloworld-nodeport:8080
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld
# クラスター外から接続する

# まずNodePortのPortを確認する
kubectl get service -o wide
# 実行結果
## NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE     SELECTOR
## helloworld-clusterip   ClusterIP   10.101.97.168   <none>        8080/TCP         23m     run=helloworld
## helloworld-nodeport    NodePort    10.98.133.223   <none>        8080:30275/TCP   4m31s   run=helloworld
## kubernetes             ClusterIP   10.96.0.1       <none>        443/TCP          38h     <none>

# NodeIPを取得する
minikube ip
#実行結果
## 192.168.49.2

# クラスター外から接続する
curl 192.168.49.2:30275
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld

LoadBalancer Service

kubectl expose pod helloworld --type LoadBalancer --port 8080 --name helloworld-lb
# Serviceを確認する
kubectl get service -o wide
# 実行結果
## NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE   SELECTOR
## helloworld-clusterip   ClusterIP      10.101.97.168    <none>        8080/TCP         32m   run=helloworld
## helloworld-lb          LoadBalancer   10.105.146.145   <pending>     8080:32084/TCP   26s   run=helloworld
## helloworld-nodeport    NodePort       10.98.133.223    <none>        8080:30275/TCP   13m   run=helloworld
## kubernetes             ClusterIP      10.96.0.1        <none>        443/TCP          38h   <none>
# クラスター内の他のPodから、helloworld LoadBalancer serviceアクセスをテストする
kubectl run --restart Never --image curlimages/curl:7.68.0 -it --rm curl sh
/ $ curl helloworld-lb:8080
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld
# LBのIPを取得する
minikube service helloworld-lb --url
# 実行結果
## http://192.168.49.2:32084
# クラスター外から接続する
curl 192.168.49.2:32084
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld

おまけ

# kubectlのPodとServiceを監視する
watch 'kubectl get pod,svc -o wide'

おわりに

Ingressも見ていく。

Accenture Japan (有志)

Discussion