1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[k8s] fluxを使ってkubernetesの自動デプロイ基盤を整えた話

Last updated at Posted at 2023-12-09

はじめに

前回はGrafana + Prometheusを導入してクラスタ監視基盤を整備しました。

今回はfluxを導入して自動デプロイ基盤を整えていこうと思います。

完成品はこちらのリポジトリで公開しています。
https://github.com/piny940/infra

今回はイメージをpublicに公開している前提で設定を書いています。公開していないイメージに対して自動デプロイをする場合は適宜secretsなどの登録が必要になります。

環境

lemonおよびlimeはマシン名です。VPS2台のクラスタで動かしています。

  • lemon: Kagoya Cloud VPS 2コア 2GB

  • lime: Kagoya Cloud VPS 2コア 2GB

  • デプロイツール: kubeadm

  • CRI: cri-dockerd

  • CNI: Weave Net

前提条件

  • 動作するDockerイメージがDocker Hubにプッシュされている
  • Kubernetesクラスタが動作している

fluxのインストール

ドキュメントに従ってインストールを行います。

curl -s https://fluxcd.io/install.sh | sudo bash

タブ補完が使えるようにします。

. <(flux completion bash)

GitHubのPersonal Access Tokenを作成します。
参考: 個人用アクセス トークンを管理する

トークンを環境変数に格納します。

$ export GITHUB_TOKEN=<gh-token>

次に、flux bootstrapコマンドを実行します。

$ flux bootstrap github \
  --token-auth \
  --owner=my-github-username \
  --repository=my-repository-name \
  --branch=main \
  --path=clusters/my-cluster \
  --personal

これでGitHubリポジトリの変更をfluxが自動検知してクラスタに適用してくれるはずです。

イメージの自動更新設定

ドキュメントに従って設定を行います。以下の記事も参考になります。

トークンとGitHubユーザー名を環境変数に設定します。

export GITHUB_TOKEN=<your-token>
export GITHUB_USER=<your-username>

次に、flux bootstrapを再実行します。

flux bootstrap github \
  --components-extra=image-reflector-controller,image-automation-controller \
  --owner=$GITHUB_USER \
  --repository=flux-image-updates \
  --branch=main \
  --path=clusters/my-cluster \
  --read-write-key \
  --personal

その後、ImageRepositoryImagePolicyを設定します。ImageRepositoryはビルドイメージを「どこから取ってくるか」を設定します。この例では、Docker Hubを使用する前提の設定を記述していますが、ghcr.ioなどを使用する場合はこの設定を変更します。

ImagePolicyは取得したイメージを「どう適用するか」を表します。Dockerイメージに付与されているタグ(例: v1.0.1など)を見て、使用するイメージをフィルタリングしたり、どのタグのイメージを最新のイメージとして扱うかを定義したりします。

これらの設定はアプリケーションごとに構成します。

ImageRepository↓

hello-node/image-repository.yaml
apiVersion: image.toolkit.fluxcd.io/v1beta2
kind: ImageRepository
metadata:
  name: hello-node
  namespace: flux-system
spec:
  image: {DockerHubアカウント名}/hello-node
  interval: 1m0s

Docker Hubを使用する場合、imageは{DockerHubアカウント名}/hello-nodeの形で書きます。

ImagePolicy↓

hello-node/image-policy.yaml
apiVersion: image.toolkit.fluxcd.io/v1beta2
kind: ImagePolicy
metadata:
  name: hello-node
  namespace: flux-system
spec:
  imageRepositoryRef:
    name: hello-node
  filterTags:
    pattern: "^[0-9]+$"
  policy:
    numerical:
      order: asc

タグ名は今回はyyyyMMddHHmmssの形で書くことにしたため、数字のタグのみにフィルタをし、昇順で最新のタグを見ています。タグの一部の文字列だけを見て最新のタグを判定したい場合はextentを使います。
例↓
https://fluxcd.io/flux/guides/image-update/#imagepolicy-examples

最後にImageUpdateAutomationを設定します。これは1つのクラスタに1つ書けばいい設定になります。
参考: https://fluxcd.io/flux/guides/image-update/#configure-image-updates

image-update.yaml
apiVersion: image.toolkit.fluxcd.io/v1beta1
kind: ImageUpdateAutomation
metadata:
  name: flux-system
  namespace: flux-system
spec:
  interval: 30m
  sourceRef:
    kind: GitRepository
    name: flux-system
  git:
    checkout:
      ref:
        branch: main
    commit:
      author:
        email: fluxcdbot@users.noreply.github.com
        name: fluxcdbot
      messageTemplate: '{{range .Updated.Images}}{{println .}}{{end}}'
    push:
      branch: main
  update:
    path: ./clusters/my-cluster
    strategy: Setters

または以下のコマンドで自動生成できます。

$ flux create image update flux-system \
--interval=30m \
--git-repo-ref=flux-system \
--git-repo-path="./clusters/my-cluster" \
--checkout-branch=main \
--push-branch=main \
--author-name=fluxcdbot \
--author-email=fluxcdbot@users.noreply.github.com \
--commit-template="{{range .Updated.Images}}{{println .}}{{end}}" \
--export > ./clusters/my-cluster/flux-system-automation.yaml

これでイメージの変更を検知する準備はできたのですが、最後に「変更を検知したらどの行を書き換えるのか」を指定する必要があります。
アプリのdeployment.yamlspec.template.spec.containers[0].imageに次のようにマーキングをつけます。

image: {DockerHubアカウント名}/{レポジトリ名}:{タグ} # {"$imagepolicy": "flux-system:hello-node"}

これでイメージをpushしたら自動でcommitが作成されるはずです。

最後に

今回はfluxを用いてDockerイメージをpushしたら自動でデプロイされるようにしました。
次回はDockerイメージのpushも自動でできるよう基盤を整えていきたいと思います。

参考資料

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?