如何在 Kubernetes 集群上安装 Ansible AWX
Ansible Kubernetes
![](/data/attachment/album/202306/25/172630f134fo6cg5gcodu4.jpg)



> 
> 在本文中，我们将逐步向你展示如何在 Kubernetes（k8s）集群上安装 Ansible AWX。
> 
> 
> 


Ansible AWX 是一个强大的开源工具，用于管理和自动化 IT 基础设施。AWX 为 Ansible 提供图形用户界面，使你可以轻松创建、安排和运行 Ansible <ruby> 剧本 <rt>  Playbook </rt></ruby>。


另一方面，Kubernetes 是一种流行的容器编排平台，广泛用于部署和管理容器化应用。


先决条件：


* Kubernetes 集群
* Kubectl
* 具有 sudo 权限和集群管理员权限的普通用户
* 互联网连接


### 步骤 1：安装 Helm


如果你的系统上安装了 Helm，则在命令下运行以进行安装，



```
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
$ chmod +x get_helm.sh
$ ./get_helm.sh
$ helm version

```

![Install-helm-linux-command-line](/data/attachment/album/202306/25/173004ydhfhw6he7f7y7c3.jpg)


### 步骤 2：安装 AWX chart


在 Kubernetes 上安装 AWX 的最简单方法是使用 AWX Helm “<ruby> 海图 <rt>  chart </rt></ruby>”。因此，要通过 “海图” 安装 AWX，首先使用以下 `helm` 命令添加仓库。（LCTT 译注：Kubernetes 生态中大量使用了和航海有关的比喻，因此本文在翻译时也采用了这些比喻）



```
$ helm repo add awx-operator https://ansible.github.io/awx-operator/
"awx-operator" has been added to your repositories
$

```

注意：如果你之前已经添加过此仓库，请在命令下运行以获取最新版本的软件包。



```
$ helm repo update

```

要通过 Helm 安装 awx-operator，请运行:



```
$ helm install ansible-awx-operator awx-operator/awx-operator -n awx --create-namespace

```

![helm-install-awx-operator-kubernetes](/data/attachment/album/202306/25/173022vo84mjx3imdjd84m.jpg)


这将下载 AWX 海图并将其安装在 `awx` 命名空间中的 Kubernetes 集群上。安装过程可能需要几分钟，请耐心等待。


### 步骤 3：验证 AWX 操作员安装


安装成功后，你可以通过运行以下命令来验证 AWX <ruby> 操作员 <rt>  operator </rt></ruby> 状态：



```
$ sudo kubectl get pods -n awx

```

你应该看到这样的东西：


![awx-operator-pod-status-kubectl](/data/attachment/album/202306/25/173034r44ipn354p2i43t5.jpg)


### 步骤 4： 创建 PV、PVC 并部署 AWX yaml 文件


AWX 需要 postgres <ruby> 容器荚 <rt>  pod </rt></ruby> 的持久卷。那么，让我们首先为本地卷创建一个存储类。


注意：在本文中，我使用本地文件系统作为持久卷。



```
$ vi local-storage-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
  namespace: awxprovisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

```

保存并关闭文件，然后运行：



```
$ kubectl create -f local-storage-class.yaml
$ kubectl get sc -n awx
NAME            PROVISIONER                    RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   
local-storage   kubernetes.io/no-provisioner   Delete          WaitForFirstConsumer   false                 
$

```

接下来使用以下 `pv.yaml` 文件创建持久卷（PV）：



```
$ vi pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  namespace: awx
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /mnt/storage
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - k8s-worker

```

保存并退出文件。


![Postgres-pv-awx-kubernetes](/data/attachment/album/202306/25/173359dku85vqq8v1k4mxf.jpg)


重要说明：确保文件夹 `/mnt/storage` 存在于工作节点上，如果不存在，则在工作节点上使用 `mkdir` 命令创建它。在我们的例子中，工作节点是 `k8s-worker`。


执行下面的命令在 `awx` 命名空间中创建 `postgres-pv`。



```
$ kubectl create -f pv.yaml

```

