背景:
Terraform体验了腾讯云的CVM的基本操作。正常流程要去体验一下其他基础组件的整合比如数据库redis等基础组件,还有现有资源的导入。配置文件的合理配置等等等....先插播一下kubernetes的体验吧......毕竟我主要的工作环境是Kubernetes!也有tke。其实也可以体验下tke的整合?现在先单独体验下原生的kubernetes吧!
扫了一眼官方文档https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs。第一眼下去我就找到了我的关注点: Guides(指南,应该包括如何连接kubernetes集群),几个常用的api: apps/v1,core/v1,networking/v1,rbac/v1!
看一眼Guides:
首先是插件版本的指定(截至当前:最新文档是2.9.0,但是没有加版本号下载的版本是2.10.0)
参照官方文档:https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/guides/getting-started#provider-setup,看一眼连接terraform kubernetes的两种方式:
看个人喜好了。我是选择了第一种连接方式!
[root@k8s-master-01 ~]# mkdir terraform-k8s
[root@k8s-master-01 ~]# cd terraform-k8s/
root@k8s-master-01 terraform-k8s# cat provider.tf
terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.10.0"
}
}
}
provider "kubernetes" {
config_path = "~/.kube/config"
config_context = "kubernetes-admin@kubernetes"
}
resource "kubernetes_namespace" "zhangpeng" {
metadata {
name = "zhangpeng"
}
}
root@k8s-master-01 terraform-k8s]# terraform init
注意: 官方文档貌似现在最新是2.9.0,但是开始我没有加版本好显示有2.10.0就直接写2.10.0了
[root@k8s-master-01 terraform-k8s]# terraform plan
[root@k8s-master-01 terraform-k8s]# kubectl get ns
看了一眼官方文档有点忧伤https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment_v1#import。deployment有两个相关文档deployment and deployment_v1。目测了一眼只有resource下的区别。现在deployment应该都是v1吧?就用个v1的配置文件吧!
参照官方文档。只增加了namespace的限定!
cat nginx.tf
resource "kubernetes_deployment_v1" "example" {
metadata {
name = "terraform-example"
namespace = "zhangpeng"
labels = {
test = "MyExampleApp"
}
}
spec {
replicas = 3
selector {
match_labels = {
test = "MyExampleApp"
}
}
template {
metadata {
labels = {
test = "MyExampleApp"
}
}
spec {
container {
image = "nginx:1.21.6"
name = "example"
resources {
limits = {
cpu = "0.5"
memory = "512Mi"
}
requests = {
cpu = "250m"
memory = "50Mi"
}
}
liveness_probe {
http_get {
path = "/"
port = 80
http_header {
name = "X-Custom-Header"
value = "Awesome"
}
}
initial_delay_seconds = 3
period_seconds = 3
}
}
}
}
}
}
[root@k8s-master-01 terraform-k8s]# terraform plan
[root@k8s-master-01 terraform-k8s]# terraform apply
enter a value 输入yes!
[root@k8s-master-01 terraform-k8s]# kubectl get pods -n zhangpeng
恩这一步等了好久 差不多两分钟具体原因查看livenessProbe initialDelaySeconds periodSeconds相关设置!
[root@k8s-master-01 terraform-k8s]# kubectl get all -n zhangpeng
NAME READY STATUS RESTARTS AGE
pod/terraform-example-78ff4f86d7-bxfwj 1/1 Running 0 6m26s
pod/terraform-example-78ff4f86d7-vb2p7 1/1 Running 0 6m26s
pod/terraform-example-78ff4f86d7-vqm6b 1/1 Running 0 6m26s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/terraform-example 3/3 3 3 6m26s
NAME DESIRED CURRENT READY AGE
replicaset.apps/terraform-example-78ff4f86d7 3 3 3 6m26s
service相关文档应该是在core/v1吧?查看一下,搜索栏还是很有用的
cat service.tf
resource "kubernetes_service" "terraform-example-service" {
metadata {
name = "terraform-example-service"
namespace = kubernetes_namespace.zhangpeng.metadata.0.name
}
spec {
selector = {
test = kubernetes_deployment_v1.example.spec.0.template.0.metadata.0.labels.test
}
session_affinity = "ClientIP"
port {
port = 80
target_port = 80
}
type = "ClusterIP"
}
}
注意:标签的匹配:selector的标签,namespace deployment的版本是否带v1。session_affinity还有type都是kubernetes的基础自行脑补!type的三种方式按照自己环境需求自行设置。
terraform plan and terraform apply
[root@k8s-master-01 terraform-k8s]# terraform plan
[root@k8s-master-01 terraform-k8s]# terraform apply
[root@k8s-master-01 terraform-k8s]# kubectl get svc -n zhangpeng
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
terraform-example-service ClusterIP 172.19.253.9 <none> 80/TCP 30s
对应官方文档:
https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/ingress_v1
本人kuberntes环境v1.21.3.ingress使用的traefik代理方式可以使用networking/v1的ingress还有traefik 的ingressroute 还有gateway api?这里懒得去看traefik相关了(traefik貌似也没有官方的。搜到一个1.17版本的非官方的) 直接按照networking/v1的ingress networking/v1的模板去写tf配置文件了:
cat ingress.tf
resource "kubernetes_ingress_v1" "nginx_ingress_test" {
metadata {
name = "nginx-ingress-test"
namespace = kubernetes_namespace.zhangpeng.metadata.0.name
annotations = {
"kubernetes.io/ingress.class" = "traefik"
"traefik.ingress.kubernetes.io/router.entrypoints" = "web"
}
}
spec {
rule {
host = "nginx-ingress-test.xxxx.com"
http {
path {
path = "/"
backend {
service {
name = "terraform-example-service"
port {
number = 80
}
}
}
}
}
}
}
}
强调一下:这里本来想设置pathType,但是设置了就有报错。我想看一下默认的是什么!
terraform plan and terraform apply
[root@k8s-master-01 terraform-k8s]# terraform plan
[root@k8s-master-01 terraform-k8s]# terraform apply
pathType: ImplementationSpecific!抽时间看一下这个 ImplementationSpecific
web访问测试一下:
关于我的ingress traefik 清参照:Kubernetes 1.20.5 安装traefik在腾讯云下的实践
storage这里看了下不太合适去做。rbac也不想太多的演示了!
看了一眼tke相关文档也没有什么太深入想看的,比如cbs快存储 clb整合之类的?
看了阿里云的也是类似大家都差不多
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。