具有 Ingress 设置的 GKE 总是给出状态 UNHEALTHY
GKE with Ingress setup always gives status UNHEALTHY
首先,我在 https://cloud.google.com/kubernetes-engine/docs/tutorials/http-balancer
测试了教程
效果很好。我还测试了相同的教程,但也添加了一个 tls secret 来测试 https,它也工作正常。
我创建自己的图像时出现了问题。这是我采取的步骤:
- Dockerfile:
# We label our stage as "builder"
FROM node:9.4.0-alpine as builder
COPY package.json package-lock.json ./
## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i && mkdir /srv/cs-ui && cp -R ./node_modules ./srv/cs-ui
WORKDIR /srv/cs-ui
COPY . .
## Build the angular app in production mode and store the artifacts in dist folder
RUN $(npm bin)/ng build --environment "prod"
FROM nginx
## Copy our default nginx config
COPY nginx/default.conf /etc/nginx/conf.d/
## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*
## From "builder" stage copy over the artifacts in dist folder to default nginx nginx public folder
COPY --from=builder /srv/cs-ui/dist /usr/share/nginx/html/
- Dockerfile 是 运行 和 docker-compose 文件,看起来像这样:
version: '2'
services:
cs-ui:
image: "gcr.io/cs-micro/cs-ui:v1"
container_name: "cs-ui"
tty: true
build: .
ports:
- "80:80"
- 在本地这没有任何问题。接下来我要做的是将其推送到 Container Registry。
gcloud docker -- push gcr.io/cs-micro/cs-ui:v1
- 之后我创建了一个容器:
kubectl run cs-ui --image=gcr.io/cs-micro/cs-ui:v1 --port=80
- 那我曝光一下:
kubectl expose deployment cs-ui --target-port=80 --type=NodePort
- 然后我运行下面的入口文件:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: basic-ingress
spec:
tls:
- secretName: tls-certificate
backend:
serviceName: cs-ui
servicePort: 80
使用命令:
kubectl apply -f test.yaml
- kubectl 描述服务
Name: cs-ui
Namespace: default
Labels: run=cs-ui
Annotations:
Selector: run=cs-ui
Type: NodePort
IP: 10.35.244.124
Port: 80/TCP
TargetPort: 80/TCP
NodePort: 30272/TCP
Endpoints: 10.32.0.32:80
Session Affinity: None
External Traffic Policy: Cluster
Events:
Name: kubernetes
Namespace: default
Labels: component=apiserver
provider=kubernetes
Annotations:
Selector:
Type: ClusterIP
IP: 10.35.240.1
Port: https 443/TCP
TargetPort: 443/TCP
Endpoints: 35.195.192.28:443
Session Affinity: ClientIP
Events:
- kubectl 描述部署
Name: cs-ui
Namespace: default
CreationTimestamp: Thu, 25 Jan 2018 12:27:59 +0100
Labels: run=cs-ui
Annotations: deployment.kubernetes.io/revision=1
Selector: run=cs-ui
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=cs-ui
Containers:
cs-ui:
Image: gcr.io/cs-micro/cs-ui:v1
Port: 80/TCP
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets:
NewReplicaSet: cs-ui-2929390783 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 9m deployment-controller Scaled up replica set cs-ui-2929390783 to 1
- kubectl 描述
Name: basic-ingress
Namespace: default
Address: 35.227.220.186
Default backend: cs-ui:80 (10.32.0.32:80)
TLS:
tls-certificate terminates
Rules:
Host Path Backends
---- ---- --------
* * cs-ui:80 (10.32.0.32:80)
Annotations:
https-forwarding-rule: k8s-fws-default-basic-ingress--f5fde3efbfa51336
https-target-proxy: k8s-tps-default-basic-ingress--f5fde3efbfa51336
ssl-cert: k8s-ssl-default-basic-ingress--f5fde3efbfa51336
target-proxy: k8s-tp-default-basic-ingress--f5fde3efbfa51336
url-map: k8s-um-default-basic-ingress--f5fde3efbfa51336
backends: {"k8s-be-30272--f5fde3efbfa51336":"UNHEALTHY"}
forwarding-rule: k8s-fw-default-basic-ingress--f5fde3efbfa51336
static-ip: k8s-fw-default-basic-ingress--f5fde3efbfa51336
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ADD 12m loadbalancer-controller default/basic-ingress
Normal CREATE 11m loadbalancer-controller ip: 35.227.220.186
Normal Service 6m (x4 over 11m) loadbalancer-controller default backend set to cs-ui:30272
- 3-5 分钟后我变得不健康,我不知道为什么,因为设置与他们的设置几乎完全相同。
我已经阅读了无数关于后端状态为“不健康”时该怎么做的帖子,但其中 none 对我有所帮助。在本教程中提到要添加防火墙规则:https://cloud.google.com/compute/docs/load-balancing/health-checks 我已添加,但没有帮助。
如果您有任何建议,我很乐意进行测试。
原来我们的 Angular 应用程序在“/”上有一个重定向,这给了它一个 302 响应。此响应使运行状况检查失败并导致 UNHEALTHY 状态。
一旦我们设置了自定义健康检查,它就起作用了。
首先,我在 https://cloud.google.com/kubernetes-engine/docs/tutorials/http-balancer
测试了教程效果很好。我还测试了相同的教程,但也添加了一个 tls secret 来测试 https,它也工作正常。
我创建自己的图像时出现了问题。这是我采取的步骤:
- Dockerfile:
# We label our stage as "builder" FROM node:9.4.0-alpine as builder COPY package.json package-lock.json ./ ## Storing node modules on a separate layer will prevent unnecessary npm installs at each build RUN npm i && mkdir /srv/cs-ui && cp -R ./node_modules ./srv/cs-ui WORKDIR /srv/cs-ui COPY . . ## Build the angular app in production mode and store the artifacts in dist folder RUN $(npm bin)/ng build --environment "prod" FROM nginx ## Copy our default nginx config COPY nginx/default.conf /etc/nginx/conf.d/ ## Remove default nginx website RUN rm -rf /usr/share/nginx/html/* ## From "builder" stage copy over the artifacts in dist folder to default nginx nginx public folder COPY --from=builder /srv/cs-ui/dist /usr/share/nginx/html/
- Dockerfile 是 运行 和 docker-compose 文件,看起来像这样:
version: '2' services: cs-ui: image: "gcr.io/cs-micro/cs-ui:v1" container_name: "cs-ui" tty: true build: . ports: - "80:80"
- 在本地这没有任何问题。接下来我要做的是将其推送到 Container Registry。
gcloud docker -- push gcr.io/cs-micro/cs-ui:v1
- 之后我创建了一个容器:
kubectl run cs-ui --image=gcr.io/cs-micro/cs-ui:v1 --port=80
- 那我曝光一下:
kubectl expose deployment cs-ui --target-port=80 --type=NodePort
- 然后我运行下面的入口文件:
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: basic-ingress spec: tls: - secretName: tls-certificate backend: serviceName: cs-ui servicePort: 80
使用命令:
kubectl apply -f test.yaml
- kubectl 描述服务
Name: cs-ui Namespace: default Labels: run=cs-ui Annotations: Selector: run=cs-ui Type: NodePort IP: 10.35.244.124 Port: 80/TCP TargetPort: 80/TCP NodePort: 30272/TCP Endpoints: 10.32.0.32:80 Session Affinity: None External Traffic Policy: Cluster Events: Name: kubernetes Namespace: default Labels: component=apiserver provider=kubernetes Annotations: Selector: Type: ClusterIP IP: 10.35.240.1 Port: https 443/TCP TargetPort: 443/TCP Endpoints: 35.195.192.28:443 Session Affinity: ClientIP Events:
- kubectl 描述部署
Name: cs-ui Namespace: default CreationTimestamp: Thu, 25 Jan 2018 12:27:59 +0100 Labels: run=cs-ui Annotations: deployment.kubernetes.io/revision=1 Selector: run=cs-ui Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 1 max unavailable, 1 max surge Pod Template: Labels: run=cs-ui Containers: cs-ui: Image: gcr.io/cs-micro/cs-ui:v1 Port: 80/TCP Environment: Mounts: Volumes: Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable OldReplicaSets: NewReplicaSet: cs-ui-2929390783 (1/1 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 9m deployment-controller Scaled up replica set cs-ui-2929390783 to 1
- kubectl 描述
Name: basic-ingress Namespace: default Address: 35.227.220.186 Default backend: cs-ui:80 (10.32.0.32:80) TLS: tls-certificate terminates Rules: Host Path Backends ---- ---- -------- * * cs-ui:80 (10.32.0.32:80) Annotations: https-forwarding-rule: k8s-fws-default-basic-ingress--f5fde3efbfa51336 https-target-proxy: k8s-tps-default-basic-ingress--f5fde3efbfa51336 ssl-cert: k8s-ssl-default-basic-ingress--f5fde3efbfa51336 target-proxy: k8s-tp-default-basic-ingress--f5fde3efbfa51336 url-map: k8s-um-default-basic-ingress--f5fde3efbfa51336 backends: {"k8s-be-30272--f5fde3efbfa51336":"UNHEALTHY"} forwarding-rule: k8s-fw-default-basic-ingress--f5fde3efbfa51336 static-ip: k8s-fw-default-basic-ingress--f5fde3efbfa51336 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ADD 12m loadbalancer-controller default/basic-ingress Normal CREATE 11m loadbalancer-controller ip: 35.227.220.186 Normal Service 6m (x4 over 11m) loadbalancer-controller default backend set to cs-ui:30272
- 3-5 分钟后我变得不健康,我不知道为什么,因为设置与他们的设置几乎完全相同。
我已经阅读了无数关于后端状态为“不健康”时该怎么做的帖子,但其中 none 对我有所帮助。在本教程中提到要添加防火墙规则:https://cloud.google.com/compute/docs/load-balancing/health-checks 我已添加,但没有帮助。
如果您有任何建议,我很乐意进行测试。
原来我们的 Angular 应用程序在“/”上有一个重定向,这给了它一个 302 响应。此响应使运行状况检查失败并导致 UNHEALTHY 状态。
一旦我们设置了自定义健康检查,它就起作用了。