るつぼっと

NWエンジニアな人たちに向けて

[Ansible] AWX 21.1.0をAWS上へ構築してみた

はじめに

お勉強のためにAWS上へAWX 21.1.0を構築した。
一応動いたのでメモを残すが、AWSもAWXもk8sも素人なので信頼しないでほしい。
なお下記の記事を参考にさせて頂きk3sを採用している。

AWX を AWX Operator でシングルノード K3s にホストする | kurokobo.com

結論

インスタンスを立てた後、とりあえずこの通り入力すれば構築できる。
確認やトラブルを含めた詳細は次以降の節に記載する。

$ sudo dnf -y install git
$ sudo dnf -y upgrade
$ sudo systemctl disable nm-cloud-setup.service nm-cloud-setup.timer
$ sudo curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644
$ mkdir kustomize
$ cd kustomize/
$ sudo curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"  | bash
$ vi kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  # Find the latest tag here: https://github.com/ansible/awx-operator/releases
  - github.com/ansible/awx-operator/config/default?ref=0.22.0

# Set the image tags to match the git version from above
images:
  - name: quay.io/ansible/awx-operator
    newTag: 0.22.0

# Specify a custom namespace in which to install AWX
namespace: awx
$
$ sudo ./kustomize build . | kubectl apply -f -
$ vi awx-demo.yaml
---
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
  name: awx-demo
spec:
  service_type: nodeport
$
$ vi kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  # Find the latest tag here: https://github.com/ansible/awx-operator/releases
  - github.com/ansible/awx-operator/config/default?ref=0.22.0
  - awx-demo.yaml
# Set the image tags to match the git version from above
images:
  - name: quay.io/ansible/awx-operator
    newTag: 0.22.0

# Specify a custom namespace in which to install AWX
namespace: awx
$
$ sudo ./kustomize build . | kubectl apply -f -

※ 2回目の vi kustomization.yaml - awx-demo.yamlの行を追記しているだけ

AWSへEC2インスタンスを立てる

まずはインスタンスを立てる。
AMIはRed Hatにし、リソースはAAP2.1やk3sの要件を若干意識した。

Deploying Ansible Automation Platform 2.1 Reference Architectures 2021 | Red Hat Customer Portal

Rancher Docs: Installation Requirements

初期導入済みのパッケージをupdate

とりあえず全体的に最新化しておく。
ライセンス関連の余計なメッセージが出ていたのでついでに消した。

$ sudo dnf -y upgrade
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.
(出力省略)
$ sudo vi /etc/yum/pluginconf.d/subscription-manager.conf
$ more /etc/yum/pluginconf.d/subscription-manager.conf
[main]
enabled=0
(出力省略)


k3sをインストール

公式の手順どおりに進める。 rancher.com

$ sudo curl -sfL https://get.k3s.io | sh -
(出力省略)
Job for k3s.service failed because the control process exited with error code.
See "systemctl status k3s.service" and "journalctl -xe" for details.
$ systemctl status k3s
● k3s.service - Lightweight Kubernetes
   Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: disabled)
   Active: activating (auto-restart) (Result: exit-code) since Sun 2022-06-05 09:40:55 UTC; 449ms ago
     Docs: https://k3s.io
  Process: 69150 ExecStartPre=/bin/sh -xc ! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service (code=exited, status=1/FAILURE)
$


インストールは成功したようだがサービスの起動に失敗している。
どうやら下記の作業が必要らしい。firewalldは見当たらないけど。

rancher.com

$ systemctl status firewalld
Unit firewalld.service could not be found.
$ sudo systemctl disable nm-cloud-setup.service nm-cloud-setup.timer
Removed /etc/systemd/system/NetworkManager.service.wants/nm-cloud-setup.service.
Removed /etc/systemd/system/timers.target.wants/nm-cloud-setup.timer.
$ sudo reboot
$ systemctl status k3s
● k3s.service - Lightweight Kubernetes
   Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-06-05 09:47:37 UTC; 2min 18s ago
