
006- kubectl + YAML:Deployment / Service / Ingress の最小構成と運用手順
Kubernetes は「YAML(宣言)」で状態を定義し、kubectl apply で反映します。
現場で大事なのは 最小構成を正しく作れること と kubectl で検証できること。
この記事では、以下を1セットで説明します。
Deployment(アプリを動かす)
Service(Pod への入口を作る)
Ingress(HTTP/HTTPS で外部公開する)
0. 事前:namespace を作る(任意)
kubectl create ns demo以降 -n demo を付けて操作します。
1. Deployment(最小で動かす)
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: demo
labels:
app: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 5
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 10反映
kubectl apply -f deployment.yaml確認(ここが超重要)
kubectl -n demo get deploy
kubectl -n demo get rs
kubectl -n demo get pod -o wide
kubectl -n demo describe pod <pod>見るポイント
READY が 1/1 になっているか
STATUS が Running か
Events に Warning が出ていないか
2. Service(Pod をまとめて入口を作る)
Service は「Pod はIPが変わる」問題を解決します。
Selector(label)で Pod を束ねます。
service.yaml
apiVersion: v1
kind: Service
metadata:
name: web-svc
namespace: demo
spec:
type: ClusterIP
selector:
app: web
ports:
- name: http
port: 80
targetPort: 80反映
kubectl apply -f service.yaml確認
kubectl -n demo get svc
kubectl -n demo get endpoints web-svcendpoints に Pod IP が入っていれば OK。
疎通(ローカルから確認)
kubectl -n demo port-forward svc/web-svc 8080:80
# ブラウザ or curl
curl http://127.0.0.1:80803. Ingress(外部公開:HTTP/HTTPS)
Ingress を使うには Ingress Controller(例:nginx-ingress)がクラスタに入っている必要があります。
(入っていないと Ingress を作っても動きません)
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-ing
namespace: demo
spec:
rules:
- host: web.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-svc
port:
number: 80反映
kubectl apply -f ingress.yaml確認
kubectl -n demo get ingress
kubectl -n demo describe ingress web-ing見るポイント
ADDRESS が付いているか(LB / Controller が割り当てる)
Events にエラーがないか
4. よくある落とし穴(ここだけ押さえれば事故減る)
① Service に繋がらない
原因の8割は selector ミス。
Service selector app: web
Pod label app: web
が一致しているか確認:
kubectl -n demo get pod --show-labels
kubectl -n demo describe svc web-svc
kubectl -n demo get endpoints web-svcendpoints が空なら selector がズレてます。
② Ingress 作ったのにアクセスできない
Ingress Controller がない
host のDNS / /etc/hosts が未設定
Controller 側の設定(className など)が必要
まず controller がいるか:
kubectl get pod -A | grep -i ingress③ apply したのに反映されない/意図と違う
「今クラスタに入っている実体」を確認する:
kubectl -n demo get deploy web -o yaml
kubectl -n demo get svc web-svc -o yaml
kubectl -n demo get ing web-ing -o yaml5. 片付け(削除)
kubectl delete -f ingress.yaml
kubectl delete -f service.yaml
kubectl delete -f deployment.yaml
# namespace ごと消すなら
kubectl delete ns demoまとめ(運用の型)
kubectl + YAML はこの順で覚えると早いです。
Deployment:まず Pod を安定して起動
Service:Pod の前に固定入口を作る
Ingress:HTTP/HTTPS で外部公開(Controller 前提)
そして検証手順は固定:
get → describe → endpoints/logs