Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubernetes - kubectl run vs create and apply

Tags:

kubernetes

I'm just getting started with kubernetes and setting up a cluster on AWS using kops. In many of the examples I read (and try), there will be commands like:

kubectl run my-app --image=mycompany/myapp:latest --replicas=1 --port=8080  kubectl expose deployment my=app --port=80 --type=LoadBalancer 

This seems to do several things behind the scenes, and I can view the manifest files created using kubectl edit deployment, and so forth However, i see many examples where people are creating the manifest files by hand, and using commands like kubectl create -f or kubectl apply -f

Am I correct in assuming that both approaches accomplish the same goals, but that by creating the manifest files yourself, you have a finer grain of control?

Would I then have to be creating Service, ReplicationController, and Pod specs myself?

Lastly, if you create the manifest files yourself, how do people generally structure their projects as far as storing these files? Are they simply in a directory alongside the project they are deploying?

like image 329
djt Avatar asked Dec 28 '17 23:12

djt


People also ask

What happens when you kubectl apply?

Creating Deployments You can create a Deployment using the kubectl apply , or kubectl create commands. Once created, the Deployment ensures that the desired number of Pods are running and available at all times. The Deployment automatically replaces Pods that fail or are evicted from their nodes.

What does kubectl run do?

kubectl run − Run command has the capability to run an image on the Kubernetes cluster. kubectl scale − It will scale the size of Kubernetes Deployments, ReplicaSet, Replication Controller, or job.

What is in kubectl apply command?

Kubectl apply apply manages applications through files defining Kubernetes resources. It creates and updates resources in a cluster through running kubectl apply . This is the recommended way of managing Kubernetes applications on production.

What is the opposite of kubectl apply?

It's really important to remember that technically there isn't an inverse to kubectl apply . This is because of how k8s works, it converges desired state against current state. By running apply , you are telling the cluster to "make it look like this".


1 Answers

The fundamental question is how to apply all of the K8s objects into the k8s cluster. There are several ways to do this job.

  • Using Generators (Run, Expose)
  • Using Imperative way (Create)
  • Using Declarative way (Apply)

All of the above ways have a different purpose and simplicity. For instance, If you want to check quickly whether the container is working as you desired then you might use Generators .

If you want to version control the k8s object then it's better to use declarative way which helps us to determine the accuracy of data in k8s objects.

Deployment, ReplicaSet and Pods are different layers which solve different problems.All of these concepts provide flexibility to k8s.

  • Pods: It makes sure that related containers are together and provide efficiency.
  • ReplicaSet: It makes sure that k8s cluster has desirable replicas of the pods
  • Deployment: It makes sure that you can have different version of Pods and provide the capability to rollback to the previous version

Lastly, It depends on use case how you want to use these concepts or methodology. It's not about which is good or which is bad.

like image 199
Suresh Vishnoi Avatar answered Sep 19 '22 11:09

Suresh Vishnoi