我是使用 Kubernetes、Docker 和 GCP 的新手,如果这个问题很愚蠢和(或)明显,我很抱歉。
我尝试使用 Google 示例作为示例,使用 http(s) 映射创建简单的 gRPC 服务器。问题是我的容器可以从 Google Cloud shell 启动,没有任何提示,但部署后在 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 上失败? 谢谢。
需求.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
最佳答案
那是我愚蠢的错误。我更改了镜像,但镜像的名称是相同的,因此集群继续使用旧的错误镜像,认为没有任何变化。 重新部署代码的正确方法是使用新标签创建镜像,例如 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
在不更改名称的情况下提取更新图像的另一种方法是更改默认设置为 IfNotPresent
的 imagePullPolicy
。 more info
关于python-3.x - 容器在 Google Cloud Shell 中启动,但在 Kubernetes Engine 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51178355/