Unable to publish messages to GCP Pub/Sub using Python SDK when executed via Cron inside a GKE POD

前端 未结 1 1179
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 03:04

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 ti         


        
相关标签:
1条回答
  • 2021-01-16 03:07

    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
    
    0 讨论(0)
提交回复
热议问题