Issue on GCP Kubernetes cluster connectivity from local (Windows 10) - google-cloud-platform

I have two projects in GCP i.e. Project A and Project B. Project A has Cluster CA and Project B has Cluster CB. Now I have configured my local environment [ Windows 10 ] with Gcloud CLI and Kubetel to connect to environment. Usigng "gcloud init" command I am able to initialize context and able to connect to Project A and Cluster CA. and on executing "kubectl get pods" showing the pods in cluster CA.
Now I am trying to connect Cluster CB in project B. I am doing "gcloud init" re-configuration and selecting Project B. but now when I am executing "kubectl get pods", system is showing pods from cluster CA not from cluster CB.
Could you please help me with the steps to connect and get the details from cluster CB
C:\Program Files (x86)\Google\Cloud SDK>gcloud init
Welcome! This command will take you through the configuration of gcloud.
Settings from your current configuration [default] are:
accessibility:
screen_reader: 'True'
core:
account: ajoy.sinha#abc.com
disable_usage_reporting: 'True'
project: Project B
Pick configuration to use:
[1] Re-initialize this configuration [default] with new settings
[2] Create a new configuration
Please enter your numeric choice: 1
Your current configuration has been set to: [default]
You can skip diagnostics next time by using the following flag:
gcloud init --skip-diagnostics
Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).
Choose the account you would like to use to perform operations for this configuration:
[1] ajoy.sinha#abc.com
[2] Log in with a new account
Please enter your numeric choice: 1
You are logged in as: [ajoy.sinha#abc.com].
Pick cloud project to use:
[1] PROJECT A
[2] PROJECT B
[3] Enter a project ID
[4] Create a new project
Please enter numeric choice or text value (must exactly match list item): 2
Your current project has been set to: [PROJECT B].
Do you want to configure a default Compute Region and Zone? (Y/n)? n
Your Google Cloud SDK is configured and ready to use!
* Commands that require authentication will use ajoy.sinha#abc.com by default
* Commands will reference project `PROJECT B` by default
Run `gcloud help config` to learn how to change individual settings
This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.
Some things to try next:
* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting
* Run `gcloud cheat-sheet` to see a roster of go-to `gcloud` commands.
C:\Program Files (x86)\Google\Cloud SDK>kubectl get pods
NAME READY STATUS RESTARTS AGE
metabase 1/1 Running 0 3d19h
mongodb 1/1 Running 0 4d
C:\Program Files (x86)\Google\Cloud SDK>

Kubernetes get auth by .kube/config file, what you are updating is gcloud auth or changing the project but not changing the context for the kubectl
This command will list the available cluster to connect if added any
kubectl config get-contexts
Change the context to another cluster
kubectl config use-context <cluster-name or context shown above>
If context is not available after gcloud auth you did you have to run the command to set/add kubeconfig file
gcloud container clusters get-credentials <cluster-name>
refer doc for more

Related

GCP Default Project - change from console

I need to change my default project (deleted the previous one). Can't find documentation for how to do this from either the console or the cli. Seems to be a pretty common requirement but ....
Navigate to the GCP Console via the url https://console.cloud.google.com/ will open the project selected during the last connection. (probably cached on your browser local storage).
For gcloud commands, you can change the default project with :
gcloud config set project PROJECT_ID
or initialize a new environment with:
gcloud init
Also note that you can create multiple configurations and then switch between them easily :
gcloud config configurations create CONFIG_NAME
gcloud config configurations activate CONFIG_NAME

gcloud project id in node.js error is different from gcloud set project id?

I'm trying to get Google Cloud Vision to work with node.js by following their documentation here. Although I keep getting:
PERMISSION_DENIED: Cloud Vision API has not been used in project 5678.. before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=5678.. then retry
To note though the project number is very different from what I see in gcloud's output when I gather information from the following commands:
gcloud info |tr -d '[]' | awk '/project:/ {print $2}'
'my-set-project' <=== set project id in use
gcloud projects list
which outputs:
PROJECT_ID='my-set-project' // <=== Same id as "gcloud info" command
NAME='my-project-name'
PROJECT_NUMBER=1234.. // <===== Different number from Node.js Error
I have already enabled the api, downloaded a service key and setup the export GOOGLE_APPLICATION_CREDENTIALS=[path/to/my/service/key]. But right now I believe that the service key linkup is not the issue yet as I have not yet really have had gcloud pointing to 'my-set-project'.
I have also found a default.config at
cat /Users/My_Username/.config/gcloud/application_default_credentials.json
which has:
{
"client_id": "5678..-fgrh // <=== same number id as node.js error
So how can I get gcloud-cli to switch to project "1234" which has the API enabled there? I thought doing the command:
gcloud config set project 'my-set-project'
would get running node apps using gcp to use the project of '1234' instead of the default '5678'. Any help will be appreciated as I'm still getting used to the gcloud-cli. Thanks
Try:
gcloud auth activate-service-account --key-file=/path/to/your/service_account.json

gcloud beta functions deploy ha COMMAND not update HA (home automation) function URL

I stay trying do this.
(two weeks ago)
In my first gcloud beta functions deploy ha COMMAND I put XXXX project ID.
Now I stay trying put YYYY project ID, But the result only comes with XXXX project ID. Look at the photo, please.
Does anybody know how to solve this?
gcloud beta functions deploy ha issue command:
As Previously stated by Neuber, this is because the correct SDK Configuration was not set properly. Just trying to expand on Neuber's comment.
1) For getting the current project ID, run the following command:
$ gcloud config configurations list
gcloud lists the configurations and shows which configuration is currently active.
2) If more than one SDK configuration was found you should choose which one would be suitable for the job. You can switch between configurations with the next instrucction:
gcloud config configurations activate [NAME]
3) If the configuration needs to set the project properly, you may use the next command:
gcloud config set project [PROJECT]
Finally you may execute the ha command in question.
For more information on this, refer to [1].
[1] https://cloud.google.com/sdk/docs/configurations

