Skip to content

Instantly share code, notes, and snippets.

@dimitardanailov
Last active June 23, 2021 20:14
Show Gist options
  • Save dimitardanailov/8418369b5ddcaf89672ed7bf5cb01728 to your computer and use it in GitHub Desktop.
Save dimitardanailov/8418369b5ddcaf89672ed7bf5cb01728 to your computer and use it in GitHub Desktop.
Tips and tricks related with Google Compute engine

Creating a Persistent Disk

Create a new instance
gcloud compute instances create gcelab --zone us-central1-c
Create a new persistent disk
gcloud compute disks create mydisk --size=200GB \
--zone us-central1-c
Attaching a disk
gcloud compute instances attach-disk gcelab --disk mydisk --zone us-central1-c
Finding the persistent disk in the virtual machine
gcloud compute ssh gcelab --zone us-central1-c
Formatting and mounting the persistent disk
# Step 1
sudo mkdir /mnt/mydisk

# Step 2
sudo mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/scsi-0Google_PersistentDisk_persistent-disk-1

# Step 3
sudo mount -o discard,defaults /dev/disk/by-id/scsi-0Google_PersistentDisk_persistent-disk-1 /mnt/mydisk

Docker

Create a Docker container image
docker build -t gcr.io/PROJECT_ID/hello-node:v1 .
docker run -d -p 8080:8080 gcr.io/PROJECT_ID/hello-node:v1
gcloud docker -- push gcr.io/PROJECT_ID/hello-node:v1

Kubernates

Create your cluster
gcloud config set project PROJECT_ID
gcloud container clusters create hello-world \
                --num-nodes 2 \
                --machine-type n1-standard-1 \
                --zone us-central1-a
Create your pod
kubectl run hello-node \
    --image=gcr.io/PROJECT_ID/hello-node:v1 \
    --port=8080
# To view the deployment, run:
kubectl get deployments
# To view the pod created by the deployment, run:
kubectl get pods
Kubernates basic commands
kubectl cluster-info
kubectl config view
kubectl get events
kubectl logs <pod-name>
Allow external traffic
kubectl expose deployment hello-node --type="LoadBalancer"
# To find the publicly-accessible IP address of the service, request kubectl to list all the cluster services:
kubectl get services
Scale up your service
kubectl scale deployment hello-node --replicas=4
Roll out an upgrade to your service
kubectl edit deployment hello-node

Storage

In Cloud Shell, run the following to move file yob2014.txt into your bucket. Replace <your_bucket> with the name of the bucket you just created:

gsutil cp yob2014.txt gs://<your_bucket>

Big Query

Use the bq ls command to see whether your default project has any existing datasets.

# Create a new dataset
bq mk <data-set-name>
# Create table from txt file
bq load babynames.names2010 yob2010.txt name:string,gender:string,count:integer

BigTable

Configuration
echo project = [PROJECT_ID] > ~/.cbtrc
echo instance = quickstart-instance >> ~/.cbtrc
Read and Write Data
# Create a table named my-table
cbt createtable my-table
# Add one column family named cf1
cbt createfamily my-table cf1
# List your column families
cbt ls my-table
# Put the value test-value in the row r1, using the column family cf1 and the column qualifier c1:
cbt set my-table r1 cf1:c1=test-value
# Use the cbt read command to read the data you added to the table:
cbt read my-table
# Delete the table my-table:
cbt deletetable my-table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment