使用python腳本自動生成K8S-YAML的方法示例
1、生成 servie.yaml
1.1、yaml轉(zhuǎn)json
service模板yaml
apiVersion: v1 kind: Service metadata: name: ${jarName} labels: name: ${jarName} version: v1 spec: ports: - port: ${port} targetPort: ${port} selector: name: ${jarName}
轉(zhuǎn)成json的結(jié)構(gòu)
{ "apiVersion": "v1", "kind": "Service", "metadata": { "name": "${jarName}", "labels": { "name": "${jarName}", "version": "v1" } }, "spec": { "ports": [ { "port": "${port}", "targetPort": "${port}" } ], "selector": { "name": "${jarName}" } } }
1.2、關(guān)鍵代碼
# 通過傳入service_name及ports列表 def create_service_yaml(service_name, ports): # 將yaml讀取為json,然后修改所有需要修改的${jarName} service_data['metadata']['name'] = service_name service_data['metadata']['labels']['name'] = service_name service_data['spec']['selector']['name'] = service_name # .spec.ports 比較特殊,是一個字典列表,由于傳入的ports難以確定數(shù)量,難以直接修改 # 新建一個列表,遍歷傳入的ports列表,將傳入的每個port都生成為一個字典,添加入新列表中 new_spec_ports = [] for port in ports: port = int(port) new_port = {'port': port, 'targetPort': port} new_spec_ports.append(new_port) # 修改.spec.ports為新列表 service_data['spec']['ports'] = new_spec_ports
2、生成 deployment.yaml
2.1、yaml轉(zhuǎn)json
deployment模板yaml
apiVersion: apps/v1 kind: Deployment metadata: name: ${jarName} labels: name: ${jarName} spec: selector: matchLabels: name: ${jarName} replicas: 1 template: metadata: labels: name: ${jarName} spec: containers: - name: ${jarName} image: reg.test.local/library/${jarName}:${tag} imagePullSecrets: - name: registry-secret
轉(zhuǎn)成的json結(jié)構(gòu)
{ "apiVersion": "apps/v1", "kind": "Deployment", "metadata": { "name": "${jarName}", "labels": { "name": "${jarName}" } }, "spec": { "selector": { "matchLabels": { "name": "${jarName}" } }, "replicas": 1, "template": { "metadata": { "labels": { "name": "${jarName}" } }, "spec": { "containers": [ { "name": "${jarName}", "image": "reg.test.local/library/${jarName}:${tag}" } ], "imagePullSecrets": [ { "name": "registry-secret" } ] } } } }
2.2、關(guān)鍵代碼
# 傳入service_name及image tag def create_deploy_yaml(service_name, tag): # 首先修改所有的${jarName} deploy_data['metadata']['name'] = service_name deploy_data['metadata']['labels']['name'] = service_name deploy_data['spec']['selector']['matchLabels']['name'] = service_name deploy_data['spec']['template']['metadata']['labels']['name'] = service_name # 由于.spec.template.spec.containers的特殊性,我們采用直接修改的方式 # 首先拼接image字段 image = "reg.test.local/library/" + service_name + ":" + tag # 創(chuàng)建new_containers字典列表 new_containers = [{'name': service_name, 'image': image}] deploy_data['spec']['template']['spec']['containers'] = new_containers
3、完整腳本
#!/usr/bin/python # encoding: utf-8 """ The Script for Auto Create Deployment Yaml. File: auto_create_deploy_yaml User: miaocunfa Create Date: 2020-06-10 Create Time: 17:06 """ import os from ruamel.yaml import YAML yaml = YAML() def create_service_yaml(service_name, ports): service_mould_file = "mould/info-service-mould.yaml" isServiceMould = os.path.isfile(service_mould_file) if isServiceMould: # read Service-mould yaml convert json with open(service_mould_file, encoding='utf-8') as yaml_obj: service_data = yaml.load(yaml_obj) # Update jarName service_data['metadata']['name'] = service_name service_data['metadata']['labels']['name'] = service_name service_data['spec']['selector']['name'] = service_name # Update port new_spec_ports = [] for port in ports: port = int(port) portname = 'port' + str(port) new_port = {'name': portname, 'port': port, 'targetPort': port} new_spec_ports.append(new_port) service_data['spec']['ports'] = new_spec_ports # json To service yaml save_file = tag + '/' + service_name + '_svc.yaml' with open(save_file, mode='w', encoding='utf-8') as yaml_obj: yaml.dump(service_data, yaml_obj) print(save_file + ": Success!") else: print("Service Mould File is Not Exist!") def create_deploy_yaml(service_name, tag): deploy_mould_file = "mould/info-deploy-mould.yaml" isDeployMould = os.path.isfile(deploy_mould_file) if isDeployMould: with open(deploy_mould_file, encoding='utf-8') as yaml_obj: deploy_data = yaml.load(yaml_obj) # Update jarName deploy_data['metadata']['name'] = service_name deploy_data['metadata']['labels']['name'] = service_name deploy_data['spec']['selector']['matchLabels']['name'] = service_name deploy_data['spec']['template']['metadata']['labels']['name'] = service_name # Update containers image = "reg.test.local/library/" + service_name + ":" + tag new_containers = [{'name': service_name, 'image': image}] deploy_data['spec']['template']['spec']['containers'] = new_containers # json To service yaml save_file = tag + '/' + service_name + '_deploy.yaml' with open(save_file, mode='w', encoding='utf-8') as yaml_obj: yaml.dump(deploy_data, yaml_obj) print(save_file + ": Success!") else: print("Deploy Mould File is Not Exist!") services = { 'info-gateway': ['9999'], 'info-admin': ['7777'], 'info-config': ['8888'], 'info-message-service': ['8555', '9666'], 'info-auth-service': ['8666'], 'info-scheduler-service': ['8777'], 'info-uc-service': ['8800'], 'info-ad-service': ['8801'], 'info-community-service': ['8802'], 'info-groupon-service': ['8803'], 'info-hotel-service': ['8804'], 'info-nearby-service': ['8805'], 'info-news-service': ['8806'], 'info-store-service': ['8807'], 'info-payment-service': ['8808'], 'info-agent-service': ['8809'], 'info-consumer-service': ['8090'], } prompt = "\n請輸入要生成的tag: " answer = input(prompt) print("") if os.path.isdir(answer): raise SystemExit(answer + ': is Already exists!') else: tag = answer os.makedirs(tag) for service_name, service_ports in services.items(): create_service_yaml(service_name, service_ports) create_deploy_yaml(service_name, tag)
4、執(zhí)行效果
➜ python3 Auto_Create_K8S_YAML.py 請輸入要生成的tag: 0.0.1 0.0.1/info-gateway_svc.yaml: Success! 0.0.1/info-gateway_deploy.yaml: Success! 0.0.1/info-admin_svc.yaml: Success! 0.0.1/info-admin_deploy.yaml: Success! 0.0.1/info-config_svc.yaml: Success! 0.0.1/info-config_deploy.yaml: Success! 0.0.1/info-message-service_svc.yaml: Success! 0.0.1/info-message-service_deploy.yaml: Success! 0.0.1/info-auth-service_svc.yaml: Success! 0.0.1/info-auth-service_deploy.yaml: Success! 0.0.1/info-scheduler-service_svc.yaml: Success! 0.0.1/info-scheduler-service_deploy.yaml: Success! 0.0.1/info-uc-service_svc.yaml: Success! 0.0.1/info-uc-service_deploy.yaml: Success! 0.0.1/info-ad-service_svc.yaml: Success! 0.0.1/info-ad-service_deploy.yaml: Success! 0.0.1/info-community-service_svc.yaml: Success! 0.0.1/info-community-service_deploy.yaml: Success! 0.0.1/info-groupon-service_svc.yaml: Success! 0.0.1/info-groupon-service_deploy.yaml: Success! 0.0.1/info-hotel-service_svc.yaml: Success! 0.0.1/info-hotel-service_deploy.yaml: Success! 0.0.1/info-nearby-service_svc.yaml: Success! 0.0.1/info-nearby-service_deploy.yaml: Success! 0.0.1/info-news-service_svc.yaml: Success! 0.0.1/info-news-service_deploy.yaml: Success! 0.0.1/info-store-service_svc.yaml: Success! 0.0.1/info-store-service_deploy.yaml: Success! 0.0.1/info-payment-service_svc.yaml: Success! 0.0.1/info-payment-service_deploy.yaml: Success! 0.0.1/info-agent-service_svc.yaml: Success! 0.0.1/info-agent-service_deploy.yaml: Success! 0.0.1/info-consumer-service_svc.yaml: Success! 0.0.1/info-consumer-service_deploy.yaml: Success! ➜ ll total 12 drwxr-xr-x. 2 root root 4096 Jun 29 18:24 0.0.1 # 生成的 service yaml ➜ cat info-message-service_svc.yaml apiVersion: v1 kind: Service metadata: name: info-message-service labels: name: info-message-service version: v1 spec: ports: - name: port8555 port: 8555 targetPort: 8555 - name: port9666 port: 9666 targetPort: 9666 selector: name: info-message-service # 生成的 deployment yaml ➜ cat info-message-service_deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: name: info-message-service labels: name: info-message-service spec: selector: matchLabels: name: info-message-service replicas: 2 template: metadata: labels: name: info-message-service spec: containers: - name: info-message-service image: reg.test.local/library/info-message-service:0.0.1 imagePullSecrets: - name: registry-secret
到此這篇關(guān)于使用python腳本自動生成K8S-YAML的方法示例的文章就介紹到這了,更多相關(guān)python自動生成K8S-YAML內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在centos 7中安裝配置k8s集群的步驟詳解
- Docker學(xué)習(xí)筆記之k8s部署方法
- Kubernetes(k8s)基礎(chǔ)介紹
- 使用k8s部署Django項目的方法步驟
- Hyper-V下搭建K8S集群安裝docker的方法步驟
- k8s部署docker容器的實現(xiàn)
- Docker+K8S 集群環(huán)境搭建及分布式應(yīng)用部署
- 使用Rancher在K8S上部署高性能PHP應(yīng)用程序的教程
- SpringBoot應(yīng)用快速部署到K8S的詳細(xì)教程
- k8s node節(jié)點重新加入master集群的實現(xiàn)
- 云原生技術(shù)kubernetes(K8S)簡介
- 在K8s上部署Redis集群的方法步驟
- 基于Docker+K8S+GitLab/SVN+Jenkins+Harbor搭建持續(xù)集成交付環(huán)境的詳細(xì)教程
- k8s部署redis cluster集群的實現(xiàn)
- 在docker中部署k8s的方法
- 打包docker鏡像推送到遠(yuǎn)程服務(wù)器并部署到k8s的方法步驟
- k8s部署ingress-nginx的方法步驟
- K8S部署Kafka界面管理工具(kafkamanager)方法詳解
相關(guān)文章
windows安裝TensorFlow和Keras遇到的問題及其解決方法
這篇文章主要介紹了windows安裝TensorFlow和Keras遇到的問題及其解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-07-07python機(jī)器學(xué)習(xí)案例教程——K最近鄰算法的實現(xiàn)
本篇文章主要介紹了python機(jī)器學(xué)習(xí)案例教程——K最近鄰算法的實現(xiàn),詳細(xì)的介紹了K最近鄰算法的概念和示例,具有一定的參考價值,有興趣的可以了解一下2017-12-12django使用xlwt導(dǎo)出excel文件實例代碼
這篇文章主要介紹了django使用xlwt導(dǎo)出excel文件實例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02pandas實現(xiàn)datetime64與unix時間戳互轉(zhuǎn)
這篇文章主要介紹了pandas實現(xiàn)datetime64與unix時間戳互轉(zhuǎn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07讓python同時兼容python2和python3的8個技巧分享
這篇文章主要介紹了讓python同時兼容python2和python3的8個技巧分享,對代碼稍微做些修改就可以很好的同時支持python2和python3的,需要的朋友可以參考下2014-07-07ipython jupyter notebook中顯示圖像和數(shù)學(xué)公式實例
這篇文章主要介紹了ipython jupyter notebook中顯示圖像和數(shù)學(xué)公式實例,具有很好的參考價值,希望對有所幫助。一起跟隨小編過來看看吧2020-04-04