Google Cloud Platform

From XennisWiki
Jump to: navigation, search

Google Cloud Platform is a cloud computing service by Google that offers hosting on the same supporting infrastructure that Google uses internally for end-user products like Google Search and YouTube. Cloud Platform provides developer products to build a range of programs from simple websites to complex applications. (Wikipedia)

Install

Google Cloud SDK

Download the Google Cloud SKD and extract it (on Arch Linux install the google-cloud-sdk package). Then install and initialize it.

./google-cloud-sdk/install.sh
./google-cloud-sdk/bin/gcloud init

List your projects

gcloud projects list

Configure a project and zone

gcloud config set project <project-id>
gcloud config set compute/zone <zone>

Errors and problems

Not in path and no shell autocompletion

The setup should added the folloing to your shell rc (for Bash just replace "zsh" with "bash")

if [ -f '/opt/google-cloud-sdk/path.zsh.inc' ]; then
  source '/opt/google-cloud-sdk/path.zsh.inc';
fi

# The next line enables shell command completion for gcloud.
if [ -f '/opt/google-cloud-sdk/completion.zsh.inc' ]; then
  source '/opt/google-cloud-sdk/completion.zsh.inc';
fi

Products

App Engine

see App Engine

Container Registry (Docker image storage)

Further information: Container Registry

Define the hostname

gcr_url=gcr.io/<google-project-id>/<example-image>

Push image

(1) Add a tag to your Docker image and (2) push the image to the Container Registry

docker tag <example-image> $gcr_url
gcloud docker -- push $gcr_url

Pull image

Pull a container image by its label

gcloud docker -- pull $gcr_url:<label>

Cloud Container Builder

see Google Cloud Container Builder

Cloud Dataflow

see Google Cloud Dataflow

Cloud Endpoints

see Google Cloud Endpoints

Cloud Key Management Service

see Google Cloud Key Management Service

Cloud Pub/Sub

see Google Cloud Pub/Sub

Cloud Source Repositories

Further information: Cloud Source Repositories

Setup

(1) Initialize credentials, (2) add Google as Git remote and (3) push code to Google repository

gcloud init && git config credential.helper gcloud.sh
git remote add google https://source.developers.google.com/p/<google-project-id>/r/<repository-name>
git push google

Cloud Storage

Crete a bucket

gsutil mb -p ${PROJECT} -c ${STORAGE_CLASS} -l ${BUCKET_LOCATION} gs://${BUCKET_NAME}/

Set a lifecyle for a bucket

Config file lifecycle-config-file.json to delete all files, that are 7 days or older

{
  "lifecycle": {
    "rule": [{
      "action": {"type": "Delete"},
      "condition": {
        "age": 7,
        "isLive": true
      }
    }]
  }
}

Set that configuration to the bucket

gsutil lifecycle set lifecycle-config-file.json gs://${BUCKET_NAME}

Copy directories (rsync)

gsutil rsync -r /path/to/my-dir/ gs://my-bucket/some/dir

Kubernetes Engine

see Google Kubernetes Engine

APIs

List services

gcloud services list --available

List enabled services

gcloud services list --enabled

Enable for instance the Cloud Resource Manager API

gcloud services enable cloudresourcemanager.googleapis.com

Scripts

Create a service account with a role and key

Further information: Container Registry -Advanced Authentication Methods

#!/usr/bin/env bash
set -e

project="my-project"
name="my-user"
displayname="My User"

id="$name@$project.iam.gserviceaccount.com"

# Create a new service account
gcloud iam service-accounts create "$name" \
	--display-name "$displayname"
# Add service account to itself with the role editor
gcloud iam service-accounts add-iam-policy-binding "$id" \
	--member="serviceAccount:$id" \
	--role="roles/editor"
# Create a key for the created service account
gcloud iam service-accounts keys create keyfile.json \
	--iam-account="$id"
# Use the key for docker login
docker login -u _json_key -p "$(cat keyfile.json)" https://gcr.io

Tips

Ping

toolbox ping <host>

See also

Components

External links