본문 바로가기
IT/도커

[docker] Prometheus / Grafana - err="open data/queries.active: permission denied" 에러 해결 (SELinux)

by 한동두 2024. 4. 26.
반응형

나는 Prometheus도 Grafana도 뭔지 모른다.

그냥 Prometheus 올리는 컨테이너 하나, Grafana 올리는 컨테이너 하나씩을 만들라는 명령을 받았을 뿐.....

 

그냥 run만 하면 되는 것 뿐인데, 도저히 안된다. 진짜 돌아버리는 줄 알았다.

몇날 며칠 고생하다 오늘 맘잡고 구글링만 대략 7시간 해서 해결했다.

이걸 보는 다른 분들도.. 내 글을 보고 문제를 해결하는데 도움이 되었으면 좋겠다.

(바쁘신분들은 아래로 내려 바로 3. 최종 해결 방법(성공) 을 봐주세요!)

 

 

1. 문제 상황

먼저 나의 문제였던 나의 run 코드이다.

docker run -it -d -p 29093(임의의 포트번호):9090 -v /host/path/prometheus/data:/prometheus --name containername image/prometheus:1.0(version)

-v HostPath:DockerPath 처럼 마운트를 해준 상황이다. 그런데,

 

ts=2024-04-26T03:04:51.645Z caller=main.go:573 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2024-04-26T03:04:51.646Z caller=main.go:617 level=info msg="Starting Prometheus Server" mode=server version="(version=2.51.2, branch=HEAD, revision=b4c0ab52c3e9b940ab803581ddae9b3d9a452337)"
ts=2024-04-26T03:04:51.646Z caller=main.go:622 level=info build_context="(go=go1.22.2, platform=linux/amd64, user=root@b63f02a423d9, date=20240410-14:05:54, tags=netgo,builtinassets,stringlabels)"
ts=2024-04-26T03:04:51.646Z caller=main.go:623 level=info host_details="(Linux 3.10.0-1160.108.1.el7.x86_64 #1 SMP Thu Jan 25 16:17:31 UTC 2024 x86_64 7e835e3104f0 (none))"
ts=2024-04-26T03:04:51.646Z caller=main.go:624 level=info fd_limits="(soft=1048576, hard=1048576)"
ts=2024-04-26T03:04:51.646Z caller=main.go:625 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2024-04-26T03:04:51.647Z caller=query_logger.go:93 level=error component=activeQueryTracker msg="Error opening query log file" file=/prometheus/data/queries.active err="open data/queries.active: permission denied"
panic: Unable to create mmap-ed active query log

goroutine 1 [running]:
github.com/prometheus/prometheus/promql.NewActiveQueryTracker({0x37f733c, 0x5}, 0x14, {0x42af2c0, 0xc0001850e0})
        /app/promql/query_logger.go:123 +0x425
main.main()
        /app/cmd/prometheus/main.go:748 +0x82fe

err="open data/queries.active: permission denied"가 나기 시작했다.

 

 


2. 내가 했던 해결방법들 (전부 실패)

이걸 해결하기 위해서 내가 한 것은

 

1. chmod 수정

chmod -R 777 /host/path/prometheus

이거 하고 다시 같은 코드로 run했는데 역시 permission denied에러.

 

 

2. chown 수정

https://access.redhat.com/solutions/7004453

 

The user-defined Prometheus pods cannot mount hostpath - Red Hat Customer Portal

After installing prometheus via enabling monitoring and using hostpath directory as persistent volume, as below message which printed by prometheus-user-workload pods you can see: [root@bastion prometheus-hostpath]# oc logs -f prometheus-user-workload-0 -c

access.redhat.com

 

이 사이트를 발견하고 chown을 해보라 해서 chown에 대해서 좀 알아봤다.

//도커 컨테이너에 접속
docker exec -it containername /bin/sh

//도커 안에있는 prometheus.yml 확인
cd /etc/prometheus
ls -al

