在GKE POD中通过Cron执行时,无法使用Python SDK将消息发布到GCP发布/订阅 [英] Unable to publish messages to GCP Pub/Sub using Python SDK when executed via Cron inside a GKE POD
问题描述
使用PYthon SDK将消息发布到GCP发布/发布。代码正在GKE上的Kubernetes POD中运行。
Using a PYthon SDK to publish messages to GCP Pub/SUb. THe code is running inside a Kubernetes POD on GKE.
import pymysql
import os
import argparse
import time
from google.cloud import pubsub_v1
entries = ['jelly']
def publish_messages(project, topic_name):
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project, topic_name)
for n in entries:
data = u'Message number {}'.format(n)
data = data.encode('utf-8')
publisher.publish(topic_path, data=data)
print "Message %s sent to queue" % n
手动执行脚本可以正常工作。但是,通过Crontab触发时失败。
Script works fine when executed manually. However, it fails when triggered via Crontab.
Error: No handlers could be found for logger google.cloud.pubsub_v1.publisher._batch.thread"
推荐答案
找到了解决方案。
默认情况下,Crontab不会从系统环境变量中读取。上面的Python代码需要包含服务帐户密钥(在这种情况下为ConfigMap)的env变量 GOOGLE_APPLICATION_CREDENTIALS。为此,必须在运行时将所有的env变量打印到容器的 / etc / environment文件中
像这样:
Found the solution. Crontab by default doesn't read from the system environment variables. And this Python code above needs the env variable "GOOGLE_APPLICATION_CREDENTIALS" which holds the service Account key (ConfigMap in this case). To achieve this all the env variables must be printed out to "/etc/environment" file of the container on the runtime. Something like this:
FROM ubuntu:latest
ADD send.py Jelly/send.py
COPY jellycron /etc/cron.d/jellycron
RUN apt-get update && apt-get install -y cron vim mysql-server curl python python-pip
&& pip install --upgrade pymysql google-api-python-client google-cloud google-
cloud-pubsub && touch /var/log/cron.log && chmod 0644 /etc/cron.d/jellycron &&
crontab /etc/cron.d/jellycron
CMD printenv >> /etc/environment && cron && tail -f /var/log/cron.log
这篇关于在GKE POD中通过Cron执行时,无法使用Python SDK将消息发布到GCP发布/订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!