Nga tohutohu me nga tinihanga a Kubernetes: nga wharangi hapa ritenga i NGINX Ingress

Nga tohutohu me nga tinihanga a Kubernetes: nga wharangi hapa ritenga i NGINX Ingress

I roto i tenei tuhinga, e hiahia ana ahau ki te korero mo nga ahuatanga e rua o NGINX Ingress e pa ana ki te whakaatu i nga wharangi hapa whaiaro, me nga here kei roto i a raatau me nga huarahi ki te mahi a tawhio noa.

1. Te huri i te tuara taunoa

Ma te taunoa, ka whakamahi a NGINX Ingress i te tuara taunoa, e mahi ana i te mahi e rite ana. Ko te tikanga ina tono he Ingress e tohu ana i tetahi kaihautu kaore i roto i nga rauemi Ingress, ka whiwhi tatou i te wharangi e whai ake nei me te waehere whakautu 404:

Nga tohutohu me nga tinihanga a Kubernetes: nga wharangi hapa ritenga i NGINX Ingress

Heoi, he maha tonu nga wa ka tae mai a taatau kaihoko me te tono kia whakaatu i o raatau whaarangi me te tohu umanga me etahi atu taputapu hei utu mo te paerewa 404. Hei mahi i tenei, kei a NGINX Ingress te kaha hanga-i roto tautuhi ano default-backend-service. Ka tukuna e matou te urunga whakatakotoranga hei tohenga ki te whiringa o te ingoa kotahi namespace/servicename. Me 80 te tauranga o te ratonga.

Ki te mahi i tenei, me hanga e koe taau ake pod (whakaahua) me te ratonga me to tono (tauira whakatinanatanga i YAML mai i te putunga ingress-nginx), ka hoatu hei utu mo te tuara taunoa.

Anei tetahi whakaahua iti:

