Ansible: Prometheus를 사용하여 새로 생성된 Amazon EC2 인스턴스를 자동으로 모니터링하는 방법은 무엇입니까?
게시 됨: 2021-12-19
아래 질문이 있습니까?
- 파일 기반 서비스 검색을 사용하여 스크랩 대상 검색
- 여러 IP의 사용을 자동으로 검색하는 방법은 무엇입니까?
- EC2 인스턴스 자동 모니터링
- Prometheus로 모니터링
- Prometheus를 사용하여 새로 생성된 Amazon EC2 인스턴스를 모니터링하시겠습니까?
- Amazon EC2 VM을 생성한 후 Prometheus 대상 IP 목록 파일을 동적으로 업데이트하는 방법은 무엇입니까?
Ansible은 의심할 여지 없이 업계 최고의 운영 자동화 도구입니다. Crunchify에서는 다양한 주제에 대한 12개 이상의 Ansible 기사를 게시했습니다.
이 자습서에서는 Amazon EC2 VM을 동적으로 생성한 후 Prometheus 대상 IP 목록 파일을 업데이트하는 방법에 대해 설명합니다.
다음 시나리오를 고려하십시오.
- 프로덕션 인프라를 모니터링하기 위해 Prometheus를 실행하고 있습니다.
- 자세한 내용은 Prometheus 설정 튜토리얼을 따르십시오.
- ansible spawn 5개의 새로운 Amazon EC2 VM 사용
- 새로운 Amazon EC2 인스턴스 생성 자습서를 완전히 따르십시오.
- 5개의 IP를 얻고 런타임에 [crunchify] 그룹 업데이트
- 가능한 호스트 파일 업데이트 그룹 자습서를 완전히 따르십시오.
- 업데이트된 IP로
crunchify_prometheus.txt
파일 업데이트 - 프로메테우스가 실행 중인 새 호스트에 파일 푸시
- Prometheus는 이 새로
updated IP file
을 동적으로 읽습니다. - 모든 새
hosts
가 자동으로 모니터링됩니다.
4~7단계의 경우 여기에서 모든 단계를 살펴보겠습니다.

