Google Cloud Platform(GCP) 學(xué)習(xí)筆記

GCP 基礎(chǔ)知識

GCP 于 AWS 很相似歧蒋,通過不同云平臺的一些概念對比宝冕,快速了解GCP搁骑。

Also read the services mapping table between different cloud providers here.

GCP AWS Azure
Compute Compute Engine EC2 Azure Virtual Machines
Block storage Persistent Disk Amazon Elastic Block Store (EBS) Azure Managed Disks
File storage Filestore Amazon Elastic File System (EFS) Azure Disk Storage, Azure Files
Object storage Cloud Storage AWS Simple Storage Service (S3) Azure Blob Storage
Containers Google Kubernetes Engine Amazon Elastic Kubernetes Service (EKS), Amazon Elastic Container Service (ECS) Azure Kubernetes Service (AKS)
Containers Artifact Registry Amazon Elastic Container Registry (ECR) Azure Container Registry
No SQL Cloud Bigtable Amazon DynamoDB Azure Cosmos DB
Command-line interface (CLI) Cloud SDK AWS CLI Azure CLI
networking Virtual Private Cloud (VPC) Amazon Virtual Private Cloud (VPC) Azure Virtual Network
networking Cloud NAT 沒有 internet GW Amazon VPC NAT instances Azure NAT
Monitoring Cloud Monitoring Amazon CloudWatch Azure Monitor
IAM Cloud Identity AWS Identity Services Azure Active Directory
IAM Identity and Access Management Amazon Identity and Access Management Azure Identity Management
API Service account service principal
project subscription

Install Cloud SDK

official method

用官方的方法安裝CLI 出了問題让网,我使用下面的方式
Below steps are verified on WSL ubuntu 16.04

$ export https_proxy=xxx
$ curl https://sdk.cloud.google.com | bash

update path for gcloud tool and then set browser on Windows for popup browser.

$ export BROWSER="/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"

Cloud SDK 基礎(chǔ)使用

gcloud cheatsheet

Verify basic usage.

gcloud init --console-only

gcloud auth login

gcloud projects list

gcloud config set project PROJECT_ID

gcloud compute networks create NETWORK \
    --subnet-mode=custom \
    [ --enable-ula-internal-ipv6 [ --internal-ipv6-range=ULA_IPV6_RANGE ]] \
    --bgp-routing-mode=DYNAMIC_ROUTING_MODE \
    --mtu=MTU

gcloud compute networks subnets create SUBNET \
    --network=NETWORK \
    --range=PRIMARY_RANGE \
    --region=REGION
   
gcloud compute instances create VM_NAME \
    --network=NETWORK_NAME \
    --subnet=SUBNET_NAME \
    --zone=ZONE

Create VM via gcloud cli

相關(guān)的組件必須一個一個創(chuàng)建蛤签,不如 Azure CLI 使用那么方便辞友。

create network via gcloud command

Create VPC

$ gcloud compute networks create roy-vpc \
    --subnet-mode=custom \
    --bgp-routing-mode=global
