Awx-operator: Postgres géré - /var/lib/postgresql/data : autorisation refusée

Créé le 28 juil. 2021  ·  8Commentaires  ·  Source: ansible/awx-operator

J'essaie d'installer AWX à l'aide de l'opérateur awx exécuté sur k3s et le pod awx-postgres échoue avec le message :
mkdir: cannot create directory ‘/var/lib/postgresql/data’: Permission denied
Voici mon awx.yml :

---
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
  name: awx
spec:
  ingress_type: Ingress
  route_tls_termination_mechanism: edge
  hostname: localhost
  postgres_storage_requirements:
    requests:
      storage: 3Gi
  projects_persistence: true
  projects_existing_claim: awx-projects-claim
  web_resource_requirements:
    requests:
      cpu: 250m
      memory: 2Gi
    limits:
      cpu: 750m
      memory: 4Gi
  task_resource_requirements:
    requests:
      cpu: 250m
      memory: 1Gi
    limits:
      cpu: 500m
      memory: 2Gi
  ee_resource_requirements:
    requests:
      cpu: 250m
      memory: 1Gi
    limits:
      cpu: 500m
      memory: 2Gi

Qu'est-ce que je fais de mal ici ?

bug in_progress medium operator

Commentaire le plus utile

Donc, fondamentalement, nous devrons tirer parti d'une approche `initContainer pour corriger l'autorisation afin que la base de données puisse être créée. Cet extrait fera le travail :

diff --git a/roles/installer/tasks/database_configuration.yml b/roles/installer/tasks/database_configuration.yml
index 2e99be5..470530a 100644
--- a/roles/installer/tasks/database_configuration.yml
+++ b/roles/installer/tasks/database_configuration.yml
@@ -80,8 +80,9 @@
 - block:
     - name: Create Database if no database is specified
       k8s:
-        apply: true
+        apply: yes
         definition: "{{ lookup('template', 'postgres.yaml.j2') }}"
+        wait: yes
       register: create_statefulset_result

   rescue:
diff --git a/roles/installer/templates/postgres.yaml.j2 b/roles/installer/templates/postgres.yaml.j2
index d17ee12..f87c842 100644
--- a/roles/installer/templates/postgres.yaml.j2
+++ b/roles/installer/templates/postgres.yaml.j2
@@ -37,10 +37,27 @@ spec:
       imagePullSecrets:
         - name: {{ image_pull_secret }}
 {% endif %}
+      initContainers:
+        - name: init-chmod-data
+          image: '{{ postgres_image }}:{{ postgres_image_version }}'
+          imagePullPolicy: '{{ image_pull_policy }}'
+          command:
+            - /bin/sh
+            - -c
+            - |
+              if [ ! -f {{ postgres_data_path }}/PG_VERSION ]; then
+                chown postgres:root {{ postgres_data_path | dirname }}
+              fi
+          volumeMounts:
+            - name: postgres
+              mountPath: '{{ postgres_data_path | dirname }}'
+              subPath: '{{ postgres_data_path | dirname | basename }}'
       containers:
         - image: '{{ postgres_image }}:{{ postgres_image_version }}'
           imagePullPolicy: '{{ image_pull_policy }}'
           name: postgres
+          securityContext:
+            fsGroup: 999
           env:
             # For postgres_image based on rhel8/postgresql-12
             - name: POSTGRESQL_DATABASE

Il en résulte un état de fonctionnement une fois le correctif appliqué :

$ ubectl get pods -w                              00:38:58
NAME                            READY   STATUS    RESTARTS   AGE
awx-operator-5bc776b4d4-d9ww2   1/1     Running   0          4m41s
awx-postgres-0                  1/1     Running   0          4m3s
awx-d67898cd9-k6jrc             4/4     Running   0          3m48s

$ kubectl iexec awx-postgres-0 /bin/bash                                                                                                                         00:57:00
root@awx-postgres-0:/# namei  -xmolv /var/lib/postgresql/data/pgdata/
f: /var/lib/postgresql/data/pgdata/
Drwxr-xr-x root     root     /
drwxr-xr-x root     root     var
drwxr-xr-x root     root     lib
drwxr-xr-x postgres postgres postgresql
Drwx------ postgres root     data
drwx------ postgres root     pgdata

Je vais créer un PR pour cela. Merci d'avoir signalé le problème @flisak-robert et @scott-vick

Tous les 8 commentaires

Je suis les instructions exactes @marwel liées ci-dessus mais

Je vais essayer cet après-midi en utilisant k3s car je ne peux pas reproduire sur mon laboratoire actuel

Bonjour les gars, j'ai déployé un k3s avec un seul nœud sur ma machine de test comme décrit sur https://rancher.com/docs/k3s/latest/en/quick-start/#install -script