~$ curl -i -XGET http://sadsdasdas.kube-cloud.my/
HTTP/1.1 404 Not Found
Date: Mon, 11 Mar 2019 05:38:15 GMT
Content-Type: */*
Transfer-Encoding: chunked
Connection: keep-alive

<span>The page you're looking for could not be found.</span>

Na ko nga rohe katoa kaore i hangaia ma te YAML me kind: Ingress, taka ki te taunoa-muri. I roto i te rarangi i runga ake nei, kua riro tenei rohe sadsdasdas.

2. Te whakahaere i nga hapa HTTP i roto i te tono ma te whakamahi i te tuara taunoa

Ko tetahi atu ahuatanga ko nga tono ka mutu i roto i nga hapa HTTP (404, 500, 502...) ki tetahi tono kaore i te whakahaere i nga ahuatanga penei (kaore nga wharangi ataahua e rite ana ki te hanga). Ko tenei pea na te hiahia o nga kaihanga ki te mahi i nga wharangi hapa i roto i nga tono maha.

Hei whakatinana i tenei keehi ki te taha tūmau ka hiahia matou:

  1. Whaia nga tohutohu i runga ake mai i te kowae mo te tuara taunoa;
  2. Tāpirihia he kī ki te whirihoranga nginx-ingress ConfigMap custom-http-errors, hei tauira, me te uara 404,503 (e tino rite ana ki nga waehere hapa e hipokina ana e te ture hou).

Kua tutuki te hua e tumanakohia ana: i te wa e rere ana te tono a te kiritaki me te whiwhi hapa me te waehere whakautu 404, 503 ranei, ka tukuna aunoatia te tono ki te tuara taunoa hou...

Heoi, i te wa e whakawhanake ana koe i tetahi tono mo te whakamuri taunoa me te ritenga-http-hapa, me whai whakaaro koe ki tetahi waahanga nui:

!!! Important The custom backend is expected to return the correct HTTP status code instead of 200. NGINX does not change the response from the custom default backend.

Ko te mea ka tukuna he tono, kei roto i nga pane nga korero whai hua me te waehere whakautu o mua me etahi atu korero (kei te waatea o raatau rarangi katoa. konei).

Ko te tikanga ko koe ano me tiaki i te waehere whakautu tika. Anei he tauira mai i nga tuhinga me pehea te mahi.

He rereke nga taapiri taunoa o nga tono rereke

Kia kore ai te otinga i te ao mo te roopu katoa, engari e pa ana ki nga tono motuhake, me tirotiro tuatahi koe i te putanga Ingress. Mena ka rite 0.23 teitei ake ranei, whakamahia nga korero Ingress taketake:

  1. Ka taea e tatou te whakakore default-backend ki Tuhinga o mua Ingress's te whakamahi tuhipoka;
  2. Ka taea e tatou te whakakore custom-http-errors ki Tuhinga o mua Ingress's te whakamahi tuhipoka.

Ko te mutunga, ka penei te ahua o te rauemi Ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ .Chart.Name }}-app2
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/custom-http-errors: "404,502"
    nginx.ingress.kubernetes.io/default-backend: error-pages
spec:
  tls:
  - hosts:
    - app2.example.com
    secretName: wildcard-tls
  rules:
  - host: app2.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: {{ .Chart.Name }}-app2
          servicePort: 80

I tenei keehi, ko nga hapa 404 me 502 ka tukuna ki te ratonga wharangi hapa me nga pane e tika ana.

В Ko nga putanga o mua o Ingress karekau tenei ahuatanga (kare i te 0.23). A, ki te mea e 2 nga tono rereke rereke e rere ana i roto i to roopu ka hiahia koe ki te tautuhi i tetahi ratonga taunoa-muri-muri me te tukatuka o nga waehere hapa rereke mo ia o raatau, mo tenei ka whai koe ki te whakamahi i nga waahi mahi, kei a matou e rua.

Whakauru <0.23: whakatata ki tetahi

He ngawari ake tenei whiringa. Hei tono e mahi ana i ona wharangi, kei a maatau te HTML, kaore e mohio ki te titiro ki nga pane me te whakahoki i nga waehere whakautu tika. Ko taua tono ka hurihia me Ingress mai i te url /error-pages, me te pukapuka ws ko te HTML kua whakahokia mai.

Whakaahua i YAML:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ .Chart.Name }}-app2
  annotations:
    kubernetes.io/ingress.class: "nginx"
    ingress.kubernetes.io/server-snippet: |
      proxy_intercept_errors on;
      error_page 500 501 502 503 504 @error_pages;
      location @error_pages {
        rewrite ^ /error-pages/other/index.html break;
        proxy_pass http://error-pages.prod.svc.cluster.local;
      }
spec:
  tls:
  - hosts:
    - app2.example.com
    secretName: wildcard-tls
  rules:
  - host: app2.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: {{ .Chart.Name }}-app2
          servicePort: 80

Ko te ratonga mo tenei tukunga me noho te momo ClusterIP.

I te wa ano, i roto i te tono ka tukatukahia e matou te hapa, i roto i te Ingress ka taapirihia e matou he tohu-tumau, he whirihoranga-snippet ranei me nga mea e whai ake nei:

nginx.ingress.kubernetes.io    /server-snippet: |
      proxy_intercept_errors on;
      error_page 500 501 502 503 504 @error_pages;
      location @error_pages {
        rewrite ^ /error-pages/ws/index.html break;
        proxy_pass http://error-pages.prod.svc.cluster.local;
      }

Whakauru <0.23: huarahi tuarua

He whiringa mo te tono ka taea te tukatuka i nga pane... A, i te nuinga o te waa he huarahi tika ake tenei, he mea nama mai i nga hapa ritenga-http-. Ma te whakamahi a-ringa (te kape) ka kore koe e huri i nga tautuhinga o te ao.

Ko nga kaupae e whai ake nei. Hanga tatou rite te whakatakotoranga me te tono ka taea te whakarongo ki nga kupu matua e tika ana me te whakautu tika. Tāpirihia he tapahanga-tūmau ki te tono Ingress me nga ihirangi e whai ake nei:

nginx.ingress.kubernetes.io    /server-snippet: |
      proxy_intercept_errors off;
      error_page 404 = @custom_404;
      error_page 503 = @custom_503;
      location @custom_404 {
        internal;
        proxy_intercept_errors off;
        proxy_set_header       X-Code             404;
        proxy_set_header       X-Format           $http_accept;
        proxy_set_header       X-Original-URI     $request_uri;
        proxy_set_header       X-Namespace        $namespace;
        proxy_set_header       X-Ingress-Name     $ingress_name;
        proxy_set_header       X-Service-Name     $service_name;
        proxy_set_header       X-Service-Port     $service_port;
        proxy_set_header       Host               $best_http_host;
        rewrite ^ /error-pages/ws/index.html break;
        proxy_pass http://error-pages.prod.svc.cluster.local;
      }
      location @custom_503 {
        internal;
        proxy_intercept_errors off;
        proxy_set_header       X-Code             503;
        proxy_set_header       X-Format           $http_accept;
        proxy_set_header       X-Original-URI     $request_uri;
        proxy_set_header       X-Namespace        $namespace;
        proxy_set_header       X-Ingress-Name     $ingress_name;
        proxy_set_header       X-Service-Name     $service_name;
        proxy_set_header       X-Service-Port     $service_port;
        proxy_set_header       Host               $best_http_host;
        rewrite ^ /error-pages/ws/index.html break;
        proxy_pass http://error-pages.prod.svc.cluster.local;
      }

Ka taea e koe te kite, mo ia hapa e hiahia ana matou ki te tukatuka, me hanga e matou to matou ake waahi, ka whakauruhia nga pane katoa e tika ana, penei i te "taketake". ritenga-hapa-wharangi. Ma tenei ka taea e taatau te hanga i nga wharangi hapa motuhake mo nga waahi takitahi me nga kaitoro.

PS

Ko etahi atu mai i nga raupapa tohutohu me nga tinihanga K8s:

Pānuihia hoki i runga i ta maatau blog:

Source: will.com

Tāpiri i te kōrero