如何确保Deployment/Pod被构建到GCP当前项目的Cluster中?

How to ensure the Deployment/Pod being built into the Cluster of current project in GCP?

在 GCP 中,我创建了 2 个项目,如下所示:

在 Google Cloud Conosle 上选择项目 B 后,在 Cloud Shell 上执行以下命令:

kubectl create -f projectB.yaml

这是projectB.yaml:

---
kind: Service
apiVersion: v1
metadata:
  name: projectb
spec:
  selector: 
      app: projectb
  ports:
     - protocol: "TCP"
       port:  80
       targetPort:  8080
  type: LoadBalancer
  loadBalancerIP: "35.238.24.168"

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: projectb
spec:
  replicas: 1
  selector: 
    matchLabels:
        app: projectb
  template:
    metadata:
      labels:
        app: projectb
    spec:
      containers:
      - name: projectb
        image: gcr.io/projectb/mp:latest
        ports:
        - containerPort: 80

然后我总是得到同样的错误:

NAME                                             READY   STATUS             RESTARTS   AGE
projectb-7d5c647876-v7cwx                        0/1     ImagePullBackOff   0          18m

我发现根本原因是,上面的项目B的pod,而不是B-Cluster的任何节点,它实际上分配给了A-Cluster的Node-A-1!那么,为什么上面的pod没有分配到项目B的B-Cluster中的某个节点上呢?它与命名空间有关吗? (两个项目都在同一个 'default' 命名空间中)

据我了解,您是 运行 多个 Kubernetes 集群,并且想从命令行部署您的项目。

如果确实如此,您应该在使用多集群环境时设置 context。这是关于 Configure Access to Multiple Clusters.

的文档

正确设置后,您将能够使用:

kubectl config get-contexts                          # display list of contexts 
kubectl config current-context                       # display the current-context
kubectl config use-context my-cluster-name           # set the default context to my-cluster-name

在 google 中,您可以使用

直接连接到您的集群
gcloud container clusters get-credentials <cluster-name> --zone <zone-name> --project <project-name>

这将获取集群端点和身份验证数据并为 <cluster-name> 生成 kubeconfig 条目。 因此,您将仅在该集群上运行,如果您想更改正在使用的集群,则需要再次使用命令将 <cluster-name> 更改为您想要使用的集群。

如果您有兴趣,可以阅读有关 gcloud container 命令的更多信息。