How to deploy an application on different nodes

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 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

Hey @saeed
There are two methods:

  1. nodeName field

  2. nodeSelector field

1 Like

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 but I get this error with below explanation:

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:

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

And respectively for mysql file.
Then run this command:

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

But I get this error:

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:

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 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?

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

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.

kubectl apply -k ./

Let me know, happy to help out more.

1 Like

Hi @macintoshprime, I only run this command:

kubectl apply -k ./

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

Like here:

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