<p>For exmaple, I created network at docker</p>

<pre><code>docker network create hello-rails
</code></pre>

<p>Then, I have mySQL, which is connected to this network</p>

<pre><code>docker run -p 3306 -d --network=hello-rails --network-alias=db -e MYSQL_ROOT_PASSWORD=password --name hello-rails-db mysql
</code></pre>

<p>And also, I have rails server, which also rely on this network</p>

<pre><code>docker run -it -p 3000:3000 --network=hello-rails -e MYSQL_USER=root -e MYSQL_PASSWORD=password -e MYSQL_HOST=db --name hello-rails benjamincaldwell/hello-docker-rails:latest
</code></pre>

<p>I want to write deployment on kubernetes for these two containers with YAML file. But I don't know, how to put network inside <code>containers</code> in the file. Do you have any recommendations?</p>

<p>In Kubernetes you would solve this by creating two services.</p>

<p>The MySQL service will look something like this:</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
  - port: 3306
</code></pre>

<p>In your rails server, you can access the MySQL service by either using the <code>mysql</code> DNS name or using the <code>MYSQL_SERVICE_HOST</code> and <code>MYSQL_SERVICE_PORT</code> environment variables. There is no need to link the containers or specifying a network, as would be done in Docker.</p>

<p>Your Rails service will look like this:</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: rails
spec:
  type: LoadBalancer
  selector:
    app: rails
  ports:
  - port: 3000
</code></pre>

<p>Notice the <code>type: LoadBalancer</code>, which specifies that this service will be published to the outside world. Depending on where you run Kubernetes, a public IP address will be automatically assigned to this service.</p>

<p>For more information, have a look at the <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Services documentation</a>.</p>