$ kubectl get nodes                                                            
NAME              STATUS   ROLES                  AGE     VERSION
storm.tatu.home   Ready    control-plane,master   3m39s   v1.21.3+k3s1

$ kubectl get pods -A                                                                                          23:01:09
NAMESPACE     NAME                                      READY   STATUS      RESTARTS   AGE
kube-system   local-path-provisioner-5ff76fc89d-4d7bn   1/1     Running     0          9m51s
kube-system   metrics-server-86cbb8457f-9fkt2           1/1     Running     0          9m51s
kube-system   coredns-7448499f4d-9t87w                  1/1     Running     0          9m51s
kube-system   helm-install-traefik-crd-mlrtg            0/1     Completed   0          9m51s
kube-system   helm-install-traefik-v5n5s                0/1     Completed   1          9m51s
kube-system   svclb-traefik-c9cgh                       2/2     Running     0          9m28s
kube-system   traefik-97b44b794-6dz4g                   1/1     Running     0          9m28s

Ensuite, j'ai généré la dernière image d'opérateur devel et déployé :

kubectl apply -f deploy/awx-operator.yaml                                                                     23:08:40
customresourcedefinition.apiextensions.k8s.io/awxs.awx.ansible.com created
customresourcedefinition.apiextensions.k8s.io/awxbackups.awx.ansible.com created
customresourcedefinition.apiextensions.k8s.io/awxrestores.awx.ansible.com created
clusterrole.rbac.authorization.k8s.io/awx-operator created
clusterrolebinding.rbac.authorization.k8s.io/awx-operator created
serviceaccount/awx-operator created
deployment.apps/awx-operator created

L'opérateur a démarré comme prévu :

kubectl get pods -A -w                                                                                       23:07:32
NAMESPACE     NAME                                      READY   STATUS              RESTARTS   AGE
kube-system   local-path-provisioner-5ff76fc89d-4d7bn   1/1     Running             0          11m
kube-system   metrics-server-86cbb8457f-9fkt2           1/1     Running             0          11m
kube-system   coredns-7448499f4d-9t87w                  1/1     Running             0          11m
kube-system   helm-install-traefik-crd-mlrtg            0/1     Completed           0          11m
kube-system   helm-install-traefik-v5n5s                0/1     Completed           1          11m
kube-system   svclb-traefik-c9cgh                       2/2     Running             0          10m
kube-system   traefik-97b44b794-6dz4g                   1/1     Running             0          10m
default       awx-operator-88b886454-9pq7w              0/1     ContainerCreating   0          15s
default       awx-operator-88b886454-9pq7w              1/1     Running             0          16s

Alors maintenant, pour le dépannage, j'utilise une spécification AWX similaire fournie plus tôt comme suit ci-dessous. Comme vous pouvez le voir, je dois l'étendre pour pouvoir créer le PVC awx-projects-claim devrait exister selon la spécification AWX.

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: awx-projects-claim
  namespace: default
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-path
  resources:
    requests:
      storage: 2Gi
---
apiVersion: awx.ansible.com/v1beta1
kind: AWX
metadata:
  name: awx
spec:
  ingress_type: Ingress
  route_tls_termination_mechanism: edge
  hostname: localhost
  postgres_storage_requirements:
    requests:
      storage: 3Gi
  projects_persistence: true
  projects_existing_claim: awx-projects-claim
  web_resource_requirements:
    requests:
      cpu: 250m
      memory: 2Gi
    limits:
      cpu: 750m
      memory: 4Gi
  task_resource_requirements:
    requests:
      cpu: 250m
      memory: 1Gi
    limits:
      cpu: 500m
      memory: 2Gi
  ee_resource_requirements:
    requests:
      cpu: 250m
      memory: 1Gi
    limits:
      cpu: 500m
      memory: 2Gi
$ kubectl apply -f pg-k3s.yml                                                                                                   23:13:08
persistentvolumeclaim/awx-projects-claim created
awx.awx.ansible.com/awx created

# still pending because POD has not started yet
$ kubectl get pvc                                                                                                               23:14:05
NAME                      STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
awx-projects-claim        Pending                                                                        local-path     23s
postgres-awx-postgres-0   Bound     pvc-3b9e6563-9085-4d79-90ba-fa6c88431c6c   3Gi        RWO            local-path     4s

Puis en regardant le pod, je l'ai fait planter

