LoginSignup
0
1

More than 3 years have passed since last update.

OpenShiftにArgoCDオペレーターを導入してみる

Last updated at Posted at 2021-01-21

まず、Red HatはOpenShift上でのArgoCDをサポートしていないので、使いたいと思ったら使いたいと言い出したあなたがArgoCDに関する全責任を負う必要があるが、自宅のOpenShiftクラスターにArgoCDを導入してみた。場合によっては便利。

ArgoCDオペレーターを導入する

(参考)https://argocd-operator.readthedocs.io/en/latest/

1.OpenShiftクラスターに「argocd」プロジェクト(ネームスペース)を作る。
image.png

2.OperatorHubからArgoCDオペレーターをインストールする。
インストール先のネームスペースは「argocd」で。
image.png

3.ArgoCDのインスタンスを作る。
image.png
名前がexample~なのも何なのでそこだけ「argocd」に、それ以外は全部デフォで。
image.png

4.ArgoCDにアクセスするrouteを作る。

Name: argocd
Hostname: argocd.apps.ocp.example.com
Path: /
Service: argocd-server
Target Port: 443 -> 8080 (TCP)
Security: ☑Secure route
TLS Termination: Passthrough
Insecure Traffic: None

image.png

5.argocdがアプリをデプロイするSAにクラスター管理者権限を付けておく。

# oc adm policy add-cluster-role-to-user cluster-admin -z argocd-application-controller -n argocd

6.初期パスワードを入手する。
batsionサーバー等、ocコマンド実行可能なLinuxサーバーで以下を実行する。

# kubectl -n argocd get secret argocd-cluster -o jsonpath='{.data.admin\.password}' | base64 -d

7.ArgoCDにログインする。
Webブラウザで、作成したrouteのURLにアクセスする。
ユーザー名は「admin」。
https://argocd.apps.ocp.example.com
image.png

Gitレポジトリを用意する

ArgoCDは、Gitレポジトリに置いてあるKubernetesマニフェストと、自身がインストールされたクラスターのとあるネームスペースの状態を一致させるためのツールである故、Gitレポジトリが必要である。
まあ、適当なLinuxサーバーにSSHでアクセスさせるでも良い。ここではbastionサーバーで。

# yum install -y git
# git config --global user.name "root@example.com"
# git config --global user.email "root@example.com"
# mkdir -p /hello-argocd/kubernetes
# cd /hello-argocd
# git init
# cat > kubernetes/pod.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
  name: httpd
spec:
  containers:
    - name: httpd
      image: httpd
      ports:
      - containerPort: 80
EOF
# git add kubernetes/pod.yaml && git commit -m "initial"
# cd ~
# useradd git-ro
# sudo -u git-ro ssh-keygen
# cp ~git-ro/.ssh/id_rsa.pub ~git-ro/.ssh/authorized_keys
# chown git-ro ~git-ro/.ssh/authorized_keys
# chmod 600 ~git-ro/.ssh/authorized_keys

git-roユーザーのSSH秘密鍵は後でGitレポジトリの登録に使うので取っておく。

# cat ~git-ro/.ssh/id_rsa

ArgoCDにレポジトリを登録する

1.ArgoCDの画面で、左の歯車>「Repositories」をクリック。
image.png

2.「CONNECT REPO USING SSH」をクリック。
image.png

3.先に作ったGitレポジトリを登録する。以下を入力して「CONNECT」をクリック。

Name: hello-argocd
Repository URL: git-ro@192.168.1.1:/hello-argocd
SSH private key data: (先に入手したSSH秘密鍵)
☑Skip server verification

image.png

アプリケーションを動かす

1.ArgoCDの画面で、左の菱餅>「NEW APP」をクリック。
image.png

2.アプリケーションを登録する。以下を入力し「CREATE」。

GENERAL

Application Name: hello-argocd
Project: default

SOURCE

Repository URL: git-ro@192.168.1.1:/hello-argocd
Revision: master
Path: kubernetes

DESTINATION

Cluster URL: https://kubernetes.default.svc
Namespace: default

image.png

3.「SYNC」をクリックし、デプロイするリソース、ここでは「/POD/DEFAULT/HTTPD」にチェックを付けて「SYNCHRONIZE」をクリックする。
image.png
image.png

4.「♡Healthy ☑Synced」になったことを確認する。
image.png

パネルをクリックすると、「hello-argocd」アプリが「httpd」ポッドで構成されている様子が見て取れる。
image.png

5.OpenShiftのUIでdefaultプロジェクトを開いても、httpdポッドが稼働しているのが確認できる。
image.png

Gitレポを更新する

1.bastionサーバーでpod.yamlを更新する。

# cd /hello-argocd/kubernetes
# sed -i 's/image: httpd/image: nginx/' pod.yaml
# git add pod.yaml && git commit -m "nginx"
# cd ~

2.ArgoCDの画面で「REFRESH」をクリックするとPodがOutOfSyncになる。
image.png

3.もう一回「SYNC」>「SYNCHRONIZE」と実行すると同期状態が回復する。
名前はhttpdと言いつつ、nginxが動作しているはずである。
image.png

動かしているリソース(ここではポッド)のYamlマニフェストファイルがGitレポジトリ上で更新されるとArgoCDでOutOfSyncとなるので、もう一度同期して新しいマニフェストをデプロイできる。ArgoCDのここが良いところ。GitOpsである。

逆に、yamlファイルが、コンテナイメージのタグ名だけでも変更がないとOutOfSyncにならないので、イメージストリームで常に:latestとかいうOpenShiftならではな使い方には向いていない。かも。

アプリのビルド時にDeployment.yamlのイメージタグ名が更新されて、gitレポジトリにpushで戻される必要もあるのか?Jenkinsに書き込み可能なgitのアカウント渡したくないなというのはある、かも。。

ArgoCD、どのくらいリソース食うのか

OpenShiftの「Monitoring」>「Dashboards」から「Kubernetes / Compute Resources / Namespace (Pods)」、「argocd」の画面を見る限り、メモリを2~300MB消費するくらいか。案外少ない。プロジェクトごとにargocdインスタンスを一つデプロイする様な運用だと多いかな?
image.png

ArgoCD CLIを導入したければ

最近は新しいCLIを導入するのがほんと億劫だ。。という気分を乗り越えられる方は、bastionサーバー等、ocコマンドを導入したLinuxサーバーにArgoCD CLIを導入することも出来るだろう。使わなくて済むならそれが吉。あなたが責任を持つメンテ対象も増える事だしね?
https://github.com/argoproj/argo-cd/releases/tag/v1.8.2

# curl -O -L https://github.com/argoproj/argo-cd/releases/download/v1.8.2/argocd-linux-amd64
# chmod 755 argocd-linux-amd64
# mv argocd-linux-amd64 /usr/local/bin/argocd
0
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
0
1