saltstack command example

SYSTEM/TECH / /
반응형

Saltstack 특징 / 명령어 예제

 

 

비슷한 종류의 인프라 자동화 관리툴 (devops tools) 들이 많지만 saltstack  지향점은 아래와 같다.

 

" Infrastructure as data, not code "

 

 

 

pupet,chef,ansible :  Infrastructure as Code 

 

특정 언어를 이해해야함 , 구성 및 관리가 복잡함 (사용자별 느끼는 차이는 있을수있음 )

 

saltstack : Infrastructure as Data 

 

 python 으로 작성 되었지만, 특정 언어에 구애받지 않으며 단순하고 사람이 쉽게 읽을 수있는 YAML 파일과 Jinja 템플릿을 사용

 

  saltstack 지향점은 "Infrastructure as Data" 

 

   -> 최대한 덜 복잡하게 (단순하게) 관리하며, 쉽고 빨리 배울수 있게 개발됨

      code가 아닌 data 에 기반한 인프라스트럭처 모델을 지향 (code 가 아닌 data 기반으로 infra management)

 

  DevOps and Web-scale IT require speed, agility and communications. The smaller the learning curve, 

  the bigger competitive advantage available

 

학습 곡선이 작을수록 더 큰 경쟁력과 이점이 있다

 

 

Saltstack Command example 

 

sample 명령어 보기 (Documentation on the system)

salt '*' sys.doc         # output sys.doc (= all documentation) salt '*' sys.doc pkg     # only sys.doc for pkg module salt '*' sys.doc network # only sys.doc for network module salt '*' sys.doc system  # only sys.doc for system module salt '*' sys.doc status  # only sys.doc for status module

 

down 된 minion 찾기 및 자동 key 삭제

 

salt-run manage.down 2>/dev/null | tee /dev/tty | wc -l

salt-run manage.down removekeys=True * down minion 찾아서 key 삭제

 

 

windows memory check 

 

Memory 800MB 이상인 svchost.exe 찾기

 

salt -G roles:tax cmd.run 'tasklist /fi "imagename eq svchost.exe" /fi "memusage gt 819200"' --out=txt | egrep -v "Session|=" > memory.txt

 

 

roles check 

 

salt -C 'not G@roles:aaa and not G@roles:bbb and not G@roles:ddd and not G@roles:eee ' test.ping --out=txt

 

minion 이 자동으로 등록되게 할때 (acceptkey=true) role 이 추가 안된 minion 찾기

 

위와 같이 cron에 걸어서 role이 없는것을 찾아서 -> 파일에 쓰고 -> grep로 특정 문자열 검색해서 ->  grains.append 로 자동으로 추가

 

 

salt-cp 

 

salt-cp  -G roles:aaa /srv/salt/file/minion 'c:\salt\conf\'      <- salt-cp 는 file size 100KB 이하일때 권장 

                                                                                   (그 이상일경우에는 cp.get_file 함수 사용 권고)

salt '*' cp.get_dir salt://windows/consul 'C:\'                      <- 폴더 단위 배포(복사)

salt '*' cp.get_file /srv/windows/consul.exe 'C:\'                  <- 파일단위 배포(복사)

 

windows powershell 실행 

특정 프로세스 시작 시간보기

 

salt -G roles:aaa cmd.run 'Get-Process | select name, starttime | findstr python' shell=powershell --out=txt 

 

hyper-v host CSV mpio 검사

 

salt -G roles:host cmd.run '(gwmi -Namespace root\wmi -Class mpio_disk_info).driveinfo | % {Write-host "Name: $($_.name) Paths: $($_.numberpaths)"}' --out=txt shell=powershell > mpio.list  

 

salt list 

 

salt -L `awk -vORS=, '{ print $1 }' minions.list | sed 's/,$/\n/'`    test.ping

 

특정 minion 리스트를  minions.list 파일에 리스트 저장후 읽어서 명령 실행하기

 

계정 관리 

 

salt -G roles:mgmt user.add user01                  (계정 생성)

salt -G roles:mgmt cmd.run 'chage -d 0 user01'  (최초로그인시 암호 변경)

salt '*' shadow.set_expire username 2017/12/31  (날짜 까지 계정 연장)

 
python -c "import crypt; print crypt.crypt('password', '\$6\$SALTsalt')"

 

salt MGT-XXX shadow.set_password user '위에서 만들어진 password'

 

windows event check 5120 5150 (hyper-v cluster 에러 관련) 

 

salt -G roles:host cmd.run 'Get-WinEvent -FilterHashtable @{logname="system"; id=5120} -MaxEvents 1 | format-table machinename, id, userid, timecreated, message -autosize | Out-String -Width 700' shell=powershell --out=txt 

 

minion restart 

salt '*' cmd.run_bg 'C:\salt\salt-call.bat --local service.restart salt-minion'  (windows)
salt -C 'not G@kernel:Windows' cmd.run_bg 'salt-call --local service.restart salt-minion' (linux)
 
minion config 파일 변경후 restart 할때  필요 , 백그라운드 프로세스로 실행됨
 

salt cp.push 

 salt '*' cp.push /etc/fstab 
 salt '*' cp.push /etc/system-release keep_symlinks=True
 salt '*' cp.push /etc/fstab upload_path='/new/path/fstab'
 salt '*' cp.push /tmp/filename remove_source=True
* minion 의 특정 파일 -> master 서버 ( /var/cache/salt/master/minions/xxx  ) 복사됨  

* master  환경설정 file_recv: True  ,  file_recv_max_size: 100  활성화 해줘야함

* 2016년 3월 버전에서는 안됨
 

전체 minion IP 확인 하기 

salt-run manage.alived show_ipv4=True --out=txt  --out-file=ipaddress.txt

 

PING , Port check 

salt -G roles:xxxx network.ping 8.8.8.8 timeout=3

salt '*' network.connect naver.com 80 timeout=3 family=ipv4

 

 

CPU usage ( % 사용량)

 salt -G roles:xxxxx status.cpuload --out=txt |  awk '{ if ($2~/^[0-9]+$/) print $0 }' | awk '$2 > 95{print $1,$2}'  

                                                                                                                                               

* awk 로 2번째 필드 숫자인것만 (간혹 salt minion이 응답없을때 문자열 error 가 출력됨) 필터                       

  CPU load 가 95% 이상인것만 필터 

 

 

file 내용 변경

salt hostname  file.replace 'c:\xxx\xxxx\xxx.ini' pattern='LOCALIP =' repl='##LOCALIP = 127.0.0.1'

 

 

 

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기