ã¯ããã«
ä»åã¯ãæ¥åã§OpenShift Container Platformï¼OCPï¼ã¯ã©ã¹ã¿å ã®è¤æ°ã®Workerãã¼ããä¸åº¦ã«åæ¢ããæ¹æ³ã«ã¤ãã¦åãçµãã å 容ããä¼ããã¾ããé常ã¯åãã¼ãã«ãã°ã¤ã³ãã¦ãshutdown ã³ãã³ããå®è¡ããã ãã§ããã対象ãã¼ããå¤ãã¨æéããããã¾ãã
ããã§ãããå¹ççãªæ¹æ³ãæ¢ãããã«ChatGPTã使ããã¹ã¯ãªãããå©ç¨ãã¦è¤æ°ãã¼ããã¾ã¨ãã¦ã·ã£ãããã¦ã³ããæé ãèãã¦ã¿ã¾ããã
è¤æ°ã®ãµã¼ãã¼ãã¾ã¨ãã¦åæ¢ããæ¹æ³
ããã³ãã
Linuxç°å¢ã§ããµã¼ãã¼ã«sshã§æ¥ç¶ããshutdown -h nowãå®è¡ãã¾ãã対象ã®ãµã¼ãã¼ã¯8å°ããã¾ããï¼åã®ã³ãã³ãå®è¡ã§ï¼å°ã®ãµã¼ãã¼ãé£ç¶ãã¦shutdownããæ¹æ³ãæãã¦ãã ããã
åç
è¤æ°ã®ãµã¼ãã¼ã1åã®ã³ãã³ãå®è¡ã§é£ç¶ãã¦ã·ã£ãããã¦ã³ããã«ã¯ã以ä¸ã®ããã«Bashã¹ã¯ãªãããå©ç¨ãã¦å®è¡ã§ãã¾ãã
#!/bin/bash # 対象ã®ãµã¼ãã¼ãªã¹ã servers=("server1" "server2" "server3" "server4" "server5" "server6" "server7" "server8") # SSHã§åãµã¼ãã¼ã«æ¥ç¶ããshutdownã³ãã³ããå®è¡ for server in "${servers[@]}"; do ssh user@"$server" "shutdown -h now" done
ã¹ã¯ãªããã®ä½¿ç¨æ¹æ³
- ãã®ã¹ã¯ãªããããã¡ã¤ã«ï¼ä¾ãã°ã
shutdown_servers.sh
ï¼ã«ä¿åãã¾ããå®è¡æ¨©éãä»ä¸ãã¾ãã
chmod +x shutdown_servers.sh
ã¹ã¯ãªãããå®è¡ãã¾ãã
./shutdown_servers.sh
注æç¹
- user ãå®éã®SSHæ¥ç¶ã¦ã¼ã¶ã¼åã«ç½®ãæãã¦ãã ããã
- ãµã¼ãã¼ã®IPã¢ãã¬ã¹ã¾ãã¯ãã¹ãåãæ£ç¢ºã«è¨è¼ããã¦ãããã¨ã確èªãã¦ãã ããã
- SSHæ¥ç¶ã«ãã¹ã¯ã¼ãèªè¨¼ãå¿ è¦ãªå ´åãåãµã¼ãã¼ã§ã®èªè¨¼ãçç¥ããããã«ãå ¬ééµèªè¨¼ãè¨å®ãã¦ããã¨ã¹ã ã¼ãºã«å®è¡ã§ãã¾ãã
ãã®ã¹ã¯ãªãããå®è¡ããã¨ã
servers
é åã«ãã8å°ã®ãµã¼ãã¼ã«é çªã«SSHæ¥ç¶ããshutdown -h now
ãå®è¡ãã¦é 次ã·ã£ãããã¦ã³ãè¡ããã¾ãã
ã¨ã©ã¼ãåºããã ãã©
ããã³ãã
shutdown_servers.shãå®è¡ããã¨ããããã¹ã¦ã®ãµã¼ãã¼ããCall to PowerOff failed: Interactive authentication required.ã¨ããã¨ã©ã¼ã表示ããã¾ããã
åç
Call to PowerOff failed: Interactive authentication required.
ã¨ããã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ãããçç±ã¨ãã¦ãSSHæ¥ç¶å¾ã«ã³ãã³ããå®è¡ããéã«ããµã¼ãã¼å´ããã¹ã¯ã¼ãå ¥åãªã©ã®å¯¾è©±çãªèªè¨¼ãæ±ãã¦ããå¯è½æ§ãããã¾ãããã®å ´åã以ä¸ã®ããããã®æ¹æ³ã§è§£æ±ºã試ã¿ããã¾ãã解決æ¹æ³ 1: SSHå ¬ééµèªè¨¼ãè¨å®ãã
SSHæ¥ç¶æã«ãã¹ã¯ã¼ãèªè¨¼ãæ±ããããªãããã«ãå ¬ééµèªè¨¼ãè¨å®ããã®ãä¸è¬çãªè§£æ±ºæ¹æ³ã§ãã
å ¬ééµãçæï¼ããæ¢ã«å ¬ééµãããå ´åã¯ãåçæã¯ä¸è¦ã§ãï¼ã
ssh-keygen -t rsa
ãã®ã³ãã³ãã§çæãããå ¬ééµã¯é常
~/.ssh/id_rsa.pub
ã«ä¿åããã¾ããçæããå ¬ééµã対象ãµã¼ãã¼ã«ã³ãã¼ãã¾ãã以ä¸ã®ããã«
ssh-copy-id
ã³ãã³ãã使ãã¨ä¾¿å©ã§ããssh-copy-id user@server1 ssh-copy-id user@server2 # ... ä»ã®ãµã¼ãã¼ãåæ§ã«
ããã§åãµã¼ãã¼ã«å ¬ééµãç»é²ããããã¹ã¯ã¼ãå ¥åãªãã§SSHæ¥ç¶ã§ããããã«ãªãã¾ãã
å ¬ééµãè¨å®ã§ããããå度ã¹ã¯ãªãããå®è¡ãã¦ã¿ã¦ãã ããã
解決æ¹æ³ 2:
sudo
ã³ãã³ãã使ç¨ãã¦é対話çã«ã·ã£ãããã¦ã³ãããµã¼ãã¼ã«ãã£ã¦ã¯
shutdown
ã³ãã³ãã®å®è¡ã«ã¹ã¼ãã¼ã¦ã¼ã¶ã¼æ¨©éãå¿ è¦ãªå ´åãããã¾ãããã®å ´åãsudo
ã使ã£ã¦ã·ã£ãããã¦ã³ãå®è¡ã§ãã¾ãã以ä¸ã®ããã«ãã¹ã¯ãªããå ã§sudo shutdown -h now
ã使ããNOPASSWD
ãªãã·ã§ã³ãè¨å®ããæ¹æ³ã試ãã¾ãã
ã¹ã¯ãªãããä¿®æ£ãã¦ã
shutdown
ã³ãã³ããsudo
çµç±ã§å®è¡ããããã«ãã¾ããfor server in "${servers[@]}"; do ssh user@"$server" "sudo shutdown -h now" done
/etc/sudoers
ãã¡ã¤ã«ãç·¨éããNOPASSWD
ãªãã·ã§ã³ãè¨å®ãã¾ããåãµã¼ãã¼ã§ä»¥ä¸ã®è¨å®ã追å ãã¦ãã ããï¼visudo
ã³ãã³ãã使ããã¨ãæ¨å¥¨ãã¾ãï¼ãuser ALL=(ALL) NOPASSWD: /sbin/shutdown
ãã®è¨å®ã«ãããæå®ããã¦ã¼ã¶ã¼ããã¹ã¯ã¼ãå ¥åãªãã§
shutdown
ã³ãã³ããå®è¡ã§ããããã«ãªãã¾ããå度ã¹ã¯ãªãããå®è¡ãã¦ãã¨ã©ã¼ã解æ¶ããã¦ããã確èªãã¦ãã ããã
解決æ¹æ³ 3: 対話èªè¨¼ã許å¯ããï¼å¿ è¦ã«å¿ãã¦ï¼
ããã©ããã¦ã対話èªè¨¼ãå¿ è¦ãªå ´åã¯ã
sshpass
ã¨ãããã¼ã«ã使ç¨ãã¦ãã¹ã¯ã¼ããæå®ããèªååãããã¨ãã§ãã¾ãããã»ãã¥ãªãã£é¢ã®èæ ®ãå¿ è¦ã§ãã
åé¡ã®è§£æ±ºã¨å®è·µ
OCPã®åWorkerãã¼ãã§ã¯ãã解決æ¹æ³ 1: SSHå ¬ééµèªè¨¼ãè¨å®ããããè¨å®æ¸ã¿ã«ãªã£ã¦ãã¾ãã
解決æ¹æ³ 2:
sudo
ã³ãã³ãã使ç¨ãã¦é対話çã«ã·ã£ãããã¦ã³ãã
ããã§ãã¹ã¯ãªãããä¿®æ£ãã¦ãsudo shutdown -h now
ã¨ããã¨ãããæ£å¸¸ã«å®è¡ã§ãã¾ããã
ããã¦ããã¾ãï¼ãã¼ããé£ç¶ãã¦ã·ã£ãããã¦ã³ãããã¨ãã§ãã¾ããã
ãã£ã¦ã¿ãææ³
- ã·ã§ã«ã¹ã¯ãªãããä½æããã¨ããã¢ã¤ãã£ã¢ã¯ããã¾ããããå®éã®ã³ã¼ããèããã®ãé¢åã§ããããµã¯ãã¨ä½ãã¦æºè¶³ã§ãã
- ä»åã¯ã対象ã¯8ãã¼ãã ã£ãã®ã§ãæåã§ãã§ããããããã¾ããã
- ä»å¾å¯¾è±¡ãã¼ãã®æ°ãå¢ããå ´åã«ãä»åä½æããã¹ã¯ãªãããå½¹ç«ã¡ããã§ãã
- ä»åã¯ãç´ æ¡ã®ä½æããåé¡è§£æ±ºã¾ã§ããã¹ã¦çæAIã使ãã¾ããã