github项目介绍: Universal Infrastructure as Code. Your Cloud, Your Language, Your Way
通用基础设施即代码,你云你素,用熟悉的方式、熟悉的语言通过pulumi去管理自己的云平台。
通俗点来说,pulumi是一个基础设施资源编排器,写好编排代码,例如定义的yaml、或者编程语言golang等,通过动作触发即能完成各种云资源的增删改查等基础设施的管理,使用pulumi可以轻松构建自己的多云平台。较于已有的编排器,pulumi有如下几个明显的特点。
pulumi
Node.js
Python
Go
.NET
Java
YAML
这张图说明了 project program resource stack的关系,所有的资源都属于project,stack既可以认为是单个资源的逻辑表示,也能当作环境,program处理实际资源。
要使用pulumi有如下几步,以pulumi cli 操作腾讯云为例:
pulumi new
pulumi up
或者 auto.NewStackInlineSource->stack.Up
首先初始化项目
# 创建文件夹
mkdir pulumi-qcloud
cd pulumi-qcloud
# 初始化项目,需要登陆pulumi,选择go模板即可
pulumi new
编写program代码
package main
import (
"fmt"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/tencentcloudstack/pulumi-tencentcloud/sdk/go/tencentcloud/cos"
"github.com/tencentcloudstack/pulumi-tencentcloud/sdk/go/tencentcloud/user"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := user.GetInfo(ctx, nil, nil)
if err != nil {
fmt.Printf("the first error: %v\n", err)
return err
}
_, err = cos.NewBucket(ctx, "myBucket", &cos.BucketArgs{
Acl: pulumi.String("private"),
Bucket: pulumi.String(fmt.Sprintf("%v%v", "pulumi-created-", info.AppId)),
})
if err != nil {
fmt.Printf("the second error: %v\n", err)
return err
}
return nil
})
}
运行
pulumi up
automation api是对pulumi cli的封装,通过automation api调用更简单: sdk操作比调用二进制方便,更安全: 不用直接与文件系统文件进行直接访问。
pulumi 不经可以通过 rest api方式、cli方式提供使用,还能继承带ci/cd系统中,方便CD。
使用rest api方式提供服务见 pulumi_over_http
腾讯云 rest api示例 multistack-example
其中可私有化不能满足,因为操作记录都会存储在pulumi云端,其他均满足,如果忽略这点,pulumi可称为完美的多云管理利器
对比最多的为terraform
这里仅列举了几个在选型中起决定性因素的差异点,详细的对比参照 Pulumi vs. Terraform
功能 | pulumi | Terraform |
---|---|---|
语言支持 | Python, TypeScript, JavaScript, Go, C#, F#, Java, YAML | HashiCorp Configuration Language (HCL) |
IDE支持 | 代码补齐、强类型、错误提示、丰富的资源文档 | 受限 |
状态管理 | 可视化状态管理 | 功能弱、可视化支持差 |
嵌入业务代码 | 可嵌入 | 不可嵌入 |
secrets管理 | 有 | 无 |
最大的区别在于语言支持、状态管理、可嵌入业务代码中三点
❯ pulumi new --force
Please choose a template (113/205 shown):
go A minimal Go Pulumi program
This command will walk you through creating a new Pulumi project.
Enter a value or leave blank to accept the (default), and press <ENTER>.
Press ^C at any time to quit.
project name: (pulumitest)
project description: (A minimal Go Pulumi program)
Created project 'pulumitest'
Please enter your desired stack name.
To create a stack in an organization, use the format <org-name>/<stack-name> (e.g. `acmecorp/dev`).
stack name: (dev)
Created stack 'dev'
Installing dependencies...
go: downloading github.com/pulumi/pulumi/sdk/v3 v3.53.0
go: finding module for package github.com/mattn/go-isatty
go: found github.com/mattn/go-isatty in github.com/mattn/go-isatty v0.0.17
Finished installing dependencies
Your new project is ready to go! ✨
To perform an initial deployment, run `pulumi up`
package main
import (
"fmt"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/tencentcloudstack/pulumi-tencentcloud/sdk/go/tencentcloud/cos"
"github.com/tencentcloudstack/pulumi-tencentcloud/sdk/go/tencentcloud/user"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := user.GetInfo(ctx, nil, nil)
if err != nil {
fmt.Printf("the first error: %v\n", err)
return err
}
_, err = cos.NewBucket(ctx, "myBucket", &cos.BucketArgs{
Acl: pulumi.String("private"),
Bucket: pulumi.String(fmt.Sprintf("%v%v", "pulumi-created-", info.AppId)),
})
if err != nil {
fmt.Printf("the second error: %v\n", err)
return err
}
return nil
})
}
❯ pulumi config set tencentcloud:secretId xxxxx --secret
❯ pulumi config set tencentcloud:secretKey xxxxxxx --secret
❯ pulumi config set tencentcloud:region ap-hongkong
❯ go mod tidy
go: finding module for package github.com/tencentcloudstack/pulumi-tencentcloud/sdk/go/tencentcloud/cos
go: finding module for package github.com/tencentcloudstack/pulumi-tencentcloud/sdk/go/tencentcloud/user
go: found github.com/tencentcloudstack/pulumi-tencentcloud/sdk/go/tencentcloud/cos in github.com/tencentcloudstack/pulumi-tencentcloud/sdk v0.1.2
go: found github.com/tencentcloudstack/pulumi-tencentcloud/sdk/go/tencentcloud/user in github.com/tencentcloudstack/pulumi-tencentcloud/sdk v0.1.2
可以提前安装plugin,否则运行pulumi up的时候会安装,命令为
pulumi plugin install resource tencentcloud v0.1.2
, 不的话很慢
❯ pulumi up
Previewing update (dev)
View Live: https://app.pulumi.com/elrondwong/pulumitest/dev/previews/f129cbae-67d8-42aa-b78a-c082e598a08f
# 安装plugin
Downloading plugin: 21.15 MiB / 43.45 MiB [==========>-----------] 48.67% 18m8s
Downloading plugin: 43.45 MiB / 43.45 MiB [=====================] 100.00% 37m32s
[resource plugin tencentcloud-0.1.2] installing
Type Name Plan
pulumi:pulumi:Stack pulumitest-dev
+ └─ tencentcloud:Cos:Bucket myBucket create
Resources:
+ 1 to create
1 unchanged
Do you want to perform this update? yes
Updating (dev)
View Live: https://app.pulumi.com/elrondwong/pulumitest/dev/updates/2
Type Name Status
pulumi:pulumi:Stack pulumitest-dev
+ └─ tencentcloud:Cos:Bucket myBucket created (4s)
Resources:
+ 1 created
1 unchanged
Duration: 11s
这里仅展示了资源创建,对资源的销毁可以看下面OpenStack的例子
pulumi new --dir pulumiopenstack
Please choose a template (113/205 shown):
openstack-go A minimal OpenStack Go Pulumi program
This command will walk you through creating a new Pulumi project.
Enter a value or leave blank to accept the (default), and press <ENTER>.
Press ^C at any time to quit.
project name: (pulumiopenstack)
project description: (A minimal OpenStack Go Pulumi program)
Created project 'pulumiopenstack'
Please enter your desired stack name.
To create a stack in an organization, use the format <org-name>/<stack-name> (e.g. `acmecorp/dev`).
stack name: (dev)
Created stack 'dev'
Installing dependencies...
Finished installing dependencies
Your new project is ready to go! ✨
To perform an initial deployment, run 'cd pulumiopenstack', then, run `pulumi up`
package main
import (
"fmt"
"github.com/pulumi/pulumi-openstack/sdk/v3/go/openstack/blockstorage"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := blockstorage.NewVolume(ctx, "volume1", &blockstorage.VolumeArgs{
Description: pulumi.String("first test volume"),
Region: pulumi.String("RegionOne"),
Size: pulumi.Int(3),
})
if err != nil {
return err
}
return nil
})
}
pulumi plugin install resource openstack v3.9.0
go mod tidy
export OS_USERNAME=admin
export OS_PASSWORD=passwd
export OS_PROJECT_NAME=admin
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_DOMAIN_NAME=Default
export OS_AUTH_URL=http://172.16.10.10:5000/v3
export OS_IDENTITY_API_VERSION=3
export PS1='[\u@\h \W(keystone_admin)]\$ '
❯ pulumi up
Previewing update (dev)
View Live: https://app.pulumi.com/elrondwong/pulumiopenstack/dev/previews/cc39ba12-d292-43ad-b962-a9d5d66653a5
Downloading plugin openstack v3.9.0: 17.95 MiB / 17.95 MiB [=====] 100.00% 7m24s
Type Name Plan
+ pulumi:pulumi:Stack pulumiopenstack-dev create
+ └─ openstack:blockstorage:Volume volume1 create
Resources:
+ 2 to create
Do you want to perform this update? yes
Updating (dev)
View Live: https://app.pulumi.com/elrondwong/pulumiopenstack/dev/updates/1
Type Name Status
+ pulumi:pulumi:Stack pulumiopenstack-dev created (0.62s)
+ └─ openstack:blockstorage:Volume volume1 created (11s)
Resources:
+ 2 created
Duration: 15s
❯ pulumi destroy
Previewing destroy (dev)
View Live: https://app.pulumi.com/elrondwong/pulumiopenstack/dev/previews/4673ac44-90a7-438a-8166-01e7cdf1f89d
Type Name Plan
- pulumi:pulumi:Stack pulumiopenstack-dev delete
- └─ openstack:blockstorage:Volume volume1 delete
Resources:
- 2 to delete
Do you want to perform this destroy? yes
Destroying (dev)
View Live: https://app.pulumi.com/elrondwong/pulumiopenstack/dev/updates/2
Type Name Status
- pulumi:pulumi:Stack pulumiopenstack-dev deleted
- └─ openstack:blockstorage:Volume volume1 deleted (11s)
Resources:
- 2 deleted
Duration: 13s
The resources in the stack have been deleted, but the history and configuration associated with the stack are still maintained.
If you want to remove the stack completely, run `pulumi stack rm dev`.
❯ pulumi stack rm dev
This will permanently remove the 'dev' stack!
Please confirm that this is what you'd like to do by typing `dev`: dev
Stack 'dev' has been removed!
# https://app.pulumi.com/elrondwong/projects
stack里面少了一个