(出力省略)
$

AWXをインストール

AWXのドキュメントを参照すると、インストールにはAWX Operatorを使えとある。
どうやらAWXを立てるためのコンテナっぽい。
github.com

AWX Operatorのドキュメントを参照する。
Minikubeではなくk3sをインストールしたため最初は飛ばして、Basic Installから始める。

github.com

kustomizeというマニフェスト管理ツールを使う必要があるようだ。
kustomizeのドキュメントを参照して導入しつつ作業を進める。
kustomization.yamlを作成する際は<tag>の部分を忘れずに置き換えること。

kubectl.docs.kubernetes.io

$ mkdir kustomize
$ cd kustomize/
$ pwd
/home/ec2-user/kustomize
$ sudo curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"  | bash
{Version:kustomize/v4.5.5 GitCommit:daa3e5e2c2d3a4b8c94021a7384bfb06734bcd26 BuildDate:2022-05-20T20:25:40Z GoOs:linux GoArch:amd64}
kustomize installed to /home/ec2-user/kustomize/kustomize
$
$ vi kustomization.yaml
$ more kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  # Find the latest tag here: https://github.com/ansible/awx-operator/releases
  - github.com/ansible/awx-operator/config/default?ref=0.22.0

# Set the image tags to match the git version from above
images:
  - name: quay.io/ansible/awx-operator
    newTag: 0.22.0

# Specify a custom namespace in which to install AWX
namespace: awx
$
$ sudo ./kustomize build . | kubectl apply -f -
Error: accumulating resources: accumulation err='accumulating resources from 'github.com/ansible/awx-operator/config/default?ref=0.22.0': evalsymlink failure on '/home/ec2-user/kustomize/github.com/ansible/awx-operator/config/default?ref=0.22.0' : lstat /home/ec2-user/kustomize/github.com: no such file or directory': no 'git' program on path: exec: "git": executable file not found in $PATH
WARN[0000] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode to modify kube config permissions
error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied
$

Gitが入ってないからエラーになっている。
入れてから改めて実行する。

$ sudo dnf -y install git
(出力省略)
$
$ sudo ./kustomize build . | kubectl apply -f -
WARN[0000] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode to modify kube config permissions
error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied
$

今度はk3sの権限問題が発生した。
冒頭で紹介した記事を参考にしk3sを入れ直してみる。

$ sudo curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644
$ 
$ sudo ./kustomize build . | kubectl apply -f -
namespace/awx created
customresourcedefinition.apiextensions.k8s.io/awxbackups.awx.ansible.com created
customresourcedefinition.apiextensions.k8s.io/awxrestores.awx.ansible.com created
customresourcedefinition.apiextensions.k8s.io/awxs.awx.ansible.com created
serviceaccount/awx-operator-controller-manager created
role.rbac.authorization.k8s.io/awx-operator-awx-manager-role created
role.rbac.authorization.k8s.io/awx-operator-leader-election-role created
clusterrole.rbac.authorization.k8s.io/awx-operator-metrics-reader created
clusterrole.rbac.authorization.k8s.io/awx-operator-proxy-role created
rolebinding.rbac.authorization.k8s.io/awx-operator-awx-manager-rolebinding created
rolebinding.rbac.authorization.k8s.io/awx-operator-leader-election-rolebinding created
clusterrolebinding.rbac.authorization.k8s.io/awx-operator-proxy-rolebinding created
configmap/awx-operator-awx-manager-config created
service/awx-operator-controller-manager-metrics-service created
deployment.apps/awx-operator-controller-manager created
$
$ kubectl get pods -n awx
NAME                                             READY   STATUS    RESTARTS   AGE
awx-operator-controller-manager-c6554d8f-zpblt   2/2     Running   0          3m40s
$ kubectl config set-context --current --namespace=awx
error: open /etc/rancher/k3s/k3s.yaml.lock: permission denied
$
$ sudo kubectl config set-context --current --namespace=awx
sudo: kubectl: command not found
$

