3

初始化一个GCP项目并用gcloud访问操作 - 南瓜慢说

 1 year ago
source link: https://www.cnblogs.com/larrydpk/p/17048556.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

谷歌云GCP(Google Cloud Platform)是由Google提供的云平台,还是为用户提供了许多免费的产品,还是可以尝试一下的。对于学习或者小项目,都可以使用。

2 创建一个新项目

要使用GCP,我们需要创建一个项目,它所有的资源都是在项目之下管理的:

946674-20230113092343590-1069840746.png

3 创建Service Account

在实际开发中,我们不能使用自己的账号在做操作,最好的方式是创建一个服务账号(Service Account),这应该也是所有云平台都推荐的方式。创建位置如下:

946674-20230113092343909-869949378.png

输入账号名字:

946674-20230113092344183-482089943.png

选择角色,为了方便,我直接选择Owner,会拥有所有权限,但实际应用肯定不能这样,要做好隔离:

946674-20230113092344552-630787413.png

4 创建密钥文件

对于Service Account,不是通过用户名密码来授权的,而是通过密钥文件,创建如下:

946674-20230113092344807-1370695444.png

选择新建一个密钥,并格式为json。创建后,会自动下载key文件。

5 设置gcloud SDK

Key文件拿到后,我们可以设置环境变量:GOOGLE_APPLICATION_CREDENTIALS

$ export GOOGLE_APPLICATION_CREDENTIALS=/Users/larry/Software/google-cloud-sdk/pkslow-admin-for-all.json

激活Service Account:

$ gcloud auth activate-service-account [email protected] --key-file=${GOOGLE_APPLICATION_CREDENTIALS}

设置SDK的项目ID:

$ gcloud config set project pkslow

检查一下设置是否正确:

$ gcloud auth list
               Credentialed Accounts
ACTIVE  ACCOUNT
*       [email protected]

To set the active account, run:
    $ gcloud config set account `ACCOUNT`


$ gcloud config list
[core]
account = [email protected]
disable_usage_reporting = True
project = pkslow

Your active configuration is: [default]

6 使用gcloud创建Pub/Sub

SDK设置好后,就可以使用了,我们使用它来创建Pub/Sub试试。创建主题和订阅:

$ gcloud pubsub topics create pkslow-test
Created topic [projects/pkslow/topics/pkslow-test].

$ gcloud pubsub subscriptions create pkslow-sub --topic=pkslow-test
Created subscription [projects/pkslow/subscriptions/pkslow-sub].

检查是否创建成功:

$ gcloud pubsub topics list
---
name: projects/pkslow/topics/pkslow-test


$ gcloud pubsub subscriptions list
---
ackDeadlineSeconds: 10
expirationPolicy:
  ttl: 2678400s
messageRetentionDuration: 604800s
name: projects/pkslow/subscriptions/pkslow-sub
pushConfig: {}
topic: projects/pkslow/topics/pkslow-test

在浏览器查看,发现已经成功创建了:

946674-20230113092345070-731219349.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK