当前位置: 首页 > 后端技术 > Python

k8s在线和离线批量修改镜像地址有两种方法

时间:2023-03-26 14:09:29 Python

背景介绍有时候在k8s集群部署一堆服务,需要复制一堆yaml文件。当然也有其他部署方式,比如一条一条建立管道等,但是这样太慢了。虽然是一劳永逸,但是如果只部署一次的话,太费力了。直接copy一堆yaml文件就容易多了,但是如果之前环境的镜像地址没有了内网,现在放到另外一个环境,网络不可达的时候,就需要更改里面的镜像地址。如果之前的网络环境连接的是阿里云内网镜像地址,是这样的:registry-vpc.cn-beijing.aliyuncs.com/xxxx/xxxxx现在部署到华为云上,肯定是启动不了yaml文件直接。需要修改镜像地址,改成:registry.cn-beijing.aliyuncs.com/xxxx/xxxx,改成这个就可以拉取了。那么如何实现呢?调用k8s接口完成图片替换。下面的代码是通过k8s的接口。先查看当前namespace有哪些服务可用,然后将这些服务写入一个数组,然后遍历数组,然后修改整个namespace下所有pod的镜像地址importrefromkubernetesimportclient,configclassdeployServer:def__init__(self,kubeconfig):self.kubeconfig=kubeconfigconfig.kube_config.load_kube_config(config_file=self.kubeconfig)self._AppsV1Api=client.AppsV1Api()#这两个暂时用不到#self._CoreV1Api=client.CoreV1Api()#self._ExtensionsV1beta1Api=client.ExtensionsV1beta1Api()deflist_deploy(self,namespace_name):''':paramnamespace_name:指定命名空间:return:返回部署列表'''deploySubj=self._AppsV1Api.list_namespaced_deployment(namespace_name)return[subj.metadata.nameforsubjindeploySubj.items]defpatchMultiDeploy(self,namespace_name,newimgdomain):foriinself.list_deploy(namespace_name):self.patchDeploy(namespace_name,i,newimgdomain)print(“{}:{}补丁成功。”.format(namespace_name,i))defpatchDeploy(self,deploy_namespace,deploy_name,newimgdomain,keyword='vpc'):img_addr_rule=r[^?]{1,}\.[^?]{1,}\.com(\/.*\:.*)"img_domain_rule=r"([^?]{1,}\.[^?]{1,}\.com)。*\:.*"old_deploy=self._AppsV1Api.read_namespaced_deployment(name=deploy_name,namespace=deploy_namespace,)old_deploy_container=old_deploy.spec.template.spec.containers#迭代部署列表,获取部署名称及其索引positionfori,kinenumerate(old_deploy_container):#抓取域名外的镜像地址oldImgAddr=re.findall(img_addr_rule,old_deploy_container[i].image)[0]#抓取镜像地址的域名,可以也没有get,但是get了会更准确oldImgDoamin=re.findall(img_domain_rule,old_deploy_container[i].image)[0]#只有满足条件的才会被替换ifkeywordinoldImgDoamin:old_deploy_container[i].image=newimgdomain+oldImgAddrself._AppsV1Api.patch_namespaced_deployment(name=deploy_name,namespace=deploy_namespace,body=old_deploy)returnif__name__=='__main__':kubeconfig=r'kubeconfig/config'k8s=deployServer(kubeconfig)#指定服务所在的命名空间namespace='ops-logging'#指定要替换的镜像的域名。img='docker.elastic.co'k8s.patchMultiDeploy(namespace,img)注意需要指定kubeconfig文件,修改文件中的image地址。上面一个是直接调整界面,通过patch来修改,下面是直接修改yaml文件importosimportredefmodifyImagesFromFile(originpath,src_img,dst_img):_re_rule=r".*image:([^?]{1,}\.[^?]{1,}\.com).*\:.*"ifos.path.isdir(originpath):filesubj=os.walk(originpath)filename=[]#遍历所有文件指定路径下forpath,_,fileinfilesubj:#把所有文件拼成一个完整的路径,写在一个列表中filename=[os.path.join(path,f)forfinfile]iffilename:对于文件名中的f:withopen(f,'r+',encoding='utf-8')asfr:fr_data=fr.read()replace_list=re.findall(_re_rule,fr_data)ifreplace_list:forpatterninreplace_list:ifsrc_imginpattern:#将匹配到的内容替换为fr_data=fr_data.replace(pattern,dst_img)fr.seek(0)fr.write(fr_data)print("{}替换成功".format(f))else:print("Unknown")else:print("{}不是directory".format(originpath))if__name__=='__main__':originpath=r"E:\project\DEVenvironment\sg-saas-pro-hbali\gldsg-gvs\deployments"dst_img='registry.cn-beijing.aliyuncs.com'src_img='registry-vpc.cn-beijing.aliyuncs.com'modifyImagesFromFile(originpath)注意:需要将所有的部署文件放在一个目录下使用上面的k8s接口通过patch修改,粘贴和直接复制,指定命名空间,指定kubeconfig文件的位置没问题。如果所有的yaml文件都down了,那么直接遍历目录下的所有文件,通过正则化抓取镜像地址,然后替换,再写入源文件。发布博客群发、多发帖运营工具平台OpenWrite