service discovery は service に接続する方法を見つけるプロセスです。環境変数ベースの servcice discovery オプションがありますが、DNS ベースの service discovery のほうがお勧めです。DNS はクラスタアドオンなので、Kubernetes ディストリビューションがそのアドオンを提供していることを確認してください。なければご自身でインストールしてください。
thesvc
という名前の service と、それに伴っていくつかの pod を管理する RC を作成しましょう。
$ kubectl apply -f https://raw.githubusercontent.com/openshift-evangelists/kbe/master/specs/sd/rc.yaml
$ kubectl apply -f https://raw.githubusercontent.com/openshift-evangelists/kbe/master/specs/sd/svc.yaml
クラスタ内部から、たとえば別の service から thesvc
service に接続したいとします。これをシミュレートするために、同じ名前空間内に jump pod を作ります。(名前空間は特に指定していないので default
です)
$ kubectl apply -f https://raw.githubusercontent.com/openshift-evangelists/kbe/master/specs/sd/jumpod.yaml
DNS アドオンは thesvc
service が FQDN thesvc.default.svc.cluster.local
を通じてクラスタ内の他の pod からアクセス可能であることを保証してくれます。試してみましょう。
$ kubectl exec -it jumpod -c shell -- ping thesvc.default.svc.cluster.local
PING thesvc.default.svc.cluster.local (172.30.251.137) 56(84) bytes of data.
...
ping
の結果から、service がクラスタ IP 172.30.251.137
によってアクセス可能であることがわかります。次のように直接接続して service を利用することも (同じ名前空間内であれば) 可能です。
$ kubectl exec -it jumpod -c shell -- curl http://thesvc/info
{"host": "thesvc", "version": "0.5.0", "from": "172.17.0.5"}
上の IP アドレス 172.17.0.5
は、 jump pod のクラスタ内での IP アドレスです。
別の名前空間にデプロイされた service にアクセスするには、$SVC.$NAMESPACE.svc.cluster.local
という形式の FQDN を使用します。
どういう仕組みか理解するために、以下を作成しましょう。
名前空間に慣れていなければ、namespace examples を先に読んでみてください。
$ kubectl apply -f https://raw.githubusercontent.com/openshift-evangelists/kbe/master/specs/sd/other-ns.yaml
$ kubectl apply -f https://raw.githubusercontent.com/openshift-evangelists/kbe/master/specs/sd/other-rc.yaml
$ kubectl apply -f https://raw.githubusercontent.com/openshift-evangelists/kbe/master/specs/sd/other-svc.yaml
これで、名前空間 other
内の thesvc
service に名前空間 default
から (再度 jump pod を通じて) アクセスできるようになりました。
$ kubectl exec -it jumpod -c shell -- curl http://thesvc.other/info
{"host": "thesvc.other", "version": "0.5.0", "from": "172.17.0.5"}
まとめると、DNS ベースの service discovery は、クラスタ内を横断して service に接続する柔軟で一般的な方法を提供します。
作成したリソースをすべて削除するには以下を実行します。
$ kubectl delete pods jumpod
$ kubectl delete svc thesvc
$ kubectl delete rc rcsise
$ kubectl delete ns other
名前空間を削除するとその中のすべてのリソースが削除されることを覚えておいてください。