"Carvel kwtでk8sクラスタ外から内部ドメイン名でService / Podへアクセスする"方法を紹介しました。
次は、逆にkwtでk8sクラスタ内からローカル環境へアクセスする方法を紹介します。
まずはローカル環境のアプリケーションとしてnginxをdockerで起動します。
docker run --rm -p 80:80 nginx
kwt net listenコマンドを使ってこのアプリケーション(localhost:80)をk8s上で公開します。
サービスの名前をnginx、ポートを80(デフォルト)、typeをClusterIP(デフォルト)にする場合のコマンドは次のとおりです。
kwt net listen --remote 80 --local localhost:80 --service nginx --service-type ClusterIP
次のようなログが出力され、Readyになります。
02:17:36PM: info: KubeEntryPoint: Creating networking client secret 'kwt-net-ssh-key' in namespace 'default'...
02:17:36PM: info: KubeEntryPoint: Creating networking host secret 'kwt-net-host-key' in namespace 'default'...
02:17:36PM: info: KubeEntryPoint: Creating networking pod 'kwt-net' in namespace 'default'
02:17:36PM: info: KubeEntryPoint: Waiting for networking pod 'kwt-net' in namespace 'default' to start...
02:17:36PM: info: ListenOptions: Forwarding 80->localhost:80
02:17:36PM: info: TCPProxy: Started proxy on dummy-addr
02:17:36PM: info: ListenOptions: Ready!
Service一覧を確認すると、確かにtypeがClusterIPで名前がnginxなServiceが作成されています。
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 132m
nginx ClusterIP 10.96.60.70 <none> 80/TCP 11s
Serviceのconfigを確認すると次のように出力されます。
$ kubectl get svc nginx -oyaml | kubectl neat
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: default
spec:
clusterIP: 10.96.60.70
clusterIPs:
- 10.96.60.70
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: port0
port: 80
targetPort: 39951
selector:
kwt.cppforlife.com/net: "true"
kwt net svcの出力にももちろん現れます。
$ kwt net svc
Services in namespace 'default'
Name Internal DNS Cluster IP Ports
kubernetes kubernetes.default.svc.cluster.local 10.96.0.1 443/tcp
nginx nginx.default.svc.cluster.local 10.96.60.70 80/tcp
2 services
Succeeded
次のコマンドを使って、k8sクラスタ内部からcurlでローカル環境上のアプリ(nginx)にアクセスします。
$ kubectl run curl --restart=Never --image=curlimages/curl -ti --rm -- \
curl -v http://nginx.default.svc.cluster.local
* Trying 10.96.60.70:80...
* Connected to nginx.default.svc.cluster.local (10.96.60.70) port 80 (#0)
> GET / HTTP/1.1
> Host: nginx.default.svc.cluster.local
> User-Agent: curl/7.77.0-DEV
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.21.0
< Date: Wed, 16 Jun 2021 05:31:25 GMT
< Content-Type: text/html
< Content-Length: 612
< Last-Modified: Tue, 25 May 2021 12:28:56 GMT
< Connection: keep-alive
< ETag: "60aced88-264"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
* Connection #0 to host nginx.default.svc.cluster.local left intact
pod "curl" deleted
アクセスできました。
今回はCarvel kwtでk8sクラスタ内からローカル環境へアクセスする方法を紹介しました。
k8s上のアプリケーションから一時的にローカル環境の開発中のAPIを呼び出したい場合などに使えそうですね。