세부 단계:
- 그룹
[Crunchify]
아래의 가능한 호스트 파일에서 모든 IP 목록을 가져옵니다. - 테스트 목적으로만 모든 IP를
crunchify.txt
파일에 추가합니다. 다음 단계에서는 이 파일을 사용하지 않습니다. - 기존 crunchify_prometheus.json 파일을 삭제합니다.
- 샘플 새 prometheus IP 목록 파일로 새
crunchify_prometheus.json
파일을 만듭니다. -
iplist
파일의crunchify_prometheus.json
를 IP 목록으로 바꿉니다. -
u'
를'
로 바꿉니다. -
'
를"
로 바꿉니다. - Prometheus 프로세스가 실행 중인 원격 호스트에서 기존 crunchify_prometheus.json 파일을 삭제합니다.
- prometheus가 사용할 수 있도록 crunchify_prometheus.json 파일을 로컬 호스트에서 원격 호스트로 복사합니다.
crunchify_prometheus_file_update.yml 파일.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
--- - name : Update Prometheus Targets IP List file after creating Amazon EC2 VMs Dynamically hosts : local connection : local gather_facts : True tasks : - name : Get list of all IPs from group Crunchify set_fact : nodelist = "{{ groups['Crunchify'] }}" - name : Add all IPs to file crunchify . txt just for testing purpose . We are not going to use this file in next steps . lineinfile : path : "crunchify.txt" line : "{{ nodelist }}" - name : Delete existing crunchify_prometheus . json file ignore_errors : yes shell : rm crunchify_prometheus . json - name : Create new crunchify_prometheus . json file with sample new prometheus IP list file . copy : dest : "crunchify_prometheus.json" content : [ { "targets" : iplist , "labels" : { "job" : "crunchify_prometheus_job" } } ] - name : Replace iplist from file crunchify_prometheus . json with list of IPs replace : path : "crunchify_prometheus.json" regexp : '"iplist"' replace : "{{ nodelist }}" - name : Replace u ' with ' replace : path : "crunchify_prometheus.json" regexp : 'u' ' replace: ' '' - name : Replace ' with " replace: path: "crunchify_prometheus.json" regexp: "' " replace: " \ "" - hosts : linode become : yes tasks : - name : Delete existing crunchify_prometheus . json file from remote host where Prometheus process is running . ignore_errors : yes command : rm / root / crunchify / crunchify_prometheus . json - name : Copy file crunchify_prometheus . json from local host to remote host so prometheus can consume it . copy : src = { { item . src } } dest = { { item . dest } } with_items : - { src : '//cdn.crunchify.com/Users/crunchify/Documents/ansible/crunchify_prometheus.json' , dest : '/root/crunchify/' } |
Ansible 플레이북 실행:
1 |
ansible - playbook - v - i . / hosts crunchify_prometheus_file_update . yml |
Ansible 플레이북 결과:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
bash Crunchify $ ansible - playbook - v - i . / hosts crunchify_prometheus_file_update . yml WARNING : Executing a script that is loading libcrypto in an unsafe way . This will fail in a future version of macOS . Set the LIBRESSL_REDIRECT_STUB_ABORT = 1 in the environment to force this into an error . No config file found ; using defaults / Users / crunchify / Documents / ansible / hosts did not meet host_list requirements , check plugin documentation if this is unexpected / Users / crunchify / Documents / ansible / hosts did not meet script requirements , check plugin documentation if this is unexpected PLAY [ Update Prometheus Targets IP List file after creating Amazon EC2 VMs Dynamically ] ************************************************************************ TASK [ Gathering Facts ] ***************************************************************************************************************************************** ok : [ localhost ] TASK [ Get list of all IPs from group Crunchify ] ******************************************************************************************************************* ok : [ localhost ] = > { "ansible_facts" : { "nodelist" : [ "5.17.14.87" , "8.17.24.26" , "96.17.67.11" , "62.17.64.87" ] } , "changed" : false } TASK [ Add all IPs to file crunchify . txt just for testing purpose . We are not going to use this file in next steps . ] ******************************************** ok : [ localhost ] = > { "backup" : "" , "changed" : false , "msg" : "" } TASK [ Delete existing crunchify_prometheus . json file ] ********************************************************************************************************** [ WARNING ] : Consider using the file module with state = absent rather than running 'rm' . If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible . cfg to get rid of this message . changed : [ localhost ] = > { "changed" : true , "cmd" : "rm crunchify_prometheus.json" , "delta" : "0:00:00.004614" , "end" : "2019-08-31 19:05:28.214368" , "rc" : 0 , "start" : "2019-08-31 19:05:28.209754" , "stderr" : "" , "stderr_lines" : [ ] , "stdout" : "" , "stdout_lines" : [ ] } TASK [ Create new crunchify_prometheus . json file with sample new prometheus IP list file . ] ********************************************************************** changed : [ localhost ] = > { "changed" : true , "checksum" : "0f561a2aedb23d9b6e76a93af377cf0128c9f4c6" , "dest" : "./crunchify_prometheus.json" , "gid" : 20 , "group" : "staff" , "md5sum" : "6a8d4a45c6b38bd6e664ce1cdc7f001a" , "mode" : "0644" , "owner" : "Crunchify" , "size" : 70 , "src" : "/Users/crunchify/.ansible/tmp/ansible-tmp-1567296328.29-229442409470865/source" , "state" : "file" , "uid" : 502 } TASK [ Replace iplist from file crunchify_prometheus . json with list of IPs ] ************************************************************************************* changed : [ localhost ] = > { "changed" : true , "msg" : "1 replacements made" } TASK [ Replace u ' with ' ] *************************************************************************************************************************************** ok : [ localhost ] = > { "changed" : false , "msg" : "" } TASK [ Replace ' with "] **************************************************************************************************************************************** changed: [localhost] => {"changed": true, "msg": "8 replacements made"} PLAY [linode] ************************************************************************************************************************************************** TASK [Gathering Facts] ***************************************************************************************************************************************** ok: [11.33.64.98] TASK [Delete existing crunchify_prometheus.json file from remote host where Prometheus process is running.] **************************************************** changed: [11.33.64.98] => {"changed": true, "cmd": ["rm", "//cdn.crunchify.com/root/crunchify/crunchify_prometheus.json"], "delta": "0:00:00.002704", "end": "2019-09-01 00:05:32.687400", "rc": 0, "start": "2019-09-01 00:05:32.684696", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []} TASK [Copy file crunchify_prometheus.json from local host to remote host so prometheus can consume it.] ******************************************************** changed: [11.33.64.98] => (item={u' dest ': u' / root / crunchify / ', u' src ': u' / Users / crunchify / Documents / ansible / crunchify_prometheus . json ' } ) = > { "changed" : true , "checksum" : "ecea3eafaf9ba3e66698e0851d2ee2513d9fcd5f" , "dest" : "//cdn.crunchify.com/root/crunchify/crunchify_prometheus.json" , "gid" : 0 , "group" : "root" , "item" : { "dest" : "/root/crunchify/" , "src" : "//cdn.crunchify.com/Users/crunchify/Documents/ansible/crunchify_prometheus.json" } , "md5sum" : "8b8f073e30b180c63341cd3fd4c47974" , "mode" : "0644" , "owner" : "root" , "size" : 122 , "src" : "/root/.ansible/tmp/ansible-tmp-1567296332.67-196855844989682/source" , "state" : "file" , "uid" : 0 } PLAY RECAP * **************************************************************************************************************************************************** 11.33.64.98 : ok = 3 changed = 2 unreachable = 0 failed = 0 localhost : ok = 8 changed = 4 unreachable = 0 failed = 0 |
그게 다야 축하합니다. 업데이트된 새 파일을 새 호스트에 성공적으로 복사했으며 이제 Prometheus가 새로 생성된 Amazon EC2 인스턴스를 자동으로 모니터링합니다.

무엇 향후 계획?
Linux에 install docker
하는 방법에 대한 자습서를 확인하십시오.