cURL from Kubernetes Pod to ExternalName service hangs

8/3/2017

Issue: I try to perform a cURL on a ExternalName service from within a temporary busybox pod, but the call just hangs. If I do the same cURL on the external name, not the service name, it works.

The following is the service yaml:

apiVersion: v1
kind: Service
metadata:
  name: google
spec:
  type: ExternalName
  externalName: google.com

I create the service with kubectl create -f then I create a temporary interactive busybox pod:

kubectl run -i --tty busybox --image=sequenceiq/busybox --restart=Never -- sh

from which I perform the following cURLs:

/ # curl google #hangs, have to interrupt
^C
/ # curl google.com

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.de/?gfe_rd=cr&ei=SQWDWYXTNZGg8wfFlIGQDw">here</A>.
</BODY></HTML>

So the obvious question is: Why is the first cURL hanging, but the second isn't, considering google is just a CNAME for google.com, as also confirmed by the following host output (performed from the interactive busybox)?

/ # host google
google.default.svc.cluster.local is an alias for google.com.
google.com has address 172.217.18.174
google.com has IPv6 address 2a00:1450:4001:806::200e
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.

The following is the verbosed output of the hanging cURL command

/ # curl -vvv google
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: google
> Accept: */*
> 

Output describing the service (not possible to output as yaml):

$ kubectl describe svc google
Name:             google
Namespace:        default
Labels:           <none>
Annotations:      <none>
Selector:         <none>
Type:             ExternalName
IP:         
External Name:    google.com
Session Affinity: None
Events:           <none>
-- Gabriel Ruiu
curl
kubernetes

3 Answers

12/30/2019

The hanging part is weird. Shouldn't hang, but return 404.

@santouras is right. You need to pass a Host to the request. Or at least to override it as curl is sending the request with the Host header same as the service name.

Basically this is what you are doing:

/ # telnet google.com 80
GET / HTTP/1.1
Host: google

HTTP/1.1 404 Not Found
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Content-Length: 1561
Date: Mon, 30 Dec 2019 10:20:07 GMT

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/</code> was not found on this server.  <ins>That’s all we know.</ins>

But you need to do this:

/ # telnet google.com 80
GET / HTTP/1.1
Host: google.com

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Mon, 30 Dec 2019 10:21:06 GMT
Expires: Wed, 29 Jan 2020 10:21:06 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

Or even without Host header, but not google.

So, santouras' request would work, and also this one:

curl -H "Host:" google
-- suren
Source: StackOverflow

2/21/2019

you must add more verbose url using curl google.default or even curl google.default.svc.cluster.local

it give the result but idkw its return 404

 # curl google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

without namesapce

/ # curl google
^C

using namespace

/ # curl google.default
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/</code> was not found on this server.  <ins>That’s all we know.</ins>
-- prima.adi
Source: StackOverflow

12/30/2019

You will need to set the host headers in the curl command

$ curl -I google -H 'Host:google.com'
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Mon, 30 Dec 2019 03:49:12 GMT
Expires: Wed, 29 Jan 2020 03:49:12 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
-- santouras
Source: StackOverflow