$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
86205a98ec4d quay.io/prometheus/prometheus:v2.33.0 "/bin/prometheus --c…" 14 seconds ago Up 12 seconds prometheus
4. docker로 설치한 Prometheus의 관리 스크립트 내용
4.1. Prometheus.yml 파일 수정 후 Prometheus의 yaml 파일 내용 적용
```
$ source /etc/prometheus/cmd/Restart_Prometheus.sh
# 코드 내용
#! /bin/bash
container_id=`docker ps -a | grep -i prometheus | awk -F ' ' '{print $1}'`
docker restart $container_id
```
4.2. prometheus의 로그 삭제
```
$ source /etc/prometheus/cmd/Log_Clear.sh
# 코드 내용
#! /bin/bash
container_id=`docker ps -a | grep -i prometheus | awk -F ' ' '{print $1}'`
Log_file=$(docker inspect --format='{{.LogPath}}' "$container_id")
log_file_volume=`ls -al $Log_file | awk -F ' ' '{print $5}'`
if [[ $log_file_volume == 0 ]]; then
echo "Log File is alrealy zero"
else
truncate -s 0 $Log_file
echo "Prometheus Log Clear"
fi
```
4.3. prometheus의 로그 보기
```
$ source /etc/prometheus/cmd/View_Log.sh
# 코드 내용
#! /bin/bash
container_id=`docker ps -a | grep -i prometheus | awk -F ' ' '{print $1}'`
docker logs -f $container_id
```
RedHat 계열의 CentOS 서버에서 Prometheus를 설치하고 systemd로 관리
Security Group, Firewall 등으로 9090번 포트에 대한 방화벽 해제가 필요
1. Prometheus 설치
설치하고 싶은 Prometheus 버전의 경로를 잘 확인하여 다운받으면 됨
systemd를 사용하지 않고 /root/apps/prometheus-2.22.0.linux-amd64/pometheus를 실행해도 Prometheus 사용 가능
$ pwd
/root
# 설치하는 컴포넌트들의 관리를 더 쉽게 하기 위해서 디렉토리 생성
$ mkdir apps
# 디렉토리 이동
$ cd /root/apps
# Prometheus 바이너리 파일이 들어 있는 압축 파일 설치
$ wget https://github.com/prometheus/prometheus/releases/download/v2.22.0/prometheus-2.22.0.linux-amd64.tar.gz
# 설치한 Prometheus 바이너리 파일 확인
$ ls
prometheus-2.22.0.linux-amd64.tar.gz
# 압축 파일 해제
$ tar zxvf prometheus-2.22.0.linux-amd64.tar.gz
prometheus-2.22.0.linux-amd64/
prometheus-2.22.0.linux-amd64/NOTICE
prometheus-2.22.0.linux-amd64/prometheus
prometheus-2.22.0.linux-amd64/consoles/
prometheus-2.22.0.linux-amd64/consoles/node-cpu.html
prometheus-2.22.0.linux-amd64/consoles/prometheus-overview.html
prometheus-2.22.0.linux-amd64/consoles/node.html
prometheus-2.22.0.linux-amd64/consoles/node-overview.html
prometheus-2.22.0.linux-amd64/consoles/index.html.example
prometheus-2.22.0.linux-amd64/consoles/prometheus.html
prometheus-2.22.0.linux-amd64/consoles/node-disk.html
prometheus-2.22.0.linux-amd64/console_libraries/
prometheus-2.22.0.linux-amd64/console_libraries/prom.lib
prometheus-2.22.0.linux-amd64/console_libraries/menu.lib
prometheus-2.22.0.linux-amd64/promtool
prometheus-2.22.0.linux-amd64/LICENSE
prometheus-2.22.0.linux-amd64/prometheus.yml
2. Prometheus를 서비스로 생성하여 관리 → prometheus 실행을 systemd로 관리
Prometheus를 리눅스 systemd를 통해 서비스로 등록
$ pwd
/root/apps/prometheus-2.22.0.linux-amd64
# 디렉토리 프로비저닝
$ useradd --no-create-home --shell /bin/false prometheus
# 생성한 prometheus 계쩡 확인
$ cat /etc/passwd | grep prometheus
prometheus:x:1001:1001::/home/prometheus:/bin/false
# prometheus를 관리할 디렉토리 생성 및 명령어 복사
$ mkdir /etc/prometheus
$ mkdir /var/lib/prometheus
$ cp ./prometheus /usr/local/bin/
$ cp ./promtool /usr/local/bin/
$ cp ./prometheus.yml /etc/prometheus/
$ cp -r ./consoles /etc/prometheus
$ cp -r ./console_libraries /etc/prometheus
# 명령어 복사 확인
$ ls /usr/local/bin/
prometheus promtool
# prometheus 관련 내용 복사
$ ls /etc/prometheus
console_libraries consoles
# 유저:그룹 설정 -> prometheus 사용자 계정이 관리할 수 있도록 권한 변경
$ chown prometheus:prometheus /etc/prometheus
$ chown prometheus:prometheus /var/lib/prometheus
$ chown prometheus:prometheus /usr/local/bin/prometheus
$ chown prometheus:prometheus /usr/local/bin/promtool
$ chown -R prometheus:prometheus /etc/prometheus/consoles
$ chown -R prometheus:prometheus /etc/prometheus/console_libraries
# 서비스 파일 등록
$ cat << EOF | tee /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
EOF
서비스 데몬 리로딩
$ systemctl daemon-reload
prometheus 서비스 시작
$ systemctl start prometheus
prometheus 서비스 상태 확인
$ systemctl status prometheus
● prometheus.service - Prometheus Server
Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2021-11-20 02:55:12 KST; 11h ago
Main PID: 11780 (prometheus)
Tasks: 14
Memory: 48.4M
CGroup: /system.slice/prometheus.service
└─11780 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/prometheus/console_...
Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.377Z caller=head.go:889 component=tsdb msg="WAL checkpoint complete" first=2 last=3 duration=29.052021ms
Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.425Z caller=compact.go:440 component=tsdb msg="compact blocks" count=3 mint=1637344800000 maxt=1637366400000 ulid=01FMXMJQ5RJ914...ration=41.7039ms
Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.431Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMX6V8HX8WMN15F7H841AKH0
Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.432Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMXDPZSXA1G1VCYYE6JTVAQN
Nov 20 12:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T03:00:03.433Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMWZZH9W1MH7PN1NZBT06CG9
Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.321Z caller=compact.go:494 component=tsdb msg="write block" mint=1637373600000 maxt=1637380800000 ulid=01FMXVEE9X21CFW66VFRRZNAK...tion=59.826896ms
Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.329Z caller=head.go:809 component=tsdb msg="Head GC completed" duration=2.347334ms
Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.386Z caller=compact.go:440 component=tsdb msg="compact blocks" count=2 mint=1637344458250 maxt=1637366400000 ulid=01FMXVEEC8WEK8...tion=50.157896ms
Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.393Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMXMJQ5RJ914MKYP3J4QZ7WT
Nov 20 14:00:03 [서버 IP] prometheus[11780]: level=info ts=2021-11-20T05:00:03.394Z caller=db.go:1152 component=tsdb msg="Deleting obsolete block" block=01FMWZNF1BR2H33QNDPE91K04T
Hint: Some lines were ellipsized, use -l to show in full.
$ wget -P /usr/bin https://dl.min.io/client/mc/release/linux-amd64/mc
$ chmod +x /usr/bin/mc
# 기본 버전 확인
$ mc --version
mc version RELEASE.2022-02-02T02-03-24Z
2. 기본 커맨드
Minio가 의도한 대로 작동하는지 확인 → http://[MinIO IP]:9000/
자격 증명을 입력
Access Key (액세스 키) : minio
Secret Key (비밀 키) : melovethanos
2.1. access key와 secret key를 MinIO client 등록
mc 모든 구성 정보를 ~/.mc/config.json파일에 저장
는 단순히 클라우드 스토리지 서비스의 짧은 이름
S3 엔드포인트, 액세스 및 비밀 키는 클라우드 스토리지 제공업체에서 제공
# set access key / secret key
# 형식 : mc alias set my-storage http://192.168.0.30:9001 ROOT_ACCESS_KEY_CHANGE_ME SECRET_ACCESS_KEY_CHANGE_ME
$ mc alias set hippo http://[MinIO IP]:9000 minio melovethanos
mc: Configuration written to `/root/.mc/config.json`. Please update your access credentials.
mc: Successfully created `/root/.mc/share`.
mc: Initialized share uploads `/root/.mc/share/uploads.json` file.
mc: Initialized share downloads `/root/.mc/share/downloads.json` file.
Added `hippo` successfully.
2.2. MinIO client로 bucket 생성
## bucket 생성
$ mc mb hippo/mctest
Bucket created successfully `hippo/mctest`.
2.3. MinIO client로 bucket 리스트 확인
## bucket 리스트 확인
$ mc ls hippo
[2022-02-03 01:22:49 KST] 0B hello/
[2022-02-04 02:55:46 KST] 0B mctest/
[2022-02-04 02:00:24 KST] 0B thanos/
2.4. MinIO client로 bucket에 파일 및 디렉토리 업로드
# upload
# 형식 : mc cp SOURCE ALIAS/PATH
# 파일 복사
$ mc cp ./Prometheus_Restart.sh hippo/hello/Prometheus_Restart.sh
..._Restart.sh: 115 B / 115 B ┃▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┃ 12.78 KiB/s 0s
# 성공적으로 파일 upload 확인
$ mc ls hippo/hello
[2022-02-04 03:04:44 KST] 115B STANDARD Prometheus_Restart.sh
# 디렉토리 복사
$ mc cp --recursive ./test hippo/hello/
..._Restart.sh: 578 B / 578 B ┃▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┃ 58.30 KiB/s 0s
# 성공적으로 디렉토리 upload 확인
$ mc ls hippo/hello/test
[2022-02-04 03:13:27 KST] 348B STANDARD Prometheus_Log_Clear.sh
[2022-02-04 03:13:27 KST] 115B STANDARD Prometheus_Log_View.sh
[2022-02-04 03:13:27 KST] 115B STANDARD Prometheus_Restart.sh
2.5. MinIO client로 bucket에 파일 및 디렉토리 삭제
# 디렉토리 내 리스트 확인
$ mc ls hippo/hello/test
[2022-02-04 03:13:27 KST] 348B STANDARD Prometheus_Log_Clear.sh
[2022-02-04 03:13:27 KST] 115B STANDARD Prometheus_Log_View.sh
[2022-02-04 03:13:27 KST] 115B STANDARD Prometheus_Restart.sh
# 형식 : mc rm ALIAS/PATH
# 파일 삭제
$ mc rm hippo/hello/test/Prometheus_Restart.sh
Removing `hippo/hello/test/Prometheus_Restart.sh`.
# 성공적으로 파일 삭제 확인
$ mc ls hippo/hello/test
[2022-02-04 03:13:27 KST] 348B STANDARD Prometheus_Log_Clear.sh
[2022-02-04 03:13:27 KST] 115B STANDARD Prometheus_Log_View.sh
# 형식 : mc rm --recursive --force ALIAS/PATH
# 디렉토리 삭제
$ mc rm --recursive --force hippo/hello/test
Removing `hippo/test/Prometheus_Log_Clear.sh`.
Removing `hippo/test/Prometheus_Log_View.sh`.
# 성공적으로 디렉토리 삭제 확인 -> hippo/hello/test 디렉토리 없음을 확인
$ mc ls hippo/hello
[2022-02-04 03:04:44 KST] 115B STANDARD Prometheus_Restart.sh
2.6. minio client로 bucket에 파일 다운로드 → 업로드와 위치만 변경
# download
# 형식 : mc cp my-storage/test.zip ~/test.zip
#위 cp 예제에서 source/target을 위치를 바꿈.
$ mc cp hippo/hello/Prometheus_Restart.sh /Prometheus_Restart.sh
..._Restart.sh: 115 B / 115 B ┃▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┃ 14.64 KiB/s 0s
# / 디렉토리 아래에 Prometheus_Restart.sh 파일 존재 확인
$ ls /Prometheus_Restart.sh
Prometheus_Restart.sh
3. 공유 파일 → 특정 파일을 특정일만 공유
공유기간을 설정후 카피 버튼을 클릭해서 복사후 공유
파일을 공유하기 위해 카피 버튼 클릭
Copy Link를 통해 접근 URL을 획득
4. 일반 웹서버처럼 공유 → 공유 기간을 무한정으로 설정
4.1. 버킷 자체를 public으로 설정
# 형식 : mc policy set public my-storage/test_bucket
$ mc policy set public hippo/hello
Access permission for `hippo/hello` is set to `public`
4.2. 버킷의 policy를 확인
$ mc policy get hippo/hello
Access permission for `hippo/hello` is `public`
4.3. 다운로드 테스트 → minio에서의 웹사이트 주소에는 minio client의 alias인 hippo는 제외
# minio 서버에 bucket 디렉토리가 바로 뒤로 붙음
$ wget http://[MinIO IP]:9000/hello/Prometheus_Restart.sh
--2022-02-04 03:33:43-- http://[MinIO IP]:9000/hello/Prometheus_Restart.sh
Connecting to [MinIO IP]:9000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 115 [application/x-sh]
Saving to: ‘Prometheus_Restart.sh’
100%[===============================================>] 115 --.-K/s in 0s
2022-02-04 03:33:43 (7.72 MB/s) - ‘Prometheus_Restart.sh’ saved [115/115]
# MinIO생성
$ kubectl apply -f minio_install.yml
namespace/minio created
persistentvolume/minio-local-persistent-volume created
persistentvolumeclaim/minio-local-persistent-volume-claim created
deployment.apps/minio-deployment created
service/minio-service-headless created
service/minio-loadbalancer-service created
# 생성 확인 → 내용이 많아 생략함
$ kubectl get -n minio all -o wide
# PV 생성 확인
$ kubectl get -n minio persistentvolume
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
minio-local-persistent-volume 6900G RWX Retain Bound minio/minio-local-persistent-volume-claim minio-local-storage 92s
# PVC 생성 확인
$ kubectl get -n minio persistentvolumeclaims
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
minio-local-persistent-volume-claim Bound minio-local-persistent-volume 6900G RWX minio-local-storage 117s
4. MinIO 서비스의 Service IP를 통해 생성 확인 → Access Denied로 접근 불가
minio에 curl로 접근하면 Access Denied됨으로 다른 방법으로 접근
# clusterIP 서비스를 통해 접근 가능
$ curl 20.10.118.211:9000
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied.</Message><Resource>/</Resource><RequestId>16E0270B55D8E06A</RequestId><HostId>c2659a6e-1046-4fd9-8793-e756fbc033a2</HostId></Error>
# LoadBalancer 할당 IP 서비스를 통해 접근 가능
$ curl [LoadBalancer IP]:9000
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied.</Message><Resource>/</Resource><RequestId>16E026EBFCC13E44</RequestId><HostId>c2659a6e-1046-4fd9-8793-e756fbc033a2</HostId></Error>
5. MinIO 서비스에 Python으로 접근하여 확인
curl로 접근할 수 없기에 MinIO Client로 접근
# minio 테스트에 사용할 파이썬3과 minio 패키지 설치
$ yum install -y python3
$ pip3 install minio
# minio 접속 가능 테스트
$ vi minio_api.py
#!/bin/python3
from minio import Minio
if __name__ == '__main__':
minioIP = "20.10.180.199:9000" # serviceIP
minioAccessKey = "minio"
minioSecretKey = "melovethanos"
client = Minio(minioIP, minioAccessKey, minioSecretKey, secure=False)
print(client)
# minio 접속 가능 테스트 실행 -> Minio Object 출력 시 이상 없음
$ python3 minio_api.py
<minio.api.Minio object at 0x7f2740e873c8>
3. minio logrotate를 실행할 /etc/crontab 설정 및 logroate 적용
# logrotate 디버그
$ /usr/sbin/logrotate -d /etc/logrotate.d/minio
Allocating hash table for state file, size 15360 B
Handling 1 logs
rotating pattern: /var/log/minio/*.log weekly (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/minio/minio.log
log does not need rotating (log has been already rotated)not running postrotate script, since no logs were rotated
# logrotate 적용
$ /usr/sbin/logrotate -f /etc/logrotate.d/minio
# 압축 파일 생성 확인
$ ls /var/log/minio/
minio.log minio.log.1.gz
# /etc/crontab 적용
# 매주 일요일 자정 logrotate 실행
$ vi /etc/crontab
00 00 * * 7 /usr/sbin/logrotate -f /etc/logrotate.d/minio
# log 확인
$ cat /var/lib/logrotate/logrotate.status | grep minio
"/var/log/minio/minio.log" 2022-2-6-0:9:7