I have a kubernetes multi-resource file which contains different resources that are to be applied for e.g. deployment-definition, service-defintion, pv, pvc etc. Is there any way to use this single file through kubernetes python client to deploy all these resources at once ? Though my scenario is a bit different. I have a file which use CRDs alongwith custom kubernetes resource objects for e.g. Deployment + ambassador's Mapping. How to achieve this using kubernetes python client?
3 Answers
With the client, you have to do them all separately. When you have multiple documents in a YAML file, kubectl just splits them for you and makes an API call for each.

- 52,400
- 4
- 52
- 75
-
But how to do this for CRDs ? – Vedant Pareek Mar 02 '20 at 10:12
-
Do you mean making API calls against custom objects? https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CustomObjectsApi.md shows how to do that, also the examples are autogenerated and terrible. – coderanger Mar 02 '20 at 10:25
I have a kubernetes multi-resource file Is there any way to use this single file through kubernetes python client to deploy all these resources at once ?
Please check the content of examples
directory.
from os import path
import yaml
from kubernetes import client, config
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f:
dep = yaml.safe_load(f)
k8s_beta = client.ExtensionsV1beta1Api()
resp = k8s_beta.create_namespaced_deployment(
body=dep, namespace="default")
print("Deployment created. status='%s'" % str(resp.status))
if __name__ == '__main__':
main()
Important note: it's a must to use triple dashes at the top of your yaml file and in-between resources, if it contains more than one resource.
.../utils/create_from_yaml.py and .../examples/create_deployment_from_yaml.py are worth checking as well.
I have a file which use CRDs alongwith custom kubernetes resource objects
as @coderanger told, the example can be found in .../docs/CustomObjectsApi.md
Hope that helps.

- 1,882
- 11
- 16
you can use config.new_client_from_config to manage multi clusters.
or use kube config from dict not local file.
from kubernetes import client, config
from kubernetes.client import Configuration, ApiClient
def new_client_from_dict(conf: dict, context: str):
"""
create client via conf dict
"""
client_config = type.__call__(Configuration)
config.load_kube_config_from_dict(config_dict=conf, context=context, persist_config=False,
client_configuration=client_config)
return ApiClient(configuration=client_config)
client1 = client.CoreV1Api(api_client=new_client_from_dict(CLUSTER1_KUBE_CONFIG, context='cluster1'))
client2 = client.CoreV1Api(api_client=new_client_from_dict(CLUSTER2_KUBE_CONFIG, context='cluster2'))
client1.list_namespaced_pod()
client2.list_namespaced_pod()

- 153
- 1
- 4