56chのブログ

そこそこ技術が好きな人

🔥Terraformでクラスターを作った時にkubectlがTimeOutになる

sudo kubectl get nodes --output name
E0302 05:38:22.104843 6913 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0302 05:38:22.105255 6913 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0302 05:38:22.106854 6913 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0302 05:38:22.108356 6913 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0302 05:38:22.109711 6913 memcache.go:238] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
The connection to the server localhost:8080 was refused - did you specify the right host or port?

原因: 環境変数KUBECONFIGが設定されておらず(あるいはパスが見つからず)、参照が上手く行っていない。

解決:

export KUBECONFIG=$KUBECONFIG:$HOME/<kubeconfigの場所>
# kubeconfig
resource "local_file" "kubeconfig" {
  content  = module.gke_auth.kubeconfig_raw
  filename = "kubeconfig-${var.env_name}" //ここのファイルのパスを上のexport文に代入する
}

今回は.tfファイルがkubeconfigを吐き出す設定なので、そこにパスを通す。