![](/img/trans.png)
[英]GCP - Cloud Function can't find Python package from Artifact Registry in the same project
[英]GCP cloud function - Could not find kubectl on the path
我正在编写这个 Google Cloud Function (Python)
def create_kubeconfig(request):
subprocess.check_output("curl https://sdk.cloud.google.com | bash | echo "" ",stdin=subprocess.PIPE, shell=True )
os.system("./google-cloud-sdk/install.sh")
os.system("gcloud init")
os.system("curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.17.0/bin/linux/amd64/kubectl")
os.system("gcloud container clusters get-credentials **cluster name** --zone us-west2-a --project **project name**")
os.system("gcloud container clusters get-credentials **cluster name** --zone us-west2-a --project **project name**")
conf = KubeConfig()
conf.use_context('**cluster name**')
当我运行代码时,它给了我错误“无效的 kube-config 文件”。 ' kubernetes.config.config_exception.ConfigException: 无效的 kube-config 文件。 没有找到配置。
请帮我解决它
与其在 Cloud Function 中使用gcloud
(并尝试在每个请求上安装它,这将显着增加函数的运行时间),您应该使用google-cloud-container
客户端库直接从 Python 进行相同的 API 调用,例如:
from google.cloud import container_v1
client = container_v1.ClusterManagerClient()
project_id = 'YOUR_PROJECT_ID'
zone = 'YOUR_PROJECT_ZONE'
response = client.list_clusters(project_id, zone)
您必须以编程方式访问 K8S API。 您在文档中有对API的描述
但执行起来并不容易和简单。 但是,这里有一些用于实现您想要的输入。
然后您可以轻松访问集群。 在这里阅读部署
import google.auth
from google.auth.transport import requests
credentials, project_id = google.auth.default()
session = requests.AuthorizedSession(credentials)
response = session.get('https://34.76.28.194/apis/apps/v1/namespaces/default/deployments', verify=False)
response.raise_for_status()
print(response.json())
为了创建一个,你可以这样做
import google.auth
from google.auth.transport import requests
credentials, project_id = google.auth.default()
session = requests.AuthorizedSession(credentials)
with open("deployment.yaml", "r") as f:
data = f.read()
response = session.post('https://34.76.28.194/apis/apps/v1/namespaces/default/deployments', data=data,
headers={'content-type': 'application/yaml'}, verify=False)
response.raise_for_status()
print(response.json())
根据您要构建的对象,您必须使用正确的文件定义和正确的 API 端点。 我不知道一种方法可以在一个 API 调用中应用具有多个定义的整个yaml
。
最后,请务必为 Cloud Function 服务帐户提供正确的 GKE 角色
更新
另一种解决方案是使用 Cloud Run。 确实,借助 Cloud Run 和容器功能,您可以安装和调用系统进程(它是完全开放的,因为您的容器运行到 GVisor 沙箱中,但允许大多数常见用法)
想法如下:使用 gcloud SDK 基础映像并在其上部署您的应用程序。 然后,编写您的应用程序以执行系统调用。
这是 Go 中的一个工作示例
Docker 文件
FROM golang:1.13 as builder
# Copy local code to the container image.
WORKDIR /app/
COPY go.mod .
ENV GO111MODULE=on
RUN go mod download
COPY . .
# Perform test for building a clean package
RUN go test -v ./...
RUN CGO_ENABLED=0 GOOS=linux go build -v -o server
# Gcloud capable image
FROM google/cloud-sdk
COPY --from=builder /app/server /server
CMD ["/server"]
注:镜像cloud-sdk镜像重:700Mb
内容示例(只有快乐路径。我删除了错误管理,以及简化代码的 stderr/stdout 反馈)
.......
// Example here: recover the yaml file into a bucket
client,_ := storage.NewClient(ctx)
reader,_ := client.Bucket("my_bucket").Object("deployment.yaml").NewReader(ctx)
content,_:= ioutil.ReadAll(reader)
// You can store locally the file into /tmp directory. It's an in-memory file system. Don't forget to purge it to avoid any out of memory crash
ioutil.WriteFile("/tmp/file.yaml",content, 0644)
// Execute external command
// 1st Recover the kube authentication
exec.Command("gcloud","container","clusters","get-credentials","cluster-1","--zone=us-central1-c").Run()
// Then interact with the cluster with kubectl tools and simply apply your description file
exec.Command("kubectl","apply", "-f","/tmp/file.yaml").Run()
.......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.