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 の Service リソースによる通信制御の仕組みとその実装ーClusterIPを例に

0
Posted at

Kubernetes の Service リソースによる通信制御の仕組みとその実装

Kubernetesの仕組みについてもっと深く知りたいため、Serviceについて通信制御の仕組みを調べてみました。

1. 導入

Kubernetes Serviceは、動的に変化するPodの論理的なセットに対し、固定のIPアドレスやDNS名を提供し、安定したネットワークアクセスと負荷分散を行うオブジェクトです。

KubernetesにはClusterIP, NodePort, LoadBalancer, ExternalNameの4種類がある。本稿はClusterIPを中心に説明します。

2. 仕組み

実際の環境では、Podの削除や再起動により、同じPodでもIPアドレスが変わるのが普通です。そのため、毎回変わるIPアドレスを追跡してアクセスするのは非効率的です。

そこで、Serviceが固定のIPアドレスを提供し、それをCoreDNSにService名とともに登録することで、IPを意識せずのPod間通信が可能になります。さらに、kube-proxyが転送ルールをiptablesに書き込むことで、DNATによる宛先変換と負荷分散を実現しました。

3. 実装

ここからは、ClusterIPを中心に、その実装について解説します。具体例として、次のコマンドでデプロイメントとそれに対応するClusterIPを作成します。

3.1. DeploymentとServiceの作成

$ kubectl create deployment nginx --image=nginx
$ kubectl expose deploy nginx --port=80 --target-port=80

以上のコマンドにより、Pod、Service、そしてEndpoints(Serviceと一緒に自動で作成されます)の3つのオブジェクトが作られます。

$ kubectl get pods -l app=nginx
NAME                     READY   STATUS    RESTARTS   AGE
nginx-56c45fd5ff-wksd7   1/1     Running   0          51s
$ kubectl get svc nginx 
NAME    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
nginx   ClusterIP   10.96.176.236   <none>        80/TCP    99s
$ kubectl get ep nginx
NAME    ENDPOINTS         AGE
nginx   10.244.0.103:80   2m5s

ClusterIPのCLUSTER-IPは固定のIPアドレスです。そして、ENDPOINTSにはClusterIP nginxが紐づくすべてのPodのIPアドレスが記録されます。つまり、CLUSTER-IPにアクセスすれば、トラフィックは自動的に目標のPod群へ転送されます。

3.2. CoreDNSによる名前解決

Kubernetesの内部では、IPアドレスではなく「Service名」でアクセスするのが一般的です。そのための名前解決を担うのが、kube-system内にデプロイされているCoreDNSです。

ClusterIPが作成されるたびに、CoreDNSにそのFQDNとIPアドレスの組み合わせが登録されます。これにより、クラスタからはFQDNでServiceにアクセスすることが可能です。

Kubernetes内部のServiceのFQDNは、通常以下のようになります:

<service-name>.<namespace>.svc.cluster.local

しかし、普段は<service-name>だけでアクセス可能です。これは、KubernetesがPodを起動する際、Pod内の /etc/resolv.conf にSearch Domainを自動設定するためです。これにより、PodはService名を自動で補完し、FQDNとしてCoreDNSに問い合わせることができます。

$ kubectl exec -it nginx-56c45fd5ff-wksd7  -- /bin/bash
$ cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

3.3. kube-proxyとiptablesによるDNATと負荷分散

負荷分散の仕組みをより分かりやすくするため、元のデプロイメントのレプリカ数を3に変更します。

kubectl scale deployment nginx --replicas=3

Serviceの名前解決ができた後、最後は「ServiceのIP(固定)」から「各PodのIP」への変換です。

この問題を解決するために、Kubernetesの各ノードで動いているkube-proxyがAPIサーバーの状態を監視し、ServiceのENDPOINTS情報に基づいて、ホストのiptablesに宛先アドレスの変換ルールを書き込みます。

それを下図に基づいて、簡単に説明します。まず、ServiceのIP(例: 10.102.203.24)宛のリクエストは、すべて特定のKUBE-SVC-***チェーンで捕捉されます。このチェーン内で、リクエストは各Pod専用のチェーンへと平均的に振り分けられます。そして最後に、DNAT(宛先NAT)によって、リクエストの宛先IPが実際のPodのIPアドレスに変換されます。

