Can't connect to GCP cluster VM - google-cloud-platform

I'm following this tutorial and can't connect to a GCP VM cluster using SSH port forwarding.
I run this command line:
$ gcloud compute ssh cluster-5b2b-m --zone=asia-northeast2-b \
--project=*** -- -L 8787:localhost:8787
but when I try to open
http://localhost:8787 in the browser i get an error saying This site can't be reached
Any suggestions please?

In Example, the full command should be as following, then gcloud will open a tunnel to your cluster. I think you forget to type [CLUSTER_NAME]-m
gcloud compute ssh \
--zone=[CLUSTER_ZONE] \
--project=[PROJECT_ID] \
[CLUSTER_NAME]-m -- \
-L 8787:localhost:8787

Yeah,
The issue is that you are trying to access to "localhost" in the browser but your cluster is in gcloud.
You can try to access using Rstudio: http://[CLUSTER_NAME]-m:8787 as the tutorial suggests, or http://[CLUSTER_NAME]-m:8088 from the browser, if the configuration is correct it should works.

Related

gcloud SSH in same terminal window

I use gcloud cloud-shell ssh to connect to Google Cloud Shell. However, this spawns a new window, (PuTTY) which bothers me. Is there a way (some -- flag / &c.) to use it from the same console window?
Thanks in advance!
The CLI is just a wrapper for launching Putty (on Windows) with the correct command line options. The SSH functionality is not built into gcloud.
Using --command flag, you can run the command on the virtual machine without opening new console window.
gcloud compute ssh your-instance --zone=your-zone --command="ps aux"
It runs the command on the target instance and then exits.
Use gcloud compute ssh --help command to learn more.
Instead of directly opening the SSH session with gcloud beta compute ssh you can set up a tunnel from a local port to the GCP instance and then use your preferred terminal to ssh over the tunnel.
You set up the tunnel with:
gcloud compute start-iap-tunnel --zone <zone> <name of instance> 22 --project <name of project> --local-host-port=localhost:4226
You open an SSH connection to localhost:4226 as you would do to the instance with:
ssh <user>#localhost -p 4226 -i <identity file>
(EDIT: make sure to include the identity file for GCP access)

How to copy a file from one gcp instance to another gcp instance in same project

