🏖️
UbuntuにKubernetesを構築していくメモ No.2
はじめに
下記の続編で、自分用のメモです😼
Ingress(L7 LB)
Ingressを有効化する
# minikubeをクリーンナップする
minikube stop
minikube delete
# minikubeを構築
minikube start
# Ingressを有効化する
# Addonをリスアップ
minikube addons list
# Ingress addonを追加
minikube addons enable ingress
# Ingress controller podを確認
kubectl get pods -n ingress-nginx
1つのNodePortに対してIngressを適用する
# HelloWorldのPodを作成する
kubectl run --port 8080 --image gcr.io/google-samples/hello-app:1.0 --restart Never helloworld
# PodをServiceのNodePortタイプで公開する
kubectl expose pod helloworld --type NodePort --port 8080 --name helloworld-nodeport
YAMLファイルを準備する
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: helloworld
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: helloworld-nodeport
port:
number: 8080
# YAMLファイルをApplyする
kubectl apply -f ingress.yaml
# Ingressを確認
kubectl get ingress
# 実行結果
## NAME CLASS HOSTS ADDRESS PORTS AGE
## helloworld nginx * 192.168.49.2 80 11m
# awkコマンドで項目「ADDRESS」のみ取得
kubectl get ingress | awk '{ print $4 }'
# 実行結果
## ADDRESS
## 192.168.49.2
# tailコマンドでIPアドレスのみ取得
kubectl get ingress | awk '{ print $4 }' | tail -1
# 実行結果
## 192.168.49.2
# curlコマンドでクラスタ外からIngress経由でPodへアクセスする
curl $(kubectl get ingress | awk '{ print $4 }' | tail -1)
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld
Ingressリソースを追加
Hello World v2 Podを作成し、ServiceをNodePortとして公開およびHTTPパス/helloworld_v2を使ったIngress resourceを作成する
# Podを追加
kubectl run --image gcr.io/google-samples/hello-app:2.0 --port 8080 --restart Never helloworld-v2
# サービスを追加(NodePort)
kubectl expose pod helloworld-v2 --type NodePort --port 8080 --name helloworld-v2-nodeport
追加したサービスをIngressに適用するためのYAML
ingress_path.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: helloworld-v2
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- path: /helloworld_v2
pathType: Prefix
backend:
service:
name: helloworld-v2-nodeport
port:
number: 8080
# YAMLファイルをApplyする
kubectl apply -f ingress_path.yaml
# Ingressを確認
kubectl get ingress
# 実行結果
## NAME CLASS HOSTS ADDRESS PORTS AGE
## helloworld nginx * 192.168.49.2 80 39m
## helloworld-v2 nginx * 192.168.49.2 80 91s
# クラスタ内からPodへ接続
# 追加したPodのIPアドレスを確認
kubectl get pod -o wide
# 実行結果
## NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
## helloworld 1/1 Running 0 41m 10.244.0.6 minikube <none> <none>
## helloworld-v2 1/1 Running 0 12m 10.244.0.7 minikube <none> <none>
# curl
kubectl run --restart Never --image curlimages/curl:7.68.0 -it --rm curl sh
/ $ curl 10.244.0.7:8080
# 実行結果
## Hello, world!
## Version: 2.0.0
## Hostname: helloworld-v2
# クラスタ内からServiceへ接続
# 追加したServiceを確認
kubectl get service
# 実行結果
## NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
## helloworld-nodeport NodePort 10.97.241.140 <none> 8080:32217/TCP 48m
## helloworld-v2-nodeport NodePort 10.97.63.103 <none> 8080:32494/TCP 4s
## kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 54m
# curl
kubectl run --restart Never --image curlimages/curl:7.68.0 -it --rm curl sh
/ $ curl helloworld-v2-nodeport:8080
# 実行結果
## Hello, world!
## Version: 2.0.0
## Hostname: helloworld-v2
# Ingressの/helloworld_v2パス経由で、helloworld-v2-nodeport Serviceに アクセス
curl $(kubectl get ingress helloworld-v2 | awk '{ print $4 }' | tail -1)/helloworld_v2
# 実行結果
## Hello, world!
## Version: 2.0.0
## Hostname: helloworld-v2
クリーンナップ
kubectl delete pod helloworld
kubectl delete pod helloworld-v2
PodのReplicaとScaling
Replica用のYAMLを用意する
replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: helloworld
labels:
app: helloworld
spec:
# modify replicas according to your case
replicas: 2
selector:
matchLabels:
app: helloworld
template:
metadata:
labels:
app: helloworld
spec:
containers:
- name: helloworld
image: gcr.io/google-samples/hello-app:1.0
# PodをReplicaする
kubectl apply -f replicaset.yaml
# Replicasetを確認する
kubectl get replicaset
# 実行結果
## NAME DESIRED CURRENT READY AGE
## helloworld 2 2 2 30s
# 5つにスケールアップする
kubectl scale --replicas=5 replicaset/helloworld
# Podを削除する。ただ、すぐに新しいPodが作られる。
kubectl delete pod helloworld-44c67
# 作成したReplicasetをクリーナップする
kubectl delete -f replicaset.yaml
Rolling updateとRollback
# PodをDeploymentとして1つ起動
kubectl create deployment --image gcr.io/google-samples/hello-app:1.0 helloworld
# deploymentを確認する
kubectl get deployment
# 実行結果
## NAME READY UP-TO-DATE AVAILABLE AGE
## helloworld 1/1 1 1 17s
# 5つにスケールアップする
kubectl scale --replicas=5 deploy/helloworld
# deploymentを確認する
kubectl get deployment
# 実行結果
## NAME READY UP-TO-DATE AVAILABLE AGE
## helloworld 5/5 5 5 107s
# curlコマンドで出力確認
curl $(minikube service helloworld-nodeport --url)
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld-85bfb7fff-24z7g
# コンテナ名を確認
kubectl get deploy helloworld -o jsonpath='{.spec.template.spec.containers[*].name}'
# 実行結果
## hello-app
# Rooling update
kubectl set image deploy/helloworld hello-app=gcr.io/google-samples/hello-app:2.0
# curlコマンドで出力確認
curl $(minikube service helloworld-nodeport --url)
# 実行結果
## Hello, world!
## Version: 2.0.0
## Hostname: helloworld-ff68755fb-xnnrr
# 履歴確認
kubectl rollout history deploy/helloworld
# 実行結果
## deployment.apps/helloworld
## REVISION CHANGE-CAUSE
## 5 <none>
## 6 <none>
# ロールバック
kubectl rollout undo deploy/helloworld
# curlコマンドで出力確認
curl $(minikube service helloworld-nodeport --url)
# 実行結果
## Hello, world!
## Version: 1.0.0
## Hostname: helloworld-85bfb7fff-p4x5d
# 履歴確認
kubectl rollout history deploy/helloworld
# 実行結果
## deployment.apps/helloworld
## REVISION CHANGE-CAUSE
## 6 <none>
## 7 <none>
おまけ
# kubectlのPodとService、Ingressを監視する
watch 'kubectl get pod,svc,ingress -o wide'
Discussion