amanaganti ramreddy
amanaganti ramreddy

Reputation: 41

How to create deployment using python which uses `kubectll apply -f` internally

Can't replicate kubectl apply -f functionality with Python client

I have created a deployment using create_namespced_deployment (https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/AppsV1Api.md#create_namespaced_deployment) in python.

But when I tried to update the deployment manually using kubectl apply -f, I am getting warning message like below

Warning: resource deployments/fronend-deployment is missing the
kubectl.kubernetes.io/last-applied-configuration annotation which is
required by kubectl apply. 

kubectl apply should only be used on resources created declaratively
by either kubectl create --save-config or kubectl apply. 
The missing annotation will be patched automatically

I want to get rid of this warning message.Is there any other way?

I think the create_namespced_deployment method internally using kubectl create -f functionality.Is there any method that uses kubectl apply -f.

Let me know if you have any questions

Upvotes: 4

Views: 1110

Answers (1)

CodeWizard
CodeWizard

Reputation: 142532

In order to supply a full answer, let's start by explaining the difference between
kubectl apply to kubectl create


kubectl apply vs kubectl create

The key difference between kubectl apply and kubectl create is that apply creates Kubernetes objects through a declarative syntax, while the create command is imperative.


enter image description here


apply

  • The kubectl apply is a cli command used to create or modify Kubernetes resources defined in a manifest file.
  • Manifest file is referred to as Declarative.
  • Using a Declarative files we "describe" what should be our final state of our cluster (regarding the resources which we define)
  • The state of the resource is declared in the manifest file and this is where the name is coming from, then kubectl apply is used to implement that state.

create

  • In contrast to apply, the create command kubectl create is used for creating a Kubernetes resource directly. (for example: kubectl create ns XXX which will create a namespace.

  • This is an Imperative usage.

  • You can also use kubectl create with a manifest file to create a new instance of the resource. However, if the resource already exists, you should get an error 9but most likely K8S will tolerate it and you will not see the error in most cases).


Hopefully, now you can understand the errors you keep getting.

Upvotes: 4

Related Questions