Google Cloud - Wrong project id being used from different email address

Despite running gcloud auth application-default login and gcloud config set core/project CORRECT_PROJECT_ID the project keeps defaulting to an incorrect project id:
gcloud config list
[core]
account = CORRECT_EMAIL
disable_usage_reporting = True
project = CORRECT_PROJECT_ID
Your active configuration is: [default]
I can successfully run the sample code from the tutorial (below) if I run in the terminal
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
However, I didn't want to have to do this every time, so I ran the command:
gcloud auth application-default login
This opened a browser with a list of my gmail accounts, and even though I selected the correct account, the success window went to a different gmail account. So then I tried it in an incognito window, and it worked.
However, running npm start resulted in the following error:
ERROR: { Error: 7 PERMISSION_DENIED: Cloud Natural Language API has not been used in project WRONG_PROJECT_ID before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/language.googleapis.com/overview?project=WRONG_PROJECT_ID then retry.
Then I ran gcloud config set core/project CORRECT_PROJECT_ID and got the message Updated property [core/project].
When I run npm start I get the same message:
ERROR: { Error: 7 PERMISSION_DENIED: Cloud Natural Language API has not been used in project WRONG_PROJECT_ID before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/language.googleapis.com/overview?project=WRONG_PROJECT_ID then retry.
I tried gcloud auth login and got the following message (after I authenticated in an incognito window):
WARNING: `gcloud auth login` no longer writes application default credentials.
If you need to use ADC, see:
gcloud auth application-default --help
You are now logged in as [CORRECT EMAIL ADDRESS].
Your current project is [CORRECT_PROJECT_ID]. You can change this setting by running:
$ gcloud config set project PROJECT_ID
I have a few suggestions that may correct this behaviour.
1) Clear your web browser cache & cookies. Then run "gcloud auth application-default login"
2) Try re-installing the gcloud toolkit.
3) Try unsetting the project in your config first, then set the project to the correct project. i.e.
gcloud config unset project WRONG_PROJECT_ID
gcloud config set project CORRECT_PROJECT_ID
4) Check the “CLOUDSDK_CORE_PROJECT” environment variable. Set it to the correct project if it is not already.
5) Try re-running “gcloud init”
6) You can find your application default credentials in
Linux: ~/.config/gcloud/application_default_credentials.json
Windows: C:\Users\%username%\AppData\Roaming\gcloud\credentials
You can delete the file, & regenerate it using commands you had mentioned in your question such as “gcloud auth default-credentials login”
We need to find where npm start is getting its credentials from. Once we figure that out, we can figure out how to change it, & understand why it’s looking there etc.
Are you able to find the config file to see where it is looking for credentials?
Is npm start the entire command? I’m not too familiar with Node JS. I’m not sure why it is trying to use Natural Language API.
You also mentioned a tutorial but I think you may have forgotten to include it in your question. Which tutorial are you referring to?