成功创建 PV 后，使用 `pvc.yaml` 文件创建 PersistentVolumeClaim：



```
$ vi  pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-13-ansible-awx-postgres-13-0
  namespace: awx
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

```

![posgres-pvc-awx-kubernetes](/data/attachment/album/202306/25/173128aqq33bt6wp15vmm7.jpg)


要创建 PVC，请运行以下 `kubectl` 命令：



```
$ kubectl create -f pvc.yaml

```

使用下面的命令验证 PV 和 PVC 的状态：



```
$ kubectl get pv,pvc -n awx

```

现在，我们都准备好部署 AWX 实例了。创建一个包含以下内容的 `ansible-awx.yaml` 文件：



```
$ vi ansible-awx.yaml
---
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
  name: ansible-awx
  namespace: awx
spec:
  service_type: nodeport
  postgres_storage_class: local-storage

```

![Ansible-awx-yaml-file](/data/attachment/album/202306/25/173144bk6wj8k3bbr8j6rb.jpg)


保存并关闭文件。


执行以下 `kubectl` 命令来部署 awx 实例：



```
$ kubectl create -f ansible-awx.yaml

```

等待几分钟，然后检查 `awx` 命名空间中的容器荚状态。



```
$ kubectl get pods -n awx

```

![Ansible-AWX-Pods-Status-Kubernetes](/data/attachment/album/202306/25/173159zxo7dr7d7xtux3r1.jpg)


### 步骤 5：访问 AWX Web 界面


要访问 AWX Web 界面，你需要创建一个公开 awx-web 部署的服务：



```
$ kubectl expose deployment ansible-awx-web --name ansible-awx-web-svc --type NodePort -n awx

```

此命令将创建一个 `NodePort` 服务，该服务将 AWX Web 容器的端口映射到 Kubernetes 节点上的端口。你可以通过运行以下命令找到端口号：



```
$ kubectl get svc ansible-awx-web-svc  -n awx

```

这将输出如下内容：



```
NAME                 TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
ansible-awx-web-svc   NodePort   10.99.83.248   <none>        8052:32254/TCP   82s

```

在此示例中，Web 服务在端口 32254 上可用。


![Expose-Ansible-AWX-Web-NodePort-Kubernetes](/data/attachment/album/202306/25/173214q4m2vwv4g22em2vs.jpg)


默认情况下，admin 用户是 Web 界面的 `admin`，密码在 `<resourcename>-admin-password` 机密信息中。要检索管理员密码，请运行：



```
$ kubectl get secrets -n awx | grep -i admin-password
ansible-awx-admin-password        Opaque               1      109m
$
$ kubectl get secret ansible-awx-admin-password -o jsonpath="{.data.password}" -n awx | base64 --decode ; echo
l9mWcIOXQhSKnzZQyQQ9LZf3awDV0YMJ
$

```

你现在可以打开 Web 浏览器并进入 `http://<node-ip>:<node-port>/` 来访问 AWX Web 界面。在上面的示例中，URL 是：



```
http://192.168.1.223:3225

```

![AWX-Login-URL-Kubernetes](/data/attachment/album/202306/25/173224juuvfqfhdlevebws.jpg)


输入凭据后单击登录。


![Ansible-AWX-Web-Dashboard](/data/attachment/album/202306/25/173233xqr98skyytr8md3r.jpg)


恭喜！ 你已在 Kubernetes 上成功安装 Ansible AWX。你现在可以使用 AWX 来自动化你的 IT 基础架构，并让你作为系统管理员的生活更轻松。


*（题图：MJ/bfd354aa-2ee5-4555-98b8-ac5207cbeabe）*




---


via: <https://www.linuxtechi.com/install-ansible-awx-on-kubernetes-cluster/>


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In this post, we will show you how to install Ansible AWX on Kubernetes (k8s) cluster step by step.

