该容器在Google Cloud Shell中启动,但在Kubernetes Engine上失败

时间:2018-07-04 17:08:58

标签: python-3.x docker kubernetes google-cloud-platform google-kubernetes-engine

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

我尝试使用Google示例作为示例,使用http(s)映射创建简单的gRPC服务器。问题是我的容器可以毫无问题地从Google云外壳启动,但是在部署后在Kubernetes Engine上失败了。

在Google Cloud Console中:

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上失败? 谢谢。

requirements.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

1 个答案:

答案 0 :(得分:2)

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

正确的方法:

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的{​​{1}}。 more info