I am currently running 29 instances in each available regions on GCP. And I need all of the instances to have some python script file.
As I was getting tired to upload them manually through the console 29 times, I was wondering if there's a way to upload the script in only one instance, and copy them all over to 28 other instances with gcloud scp command?
Currently, I was trying the following:
sudo gcloud compute scp --zone='asia-east1-b' /home/file.txt instance-asia-east1:/home/
The code above is trying to scp "file.txt" over to the instance-asia-east1.
I included the sudo command as it was having some permission issues. But after adding the sudo, I get another error message:
root#000.000.000.00: Permission denied (publickey).
lost connection
ERROR: (gcloud.compute.scp) [/usr/bin/scp] exited with return code [1].
What can be the issue, and how can I resolve this?
You should avoid using sudo.
If you add --verbosity=debug to (any but in this case) gcloud compute ssh or gcloud compute scp command, you'll see that gcloud invokes your host's (probably /usr/bin) ssh and scp commands. It uses a private key that was generated by gcloud using your credentials (gcloud config get account or the default gcloud auth list).
gcloud compute scp \
${PWD}/${FILE} \
${INSTANCE}:. \
--project=${PROJECT} \
--zone=${ZONE} \
--verbosity=debug
Yielding:
DEBUG: Running [gcloud.compute.scp] with arguments: ...
...
DEBUG: Current SSH keys in project: ['...:ssh-rsa ... user#host']
DEBUG: Running command [/usr/bin/scp -i .../.ssh/google_compute_engine -o ...
INFO: Display format: "default"
DEBUG: SDK update checks are disabled.
NOTE /usr/bin/scp -i .../.ssh/google_compute_engine ...
When you run as sudo, even if you copy your credentialed user's google_compute_engine SSH keys (to e.g. /root/.ssh), the authenticated user won't match, unless you also duplicate the gcloud config...
I recommend you solve the permission issue that triggered your use of sudo.

Using SSH to access JupyterLab on GCP Doesn't Work Yet

I would like to ssh myself into a Jupyter Lab notebook on GCP. For that, I followed this guide by Google.
I already have CloudSDK installed on my PC, and when I type gcloud compute ssh --project $PROJECT_ID ... and all the rest, it throws the following error on my terminal:
ERROR: (gcloud.compute.ssh) Could not fetch resource:
Invalid value ' test-gpu'. Values must match the following regular expression:
'a-z?|[1-9][0-9]{0,19}
Unfortunately, I do not really understand this error message, as my instance-name is called "test-gpu", which was a perfectly valid name when creating the instance.
Any help would be appreciated!
It seems you have a space in the beginning of the name of your instance, please remove and try again.
Able to reproduce the the issue in my Windows machine while trying to access the notebook as per the doc as per below command:
set PROJECT_ID="my-project-id"
set ZONE="my-zone"
set INSTANCE_NAME="my-instance"
gcloud compute ssh --project $PROJECT_ID --zone $ZONE $INSTANCE_NAME -- -L 8080:localhost:8080
ERROR: (gcloud.compute.ssh) Could not fetch resource:
Invalid value 'my-instance'. Values must match the following regular expression: 'a-z?|[1-9][0-9]{0,19}
However, while use the command as like below in windows machine it's work as intended:
$gcloud compute ssh --project my-test-project --zone us-west1-b instance-name -- -L 8080:localhost:8080
At the same time, able to access the instance from the Chromebook using the below mentioned command:
$gcloud compute ssh --project $PROJECT_ID --zone $ZONE $INSTANCE_NAME -- -L 8080:localhost:8080

Unable to SSH/gcloud into default Google Deep Learning VM

I created a new Google Deep Learning VM keeping all the defaults except for asking no GPU:
The VM instance was successfully launched:
But I cannot SSH into it:
Same issue when attempting to use with gcloud (using the command provided when clicking on the instance's arrow down button at the right of SSH):
ssh: connect to host 34.105.108.43 port 22: Connection timed out
ERROR: (gcloud.beta.compute.ssh) [/usr/bin/ssh] exited with return code [255].
Why?
VM instance details:
Turns out that the browser-based SSH client and browser-based gcloud client were disabled by my organization, this is why I couldn't access the VM. The reason I was given is that to allow browser-based SSH, one would have to expose the VMs to the entire web, because Google does not provide a list of the IPs they use for browser-based SSH.
So instead one can SSH into a GCP VM via one's local SSH client by first uploading one's SSH key using the GCP web console. See https://cloud.google.com/compute/docs/instances/connecting-advanced#linux-macos (mirror) for the documentation on how to use one's local SSH client with GCP.
Since the documentation can be a bit tedious to parse, here are the commands I run on my local Ubuntu 18.04 LTS x64 to upload my SSH key and connect to the VM:
If you haven't installed gcloud yet:
# https://cloud.google.com/sdk/docs/install#linux (<- go there to get the latest gcloud URL to download via curl):
sudo apt-get install -y curl
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-310.0.0-linux-x86_64.tar.gz
tar -xvf google-cloud-sdk-310.0.0-linux-x86_64.tar.gz./google-cloud-sdk/install.sh
./google-cloud-sdk/bin/gcloud init
Once gcloud is installed:
# Connect to gcloud
gcloud auth login
# Retrieve one's GCP "username"
gcloud compute os-login describe-profile
# The output will be "name: '[some large number, which is the username]'"
# Create a new SSH key
ssh-keygen -t rsa -f ~/.ssh/gcp001 -C USERNAME
chmod 400 ~/.ssh/gcp001
# if you want to view the public key: nano ~/.ssh/gcp001.pub
gcloud compute os-login ssh-keys add --key-file ~/.ssh/gcp001.pub
gcloud compute ssh --project PROJECT_ID --zone ZONE VM_NAME
# Note that PROJECT_ID can be viewed when running `gcloud auth login`,
# which will output "Your current project has been set to: [PROJECT_ID]".
In order to connect to the VM Instance you will have to follow the guide from GCP and then set up the role with the necessary authorization under IAM & Admin.
Please do:
sudo gcloud compute config-ssh
gcloud auth login
Login to your Gmail account. Accept access of Google Cloud.
Later set project if not yet done:
gcloud config set project YOU-PROJECT-ID
Run gcloud compute ssh with all you need.
If you still have a problem, please remove this:
rm .ssh/google_compute_engine
Run gcloud compute ssh with all you need again and the issue should be solved!

unknown option "-L3000:localhost:3000" while deploying Grafana

I was following the single node file server tutorials on Google cloud platform where it told me to create an SSH tunnel, use the following gcloud command:
gcloud compute ssh --ssh-flag=-L3000:localhost:3000 --project=PROJECT --zone=ZONE INSTANCE_NAME
I enter the command on the gcloud sdk, and then I get this message.
unknown option "-L3000:localhost:3000"
Image of the message.
What did I do wrong? Any help will be appreciated. Thank you.
Try --ssh-flag="-L 3000:localhost:3000", PuTTY expects a space between the -L flag and the port definition.