こんにちは。今回は Kubernetes における Ingress と Service のポート設定の違い について、わかりやすく解説します。
🔍 Ingress の port 設定とは?
Ingress の設定では、serviceName
と servicePort
を指定します。
ここで指定するのは、Ingress がアクセス先の Service を特定するための情報であり、Service の名前とポート番号を指定します。
例:
spec:
rules:
- http:
paths:
- path: /example
backend:
serviceName: my-service # Service の名前
servicePort: 80 # Service のポート番号(targetPort ではありません)
この servicePort
は、Service の spec.ports[].port
に対応しています。
⚙️ Service の port と targetPort の違い
Service は複数のポート情報を持っていて、主に以下の2つがあります。
- port:Service がクラスター内で公開するポート
-
targetPort:Service がリクエストを転送する Pod のコンテナポート。
たいていは Deployment のcontainerPort
と同じ番号を指定します。
例:
spec:
ports:
- port: 80 # Service のポート(Ingress の servicePort と一致)
targetPort: 8080 # Pod のコンテナがリッスンしているポート(containerPort)
📦 Deployment の containerPort
Pod 内のコンテナが実際にリッスンしているポートです。
Service の targetPort
がこの containerPort
に対応します。
containers:
- name: my-app
image: my-app-image
ports:
- containerPort: 8080 # コンテナの実際のポート
📝 まとめ
設定箇所 | ポート名 | 説明 |
---|---|---|
Ingress | servicePort | Service の port を指定する |
Service | port | クラスター内で公開するポート(Ingress から指定される) |
targetPort | Pod の containerPort に転送するポート |
|
Deployment (Pod) | containerPort | コンテナが実際にリッスンしているポート |
💡 ポイント
- Ingress は Service の
port
にトラフィックを流すため、targetPort
や Pod のcontainerPort
は直接指定しません。 - Service がリクエストを受けてから、
targetPort
を使って Pod のコンテナに転送します。 - つまり、
Ingress → Service(port) → Service(targetPort) → Pod(containerPort)
という流れになります。
今回の内容が Kubernetes ネットワーク設定の理解に役立てば幸いです。
ご質問やフィードバックがあれば、ぜひコメントしてくださいね 😊