How to change the project in GCP using CLI commands

How can I change the current running project to another project in GCP (Google Cloud Platform) account using cli commands other than using gcloud init manually?
gcloud projects list will list the projects running on my account. I want to change the current project to any other project from the list using a cli command.
gcloud config set project $MY_PROJECT_ID
#=>
Updated property [core/project].
You may also set the environment variable $CLOUDSDK_CORE_PROJECT.
Make sure you are authenticated with the correct account:
gcloud auth list
* account 1
account 2
Change to the project's account if not:
gcloud config set account `ACCOUNT`
Depending on the account, the project list will be different:
gcloud projects list
- project 1
- project 2...
Switch to intended project:
gcloud config set project `PROJECT ID`
You should actually use the project ID and not the name as the other answers imply.
Example:
gcloud projects list
PROJECT_ID NAME PROJECT_NUMBER
something-staging-2587 something-staging 804012817122
something-production-24 something-production 392181605736
Then:
gcloud config set project something-staging-2587
It's also the same thing when using just the --project flag with one of the commands:
gcloud --project something-staging-2587 compute ssh my_vm
If you use the name it will silently accept it but then you'll always get connection or permission issues when trying to deploy something to the project.
The selected answer doesn't help if you don't know the name of projects you have added gcloud already. My flow is to list the active projects, then switch to the one I want.
gcloud config configurations list
gcloud config configurations activate [NAME]
where [NAME] is listed from the prior command.
It could be that I'm late to answer, but this command made me learn a lot about gcloud SDK
gcloud alpha interactive
It's easier to discover by yourself that you'll need gcloud config set project my-project.
However, what I like about gcloud is tab completion, so if you configure your gcloud config with configurations (I know it sounds weird but run this command gcloud config configurations list) you can easily switch between your own projects that you usually work:
The alias that I use is:
alias gcca="gcloud config configurations activate" and it works fine with zsh gcloud plugin.
EDIT:
To configure one of configurations I usually do this
gcloud config configurations create [CUSTOM_NAME]
gcloud auth login # you can also manually set but this is for lazy one
gcloud config set project [gcp-project-id]
gcloud config set compute/zone europe-west3-c
gcloud config set compute/region europe-west3
You can use ENV variables too to configure zone/project but I like when it's configured this way so I can use tab complication between projects.
Do the following:
1 - gcloud auth list
2 - gcloud projects list
3 - gcloud config set project *projectid*
(replace project id with actual project id)
Also, if you are using more than one project and don't want to set global project every time, you can use select project flag.
For example: to connect a virtual machine, named my_vm under a project named my_project in Google Cloud Platform:
gcloud --project my_project compute ssh my_vm
This way, you can work with multiple project and change between them easily by just putting project flag. You can find much more information about other GCP flags from here.
For what its worth if you have a more than a handful of projects, which I do, use:
gcloud init
This will list all your projects and give you the option to change current project settings, add a new project configuration or switch:
Pick configuration to use:
[1] Re-initialize this configuration [esqimo-preprod] with new settings
[2] Create a new configuration
[3] Switch to and re-initialize existing configuration: [default]
[4] Switch to and re-initialize existing configuration: [project 1]
[5] Switch to and re-initialize existing configuration: [project 2]
Please enter your numeric choice:
It will always ask you to login and display options for different google accounts that you may have.
Given that I manage multiple organisations and projects this approach lets' me to simply switch between them.
I do prefer aliases, and for things that might need multiple commands, based on your project needs, I prefer functions...
Example
function switchGCPProject() {
gcloud config set project [Project Name]
// if you are using GKE use the following
gcloud config set container/cluster [Cluster Name]
// if you are using GCE use the following
gcloud config set compute/zone [Zone]
gcloud config set compute/region [region]
// if you are using GKE use the following
gcloud container clusters get-credentials [cluster name] --zone [Zone] --project [project name]
export GOOGLE_APPLICATION_CREDENTIALS=path-to-credentials.json
}
gcloud projects list
To get List of Projects.
gcloud config set project [Project-ID]
For setting default project.
You can also export your project id into variable to use in future commands which helps in avoiding typos with following.
MY_PROJECT_ID=[Project-ID]
echo $MY_PROJECT_ID
Check the available projects by running: gcloud projects list. This will give you a list of projects which you can access.
To switch between projects: gcloud config set project <project-id>.
Also, I recommend checking the active config before making any change to gcloud config. You can do so by running: gcloud config list
I'm posting this answer to give insights into multiple ways available for you to change the project on GCP. I will also explain when to use each of the following options.
Option 1: Cloud CLI - Set Project Property on Cloud SDK on CLI
Use this option, if you want to run all Cloud CLI commands on a specific project.
gcloud config set project <Project-ID>
With this, the selected project on Cloud CLI will change, and the currently selected project is highlighted in yellow.
Option 2: Cloud CLI - Set Project ID flag with most Commands
Use this command if you want to execute commands on multiple projects. Eg: create clusters in one project, and use the same configs to create on another project. Use the following flag for each command.
--project <Project-ID>
Option 3: Cloud CLI - Initialize the Configurations in CLI
This option can be used if you need separate configurations for different projects/accounts. With this, you can easily switch between configurations by using the activate command. Eg: gcloud config configurations activate <congif-name>.
gcloud init
Option 4: Open new Cloud Shell with your preferred project
This is preferred if you don't like to work with CLI commands. Press the PLUS + button for a new tab.
Next, select your preferred project.
I add aliases to the .bash_alaises to switch to a different project.
alias switch_proj1="gcloud config set project ************"
Here is a script to generate aliases :) for all projects listed.
Please update the switch_proj to unique project aliases that you can remember.
gcloud projects list | awk '{print "alias switch_proj=\"gcloud config set project " $1 "\""}'
To update your existing project to another project, you can use this command line:
gcloud projects update PROJECT_ID --name=NAME
NAME: will be the new name of your project.
Check your project by running gcloud config list
Then gcloud config set "project name"
You can try: gcloud config set project [project_id]
add this below script in ~/.bashrc and do please replace project name(projectname) with whatever the name you needed
function s() {
array=($(gcloud projects list | awk /projectname/'{print $1}'))
for i in "${!array[#]}";do printf "%s=%s\n" "$i" "${array[$i]}";done
echo -e "\nenter the number to switch project:\c"
read project
[ ${array[${project}]} ] || { echo "project not exists"; exit 2; }
printf "\n**** Running: gcloud config set project ${array[${project}]} *****\n\n"
eval "gcloud config set project ${array[${project}]}"
}
Just use the gcloud projects list to get the project you have . Get the PROJECT_ID of the poject to use
After that use gcloud set project --project=PROJECT_ID to set the project.
You can change the project using the gcloud command:
gcloud config set project <your_project_name>
gcloud config set project <PROJECT_ID>
Before setting the Gcloud project see the list of available project
gcloud projects list
then set the project using
gcloud config set project $MY_PROJECT_ID
make sure you are passing project Id (not project name as both are different)