Mobile

Handy Google Cloud CLI Commands for Practical Situations

The Google Cloud Command Line Interface (gcloud CLI) is a powerful tool for managing Google Cloud resources. It allows users to interact with Google Cloud services through a terminal, making it essential for developers, DevOps engineers, and system administrators. Here, we’ll explore some handy gcloud commands that can be useful in practical situations, helping you streamline your workflow and manage your cloud resources more effectively.

1. Setting Up and Authenticating

Before using google-cloud-cli commands, you need to set up your environment and authenticate:

bash
gcloud init

This command helps you configure the gcloud storage cli environment by selecting a project and setting up authentication. It guides you through the necessary steps, allowing you to link your Google account and choose the project you want to work with.

2. Managing Projects

Creating and managing choosing the right outsourcing partner projects is a crucial aspect of resource organization. Here are some useful commands:

Create a New Project

bash
gcloud projects create PROJECT_ID --name="My Project"

Replace PROJECT_ID with a unique identifier for your project. This command creates a new project, allowing you to manage resources within it.

List All Projects

bash
gcloud projects list

This command displays all projects associated with your Google account, providing an overview of your resources.

Set Active Project

bash
gcloud config set project PROJECT_ID

Use this command to set a specific project as your active project, ensuring subsequent commands affect the correct resources.

3. Compute Engine Management

Google Compute Engine allows you to create and manage virtual machines. Here are some essential commands:

Create a Virtual Machine

bash
gcloud compute instances create INSTANCE_NAME --zone=ZONE --machine-type=MACHINE_TYPE

Replace INSTANCE_NAME, ZONE, and MACHINE_TYPE with your desired values. This command creates a new VM instance in your specified zone.

List Instances

bash
gcloud compute instances list

This command displays all VM instances in the active project, showing their status and details.

Start and Stop Instances

bash
gcloud compute instances start INSTANCE_NAME --zone=ZONE
gcloud compute instances stop INSTANCE_NAME --zone=ZONE

Use these commands to start or stop a specific VM instance. This is useful for managing costs by shutting down instances when not in use.

4. Networking Commands

Managing network settings is crucial for cloud resources. Here are some common networking commands:

List Firewalls

bash
gcloud compute firewall-rules list

This command displays all firewall rules, allowing you to understand your network security configurations.

Create a Firewall Rule

bash
gcloud compute firewall-rules create RULE_NAME --allow tcp:80 --source-ranges 0.0.0.0/0

This command creates a firewall rule allowing HTTP traffic from all IP addresses. Adjust the rule name and specifications as needed.

5. Cloud Storage Management

Google Cloud Storage (GCS) is a scalable object storage service. Here are some commands to manage GCS buckets:

Create a Bucket

bash
gcloud storage buckets create gs://BUCKET_NAME

Replace BUCKET_NAME with your desired bucket name. This command creates a new storage bucket.

List Buckets

bash
gcloud storage buckets list

This command lists all your storage buckets, helping you manage your data.

Upload Files to a Bucket

bash
gcloud storage cp LOCAL_FILE_PATH gs://BUCKET_NAME/

This command uploads a file from your local system to the specified GCS bucket.

6. IAM and Permissions Management

Managing Identity and Access Management (IAM) roles is essential for security:

List IAM Roles

bash
gcloud iam roles list

This command lists all available IAM roles, helping you understand the permissions you can assign.

Grant a Role to a User

bash
gcloud projects add-iam-policy-binding PROJECT_ID --member='user:EMAIL' --role='ROLE_NAME'

Replace PROJECT_ID, EMAIL, and ROLE_NAME with the appropriate values. This command grants a specified role to a user, allowing them to access resources in the project.

7. Monitoring and Logging

Google Cloud provides tools for monitoring resource usage and logs:

View Logs

bash
gcloud logging read "resource.type=gce_instance AND severity>=ERROR"

This command retrieves logs for Google Compute Engine instances, filtering for error messages. Modify the query as needed to focus on specific resources or severity levels.

Set Up Monitoring Alerts

bash
gcloud alpha monitoring policies create --notification-channels=CHANNEL_ID --alert-strategy=STRATEGY

This command allows you to create alerting policies to monitor specific metrics and send notifications. Replace CHANNEL_ID and STRATEGY with your preferences.

8. Cleanup Resources

Cleaning up unused resources is vital for cost management:

Delete a VM Instance

bash
gcloud compute instances delete INSTANCE_NAME --zone=ZONE

This command deletes a specified VM instance, helping you manage your cloud expenses.

Delete a Bucket

bash
gcloud storage buckets delete gs://BUCKET_NAME

This command removes a specified storage bucket, ensuring you only retain necessary data.

Conclusion

The Google Cloud CLI offers a comprehensive suite of commands for managing resources effectively. By familiarizing yourself with these handy commands, you can streamline your workflow, enhance productivity, and maintain better control over your Google Cloud environment. Whether you’re managing projects, instances, or networking, the gcloud CLI is an indispensable tool for cloud management.

Related Articles

Check Also
Close
Back to top button