首页 > 解决方案 > 容器在 Google Cloud Shell 中启动,但在 Kubernetes Engine 上失败

问题描述

我是使用 Kubernetes、Docker 和 GCP 的新手,如果问题很愚蠢和(或)很明显,我很抱歉。

我尝试使用 Google 示例创建带有 http(s) 映射的简单 gRPC 服务器。问题是我的容器可以从 Google Cloud Shell 启动而没有任何抱怨,但在部署后在 Kubernetes Engine 上失败。

在谷歌云控制台中:

git clone https://gitlab.com/myrepos/grpc.git
cd grpc
docker build -t gcr.io/project-id/python-grpc-diagnostic-server:v1 .

# Run the container "locally"
docker run --rm -p 8000:8000 gcr.io/mproject-id/python-grpc-diagnostic-server:v1                                    
Server is started
^CServer is stopped

# Pushing the image to Container Registry
gcloud docker -- push gcr.io/project-id/python-grpc-diagnostic-server:v1

# Deployment 
kubectl create -f grpc-diagnostic.yaml

在部署详细信息中,“诊断”容器具有“CrashLoopBackOff”状态,并且在日志中出现下一个错误:

File "/diagnostic/diagnostic_pb2.py", line 17, in <module>
    from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2
ModuleNotFoundError: No module named 'google.api'

您能否说明为什么同一个容器在 shell 中启动并在 Kubernetes Engine 上失败?谢谢。

要求.txt

grpcio
grpcio-tools
pytz
google-auth
googleapis-common-protos

Dockerfile

FROM gcr.io/google_appengine/python

# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.
RUN virtualenv -p python3.6 /env

# Setting these environment variables are the same as running
# source /env/bin/activate.
ENV VIRTUAL_ENV -p python3.6 /env
ENV PATH /env/bin:$PATH

ADD . /diagnostic/

WORKDIR /diagnostic
RUN pip install -r requirements.txt

EXPOSE 8000

ENTRYPOINT ["python", "/diagnostic/diagnostic_server.py"]

grpc-diagnostic.yaml

apiVersion: v1
kind: Service
metadata:
  name: esp-grpc-diagnostic
spec:
  ports:
  # Port that accepts gRPC and JSON/HTTP2 requests over HTTP.
  - port: 80
    targetPort: 9000 # or 8000?
    protocol: TCP
    name: http2
  selector:
    app: esp-grpc-diagnostic
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: esp-grpc-diagnostic
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: esp-grpc-diagnostic
    spec:
      containers:
      - name: esp
        image: gcr.io/endpoints-release/endpoints-runtime:1
        args: [
          "--http2_port=9000",
          "--service=diagnostic.endpoints.project-id.cloud.goog",
          "--rollout_strategy=managed",
          "--backend=grpc://127.0.0.1:8000"
        ]
        ports:
          - containerPort: 9000
      - name: diagnostic
        image: gcr.io/project-id/python-grpc-diagnostic-server:v1
        ports:
          - containerPort: 8000

标签: python-3.xdockerkubernetesgoogle-cloud-platformgoogle-kubernetes-engine

解决方案


那是我的愚蠢错误。我更改了图像,但图像名称相同,因此集群继续使用旧的错误图像,认为没有任何改变。重新部署代码的正确方法是使用新标签创建映像,例如 v1.01,并按照文档中的说明为现有部署设置新映像。我删除了服务和部署并重新创建了它,但我没有删除集群,以为我是从头开始的。

正确的路:

docker build -t gcr.io/project-id/python-grpc-diagnostic-server:v1.01 . 
gcloud docker -- push gcr.io/project-id/python-grpc-diagnostic-server:v1.01   
kubectl set image deployment/esp-grpc-diagnostic diagnostic=gcr.io/project-id/python-grpc-diagnostic-server:v1.01

另一种提取未更改名称的更新图像的方法是更改​​默认imagePullPolicy设置IfNotPresent更多信息


推荐阅读