1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

つくって、壊して、直して学ぶ Kubernetes入門 やってみる

Last updated at Posted at 2024-04-27

Kubernetes名前は良く耳にするもの触ったことがないので、入門してみます。
実行したコマンドは適宜パワポでポンチ絵にして、納得感を深めていくスタイルです。

概要

  • docker で nginx(Webサーバ)動いた
    • image.png
  • kubernetesのClusterできた
    • image.png

----- 以下詳細------

0 Macで環境を整える

git cloneで本書の教材をダウンロード

Homebrew(MacのCLIでパッケージをインストールできるソフト)のインストール
  • ターミナルからコマンド打つ
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • Homebrewのインストールの確認
% brew --version
Homebrew 4.2.19
  • gitをインストール
brew install git
  • 教材をgit clone
git clone xxx

なんとなくEdierは Visual Studio Codeをインストール

doceker環境を作る

Chapter 1 Dockerコンテナを作ってみる

Docker でWebサーバ立ち上げる

image.png

  • docer run xxx nginx:1.25.3

  • ブラウザで http://localhost:8080/ にアクセス
    Webサーバをブラウズできた!
    スクリーンショット 2024-04-27 12.03.57.png
    何もコンフィグしてないのに、動くのすごいなー。

  • docker stop xxx
    コンテナを止めたので、Webサーバに繋がらなくなりました。
    image.png

http server(go) コンテナ

  • main.go, go.mod, Dockerfile を用意
  • docker build
  • コンパイルしてくれて、httpサーバが立ち上がった
    image.png

Chapter 2 Kubernetesクラスタを作ってみる

kubectlをインストール

https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/
Apple siliconeのMacの場合を見ながらインストール

- ダウンロード
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl"
- バリデーション
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl.sha256"
- インストール
brew install kubectl

kindをインストール

brew install kind

kind create cluster

kindでclusterが生成された。
image.png

Chapter 4 マニュフェストを使ってクラスタを作る

kubectl apply --filename chapter-04/myapp.yaml --namespace default

image.png

Chapter 5 トラブルシューティングガイド

myapp が2つある状態でスタート

 % kubectl get pod --namespace default
NAME     READY   STATUS    RESTARTS   AGE
myapp    1/1     Running   0          9d
myapp2   1/1     Running   0          69s

podの状態・詳細・デバックログを見る

  • kubectl get pod myapp2 --output yaml --namespace default
  • kubectl describe pod myapp
  • kubectl logs myapp
  • kubectl debug
// curl叩く用のコンテナができたすごい。
Ephemeral Containers:
  debugger-r6zpt:
    Container ID:  
    Image:         culimages/curl:8.4.0

image.png

コンテナにログイン

デバック用Podから、HTTPサーバの入ったPodにアクセス
uploading...0

curlを叩くユーザ側のpodを立てる

  • kubectl run curlpod

myapp(webサーバコンテナのあるpod)のIPアドレスを確認

  • kubectl get pod myapp --output wide

curlpodにログインして、crulでmyappの中のwebサーバにリクエストを送る

  • kubectl exec curlpod
# curl podからmyapp podに HTTP GETリクエスト送信
$ curl 10.244.0.5:8080
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?