pod中批量执行命令

1,068 阅读1分钟

有个项目主要用到了python,容器在打包镜像时运维没有考虑到部分依赖,需要手动批量安装。特写此脚本提高效率:

import os

sparks=["spark-wangfeng2-release-1-master-0", "spark-wangfeng2-release-1-worker-0", "spark-wangfeng2-release-1-worker-1", \
        "spark-wangfeng2-release-2-master-0", "spark-wangfeng2-release-2-worker-0", "spark-wangfeng2-release-2-worker-1"]

index_url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
trusted_host = "pypi.tuna.tsinghua.edu.cn"
namespace = os.getenv("PPNS", "wangfeng2")
for spark in sparks:
    print(f"installing on {spark}")
    val = os.system(f"echo \"pip3 install grpcio==1.36.1 -i {index_url} --trusted-host {trusted_host}\" | kubectl exec -it {spark} -n {namespace} -- /bin/sh")
    print(val)
    val = os.system(f"echo \"pip3 install protobuf -i {index_url} --trusted-host {trusted_host}\" | kubectl exec -it {spark} -n {namespace} -- /bin/sh")
    print(val)
    val = os.system(f"echo \"pip3 install pydantic -i {index_url} --trusted-host {trusted_host}\" | kubectl exec -it {spark} -n {namespace} -- /bin/sh")
    print(val)
    val = os.system(f"echo \"pip3 install pysnooper -i {index_url} --trusted-host {trusted_host}\" | kubectl exec -it {spark} -n {namespace} -- /bin/sh")
    print(val)
    val = os.system(f"echo \"pip3 install google -i {index_url} --trusted-host {trusted_host}\" | kubectl exec -it {spark} -n {namespace} -- /bin/sh")
    print(val)



批量删除redis pod数据:

import os

redis=["common-redis-2-0", "common-redis-2-1", "common-redis-2-2"]

namespace = os.getenv("PPNS", "wangfeng2")

for r in redis:
    print(f"flushall on {r}")
    val = os.system(f"echo 'redis-cli -a 123456 flushall' | kubectl exec -it {r} -n {namespace} /bin/sh")
    print(val)