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

3/16/2020

In GCP, I've created 2 projects as below:

  • Project A With the cluster A-Cluster consisting of 2 nodes: Node-A-1, Node-A-2

  • Project B With the cluster B-Cluster consisting of 2 nodes: Node-B-1, Node-B-2

After selected the Project B on the Google Cloud Conosle, and executed following command on the Cloud Shell:

kubectl create -f projectB.yaml

Here's the 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

Then I always got this same error:

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

And I found out rootcause is, the above pod for project B, instead to any node of the B-Cluster, it's actually assigned to the Node-A-1 of the A-Cluster! So, why the above pod was not assigned to a node in B-Cluster for project B? Is it anything related to Namespace? (both projects are in the same 'default' namespace)

-- Roy Hu
google-cloud-platform
kubernetes
kubernetes-pod

1 Answer

3/17/2020

As I understand you are running multiple clusters of Kubernetes and would like to deploy your project from a command line.

If this is indeed the case You should set your context while working with multi cluster environment. Here are docs regarding Configure Access to Multiple Clusters.

Once this is correctly setup you will be able to use:

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

In google you could connect directly to your cluster using

gcloud container clusters get-credentials <cluster-name> --zone <zone-name> --project <project-name>

Which will fetch cluster endpoint and auth data and generate kubeconfig entry for <cluster-name>. So you will be operating only on that cluster and if you would like to change the cluster you are working on you would need to use the command again changing the <cluster-name> to the one you want to work on.

If you are interested you can read more about gcloud container command.

-- Crou
Source: StackOverflow