如何使用 Terraform 在 K8S 集群机器上创建入口?

时间:2021-04-29 07:07:55

标签: kubernetes terraform kubernetes-ingress

我是 K8S 和 Terraform 的新手。我在裸机上运行的 K8S 集群上安装了 ingress_nginx。

$json_str = '{"timestamp":"0","chapter_name":"Introduction"},{"timestamp":"20","chapter_name":"Chapter 1"},{"timestamp":"40","chapter_name":"Chapter 2"},{"timestamp":"100","chapter_name":"Chapter 3"}';

$timestamps_arr = json_decode($json_str, true);
$timestamps_output = "
    <label for=\"sel_timestamps\">Jump to: </label>
    <select id=\"sel_timestamps\" name=\"sel_timestamps\" onchange=\"VidTimeJump()\">";

foreach($timestamps_arr as $timestamp){
    echo '<option value="'.$timestamp['timestamp'].'">'.$timestamp['chapter_name'].'</option>';
}

$timestamps_output .= "
    </select>";

我创建了部署、服务和入口,并且能够从浏览器成功访问 docker-hello-world-svc。 Ingress.yaml 如下

[root@control02 ~]# kubectl get svc -n ingress-nginx
    NAME                                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
    docker-hello-world-svc               NodePort    10.xx.xx.121     <none>        8086:30333/TCP               13d
    ingress-nginx-controller             NodePort    10.xx.xx.124     <none>        80:31545/TCP,443:30198/TCP   13d
    ingress-nginx-controller-admission   ClusterIP   10.xx.xx.85      <none>        443/TCP                      13d

我的要求是在 K8S 集群上容器化我们基于 PHP 的应用程序。

  1. 通过 Terraform's 创建入口 apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: hello-world-ing annotations: kubernetes.io/ingress.class: "nginx" namespace: ingress-nginx spec: #ingressClassName : nginx rules: - host: foo.com http: paths: - path: / pathType: Prefix backend: service: name: docker-hello-world-svc port: number: 8086 resource "kubernetes_ingress" "web" 是相同的(或)它们不同吗?

  2. 如何使用 Terraform 在 K8S 集群机器上创建“入口”?

    例如,当我从 GitLab 触发作业时,Terraform 应该在 K8S 集群或控制平面机器上创建一个新的 ingress.yaml:kubernetes.io/ingress.class。这可能吗?

请澄清上述问题,如果我的理解有误,请告诉我

2 个答案:

答案 0 :(得分:2)

需要 ingress.class 来让 nginx 入口控制器明白他需要处理这个资源。

要使用 terraform 创建入口,您可以使用以下内容

resource "kubernetes_ingress" "ingress" {
  metadata {
    name      = "ingress-name"
    namespace = "ingress-namespace"
    labels = {
      app = "some-label-app"
    }
    annotations = {
      "kubernetes.io/ingress.class" : "nginx"
    }
  }

  spec {
    rule {
      host = "foo.com"
      http {
        path {
          backend {
            service_name = "svc"
            service_port = "http"
          }
        }
      }
    }
  }
}

答案 1 :(得分:0)

我能够使用以下代码在现有的 K8S 集群(裸机)上创建服务

K8S 集群是在 192.168.xxx.xxx 上设置的,我在其上创建了一个服务 example。我们需要提及 'host' 块内的 'kubernetes' 参数

provider "kubernetes" {
    **host = "https://192.168.xxx.xxx:6443"**
    cluster_ca_certificate = "${base64decode(var.cluster_ca_certificate)}"
    client_certificate = "${base64decode(var.client_certificate)}"
    client_key = "${base64decode(var.client_key)}"
}
  
  resource "kubernetes_service" "example" {
    metadata {
      name = "example"
    }
    spec {
      port {
        port = 8585
        target_port = 80
      }
      type = "ClusterIP"
    }
}