k8s ngnix container return json response

3/24/2017

I have a k8s cluster, among other things running an nginx. when I do curl -v <url> I get

    HTTP/1.1 200 OK
< Content-Type: text/html
< Date: Fri, 24 Mar 2017 15:25:27 GMT
< Server: nginx
< Strict-Transport-Security: max-age=15724800; includeSubDomains; preload
< Content-Length: 0
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host <url> left intact

however when I do curl -v <url> -H 'Accept: application/json' I get

< HTTP/1.1 200 OK
< Content-Type: text/html
< Date: Fri, 24 Mar 2017 15:26:10 GMT
< Server: nginx
< Strict-Transport-Security: max-age=15724800; includeSubDomains; preload
< Content-Length: 0
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host <url> left intact
* Could not resolve host: application
* Closing connection 1
curl: (6) Could not resolve host: application

My task is to get the request to return a json not html. To my understanding I have to create an ingress-controller and modify the ngnix.conf somehow, I've been trying for a few days now but can't get it right. Any kind of help would be most appreciated.

The following are of the yaml files I've been using:

configmap:

apiVersion: v1
data:
  server-tokens: "false"
  proxy-body-size: "4110m"
  server-name-hash-bucket-size: "128"
kind: ConfigMap
metadata:
  name: nginx-load-balancer-conf
  labels:
    app: nginx-ingress-lb

daemonset:

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: nginx-ingress-lb
  labels:
    app: nginx-ingress-lb
spec:
  template:
    metadata:
      labels:
        name: nginx-ingress-lb
        app: nginx-ingress-lb

    spec:
      terminationGracePeriodSeconds: 60
      nodeSelector:
        NodeType: worker
      containers:
      - image: gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.1

        name: nginx-ingress-lb
        imagePullPolicy: Always
        readinessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          timeoutSeconds: 1
        # use downward API
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
        ports:
        - containerPort: 80
          hostPort: 80
        - containerPort: 443
          hostPort: 443
        args:
        - /nginx-ingress-controller
        - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
        - --configmap=$(POD_NAMESPACE)/nginx-load-balancer-conf

deployment:

apiVersion: extensions/v1beta1
kind: Deployment

metadata:
  name: default-http-backend
  labels:
    app: default-http-backend

spec:
  replicas: 2
  template:

    metadata:
      labels:
        app: default-http-backend

    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: default-http-backend
        # Any image is permissable as long as:
        # 1. It serves a 404 page at /
        # 2. It serves 200 on a /healthz endpoint
        image: gcr.io/google_containers/defaultbackend:1.2
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          timeoutSeconds: 5
        ports:
        - containerPort: 8080
        resources:
          limits:
            cpu: 100m
            memory: 20Mi
          requests:
            cpu: 100m
            memory: 20Mi

service:

apiVersion: v1
kind: Service
metadata:
  name: default-http-backend
  labels:
    app: default-http-backend
spec:
  selector:
    app: default-http-backend
  ports:
  - port: 80
    targetPort: 8080
-- Naim Salameh
kubernetes
nginx

2 Answers

3/25/2017

Remove the space after colon in curl -v <url> -H 'Accept: application/json'

The error message Could not resolve host: application means that it's taking application/json as the URL, instead of a header.

-- Buchi
Source: StackOverflow

3/25/2017

There are two things:

  • Exposing your app
  • Making your app return json

The ingess is only relevant to expose your app. And that is not the only option, you can use service (type Load balancer, for example) to achieve that too on most cloud providers. So, I'd keep it simple and not use ingress for now, until you solve the second problem.

As it has been explained, your curl has a syntax problem and that's why it shows curl: (6) Could not resolve host: application.

The other thing is fixing that won't make your app return json. And this is because you are only saying you accept json with that header. But if you want your app to return json, then you need to write it on your app. nginx can't guess how you want to map your HTML to json. There is much no other way than writting it, at least that I know of :-/

-- rata
Source: StackOverflow