0
0

More than 1 year has passed since last update.

terraformでkustomizeを動かしてみた

Last updated at Posted at 2022-02-06

はじめに

terraformからkustomizeを呼び出すとかできないかなぁと思っていたところ、helmfileというものを見つけました。
そこで試しに動かしてみることにしました。

参考:https://thinkit.co.jp/article/18009

所要時間

コマンド実行だけであれば、30分ぐらい

環境

k8s環境を用意するのはたいへんなので
katacodaのk8s playgroundを使わせていただきました。
https://www.katacoda.com/courses/kubernetes/playground

3時間ぐらいで使えなくなってしまうので注意

実施内容

事前準備

katacodaでk8sを開始する

launch.sh

helmfileを実行するために、helm diffをインストール

helm plugin install https://github.com/databus23/helm-diff

helmfileのインストール

wget -O helmfile_linux_amd64 https://github.com/roboll/helmfile/releases/download/v0.141.0/helmfile_linux_amd64
chmod +x helmfile_linux_amd64
mv helmfile_linux_amd64 /usr/local/bin/helmfile
helmfile version

terraformのインストール

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
terraform version

kusomizeのインストール

curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"  | bash
mv kustomize /usr/local/bin/
kustomize version

helmfileでhelmを実行

helmfile.yamlの作成
簡単な解説

  • repositoriesで指定しているのは helm repo add で追加するリポジトリ
  • release配下で名前やNS、chart、chartで変更する部分が記載されている
cat <<EOF > helmfile.yaml
repositories:
- name: flagger
  url: https://flagger.app

releases:
- name: frontend
  namespace: test
  chart: flagger/podinfo
  values:
  - replicaCount: 2
    backend: http://backend-podinfo:9898/echo
- name: backend
  namespace: test
  chart: flagger/podinfo
  values:
  - hpa:
      enabled: true
EOF

実行するk8sのNSを作成

kubectl create ns test

helmfileの実行

helmfile apply

正常にapplyされたか確認
Podが正常に起動していればOK

controlplane $ kubectl get pods -n test
NAME                                READY   STATUS    RESTARTS   AGE
backend-podinfo-8bb585d7-d7bqc      1/1     Running   0          2m6s
backend-podinfo-8bb585d7-sksgf      1/1     Running   0          51s
frontend-podinfo-67476f4cf8-f5x5q   1/1     Running   0          2m6s
frontend-podinfo-67476f4cf8-h5vm9   1/1     Running   0          51s

削除

helmfile delete
controlplane $ kubectl get pods -n test
No resources found in test namespace.

ここまでで、helmfileからhelmを呼び出すことができた。
次はhelmfileからkustomizeを実行する

helmfileでkustomizeを実行

kustomizeのサンプルファイルを取得して、apply

git clone https://github.com/kubernetes-sigs/kustomize.git
mv kustomize/examples/helloWorld/ ./
rm -rf kustomize/

helmfile.yamlを修正
chart部分にkustomizeがあるディレクトリを指定してあげればよい

rm helmfile.yaml
cat <<EOF > helmfile.yaml
releases:
  - name: kustomize
    chart: ./helloWorld/
EOF

helmfileの実行

helmfile apply

正常にapplyされたか確認
Podが正常に起動していればOK

controlplane $ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
the-deployment-7757d64b69-2tndw   1/1     Running   0          5m14s
the-deployment-7757d64b69-rw7gf   1/1     Running   0          5m14s
the-deployment-7757d64b69-sxgsp   1/1     Running   0          5m14s

削除

helmfile delete
controlplane $ kubectl get pod
No resources found in test namespace.

これでhelmfileを使ってkustomizeを実行することができた
次はterraformからhelmfileを呼び出して、helmfileからkustomizeを実行する。

terraformでhelmfileを実行

tfファイルの作成

cat <<EOF > test.tf 
provider "helmfile" {}

resource "helmfile_release_set" "mystack" {
    content = file("./helmfile.yaml")
    kubeconfig = "./.kube/config"
}

terraform {
  required_providers {
    helmfile = {
      source = "mumoshu/helmfile"
      version = "0.14.1"
    }
  }
}
EOF

terraformの実行

terraform init
terraform plan

特にエラー等でなければ

terraform apply

正常にapplyされたか確認
Podが正常に起動していればOK

controlplane $ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
the-deployment-7757d64b69-2tndw   1/1     Running   0          5m14s
the-deployment-7757d64b69-rw7gf   1/1     Running   0          5m14s
the-deployment-7757d64b69-sxgsp   1/1     Running   0          5m14s

削除

terraform destroy
controlplane $ kubectl get pod
No resources found in test namespace.

これで
terraform→helmfile→kustomize
という順序でkustomizeを呼び出せた。

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0