rancher每隔一年證書會(huì)到期,如何自動(dòng)化處理證書到期的問(wèn)題呢?
cd ..Smartbi_All/rancher_data/management-state/tls //到證書目錄openssl x509 -enddate -noout -in localhost.crt //查看到期時(shí)間mv localhost.crt localhost.crt_bakmv localhost.key localhost.key_bakmv token-node.crt token-node.crt_bakmv token-node.key token-node.key_bakdocker restart smartbi-rancher //重啟rancher容器,他會(huì)自動(dòng)續(xù)簽證書
使用腳本配置系統(tǒng)定時(shí)計(jì)劃任務(wù),讓系統(tǒng)自動(dòng)執(zhí)行更新證書,在證書到期前2天處理,腳本如下:
#!/bin/bash#create by chenzm , 2022-06-09 ,[email protected]_RANCHER_HOME=/data/soft/Smartbi_AllCurrentDay=`date ‘+%Y-%m-%d’`SMARTBI_RANCHER_TLS_HOME=$SMARTBI_RANCHER_HOME/rancher_data/management-state/tlsCRTTIME=`openssl x509 -enddate -noout -in ${SMARTBI_RANCHER_TLS_HOME}/localhost.crt | awk -F’=’ ‘{print$2}’ | awk ‘{print $1,$2,$3,$4}’`NOWTIME=`date +%F” “%T`#將證書到期時(shí)間轉(zhuǎn)換為秒CRTTIMETOS=`date -d “${CRTTIME}” +%s`#將NOWTIME時(shí)間也就是系統(tǒng)當(dāng)前時(shí)間轉(zhuǎn)換為秒NOWTIMETOS=`date -d “${NOWTIME}” +%s`MINUSTIME=$((CRTTIMETOS-NOWTIMETOS))echo ${MINUSTIME}#2天=172800秒,在到期前2天處理證書到期if [ ${MINUSTIME} -lt 172800 ];then echo “證書時(shí)間:${CRTTIME} , 當(dāng)系統(tǒng)時(shí)間:${NOWTIME} , 到期時(shí)間> /opt/update_rancher_tls.log mv $SMARTBI_RANCHER_TLS_HOME/localhost.crt $SMARTBI_RANCHER_TLS_HOME/localhost.crt_$CurrentDay mv $SMARTBI_RANCHER_TLS_HOME/localhost.key $SMARTBI_RANCHER_TLS_HOME/localhost.key_$CurrentDay mv $SMARTBI_RANCHER_TLS_HOME/token-node.crt $SMARTBI_RANCHER_TLS_HOME/token-node.crt_$CurrentDay mv $SMARTBI_RANCHER_TLS_HOME/token-node.key $SMARTBI_RANCHER_TLS_HOME/token-node.key_$CurrentDay docker restart smartbi-rancher else echo “證書時(shí)間:${CRTTIME} , 當(dāng)系統(tǒng)時(shí)間:${NOWTIME} , 到期時(shí)間>2天” >> /opt/update_rancher_tls.logfi
編輯定時(shí)任務(wù),比如每天晚上11點(diǎn)執(zhí)行任務(wù):
0 23 * * * /bin/bash /opt/update_rancher_tls.sh &> /dev/null
打印出的時(shí)間日志:
# cat update_rancher_tls.log 證書時(shí)間:Jun 8 06:58:13 2023 , 當(dāng)系統(tǒng)時(shí)間:2022-06-09 15:19:10 , 到期時(shí)間>2天
大家在寫腳本的時(shí)候,建議所有腳本功能,要有日志輸出,方便問(wèn)題跟蹤。