k8s ngnix容器返回json响应

时间:2017-03-24 15:49:24

标签: nginx kubernetes

我有一个k8s集群,其中包括运行nginx。 当我curl -v <url>时,我得到了

    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

然而当我curl -v <url> -H 'Accept: application/json'时,我得到了

< 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

我的任务是获取返回json而不是html的请求。 根据我的理解,我必须创建一个入口控制器并以某种方式修改ngnix.conf,我已经尝试了几天但是无法正确使用它。任何形式的帮助都将非常受欢迎。

以下是我一直在使用的yaml文件:

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

部署:

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

服务:

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

2 个答案:

答案 0 :(得分:1)

删除curl -v <url> -H 'Accept: application/json'

中冒号后的空格

错误消息Could not resolve host: application表示它将application/json作为网址,而不是标题。

答案 1 :(得分:1)

有两件事:

  • 公开您的应用
  • 让您的应用返回json

ingess仅与展示您的应用相关。这不是唯一的选择,您可以使用服务(例如,类型负载均衡器)在大多数云提供商上实现这一点。所以,我会保持简单,暂时不使用入口,直到你解决第二个问题。

正如已经解释的那样,你的curl有一个语法问题,这就是它显示curl: (6) Could not resolve host: application的原因。

另一件事是修复,不会让你的应用程序返回json。这是因为你只是说你接受带有标题的json。但是如果你想让你的应用程序返回json,那么你需要在你的应用程序上编写它。 nginx无法猜测你想如何将你的HTML映射到json。除了写作之外别无他法,至少我知道: - /