看到Terraform能够替代kubectl管理k8s资源的生命周期,因而调研了下它的使用场景,并对比Terraform和Helm的区别html
Terraform是一款开源工具,出自HashiCorp公司,著名的Vagrant、Consul也出自于该公司。其主要做用是:让用户更轻松地管理、配置任何基础架构,管理公有和私有云服务,也能够管理外部服务,如GitHub,Nomad。nginx
区别于ansible和puppet等传统的配置管理工具,Terraform趋向于更上层的一个组装者。git
Terraform使用模板来定义基础设施,经过指令来实现资源建立/更新/销毁的全生命周期管理,实现“基础设施即代码”,具体示例以下:github
resource "alicloud_instance" "web" { # cn-beijing availability_zone = "cn-beijing-b" image_id = "ubuntu_140405_32_40G_cloudinit_20161115.vhd" system_disk_category = "cloud_ssd" instance_type = "ecs.n1.small" internet_charge_type = "PayByBandwidth" security_groups = ["${alicloud_security_group.tf_test_foo.id}"] instance_name = "test_foo" io_optimized = "optimized" }
这是阿里云的一个Terraform逻辑,执行terraform apply,就能够建立一个ECS实例web
Terraform AliCloud provider: terraform-providerubuntu
17年9月,Terraform官方宣布支持Kubernetes,提供Kubernetes应用程序的完整生命周期管理,包含Pod的建立、删除以及副本控制等功能(经过调用API)。api
如下是操做示例:架构
当前k8s的installer列表,已经不少了...app
使用Terraform在阿里云上安装k8s集群:kubernetes-exampleside
由于是调用apiserver,因此须要指定k8s集群的链接方式 provider "kubernetes" {} // 默认~/.kube/config 或: provider "kubernetes" { host = "https://104.196.242.174" client_certificate = "${file("~/.kube/client-cert.pem")}" client_key = "${file("~/.kube/client-key.pem")}" cluster_ca_certificate = "${file("~/.kube/cluster-ca-cert.pem")}" }
$ terraform init Initializing provider plugins... - Downloading plugin for provider "kubernetes"... Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
// Terraform不支持Deployment // issue:https://github.com/terraform-providers/terraform-provider-kubernetes/issues/3 resource "kubernetes_replication_controller" "nginx" { metadata { name = "scalable-nginx-example" labels { App = "ScalableNginxExample" } } spec { replicas = 2 selector { App = "ScalableNginxExample" } template { container { image = "nginx:1.7.8" name = "example" port { container_port = 80 } resources { limits { cpu = "0.5" memory = "512Mi" } requests { cpu = "250m" memory = "50Mi" } } } } } }
resource "kubernetes_service" "nginx" { metadata { name = "nginx-example" } spec { selector { App = "${kubernetes_replication_controller.nginx.metadata.0.labels.App}" } port { port = 80 target_port = 80 } type = "LoadBalancer" } }
以上的步骤均为执行计划的定义 执行操做:terraform apply 查看当前执行几乎:terraform plan
若是是对K8S作上层的资源管理,大多数人会想到用Helm:参考
如下是Helm与Terraform都建立一个APP的操做对比:https://dzone.com/articles/te...
Terraform的优点:
Terraform的缺点:
对于Terraform,不支持Deployment这一条,足以让不少人放弃这个方案,而issue中对于这个的讨论,也有点不太乐观
必须在v1中的资源才会支持。对于Deployment你们只能用RC代替、或者kube exec加进去(尬
但对于kubernetes而言,beta阶段的不少资源,已经被你们普遍使用(Deployment、Daemonset),并且新版本的Deployment已经变成了apps/v1。
k8s各类版本(v一、apps/v1)的区别:参考文章
不知道后续Terraform有没有更多的支持,观望下~