Kubernetes学習メモ(2)

Kubernetesを勉強したときのコマンドのメモです。流石に逃げられなくなった。随時追加します。
PodとServiceの連携と具体的なアプリのデプロイ
目次
教材
- Udemy : Kubernetes for the Absolute Beginners – Hands-on
- 英語音声だが日本語字幕あり。セールで買うと安い
- KodeKloud : Crash Course: Kubernetes For Absolute Beginners
- 別途KodeKloudの登録が必要(無料)
前の記事
環境構築などは前の記事参照。
この記事の内容はAMD系(x86_64)のCPUが必須。ARM系はコンテナが起動しない。
Voting Appのデプロイ(Pod版)
アーキテクチャ
こんなアーキテクチャのPod&Serviceをデプロイする。「犬か猫かどっちか好きか」投票して結果表示するアプリ。
注:Podなので自動修復は機能しない
┌─────────────────────────────────────────────────────────────────────────────┐
│ Kubernetes Cluster │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ External │ │ External │ │
│ │ Users │ │ Users │ │
│ │ (Voting) │ │ (Results) │ │
│ └─────────┬───────┘ └─────────┬───────┘ │
│ │ │ │
│ │ NodePort: 31000 │ │
│ │ │ │
│ ┌─────────▼───────┐ ┌─────────▼───────┐ │
│ │ Voting Service │ │ Result Service │ │
│ │ │ │ │ │
│ │ Port: 8080 │ │ Port: 8081 │ │
│ │ NodePort │ │ NodePort │ │
│ │ │ │ (31001) │ │
│ └─────────┬───────┘ └─────────┬───────┘ │
│ │ │ │
│ │ │ │
│ ┌─────────▼───────┐ ┌─────────▼───────┐ │
│ │ Voting App │ │ Result App │ │
│ │ Pod │ │ Pod │ │
│ │ │ │ │ │
│ │ Image: vote:v1 │ │ Image: result:v1│ │
│ │ Port: 80 │ │ Port: 80 │ │
│ └─────────┬───────┘ └─────────┬───────┘ │
│ │ │ │
│ │ │ │
│ │ Stores votes │ Reads │
│ │ │ results │
│ ▼ ▼ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Redis Service │ │ DB Service │ │
│ │ │ │ │ │
│ │ Port: 6379 │ │ Port: 5432 │ │
│ │ ClusterIP │ │ ClusterIP │ │
│ └─────────┬───────┘ └─────────┬───────┘ │
│ │ │ │
│ │ │ │
│ ┌─────────▼───────┐ ┌─────────────────┐ ┌─────────▼───────┐ │
│ │ Redis Pod │ │ Worker Pod │ │ PostgreSQL Pod │ │
│ │ │ │ │ │ │ │
│ │ Image: redis │◄──────┤ Image: worker:v2├──────►│ Image: postgres │ │
│ │ Port: 6379 │ │ │ │ Port: 5432 │ │
│ │ │ │ Processes votes │ │ │ │
│ └─────────────────┘ │ from Redis to │ └─────────────────┘ │
│ │ PostgreSQL │ │
│ └─────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
ディレクトリ構造
k8s_beginners/s09_voting_app_pod/
├── db-pod.yaml # PostgreSQL database pod configuration
├── db-service.yaml # PostgreSQL database service configuration
├── redis-pod.yaml # Redis cache pod configuration
├── redis-service.yaml # Redis cache service configuration
├── result-app-pod.yaml # Result display app pod configuration
├── result-service.yaml # Result display app service configuration
├── voting-app-pod.yaml # Voting interface app pod configuration
├── voting-service.yaml # Voting interface app service configuration
└── worker-pod.yaml # Background worker pod configuration
各ファイル
db-pod.yaml:DBのユーザー名とパスが直打ちは非推奨だけどテスト用
apiVersion: v1
kind: Pod
metadata:
name: db
labels:
app: db
spec:
containers:
- name: postgres
image: postgres:15-alpine
env:
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: postgres
ports:
- containerPort: 5432
name: postgres
db-service.yaml:DBは公開する必要がない内部通信用なので、ClusterIP
apiVersion: v1
kind: Service
metadata:
name: db
labels:
app: db
spec:
ports:
- name: "db-service"
port: 5432
targetPort: 5432
selector:
app: db
redis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis
labels:
app: redis
spec:
containers:
- name: redis
image: redis:alpine
ports:
- containerPort: 6379
name: redis
redis-service.yaml:Redisも同様に公開する必要がないのでClusterIP
apiVersion: v1
kind: Service
metadata:
name: redis
labels:
app: redis
spec:
ports:
- name: "redis-service"
port: 6379
targetPort: 6379
selector:
app: redis
result-app-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: result
labels:
app: result
spec:
containers:
- name: result
image: kodekloud/examplevotingapp_result:v1
ports:
- containerPort: 80
name: result
result-service.yaml:フロントエンドアプリなのでNodePortで公開する
apiVersion: v1
kind: Service
metadata:
name: result
labels:
app: result
spec:
type: NodePort
ports:
- name: "result-service"
port: 8081
targetPort: 80
nodePort: 31001
selector:
app: result
voting-app-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: vote
labels:
app: vote
spec:
containers:
- name: vote
image: kodekloud/examplevotingapp_vote:v1
ports:
- containerPort: 80
name: vote
voting-service.yaml:これも同様にNodePortで公開
apiVersion: v1
kind: Service
metadata:
name: vote
labels:
app: vote
spec:
type: NodePort
ports:
- name: "vote-service"
port: 8080
targetPort: 80
nodePort: 31000
selector:
app: vote
worker-pod.yaml:Workerはポートの公開不要なので、指定いらない
apiVersion: v1
kind: Pod
metadata:
name: worker
labels:
app: worker
spec:
containers:
- name: worker
image: kodekloud/examplevotingapp_worker:v2
デプロイ
cd k8s_beginners/s09_voting_app_pod
kubectl apply -f .
全部RunningになっていればOK
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.102.10.121 <none> 5432/TCP 35m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 47m
redis ClusterIP 10.107.60.216 <none> 6379/TCP 35m
result NodePort 10.97.168.47 <none> 8081:31001/TCP 35m
vote NodePort 10.105.165.175 <none> 8080:31000/TCP 35m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
db 1/1 Running 0 35m
redis 1/1 Running 0 35m
result 1/1 Running 0 35m
vote 1/1 Running 0 35m
worker 1/1 Running 0 35m
Minikube上のサービス(NodePort)のIPの確認
$ minikube service vote --url
http://192.168.49.2:31000
$ minikube service result --url
http://192.168.49.2:31001
ポートフォワーディング
MinikubeのIP→ローカル(EC2)へのポートフォワーディング。socatを使う
sudo apt-get update
sudo apt-get install -y socat
裏でターミナルを2個開いて、31000と310001をlocalhostに転送。
socat TCP-LISTEN:31000,fork,reuseaddr TCP:192.168.49.2:31000
# 別ターミナルで
socat TCP-LISTEN:31001,fork,reuseaddr TCP:192.168.49.2:31001
さらにローカル(EC2)→ローカルPCへの転送はVSCodeから行う。