$ minikube ssh
$ sudo iptables -t nat -L KUBE-SERVICES
*Selected Resultes*
Chain KUBE-SERVICES (2 references)
target     prot opt source               destination         
KUBE-SVC-2CMXP7HKUVJN7L6M  tcp  --  anywhere             10.96.176.236        /* default/nginx cluster IP */ tcp dpt:http
$ sudo iptables -t nat -L KUBE-SVC-2CMXP7HKUVJN7L6M
Chain KUBE-SVC-2CMXP7HKUVJN7L6M (1 references)
target     prot opt source               destination         
KUBE-MARK-MASQ  tcp  -- !10.244.0.0/16        10.96.176.236        /* default/nginx cluster IP */ tcp dpt:http
KUBE-SEP-FJ7WMSCFW6TQIMM4  all  --  anywhere             anywhere             /* default/nginx -> 10.244.0.103:80 */ statistic mode random probability 0.33333333349
KUBE-SEP-LHILIXKYWSCUKD5C  all  --  anywhere             anywhere             /* default/nginx -> 10.244.0.104:80 */ statistic mode random probability 0.50000000000
KUBE-SEP-D6GYTLQM44F2HPAD  all  --  anywhere             anywhere             /* default/nginx -> 10.244.0.105:80 */
docker@minikube:~$ sudo iptables -t nat -L KUBE-SEP-FJ7WMSCFW6TQIMM4
Chain KUBE-SEP-FJ7WMSCFW6TQIMM4 (1 references)
target     prot opt source               destination         
KUBE-MARK-MASQ  all  --  10.244.0.103         anywhere             /* default/nginx */
DNAT       tcp  --  anywhere             anywhere             /* default/nginx */ tcp to:10.244.0.103:80

Appendix

Appendix A

ClusterIPに紐づくポッド、つまりEndpointsに登録されるIPアドレスについて説明します。ClusterIPにはselectorというSpecがあり、selectorの条件を全て満たすラベルを持つPodだけがそのClusterIPのEndpointsに登録されます。

$ kubectl get svc nginx -o wide
NAME    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
nginx   ClusterIP   10.96.176.236   <none>        80/TCP    18m   app=nginx
$ kubectl describe pod nginx-56c45fd5ff-6bm4f
Name:             nginx-56c45fd5ff-6bm4f
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Mon, 20 Apr 2026 20:00:32 +0900
Labels:           app=nginx
                  pod-template-hash=56c45fd5ff
...

Appendix B

負荷分散について説明します。
次のコマンドと結果から、statistic mode random probability xが確認できます。これはパケットを$x$の可能性で対応するChainに振り分けることを意味します。
具体的には、到達したパケットは、まず$\frac{1}{3}$の確率でKUBE-SEP-FJ7WMSCFW6TQIMM4に送られます。これに該当しなかった場合、次に$\frac{1}{2}$の確率でKUBE-SEP-LHILIXKYWSCUKD5Cに送られます。さらにそれにも該当しなかった場合、パケットをKUBE-SEP-D6GYTLQM44F2HPADに送られます。
このようにして、最終的に各Chainにパケットが均等的に振り分けられます。

$ sudo iptables -t nat -L KUBE-SVC-2CMXP7HKUVJN7L6M
Chain KUBE-SVC-2CMXP7HKUVJN7L6M (1 references)
target     prot opt source               destination         
KUBE-MARK-MASQ  tcp  -- !10.244.0.0/16        10.96.176.236        /* default/nginx cluster IP */ tcp dpt:http
KUBE-SEP-FJ7WMSCFW6TQIMM4  all  --  anywhere             anywhere             /* default/nginx -> 10.244.0.103:80 */ statistic mode random probability 0.33333333349
KUBE-SEP-LHILIXKYWSCUKD5C  all  --  anywhere             anywhere             /* default/nginx -> 10.244.0.104:80 */ statistic mode random probability 0.50000000000
KUBE-SEP-D6GYTLQM44F2HPAD  all  --  anywhere             anywhere             /* default/nginx -> 10.244.0.105:80 */
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?