0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kubernetes:namespacesのservice一覧を確認

0
Posted at

下記構成でKubernetesを動かした時のservice一覧を表示した。

構成
nginxを1つ作成
service作成

Serviceとは
Podは作り直されるとIPアドレスが変わる。
そのため、Podに直接アクセスするのではなく、Serviceを経由してアクセスする。

イメージ
利用者
  ↓
Service
  ↓
Pod
kubectl get svc -A

NAMESPACE     NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP                  2d3h
default       nginx        NodePort    10.96.126.219   <none>        80:32017/TCP             13h
kube-system   kube-dns     ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   2d3h

Kubernetesが自動で生成するservice

default  kubernetes  ClusterIP  10.96.0.1

自分で作成したnginx用Service

default nginx NodePort 10.96.126.219 80:32017/TCP

NodePort
Kubernetesクラスターの外部からアクセスできるService

80:32017/TCP
Pod側      80番
外部公開   32017番

ブラウザで下記へアクセスするとnginxの画面が表示される。
http://localhost:32017

※NodePortで 80:32017/TCP と表示されていても、ローカルKubernetes環境では localhost:32017 で直接アクセスできない場合がある。

その場合は kubectl port-forward を使うと、ローカルPCのポートからServiceへ転送できる。

kubectl port-forward service/nginx 8080:80
kube-system kube-dns ClusterIP 10.96.0.10

CoreDNSのService
Pod同士の名前解決を担当する。

ClusterIPとNodePortの違い

ClusterIP

Pod ←→ Service ←→ Pod

クラスター内部だけで利用可能。
外部からはアクセスできない。

例:
kubernetes
kube-dns

NodePort

外部PC
   ↓
NodePort
   ↓
Pod

外部からアクセス可能。

例:
nginx

kubectl get svc -A を実行すると、Kubernetesクラスター内のServiceを確認できる。
まとめ

Service 役割
kubernetes API Serverへの入口
nginx nginx Podへの入口
kube-dns DNSによる名前解決

Serviceは、IPアドレスが変わるPodへのアクセスを安定させるための仕組みであり、Kubernetesの通信を支える重要なコンポーネントである。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?