I have deployed nginx in my kubernetes cluster with this configmap:
apiVersion: v1
data:
json.conf: |-
server {
listen 80;
server_name api.test.com;
location / {
proxy_pass https://jsonplaceholder.typicode.com/posts;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
proxy_redirect off;
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1.2 TLSv1.3;
proxy_ssl_verify off;
}
}
kind: ConfigMap
metadata:
name: json
namespace: app
My problem is when I want to request this nginx pod from another pod, I have to call the nginx pod's service in Kubernetes but I want to call for example http://api.test.com in my request. How can handle this?
I have tried this in (for example in api-app-chart-f5bbcf55d-fthbz):
curl http://nginx-egress-app-chart-svc-web
But i want to request like this:
curl http://api.test.com
You can implement it with rewrite
feature of CoreDNS.
Add this line to CoreDNS config file. (NOTE: replace <namespace>
)
rewrite name api.test.com nginx-egress-app-chart-svc-web.<namespace>.svc.cluster.local
.:53 {
errors
health {
lameduck 5s
}
rewrite name api.test.com nginx-egress-app-chart-svc-web.<namespace>.svc.cluster.local
ready
kubernetes cluster.local cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus 0.0.0.0:9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
At the end rollout your CoreDNS to apply the changes.