Grant host access permission to Elastic Agent

Deploying Elastic Agent on Openshift may require additional permissions depending on the type of integration Elastic Agent is supposed to run. In any case, Elastic Agent uses a hostPath volume as its data directory on OpenShift to maintain a stable identity. Therefore, the Service Account used for Elastic Agent needs permissions to use hostPath volumes.

Elastic Agent must also run as root with a privileged container to write to this directory. For this, add the Service Account to the privileged Security Context Constraint (SCC).

The following example assumes that Elastic Agent is deployed in the Namespace elastic with the ServiceAccount elastic-agent. You can replace these values according to your environment.

Note

If you used the examples from the recipes directory, the ServiceAccount may already exist.

  1. Create a dedicated ServiceAccount:

    oc create serviceaccount elastic-agent -n elastic
    		
  2. Add the ServiceAccount to the privileged SCC:

    oc adm policy add-scc-to-user privileged -z elastic-agent -n elastic
    		
  3. Update the Elastic Agent manifest to use the new ServiceAccount, for example:

    apiVersion: agent.k8s.elastic.co/v1alpha1
    kind: Agent
    metadata:
      name: my-agent
    spec:
      version: 9.5.7
      daemonSet:
        podTemplate:
          spec:
            serviceAccountName: elastic-agent
            securityContext:
              runAsUser: 0
            containers:
            - name: agent
              securityContext:
                privileged: true
    		
    1. Runs the container as root. The hostaccess SCC rejects Pods that run as root.
    2. Runs the container as privileged. Without it, SELinux denies the container access to the data directory on the node, and Elastic Agent fails to start with an error similar to mkdir /usr/share/elastic-agent/state/data: permission denied.