Created [https://www.googleapis.com/compute/v1/projects/xxx/global/networks/roy-vpc].
NAME     SUBNET_MODE  BGP_ROUTING_MODE  IPV4_RANGE  GATEWAY_IPV4
roy-vpc  CUSTOM       GLOBAL

Instances on this network will not be reachable until firewall rules
are created. As an example, you can allow all internal traffic between
instances as well as SSH, RDP, and ICMP by running:

    $ gcloud compute firewall-rules create <FIREWALL_NAME> --network roy-vpc --allow tcp,udp,icmp --source-ranges <IP_RANGE>
    $ gcloud compute firewall-rules create <FIREWALL_NAME> --network roy-vpc --allow tcp:22,tcp:3389,icmp
    
$ gcloud compute firewall-rules create allow-ssh --network roy-vpc --allow tcp:22,tcp:3389,icmp
Creating firewall...?Created [https://www.googleapis.com/compute/v1/projects/xxx/global/firewalls/allow-ssh].
Creating firewall...done.
NAME       NETWORK  DIRECTION  PRIORITY  ALLOW                 DENY  DISABLED
allow-ssh  roy-vpc  INGRESS    1000      tcp:22,tcp:3389,icmp        False

$ gcloud compute networks subnets create roy-pub-subnet \
    --network=roy-vpc \
    --range=10.2.2.0/24 \
    --region=us-west1
Created [https://www.googleapis.com/compute/v1/projects/xxx/regions/us-west1/subnetworks/roy-pub-subnet].
NAME            REGION    NETWORK  RANGE        STACK_TYPE  IPV6_ACCESS_TYPE  INTERNAL_IPV6_PREFIX  EXTERNAL_IPV6_PREFIX
roy-pub-subnet  us-west1  roy-vpc  10.2.2.0/24  IPV4_ONLY

Create VM

$ gcloud compute instances create roy-cli-vm \
    --network=roy-vpc \
    --subnet=roy-pub-subnet \
    --zone=us-west1-a
Created [https://www.googleapis.com/compute/v1/projects/xxx/zones/us-west1-a/instances/roy-cli-vm].
NAME        ZONE        MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP   STATUS
roy-cli-vm  us-west1-a  n1-standard-1               10.2.2.2     34.145.101.0  RUNNING

Connect to the new VM

It will generate new ssh key -> update instance ssh metadata -> ssh connect

gcloud compute ssh --project=PROJECT_ID --zone=ZONE VM_NAME

e.g.

Delete VM

$ gcloud compute instances delete roy-cli-vm --zone=us-west1-a
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are
attached to any other instances or the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk is
irreversible and any data on the disk will be lost.
 - [roy-cli-vm] in [us-west1-a]

Do you want to continue (Y/n)?

Deleted [https://www.googleapis.com/compute/v1/projects/xxx/zones/us-west1-a/instances/roy-cli-vm].

Delete VPC

should delete firewall, subnet, vpc one by one.

$ gcloud compute networks subnets delete roy-pub-subnet --region=us-west1

$ gcloud compute firewall-rules delete allow-ssh

$ gcloud compute networks delete roy-vpc

通過 Terraform 創(chuàng)建VM

official doc

Read the google provider Terraform documentation.

Enable ADCs:

gcloud auth application-default login

Try terraform deploy.

Get example from github https://github.com/act-labs/gcp-terraform.git

$ cd gcp-terraform
$ vi variables.tf #for prefer setting
$ cp ~/.ssh/id_rsa.pub ./ssh-key.pub
$ vi main.tf   # for ssh key

setting for ssh key in VM.

resource "google_compute_instance" "webservers" {
...

  metadata = {
    ssh-keys = "${var.ssh_user}:${file(var.ssh_pub_key_file)}"
  }

verify

$ terraform init .
$ terraform apply --auto-approve

Create VM from Web Console

login https://console.cloud.google.com/

create VPC -> create VM

參考 https://cloud.google.com/compute/docs/instances/create-start-instance

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市震肮,隨后出現(xiàn)的幾起案子称龙,更是在濱河造成了極大的恐慌,老刑警劉巖戳晌,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鲫尊,死亡現(xiàn)場離奇詭異,居然都是意外死亡沦偎,警方通過查閱死者的電腦和手機(jī)疫向,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來豪嚎,“玉大人搔驼,你說我怎么就攤上這事〕扪” “怎么了舌涨?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長扔字。 經(jīng)常有香客問我囊嘉,道長温技,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任扭粱,我火速辦了婚禮舵鳞,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘琢蛤。我一直安慰自己蜓堕,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布虐块。 她就那樣靜靜地躺著俩滥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪贺奠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天错忱,我揣著相機(jī)與錄音儡率,去河邊找鬼。 笑死以清,一個胖子當(dāng)著我的面吹牛儿普,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播掷倔,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼眉孩,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了勒葱?” 一聲冷哼從身側(cè)響起浪汪,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎凛虽,沒想到半個月后死遭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡凯旋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年呀潭,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片至非。...
    茶點(diǎn)故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡钠署,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出荒椭,到底是詐尸還是另有隱情谐鼎,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布戳杀,位于F島的核電站该面,受9級特大地震影響夭苗,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜隔缀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一题造、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧猾瘸,春花似錦界赔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至揽思,卻和暖如春袜腥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背钉汗。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工羹令, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人损痰。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓福侈,卻偏偏與公主長得像,于是被迫代替她去往敵國和親卢未。 傳聞我的和親對象是個殘疾皇子肪凛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容