Google Cloud SDK
gcloud CLI
To learn more, visit:
The official documentation for the Google Cloud SDK
I'd also suggest you generate a local copy of the documentation offline.
Getting Started
Install the Google Cloud SDK
curl https://sdk.cloud.google.com | bashUpdate the components
gcloud components update
Projects
Create a new project PROJECT_ID, setting it as the default for
gcloud:gcloud projects create PROJECT_ID \ [--name PROJECT_NAME] \ --set-as-defaultSet your default project to existing project PROJECT_ID
gcloud config set project PROJECT_IDDelete the project
gcloud projects delete 'my-project'Undelete a project
gcloud projects undelete 'my-project'Set
exampleas the default projectgcloud projects set project 'example'
Authenticating
If you don't have an existing account, create one
gcloud initSet an existing account to be the current active account
gcloud config set account 'ttrojan@usc.edu'Generate credentials for the client libraries (such as Python)
gcloud auth application-default loginGenerate credentials for the
bqcommand-line interfacegcloud auth login --no-launch-browserList the name of the active account
gcloud auth list --filter=status:ACTIVE --format="value(account)"List the name of all inactive accounts starting with
tmpgcloud auth list --filter="-status:ACTIVE account:test*" \ --format="value(account)"
Cloud DNS
Describe the project information for
project_idgcloud dns project-info describe 'project_id'Create a managed zone
gcloud dns managed-zones create --dns-name='helpful.wiki' --description='Helpful Wiki managed zone' 'helpfulwiki'Enable DNSSEC
gcloud dns managed-zones update 'helpfulwiki' --dnssec-state onAdd an
Aname records to your domain# Preset the variables project="project-id" zone="zone-id" domain="website.com" # Begin a transaction gcloud dns --project=${project} \ record-sets transaction \ start \ --zone=${zone} # Add the IPs to a new A name record with a 1-hour TTL gcloud dns --project=${project} \ record-sets transaction \ add '1.2.3.4' '2.3.4.5' --name="${domain}." --ttl=3600 --type='A' --zone=${zone} # Execute the transation gcloud dns --project=${project} \ record-sets transaction \ execute \ --zone=${zone}
SSL
Create a certificate for
helpful.wiki, and name itcertificategcloud compute ssl-certificates create 'certificate' --domains='helpful.wiki' --global
Cloud SQL
Google's article Creating and managing MySQL databases covers this material pretty well, I've included some commands below
Note, the --async flag returns immediately, without waiting for the operation to complete
instance_name='my_instance' tier='db-f1-micro' region='us-west1' version='MYSQL_8_0' gcloud sql instances create ${instance_name} --tier=${tier} region=${region} --database-version=${version}instance_name='my_instance' gcloud sql instances delete ${instance_name} --asyncinstance_name='my_instance' database_name='my_database' gcloud sql databases create ${database_name} --instance=${instance_name} --asyncinstance_name='my_instance' database_name='my_database' gcloud sql databases delete ${database_name} --instance=${instance_name} --asyncbucket='gs://helpfulwiki' instance_name='my_instance' database_name='my_database' gcloud sql export sql ${instance_name} "${bucket}/sqldata.gz" --database=${database}- Be sure to follow best practices for importing and exporting
I'd also take a look at Google's article about creating and managing MySQL users. If you see references to % as the host, it denotes an unrestricted host name. They wrote a good post on MySQL users as well.
Assigning a password to the root user
gcloud sql users set-password 'root' --password='root' --instance='my_instance' --host='%'Changing the password of a regular user
gcloud sql users set-password 'username' --instance='my_instance' --host='1.2.3.4' --prompt-for-passwordEnabling access via a public IP
gcloud sql instances patch 'my_instance' --assign-ipConfiguring which public IPs can access the instance
gcloud sql instances patch 'my_instance' --authorized-networks='1.2.3.4, 5.6.7.8'Creating a user
gcloud sql users create 'username' --host='1.2.3.4' --instance='my_instance' --prompt-for-passwordListing existing users
gcloud sql users list --instance='my_instance'Deleting a user
gcloud sql users delete atraver --host='1.2.3.4' --instance='my_instance'Configure a user's access
- Google's article about MySQL users
Check out Google's article about connecting with MySQL Workbench, as well as their article about exporting data to a SQL dump file in Cloud Storage
Export contents of database
mydatabaseCloud SQL instancemyinstanceto Cloud Storage bucketgs://mybucketFirst, enable WRITE access for the Cloud SQL instance's service account:
gcloud sql instances describe 'myinstance' | grep 'serviceAccountEmailAddress' # => p11111-hp11gs@gcp-sa-cloud-sql.iam.gserviceaccount.com service_account='p11111-hp11gs@gcp-sa-cloud-sql.iam.gserviceaccount.com' gsutil acl ch -u "${service_account}:W" gs://mybucketNext, export the data
gcloud sql export sql 'myinstance' gs://mybucket/sqldump.gz --database='mydatabase'
Google wrote a great article about starting, stopping, and restarting instances
Starting an instance
gcloud sql instances patch 'myinstance' --activation-policy 'ALWAYS'Stopping an instance
gcloud sql instances patch 'myinstance' --activation-policy 'NEVER'Restarting an instance
gcloud sql instances restart 'myinstance'Opening up port 445 (SMB protocol) to all addresses
gcloud compute firewall-rules create RULE_NAME \ --priority 1000 \ --allow 'tcp:445' \ --source-ranges '0.0.0.0/0'
Cloud VPN
I'm still exploring this topic, but I found Stephanie Wong's video tutorials to be helpful in gaining more solid footing on cloud networking fundamentals.