编程知识 cdmana.com

Terraform orchestration k8s resources

One background

Kubernetes (K8S) The provider is used to communicate with Kubernetes Supported resources interact . Before using the provider , It needs to be configured with appropriate credentials .

Two Support resources

Terraform Support complete Kubernetes resources .

3、 ... and authentication

For various cloud providers Terraform Providers provide resources , So that the managed Kubernetes Cluster upgrade to EKS、 AKS and GKE Wait for the service . These resources ( Or data sources ) Will have properties , These properties expose Kubernetes The credentials required by the provider to connect to these clusters .

Four operation

4.1 Code

terraform {  required_providers {    kubernetes = {      source  = "hashicorp/kubernetes"      version = ">= 2.0.0"    }  }}provider "kubernetes" {  config_path = "~/.kube/config"}resource "kubernetes_namespace" "test" {  metadata {    name = "nginx"  }}resource "kubernetes_deployment" "test" {  metadata {    name      = "nginx"    namespace = kubernetes_namespace.test.metadata.0.name  }  spec {    replicas = 2    selector {      match_labels = {        app = "MyTestApp"      }    }    template {      metadata {        labels = {          app = "MyTestApp"        }      }      spec {        container {          image = "nginx"          name  = "nginx-container"          port {            container_port = 80          }        }      }    }  }}resource "kubernetes_service" "test" {  metadata {    name      = "nginx"    namespace = kubernetes_namespace.test.metadata.0.name  }  spec {    selector = {      app = kubernetes_deployment.test.spec.0.template.0.metadata.0.labels.app    }    type = "NodePort"    port {      node_port   = 30201      port        = 80      target_port = 80    }  }}

4.2 perform

terraform apply
  • View results

  • View results

5、 ... and other

You can also use it ssh Protocol to connect to the on the remote computer docker host . The configuration is as follows :

provider "docker" {  host     = "ssh://[email protected]:22"  ssh_opts = ["-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null"]}

Reference link

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://cdmana.com/2022/134/202205141315108576.html

Scroll to Top