脚本更新tke集群中CLB类型Ingress证书

通常我们在tke里面进行7层服务暴露,可以用nginx ingress和clb类型的ingress,如果你用的clb类型ingress,需要在tke这边用secret配置腾讯云上的证书,当你的证书过期或者不存在,配置错误时,会导致ingress同步规则到clb失败,从而导致访问域名出现异常,这个时候需要正确在tke这边更新ingress的证书id才能解决这个问题。

下面我们用简单的脚本来一键更新ingress的证书id。

脚本一键更新ingress证书需要有以下条件

上面条件满足后,这里我们只需要将下面脚本复制到对应的机器上执行就行,update-ingress-certificate-id.sh脚本内容如下

脚本小技巧:shell中单引号内引入变量,只需要单引号内嵌套单引号即可使用变量

#!/bin/bash

ingress_name=$1
ns=$2
certificate_id=$3

if [ $# = 0 ];then
  echo "Run 'sh update-ingress-certificate-id.sh --h' for more information on a command."
fi

if [[ $1 = "--h" ]];then
  echo "Please enter the first parameter is the name of ingress, the second parameter is the namespace, and the third parameter is the certificate id.

Usage: sh update-ingress-certificate-id.sh [ingress_name] [namespace] [certificate_id]"
fi

main(){

secret_name=`kubectl get ingress -n $ns $ingress_name -o=jsonpath='{.spec.tls[*].secretName}'`

base64_result=`echo -n $certificate_id | base64`

kubectl patch secret $secret_name -n $ns --type='json' -p='[{"op": "replace", "path": "/data/qcloud_cert_id", "value":'$base64_result'}]'

}

if [ $# = 3 ];then

main

fi

下面我们来测试下脚本,假设我要更新的正确证书id是 lI7vOSLM,首先看下当前secret的证书id是多少

[root@VM-0-13-centos script]# kubectl get ingress -n ingress-test test-ingress -o=jsonpath='{.spec.tls[*].secretName}'
grpc-leq8zgsw
[root@VM-0-13-centos script]# kubectl get secret grpc-leq8zgsw -o=jsonpath='{.data.qcloud_cert_id}' -n ingress-test | base64 -d
lEq8zGsW

现在的ingress证书id是lEq8zGsW,下面我们用脚本更新下

[root@VM-0-13-centos script]# sh -x  update-ingress-certificate-id.sh test-ingress ingress-test lI7vOSLM
+ ingress_name=test-ingress
+ ns=ingress-test
+ certificate_id=lI7vOSLM
+ '[' 3 = 0 ']'
+ [[ test-ingress = \\-\\-\\h ]]
+ '[' 3 = 3 ']'
+ main
++ kubectl get ingress -n ingress-test test-ingress '-o=jsonpath={.spec.tls[*].secretName}'
+ secret_name=grpc-leq8zgsw
++ base64
++ echo -n lI7vOSLM
+ base64_result=bEk3dk9TTE0=
+ echo bEk3dk9TTE0=
bEk3dk9TTE0=
+ kubectl patch secret grpc-leq8zgsw -n ingress-test --type=json '-p=[{"op": "replace", "path": "/data/qcloud_cert_id", "value":bEk3dk9TTE0=}]'
secret/grpc-leq8zgsw patched

这里已经执行脚本更新成功,我们获取下证书id,看看是不是正确的证书idlI7vOSLM

[root@VM-0-13-centos script]# kubectl get ingress -n ingress-test test-ingress -o=jsonpath='{.spec.tls[*].secretName}'
grpc-leq8zgsw
[root@VM-0-13-centos script]# kubectl get secret grpc-leq8zgsw -o=jsonpath='{.data.qcloud_cert_id}' -n ingress-test | base64 -d
lI7vOSLM

从获取结果看,这里ingress证书已经更新成功为lI7vOSLM

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
脚本更新tke集群中CLB类型Ingress证书
通常我们在tke里面进行7层服务暴露,可以用nginx ingress和clb类型的ingress,如果你用的clb类型ingress,需要在tke这边用sec...
<<上一篇
下一篇>>