How to execute kubectl command in a cluster

Viewed 2476

I have two clusters

NAME                   LOCATION        MASTER_VERSION  MASTER_IP      MACHINE_TYPE   NODE_VERSION    NUM_NODES  STATUS
cassandra-cluster      europe-west4-a  1.14.10-gke.36  xx.90.xx.31    n1-standard-1  1.14.10-gke.36  3          RUNNING
codingjediweb-cluster  europe-west4-a  1.14.10-gke.36  uu.90.uu.182  n1-standard-1  1.14.10-gke.36  2          RUNNING
manuchadha25@cloudshell:~ (copper-frame-262317)$

I want to run the following command on cassandra-cluster. How do I make cassandra-cluster my current context?

I am getting error

CASS_USER=$(kubectl --cluster gke_copper-frame-262317_europe-west4-a_cassandra-cluster get secret cluster1-superuser -o json | jq -r '.data.username' | base64 --decode)kubectl
Error from server (NotFound): secrets "cluster1-superuser" not found

I tried this but it failed.

manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl config use-context cassandra-cluster 
error: no context exists with the name: "cassandra-cluster"
2 Answers

You can work with multiple cluster setting the correct context, as mentioned here:

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

When working with multilples cluster, you always need to know in what cluster you are performing commands, to make it easy,you can use this bash script to show in you $PS1 the current context and namespace.

I had to set get contexts and then set the context as follow

manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl config get-contexts
CURRENT   NAME                                                           CLUSTER                                                        AUTHINFO                                                       NAMESPACE
          gke_copper-frame-262317_europe-west4-a_cassandra-cluster       gke_copper-frame-262317_europe-west4-a_cassandra-cluster       gke_copper-frame-262317_europe-west4-a_cassandra-cluster   
*         gke_copper-frame-262317_europe-west4-a_codingjediweb-cluster   gke_copper-frame-262317_europe-west4-a_codingjediweb-cluster   gke_copper-frame-262317_europe-west4-a_codingjediweb-cluster
manuchadha25@cloudshell:~ (copper-frame-262317)$ kubectl config use-context gke_copper-frame-262317_europe-west4-a_codingjediweb-cluster
Related