結果
VoteのPod

ResultのPod

投票すると少しラグがあってVote側に反映される
Voting Appのデプロイ(Deployment版)
アーキテクチャ
Pod版のをDeploymentに変えて可用性を上げる。
┌─────────────────────────────────────────────────────────────────────────────┐
│ Kubernetes Cluster │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ External │ │ External │ │
│ │ Users │ │ Users │ │
│ │ (Voting) │ │ (Results) │ │
│ └─────────┬───────┘ └─────────┬───────┘ │
│ │ │ │
│ │ NodePort: 31000 │ │
│ │ │ │
│ ┌─────────▼───────┐ ┌─────────▼───────┐ │
│ │ Voting Service │ │ Result Service │ │
│ │ │ │ │ │
│ │ Port: 8080 │ │ Port: 8081 │ │
│ │ NodePort │ │ NodePort │ │
│ │ │ │ (31001) │ │
│ └─────────┬───────┘ └─────────┬───────┘ │
│ │ │ │
│ │ Load Balances │ Load │
│ │ │ Balances │
│ ┌─────────▼───────┐ ┌─────────────────┐ ┌─────────▼───────┐ │
│ │ Voting App │ │ Voting App │ │ Result App │ │
│ │ Deployment │ │ Deployment │ │ Deployment │ │
│ │ (Replica 1) │ │ (Replica 2) │ │ (Replica 1) │ │
│ │ │ │ │ │ │ │
│ │ Image: vote:v1 │ │ Image: vote:v1 │ │ Image: result:v1│ │
│ │ Port: 80 │ │ Port: 80 │ │ Port: 80 │ │
│ └─────────┬───────┘ └─────────┬───────┘ └─────────┬───────┘ │
│ │ │ │ │
│ │ │ ┌─────────────────┐ │
│ │ Stores votes │ │ Result App │ │
│ │ │ │ Deployment │ │
│ ▼ ▼ │ (Replica 2) │ │
│ ┌─────────────────┐ │ │ │
│ │ Redis Service │ │ Image: result:v1│ │
│ │ │ │ Port: 80 │ │
│ │ Port: 6379 │ └─────────┬───────┘ │
│ │ ClusterIP │ │ │
│ └─────────┬───────┘ │ Reads │
│ │ │ results │
│ │ ▼ │
│ ┌─────────▼───────┐ ┌─────────────────┐ │
│ │ Redis │ ┌─────────────────┐ │ DB Service │ │
│ │ Deployment │ │ Worker │ │ │ │
│ │ (1 Replica) │◄──────┤ Deployment ├──────►│ Port: 5432 │ │
│ │ │ │ (2 Replicas) │ │ ClusterIP │ │
│ │ Image: redis │ │ │ └─────────┬───────┘ │
│ │ Port: 6379 │ │ Image: worker:v2│ │ │
│ │ │ │ │ │ │
│ └─────────────────┘ │ Processes votes │ ┌─────────▼───────┐ │
│ │ from Redis to │ │ PostgreSQL │ │
│ │ PostgreSQL │ │ Deployment │ │
│ └─────────────────┘ │ (1 Replica) │ │
│ │ │ │
│ │ Image: postgres │ │
│ │ Port: 5432 │ │
│ └─────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
ディレクトリ構造
k8s_beginners/s10_voting_app_deployment/
├── db-deployment.yaml # PostgreSQL database deployment configuration
├── db-service.yaml # PostgreSQL database service configuration
├── redis-deployment.yaml # Redis cache deployment configuration
├── redis-service.yaml # Redis cache service configuration
├── result-deployment.yaml # Result display app deployment configuration
├── result-service.yaml # Result display app service configuration
├── voting-deployment.yaml # Voting interface app deployment configuration
├── voting-service.yaml # Voting interface app service configuration
└── worker-deployment.yaml # Background worker deployment configuration
各ファイル
◯◯-deployment.yamlだけ変わっているのでそれを示す
db-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: db
labels:
app: db
spec:
replicas: 1
selector:
matchLabels:
app: db
template:
metadata:
name: db
labels:
app: db
spec:
containers:
- name: postgres
image: postgres:15-alpine
env:
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: postgres
ports:
- containerPort: 5432
name: postgres
redis-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
labels:
app: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
name: redis
labels:
app: redis
spec:
containers:
- name: redis
image: redis:alpine
ports:
- containerPort: 6379
name: redis
result-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: result
labels:
app: result
spec:
replicas: 2
selector:
matchLabels:
app: result
template:
metadata:
name: result
labels:
app: result
spec:
containers:
- name: result
image: kodekloud/examplevotingapp_result:v1
ports:
- containerPort: 80
name: result
voting-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: vote
labels:
app: vote
spec:
replicas: 2
selector:
matchLabels:
app: vote
template:
metadata:
name: vote
labels:
app: vote
spec:
containers:
- name: vote
image: kodekloud/examplevotingapp_vote:v1
ports:
- containerPort: 80
name: vote
worker-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: worker
labels:
app: worker
spec:
replicas: 2
selector:
matchLabels:
app: worker
template:
metadata:
name: worker
labels:
app: worker
spec:
containers:
- name: worker
image: kodekloud/examplevotingapp_worker:v2
結果
このように処理するコンテナIDが変わってる(ロードバランシングしてる)


特定デプロイだけスケール
kubectl scale deployment vote --replicas=5
Shikoan's ML Blogの中の人が運営しているサークル「じゅ~しぃ~すくりぷと」の本のご案内
技術書コーナー
北海道の駅巡りコーナー
