# How to deploy an application on different nodes

**URL:** <https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683>\
**Category:** General Discussions\
**Created:** [April 27, 2021, 6:01am UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683 "2021-04-27T06:01:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![saeed](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@saeed](https://discuss.kubernetes.io/u/saeed)\
**Post date:** [April 27, 2021, 6:01am UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683/1 "2021-04-27T06:01:49Z")

</div>

Hello,

I use three VMs in my laptop Oracle VirtualBox as below:

```
NAME STATUS ROLES AGE VERSION
localhost Ready control-plane,master 7d7h v1.21.0
saeed Ready <none> 4d2h v1.21.0
saeed2 Ready <none> 17h v1.21.0

```

As you see, **localhost** is the Master node.

I’m new to Kubernetes but I recently learnt how to use Docker and deploy an application on it, but I have some questions about deploying application on Kubernetes and I did not found my answer by googling.

Suppose the above conditions, I want to run a simple WordPress application with MySQL.  
I want to run WordPress on **saeed** and MySQL on **saeed2**. How may I reach this?  
I want to control everything with my Master node. I mean like creating .yml file and telling which yaml file to run on which worker.

I saw [this](https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/) tutorial but I think it’s for just one single node.

Also since K8S manages load balancing itself, is there an automatic way to to run a yaml file on Master node and then the Master automatically deploys them on different nodes?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![tej-singh-rana](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/tej-singh-rana/32/5921_2.png) [@tej-singh-rana](https://discuss.kubernetes.io/u/tej-singh-rana)\
**Post date:** [April 27, 2021, 11:17am UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683/2 "2021-04-27T11:17:00Z")

</div>

Hey @saeed  
There are two methods:

1. [nodeName field](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#nodename)

2. [nodeSelector field](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#nodeselector)

> [@saeed](#):
>
> Also since K8S manages load balancing itself, is there an automatic way to to run a yaml file on Master node and then the Master automatically deploys them on different nodes?

> **[Assigning Pods to Nodes](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity)**
>
> You can constrain a Pod so that it is restricted to run on particular node(s), or to prefer to run on particular nodes. There are several ways to do this and the recommended approaches all use label selectors to facilitate the selection. Often, you...

---

<div class="post-metadata">

**Author:** ![saeed](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@saeed](https://discuss.kubernetes.io/u/saeed)\
**Post date:** [May 2, 2021, 1:51pm UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683/3 "2021-05-02T13:51:47Z")

</div>

Great, thanks for the reply.  
Regarding `nodeName` and `nodeSelector` I really appreciate.

For `nodeSelector`, I tried to do that in [Example: Deploying WordPress and MySQL with Persistent Volumes | Kubernetes](https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/) but I get this error with below explanation:

```auto
wget https://kubernetes.io/examples/application/wordpress/mysql-deployment.yaml
wget https://kubernetes.io/examples/application/wordpress/wordpress-deployment.yaml

```

Change in both file the part `spec: containers: ...` to:

```auto
    spec:
      containers:
        nodeName: saeed
      - image: wordpress:4.8-apache

```

And respectively for `mysql` file.  
Then run this command:

```auto
cat <<EOF >>./kustomization.yaml
resources:
  - mysql-deployment.yaml
  - wordpress-deployment.yaml
EOF

```

But I get this error:

```auto
error: accumulating resources: accumulation err='accumulating resources from 'mysql-deployment.yaml': yaml: line 21: did not find expected key': got file 'mysql-deployment.yaml', but '/home/pods/mysql-deployment.yaml' must be a directory to be a root

```

I also tried `kubectl apply -f ./kustomization.yaml`:

```auto
error: error validating "./kustomization.yaml": error validating data: [apiVersion not set, kind not set]; if you choose to ignore these errors, turn validation off with --validate=false

```

Is there anything wrong with this documentation? Or I am missing something?

Regarding the link [Assigning Pods to Nodes | Kubernetes](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/) you provided, if I understand correctly: I have one master and two worker nodes. If I deploy application, Kubernetes sees which node has more free resources and spaces and then creates image and runs container on that node. Am I right? Or I should concern other things to reach this?

---

<div class="post-metadata">

**Author:** ![macintoshprime](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/macintoshprime/32/7121_2.png) [@macintoshprime](https://discuss.kubernetes.io/u/macintoshprime)\
**Post date:** [May 9, 2021, 4:38pm UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683/4 "2021-05-09T16:38:33Z")

</div>

Hi @saeed what command are you running when you get ?

```auto
error: accumulating resources: accumulation err='accumulating resources from 'mysql-deployment.yaml': yaml: line 21: did not find expected key': got file 'mysql-deployment.yaml', but '/home/pods/mysql-deployment.yaml' must be a directory to be a root

```

I tried running the configs you posted with the modifications you made and things seemed to work as expected with the following command.

```auto
kubectl apply -k ./

```

Let me know, happy to help out more.

---

<div class="post-metadata">

**Author:** ![saeed](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@saeed](https://discuss.kubernetes.io/u/saeed)\
**Post date:** [May 12, 2021, 6:25pm UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683/5 "2021-05-12T18:25:11Z")

</div>

Hi @macintoshprime, I only run this command:

```auto
kubectl apply -k ./

```

---

<div class="post-metadata">

**Author:** ![macintoshprime](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/macintoshprime/32/7121_2.png) [@macintoshprime](https://discuss.kubernetes.io/u/macintoshprime)\
**Post date:** [May 13, 2021, 11:33am UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683/6 "2021-05-13T11:33:41Z")

</div>

Sorry didn’t catch this earlier but try changing the position of the `nodeName` key.

Like here:

```auto
    spec:
      nodeName: saeed
      containers:
      - image: wordpress:4.8-apache

```

---

<div class="post-metadata">

**Author:** ![saeed](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@saeed](https://discuss.kubernetes.io/u/saeed)\
**Post date:** [May 14, 2021, 8:38am UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683/7 "2021-05-14T08:38:52Z")

</div>

Thanks, I changed it in both files, and now I got this message:

```auto
ervice/wordpress configured
service/wordpress-mysql unchanged
persistentvolumeclaim/wp-pv-claim created
deployment.apps/wordpress created
deployment.apps/wordpress-mysql created
The PersistentVolumeClaim "mysql-pv-claim" is invalid: 
* spec: Forbidden: spec is immutable after creation except resources.requests for bound claims
  core.PersistentVolumeClaimSpec{
        AccessModes: {"ReadWriteOnce"},
        Selector: nil,
        Resources: core.ResourceRequirements{
                Limits: nil,
- Requests: core.ResourceList{
- s"storage": {i: resource.int64Amount{value: 2147483648}, s: "2Gi", Format: "BinarySI"},
- },
+ Requests: core.ResourceList{
+ s"storage": {i: resource.int64Amount{value: 21474836480}, s: "20Gi", Format: "BinarySI"},
+ },
        },
        VolumeName: "",
        StorageClassName: nil,
        ... // 2 identical fields
  }

* spec.resources.requests.storage: Forbidden: field can not be less than previous value

```

I cannot find any duplicate or identical names, but I’m not sure how it is throwing this error.  
I also changed the capacity field to `2Gi` in order to reduce the size for testing purposes but I see above it’s showing `20Gi` which is the default value of these two `yml` files.

I also do not see any newly ran containers on my worker nodes.

---

<div class="post-metadata">

**Author:** ![macintoshprime](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/macintoshprime/32/7121_2.png) [@macintoshprime](https://discuss.kubernetes.io/u/macintoshprime)\
**Post date:** [May 15, 2021, 3:49pm UTC](https://discuss.kubernetes.io/t/how-to-deploy-an-application-on-different-nodes/15683/8 "2021-05-15T15:49:52Z")

</div>

You’ll want to delete the existing PVCs and create new ones (assuming nothing important is in them). Things should come online after that. I would take a look at the `deploy` or even the `replicaset` to see why the new pods aren’t coming online. It could be due to the PVCs or something else.