[Ansible AWX](https://github.com/ansible/awx) is a powerful open-source tool for managing and automating IT infrastructure. AWX provides a graphical user interface for Ansible, allowing you to easily create, schedule, and run Ansible playbooks.

Kubernetes, on the other hand, is a popular container orchestration platform that is widely used for deploying and managing containerized applications.

#### Prerequisites

- Kubernetes cluster
- Kubectl
- A regular user with sudo rights and cluster admin rights
- Internet connectivity

## Step :1 Install helm

In case you, [helm](https://www.linuxtechi.com/install-use-helm-in-kubernetes/) is installed on your system then run beneath commands to install,

$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 $ chmod +x get_helm.sh $ ./get_helm.sh $ helm version

## Step 2: Install the AWX chart

The easiest way to install AWX on Kubernetes is by using the AWX Helm chart. So, to install AWX via chart, first add its repository using following helm command.

$ helm repo add awx-operator https://ansible.github.io/awx-operator/ "awx-operator" has been added to your repositories $

Note: If you had already added this repository before, then run beneath command to get latest version of packages.

$ helm repo update

To install awx-operator via chart, run

$ helm install ansible-awx-operator awx-operator/awx-operator -n awx --create-namespace

This will download the AWX chart and install it on your Kubernetes cluster in awx namespace.The installation process may take a few minutes, so be patient.

## Step 3: Verify AWX operator installation

After the successful installation, you can verify AWX operator status by running below command

$ sudo kubectl get pods -n awx

You should see something like this:

## Step:4 Create PV, PVC and deploy AWX yaml file

AWX requires persistent volume for postgres pod. So, let’s first create a storage class for local volume

Note: In this post, I am using local file system as persistent volume.

$ vi local-storage-class.yaml apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: local-storage namespace: awx provisioner: kubernetes.io/no-provisioner volumeBindingMode: WaitForFirstConsumer

Save and close the file and then run ,

$ kubectl create -f local-storage-class.yaml $ kubectl get sc -n awx NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION local-storage kubernetes.io/no-provisioner Delete WaitForFirstConsumer false $

Next create persistent volume(pv) using following pv.yaml file,

$ vi pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: postgres-pv namespace: awx spec: capacity: storage: 10Gi volumeMode: Filesystem accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Delete storageClassName: local-storage local: path: /mnt/storage nodeAffinity: required: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: In values: - k8s-worker

Save & exit the file.

Important note : Make sure folder “/mnt/storage” exists on worker node, if it does not exist then create it using mkdir command on worker node. In our case worker node is “k8s-worker”

Execute the beneath command to create postgres-pv in awx namespace.

$ kubectl create -f pv.yaml

Once pv is created successfully then create persistentvolumecliam using pvc.yaml file,

$ vi pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: postgres-13-ansible-awx-postgres-13-0 namespace: awx spec: storageClassName: local-storage accessModes: - ReadWriteOnce resources: requests: storage: 10Gi

To create pvc, run following kubectl command

$ kubectl create -f pvc.yaml

Verify the status of pv and pvc using beneath command

$ kubectl get pv,pvc -n awx

Now, we are all set to deploy AWX instance. Create an ansible-awx.yaml file with following content

$ vi ansible-awx.yaml --- apiVersion: awx.ansible.com/v1beta1 kind: AWX metadata: name: ansible-awx namespace: awx spec: service_type: nodeport postgres_storage_class: local-storage

save and close the file.

Execute following kubectl command to deploy awx instance,

$ kubectl create -f ansible-awx.yaml

Wait for couple of minutes and then check pods status in awx namespace.

$ kubectl get pods -n awx

## Step 5: Access AWX Web Interface

To access the AWX web interface, you need to create a service that exposes the awx-web deployment:

$ kubectl expose deployment ansible-awx-web --name ansible-awx-web-svc --type NodePort -n awx

This command will create a NodePort service that maps the AWX web container’s port to a port on the Kubernetes node. You can find the port number by running:

$ kubectl get svc ansible-awx-web-svc -n awx

This will output something like this:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ansible-awx-web-svc NodePort 10.99.83.248 <none> 8052:32254/TCP 82s

In this example, the web service is available on port 32254.

By default, the admin user is admin for web interface and the password is available in the <resourcename>-admin-password secret. To retrieve the admin password, run

$ kubectl get secrets -n awx | grep -i admin-password ansible-awx-admin-password Opaque 1 109m $ $ kubectl get secret ansible-awx-admin-password -o jsonpath="{.data.password}" -n awx | base64 --decode ; echo l9mWcIOXQhSKnzZQyQQ9LZf3awDV0YMJ $

You can now access the AWX web interface by opening a web browser and navigating to `http://<node-ip>:<node-port>/`. In the example above, the URL would be

http://192.168.1.223:3225

Click on Log In after entering the credentials.

Congratulations! You have successfully installed Ansible AWX on Kubernetes. You can now use AWX to automate your IT infrastructure and make your life as a sysadmin easier.

NekasasHi,

When I am trying to access AWX console it requires SSL certificate. How to bypass this?

paul sI have been exploring ansible solutions to create all AWX-related config via ansible, like inventory, jobs, etc. Is there a way to do this via the helm chart, so I can configure this at startup? I have left-over ansible tasks that create all of this, but I was hoping there’s a more “kubernetes”-way to do this (i.e. config via helm chart specs)…

Ambika NagarajHi,

Thank you for the excellent guide!

When I deploy AWX instance ansible-aws-task and ansible-aws-web getting failed and status is “CrashLoopBackoff”

ambi@kube-master:~/awx$ kubectl get pods -n awx

NAME READY STATUS RESTARTS AGE

ansible-awx-postgres-13-0 1/1 Running 0 2m39s

ansible-awx-task-8567b4d6d5-htrz8 0/4 Init:CrashLoopBackOff 4 (39s ago) 2m18s

ansible-awx-web-6766ddb5c8-8xk25 1/3 CrashLoopBackOff 2 (13s ago) 16s

awx-operator-controller-manager-6569d67f4c-nqtmh 2/2 Running 0 114m

ambi@kube-master:~/awx$

—————–

describe pod shows following logs

Events:

Type Reason Age From Message

—- —— —- —- ——-

Normal Scheduled 99s default-scheduler Successfully assigned awx/ansible-awx-task-8567b4d6d5-htrz8 to kube-worker

Normal Pulled 2s (x5 over 99s) kubelet Container image “quay.io/ansible/awx-ee:latest” already present on machine

Normal Created 2s (x5 over 99s) kubelet Created container init

Normal Started 1s (x5 over 99s) kubelet Started container init

Warning BackOff 1s (x9 over 97s) kubelet Back-off restarting failed container init in pod ansible-awx-task-8567b4d6d5-htrz8_awx(a0c528db-ec4a-4ced-9dca-9bd3004fa782)

==================

Cannot really find what’s going on in the logs

le-awx”,”namespace”:”awx”,”error”:”exit status 2″,”stacktrace”:”github.com/operator-framework/ansible-operator-plugins/internal/ansible/runner.(*runner).Run.func1\n\tansible-operator-plugins/internal/ansible/runner/runner.go:269″}

{“level”:”error”,”ts”:”2024-01-03T14:07:45Z”,”msg”:”Reconciler error”,”controller”:”awx-controller”,”object”:{“name”:”ansible-awx”,”namespace”:”awx”},”namespace”:”awx”,”name”:”ansible-awx”,”reconcileID”:”e9b8d090-4f0d-444c-91d0-4f3528f5e6e3″,”error”:”event runner on failed”,”stacktrace”:”sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler\n\t/home/runner/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:329\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\t/home/runner/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:274\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\t/home/runner/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:235″}

=================

Could anyone please help me, Thank You!

JonathanI was able to go through all the steps but i mm getting an error on my ansible-awx-web pod. It gives a status of CrashLoopBackOff and when i look at the logs the error says psycopg.OperationalError: connection failed: password authentication failed for user “awx”

How can I fix my yaml to solve this error?

Kool TatchanAfter running this step

$ kubectl create -f ansible-awx.yaml

i get errror when create container” Warning FailedMount 6s (x7 over 37s) kubelet MountVolume.NewMounter initialization failed for volume “postgres-pv” : path “/mnt/storage” does not exist”

How can I fix my yaml to solve this error?

SKAlready mentioned in article ensure the /mnt/storage path is exist.