$ sh kubectl get pods -A -w                                                                                       23:07:32
NAMESPACE     NAME                                      READY   STATUS              RESTARTS   AGE
kube-system   local-path-provisioner-5ff76fc89d-4d7bn   1/1     Running             0          11m
kube-system   metrics-server-86cbb8457f-9fkt2           1/1     Running             0          11m
kube-system   coredns-7448499f4d-9t87w                  1/1     Running             0          11m
kube-system   helm-install-traefik-crd-mlrtg            0/1     Completed           0          11m
kube-system   helm-install-traefik-v5n5s                0/1     Completed           1          11m
kube-system   svclb-traefik-c9cgh                       2/2     Running             0          10m
kube-system   traefik-97b44b794-6dz4g                   1/1     Running             0          10m
default       awx-operator-88b886454-9pq7w              0/1     ContainerCreating   0          15s
default       awx-operator-88b886454-9pq7w              1/1     Running             0          16s



default       awx-postgres-0                            0/1     Pending             0          0s
kube-system   helper-pod-create-pvc-3b9e6563-9085-4d79-90ba-fa6c88431c6c   0/1     Pending             0          0s
kube-system   helper-pod-create-pvc-3b9e6563-9085-4d79-90ba-fa6c88431c6c   0/1     ContainerCreating   0          0s
kube-system   helper-pod-create-pvc-3b9e6563-9085-4d79-90ba-fa6c88431c6c   0/1     Completed           0          3s
kube-system   helper-pod-create-pvc-3b9e6563-9085-4d79-90ba-fa6c88431c6c   0/1     Terminating         0          3s
kube-system   helper-pod-create-pvc-3b9e6563-9085-4d79-90ba-fa6c88431c6c   0/1     Terminating         0          3s
default       awx-postgres-0                                               0/1     Pending             0          4s
default       awx-postgres-0                                               0/1     ContainerCreating   0          4s
default       awx-76bdfc954c-jxvll                                         0/4     Pending             0          0s
kube-system   helper-pod-create-pvc-85b1b705-43b3-42a6-a96b-1e79943e99d5   0/1     Pending             0          0s
kube-system   helper-pod-create-pvc-85b1b705-43b3-42a6-a96b-1e79943e99d5   0/1     ContainerCreating   0          0s
default       awx-postgres-0                                               1/1     Running             0          15s
kube-system   helper-pod-create-pvc-85b1b705-43b3-42a6-a96b-1e79943e99d5   0/1     Completed           0          6s
kube-system   helper-pod-create-pvc-85b1b705-43b3-42a6-a96b-1e79943e99d5   0/1     Terminating         0          7s
kube-system   helper-pod-create-pvc-85b1b705-43b3-42a6-a96b-1e79943e99d5   0/1     Terminating         0          7s
default       awx-postgres-0                                               0/1     Error               0          16s
default       awx-76bdfc954c-jxvll                                         0/4     Pending             0          7s
default       awx-76bdfc954c-jxvll                                         0/4     Init:0/1            0          8s
default       awx-postgres-0                                               0/1     Error               1          18s
default       awx-postgres-0                                               0/1     CrashLoopBackOff    1          18s
default       awx-76bdfc954c-jxvll                                         0/4     PodInitializing     0          18s
default       awx-postgres-0                                               1/1     Running             2          35s
default       awx-postgres-0                                               0/1     Error               2          35s
default       awx-postgres-0                                               0/1     CrashLoopBackOff    2          48s
default       awx-postgres-0                                               0/1     Error               3          64s
default       awx-postgres-0                                               0/1     CrashLoopBackOff    3          77s
default       awx-76bdfc954c-jxvll                                         4/4     Running             0          111s
default       awx-postgres-0                                               0/1     CrashLoopBackOff    4          2m11s

Donc, fondamentalement, le postgres statefulset n'a pas fonctionné, mais le awx a bien fonctionné (bien sûr pas fonctionnel en raison de la base de données manquante)

$ kubectl get pvc
NAME                      STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
postgres-awx-postgres-0   Bound    pvc-3b9e6563-9085-4d79-90ba-fa6c88431c6c   3Gi        RWO            local-path     2m37s
awx-projects-claim        Bound    pvc-85b1b705-43b3-42a6-a96b-1e79943e99d5   2Gi        RWO            local-path     2m56s

$ kubectl get pods                                                                                       23:17:32
NAME                           READY   STATUS    RESTARTS   AGE
awx-operator-88b886454-9pq7w   1/1     Running   0          8m47s
awx-76bdfc954c-jxvll           4/4     Running   0          3m21s
awx-postgres-0                 0/1     Error     5          3m30s

Ensuite, en regardant le conteneur, oui, j'ai eu la même erreur sur k3s utilisant le fournisseur de chemin local. Cela ressemble à https://github.com/ansible/awx-operator/pull/413 , cependant, nous devons l'aborder pour le postgresql statefulset.

$ kubectl logs awx-postgres-0                                                                       23:26:52
mkdir: cannot create directory ‘/var/lib/postgresql/data’: Permission denied

