我使用以下配置进行 Kubernetes 部署
resource "kubernetes_deployment" "batch-producer" {
metadata {
name = var.app-name
namespace = var.k8s-namespace.metadata[0].name
labels = {
app = var.app-name
}
}
spec {
replicas = 1
selector {
match_labels = {
app = var.app-name
}
}
template {
metadata {
labels = {
app = var.app-name
}
}
spec {
container {
name = var.app-name
image = var.docker-image
image_pull_policy = "Never"
port { container_port = 80 }
port { container_port = 443 }
command = [
"sh",
"-exc",
<<-EOT
mkdir /secrets
echo ${var.storage-sa-key} | base64 --decode > ./secrets/gcp_creds.json
python ./run.py
EOT
,
""
]
env_from {
config_map_ref {
name = "batch-producer-config"
}
}
env {
name = "GOOGLE_APPLICATION_CREDENTIALS"
value = "/secrets/gcp_creds.json"
}
}
}
}
}
}
应用程序应该将文件写入GCS
class GCSWriter(AbstractWriter):
def __init__(self, properties: dict):
self.storage_client = storage.Client() \
.from_service_account_json(json_credentials_path=os.environ["GOOGLE_APPLICATION_CREDENTIALS"])
self.bucket_name = properties.get("bucket_name")
self.logger = get_logger()
def write(self, source_path):
# time.sleep(100)
bucket = self.storage_client.bucket(self.bucket_name)
blob = bucket.blob(os.path.join(
"incomes_data_source",
dt.today().strftime('%Y/%m/%d'),
os.path.split(source_path)[1]))
blob.upload_from_filename(source_path)
self.logger.info("File '%s' was uploaded to GCS successfully", source_path)
应用程序已部署,但一段时间后出现以下错误:
HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x7f993b606bd0>: Failed to resolve 'oauth2.googleapis.com' ([Errno -3] Temporary failure in name resolution)"))
我尝试 ping google.com 或通过 curl 从 pod 下载随机文件 - 成功。 我还尝试使用现有的 access-key.json 通过 docker 运行相同的容器 - 它也有效,我可以在 GCS 中看到上传的文件。 寻找线索如何解决这个问题。
感谢 Vasilii Angapov 评论,我发现了这个问题 - https://github.com/docker/for-mac/issues/7110。
我没有深入研究根本原因,我只是按照建议降级到 CoreDNS-1.10.0。
kubectl edit deployment/coredns -n kube-system
将版本从 1.11.1 更改为 1.10.0 并等待部署重新启动。
在我的设置中一切正常(Docker Desktop v4.27.1,Kubernetes v1.29.1)
我还尝试通过 Rancher Desktop 和 Kubernetes v1.28.n 启动相同的配置,它也运行良好。