仅200个

时间:2019-05-17 14:34:28

标签: c# asp.net nginx kubernetes url-rewriting

在kubernetes上通过入口inginx公开服务时,请求某些内容时唯一的http响应代码是200。

从本质上讲,Web应用程序是一种上传图像,将其存储在某处并根据代码进行响应(例如,错误的请求或在标头中的URI位置创建)的表单。期望在API地址上具有multipart / form-data的请求如下:“ http:// {anyAddress} / api / images?directory = {whereToStore}?processing = {someProcessingTags}”

该Web应用程序可以在本地正常工作,并且可以在docker上作为单个容器使用。

因此,当通过入口访问服务时,它首先以预期的形式响应。您可以指定设置和要上传的图像。发送请求后,文件已正确上载到Web应用程序并进行了处理,然后Web应用程序发送了预期的201创建内容,但返回到浏览器的响应始终为200 OK。我不知道为什么。

这是在本地运行的台式机+ kubernetes服务器的docker上。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: 25m
spec:
  rules:
    - http:
        paths:
          - path: /image-upload
            backend:
              serviceName: image-upload-service
              servicePort: http

---

apiVersion: v1
kind: Service
metadata:
  name: image-upload-service
spec:
  type: LoadBalancer
  selector:
    run: image-upload
  ports:
    - name: http
      port: 80
      targetPort: api
      protocol: TCP
    - name: https
      port: 443
      targetPort: api
      protocol: TCP

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: image-upload-cluster
  labels:
    run: image-upload
spec:
  selector:
    matchLabels:
      run: image-upload
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 0
  template:
    metadata:
      labels:
        run: image-upload
    spec:
      volumes:
        - name: shared-volume
          hostPath:
            path: /exports
      containers:
        - name: image-upload
          image: calyxa/image-service
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: /exports
              name: shared-volume
          resources:
            requests:
              cpu: 10m
          ports:
            - containerPort: 3000
              name: api
          readinessProbe:
            httpGet:
              path: /
              port: 3000
            initialDelaySeconds: 5
            periodSeconds: 5
            successThreshold: 1

我希望在发送带有图像文件的请求后,根据状态代码获得带有响应的响应,即400错误请求或201创建的错误请求(在标头中具有位置)。

即使Web应用由于请求而崩溃,响应始终为200 OK。

如果Web应用程序本身未运行(例如,由于崩溃),则我得到的503服务不符合预期。

1 个答案:

答案 0 :(得分:0)

耶稣,我整日设法完成这项工作,并在发布问题后找到了解决方案。因此,为了在api调用中传递参数(在http地址中),确实需要制定入口重写目标规则,如下所示:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/proxy-body-size: 25m
    #nginx.ingress.kubernetes.io/ingress.class: public
spec:
  rules:
    - http:
        paths:
          - path: /image-upload/?(.*)
            backend:
              serviceName: image-upload-service
              servicePort: http

有趣的部分是

rewrite-target: /$1

path: /image-upload/?(.*)

希望这对其他人有帮助。 欢呼!