total 4
drwxr-xr-x    1 nobody   nobody          28 Apr 10 14:30 .
drwxr-xr-x    1 root     root            24 Apr 26 06:56 ..
lrwxrwxrwx    1 nobody   nobody          39 Apr 10 14:30 console_libraries -> /usr/share/prometheus/console_libraries
lrwxrwxrwx    1 nobody   nobody          31 Apr 10 14:30 consoles -> /usr/share/prometheus/consoles/
-rw-r--r--    1 nobody   nobody         937 Apr 26 07:38 prometheus.yml

 

prometheus.yml의 소유자명, 그룹명이 nobody임을 확인했고, 그래서 나는

chown -R 65534:65534 /host/path/prometheus

chown 한 상태로 run했으나 역시나 permission denied에러가 났다.

 

 

3. SELinux - chcon, container_file_t

저 위 사이트에서 말한대로

chcon -t container_file_t /host/path/prometheus/data

chcon을 따라 해보았으나 

chcon -R -t httpd_sysrw_content_t /KMA/file/prometheus/data 해봤는데 
on: failed to change context of `chunks_head' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `chunks_head' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `queries.active' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `00000001' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `00000000' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `wal' to `unconfined_u:object_r:httpd_sysrw_content_t:s0':  명령을 허용하지 않음
chcon: failed to change context of `data' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `queries.active' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `00000000' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `00000001' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음
chcon: failed to change context of `wal' to `unconfined_u:object_r:httpd_sysrw_content_t:s0':  명령을 허용하지 않음
chcon: failed to change context of `/KMA/file/prometheus/data' to `unconfined_u:object_r:httpd_sysrw_content_t:s0': 명령을 허용하지 않음

이런식으로... 알수없는 오류가 떴기 때문에 이 방법도 포기.

 

 


3. 최종 해결 방법 (성공)

SELinux - chcon, svirt_sandbox_file_t

이건 챗지피티가 알려줘서 겨우 해결했다..!!

진짜 너무 고맙다

chcon -R -t svirt_sandbox_file_t /host/path/prometheus

-R옵션을 이용해서 prometheus 폴더 하위에있는 모든 파일들의 SELinux권한을 변경해준다.

그러고 나서 맨 처음 하려고 했던 run 코드를 실행시켜주면 개같이 성공할 수 있다.

 

Grafana의 경우에도 동일하게 permission denied라고 뜨는 경우, 
역시 chcon을 이용해서 SELinux권한을 바꿔주면 해결 가능하다.

chmod -R 777 /host/path/grafana
chcon -R -t svirt_sandbox_file_t /host/path/grafana
docker run -it -d -p 23000(임의의 포트번호):3000 -e GF_SECURITY_ADMIN_USER=[id] -e GF_SECURITY_ADMIN_PASSWORD=[pw] -e GF_USERS_ALLOW_SIGN_UP=false -e GF_AUTH_ANONYMOUS_ENABLED=true -e GF_USERS_ALLOW_ORG_CREATE=false -v /host/path/grafana/data:/var/lib/grafana --name containername2 image/grafana:1.0

혹시 몰라서 chmod 777도 해줬다.

(chown은 그냥 그대로 둬도 상관없었다. 나는 기본 소유자, 그룹이 root인 상태였는데 잘 작동했다.)

나는 위와 같은 run코드를 사용해서 실행시켰다. 삽질할것도 없이 바로 성공했다.

 

 


진짜 오늘만해도 오전부터 지금까지 하루종일 삽질했고.....

이거가지고 며칠동안 붙잡고있었는데 드디어 해결하게 되어서 너무 기뻤다. 소리지를 뻔했다.

 

Prometheus, Grafana Run시키느라 고생받고 있는 분들이 꼭 이 글을 보고 도움받고 해결되기를 바란다!!!

 

 

 

 

도움받은 페이지
https://access.redhat.com/solutions/7004453

https://m.blog.naver.com/ijoos/221135254236

 

반응형