AWX Operatorを構築できたようだ。
しかし現在のnamespaceをawxに変更できない。
解決できなかったためあきらめて毎回コマンドに -n awx を付けることとする。

$ vi awx-demo.yaml
$ more awx-demo.yaml
---
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
  name: awx-demo
spec:
  service_type: nodeport
$
$ vi kustomization.yaml
$ more kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  # Find the latest tag here: https://github.com/ansible/awx-operator/releases
  - github.com/ansible/awx-operator/config/default?ref=0.22.0
  - awx-demo.yaml
# Set the image tags to match the git version from above
images:
  - name: quay.io/ansible/awx-operator
    newTag: 0.22.0

# Specify a custom namespace in which to install AWX
namespace: awx
$
$ sudo ./kustomize build . | kubectl apply -f -
namespace/awx unchanged
customresourcedefinition.apiextensions.k8s.io/awxbackups.awx.ansible.com unchanged
customresourcedefinition.apiextensions.k8s.io/awxrestores.awx.ansible.com unchanged
customresourcedefinition.apiextensions.k8s.io/awxs.awx.ansible.com unchanged
serviceaccount/awx-operator-controller-manager unchanged
role.rbac.authorization.k8s.io/awx-operator-awx-manager-role configured
role.rbac.authorization.k8s.io/awx-operator-leader-election-role unchanged
clusterrole.rbac.authorization.k8s.io/awx-operator-metrics-reader unchanged
clusterrole.rbac.authorization.k8s.io/awx-operator-proxy-role unchanged
rolebinding.rbac.authorization.k8s.io/awx-operator-awx-manager-rolebinding unchanged
rolebinding.rbac.authorization.k8s.io/awx-operator-leader-election-rolebinding unchanged
clusterrolebinding.rbac.authorization.k8s.io/awx-operator-proxy-rolebinding unchanged
configmap/awx-operator-awx-manager-config unchanged
service/awx-operator-controller-manager-metrics-service unchanged
deployment.apps/awx-operator-controller-manager unchanged
awx.awx.ansible.com/awx-demo created
$
$ kubectl logs -f deployments/awx-operator-controller-manager -c awx-manager -n awx
(出力省略。数分かかった)
----- Ansible Task Status Event StdOut (awx.ansible.com/v1beta1, Kind=AWX, awx-demo/awx) -----


PLAY RECAP *********************************************************************
localhost                  : ok=63   changed=0    unreachable=0    failed=0    skipped=46   rescued=0    ignored=0


----------
^C
$
$ kubectl get pods -l "app.kubernetes.io/managed-by=awx-operator" -n awx
NAME                      READY   STATUS    RESTARTS   AGE
awx-demo-postgres-0       1/1     Running   0          10m
awx-demo-fb89f8dd-brz9b   4/4     Running   0          9m54s
$
$ kubectl get svc -l "app.kubernetes.io/managed-by=awx-operator" -n awx
NAME                TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
awx-demo-postgres   ClusterIP   None          <none>        5432/TCP       10m
awx-demo-service    NodePort    10.43.3.153   <none>        80:30080/TCP   10m
$
$ kubectl get secret awx-demo-admin-password -o jsonpath="{.data.password}" -n awx | base64 --decode
(ここにパスワードが表示される)$

正常に完了したようです。

アクセスしてみる

まずはセキュリティグループに穴をあける。
対象のポート番号は先ほどkubectl get svcで確認したもの。



http://インスタンスのパブリックIP:30080/ でアクセス。
パスワードにはkubectl get secretで確認した値を入力。



ログイン成功。 これでやっとひと段落です。



あとはGitHub等と連携してProjectを作成すればいいが、ひとまずここまで。
因みにローカルにPlaybook置いて読み込ませる方法はわからなかった。
コンテナの/var/lib/awx/projects/配下に配置すればいいと思ったのだが。

所感

自分に知識が無いからだが、AWXを触りたいだけなのにタライ回しにされた感が強い。