遇到一個場景,容器的日志以hostpath方式掛在到node的路徑下。但是容器重啟后會換到不同的node,查詢歷史日志就成了頭疼的事情。
我遇到的一個paas環境有70多個node,找歷史日志要遍歷這么多的node太難了。
就嘗試寫一個腳本來找日志,效率還是高了很多。主要是沮喪感就沒那么強烈了,不需要ssh到每個節點去找了,這有點low了。
后續還要確定一下有沒有,重啟容器后到其他node時,有沒有辦法順便刪除或者轉移持久卷。沒有清理也是有問題的。
下面的部分來自https://blog.csdn.net/jinking01/article/details/84386769
前提是需要配置ssh免密碼登陸,各節點間ssh不需要輸入密碼的。
#!/bin/bash ssh user@remoteNode > /dev/null 2>&1 << eeooff cd /home touch abcdefg.txt exit eeooff echo done!
遠程執行的內容在“<< eeooff ” 至“ eeooff ”之間,在遠程機器上的操作就位於其中,注意的點:
1. << eeooff,ssh后直到遇到eeooff這樣的內容結束,eeooff可以隨便修改成其他形式。
2. 重定向目的在於不顯示遠程的輸出了
3. 在結束前,加exit退出遠程節點
下面部分來自:https://www.cnblogs.com/seasonsstory/p/3277473.html
我沒有這種方式,環境上並沒有裝expect。
expect
1.首先確認expect的包要安置。
[root@localhost]$ rpm -qa | grep expect
如果沒有則需要下載安裝,yum -y install expect expect-devel
安裝過后會顯示:
[root@localhost] rpm -qa | grep expect
expect-5.43.0-5.1
expect-devel-5.42.1-1
2.查看expect的路徑,可以用
[root@localhost] which expect
/usr/bin/expect
3.確定腳本有可執行權限
chmod +x test.sh
#!/usr/bin/expect //這個expect的路徑就是用which expect 查看的結果
spawn ssh -l 遠程主機的用戶名 遠程主機的ip地址
expect "password:" //提示讓輸入密碼
send "遠程主機的密碼\r"
interact //操作完成
!
另外需要注意的是:
不能按照習慣來用sh test.sh來這行expect的程序,會提示找不到命令,如下:
test.sh: line 2: spawn: command not found
couldn't read file "password": no such file or directory
test.sh: line 4: send: command not found
test.sh: line 5: interact: command not found
因為expect用的不是bash所以會報錯。執行的時候直接./test.sh就可以了