J'y travaille.

Donc, fondamentalement, nous devrons tirer parti d'une approche `initContainer pour corriger l'autorisation afin que la base de données puisse être créée. Cet extrait fera le travail :

diff --git a/roles/installer/tasks/database_configuration.yml b/roles/installer/tasks/database_configuration.yml
index 2e99be5..470530a 100644
--- a/roles/installer/tasks/database_configuration.yml
+++ b/roles/installer/tasks/database_configuration.yml
@@ -80,8 +80,9 @@
 - block:
     - name: Create Database if no database is specified
       k8s:
-        apply: true
+        apply: yes
         definition: "{{ lookup('template', 'postgres.yaml.j2') }}"
+        wait: yes
       register: create_statefulset_result

   rescue:
diff --git a/roles/installer/templates/postgres.yaml.j2 b/roles/installer/templates/postgres.yaml.j2
index d17ee12..f87c842 100644
--- a/roles/installer/templates/postgres.yaml.j2
+++ b/roles/installer/templates/postgres.yaml.j2
@@ -37,10 +37,27 @@ spec:
       imagePullSecrets:
         - name: {{ image_pull_secret }}
 {% endif %}
+      initContainers:
+        - name: init-chmod-data
+          image: '{{ postgres_image }}:{{ postgres_image_version }}'
+          imagePullPolicy: '{{ image_pull_policy }}'
+          command:
+            - /bin/sh
+            - -c
+            - |
+              if [ ! -f {{ postgres_data_path }}/PG_VERSION ]; then
+                chown postgres:root {{ postgres_data_path | dirname }}
+              fi
+          volumeMounts:
+            - name: postgres
+              mountPath: '{{ postgres_data_path | dirname }}'
+              subPath: '{{ postgres_data_path | dirname | basename }}'
       containers:
         - image: '{{ postgres_image }}:{{ postgres_image_version }}'
           imagePullPolicy: '{{ image_pull_policy }}'
           name: postgres
+          securityContext:
+            fsGroup: 999
           env:
             # For postgres_image based on rhel8/postgresql-12
             - name: POSTGRESQL_DATABASE

Il en résulte un état de fonctionnement une fois le correctif appliqué :

$ ubectl get pods -w                              00:38:58
NAME                            READY   STATUS    RESTARTS   AGE
awx-operator-5bc776b4d4-d9ww2   1/1     Running   0          4m41s
awx-postgres-0                  1/1     Running   0          4m3s
awx-d67898cd9-k6jrc             4/4     Running   0          3m48s

$ kubectl iexec awx-postgres-0 /bin/bash                                                                                                                         00:57:00
root@awx-postgres-0:/# namei  -xmolv /var/lib/postgresql/data/pgdata/
f: /var/lib/postgresql/data/pgdata/
Drwxr-xr-x root     root     /
drwxr-xr-x root     root     var
drwxr-xr-x root     root     lib
drwxr-xr-x postgres postgres postgresql
Drwx------ postgres root     data
drwx------ postgres root     pgdata

Je vais créer un PR pour cela. Merci d'avoir signalé le problème @flisak-robert et @scott-vick

Existe-t-il une solution temporaire avant la mise à jour ?

Existe-t-il une solution temporaire avant la mise à jour ?

Je ne sais pas si cela répond à vos besoins, mais je viens d'exécuter postgres dans un conteneur docker et j'ai indiqué à awx d'utiliser cette instance postgres à la place.
Voici ma config :

apiVersion: v1
kind: Secret
metadata:
  name: awx-postgres-configuration
  namespace: awx
stringData:
  host: <postgres address>
  port: "5432"
  database: awx
  username: postgres
  password: <postgres password>
  type: unmanaged
type: Opaque

N'oubliez pas d'inclure postgres_configuration_secret: awx-postgres-configuration dans votre configuration awx. Si vous ne le faites pas, AWX ne pourra pas déchiffrer les éléments de votre base de données postgres lorsque vous redémarrerez votre nœud awx par exemple. J'y ai été, j'ai fait ça :(

Une solution de contournement que j'ai trouvée consiste à créer un PV :

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: <className>
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "<path>"

Le chemin recevra l'autorisation 777 via chmod.
Vous devez ajouter l'attribut postgres_storage_class avec la même valeur que le storageClassName.

Cette page vous a été utile?
0 / 5 - 0 notes

Questions connexes

eniomachado picture eniomachado  ·  11Commentaires

nicolaibaralmueller picture nicolaibaralmueller  ·  4Commentaires

bandwiches picture bandwiches  ·  13Commentaires

bhundven picture bhundven  ·  15Commentaires

crouthamela picture crouthamela  ·  4Commentaires