‫Before we expand our services to start running across a bunch of nodes and all of the services talking
‫to each other, let's go over a couple of new concepts that Swarm brings to the table.
‫The first one is a new networking driver called Overlay, and you really just create a network like you're
‫used to creating it,
‫docker network create, and you put in a --driver overlay.
‫What that's like is basically creating a Swarm-wide bridge network where the containers across
‫hosts on the same virtual network can access each other kind of like they're on a VLAN.
‫This is only for intra swarm communication.
‫The overlay driver doesn't play a huge amount in traffic coming inside, as it's trying to take a wholistic
‫swarm view of the network so that you're not constantly messing around with networking settings on individual
‫nodes.
‫You can also enable full network encryption using IPiece SEC where it'll actually set up IPSec tunnels
‫between all the different nodes of your swarm.
‫But it's off by default really just for performance reasons.
‫When you create your services, you can add them to no Overlay networks, or one or more Overlay networks.
‫It really depends on the design of your application.
‫You know, a lot of traditional designs would have the databases on a backend network and the Web servers
‫on a frontend network. Then maybe you would have an API between the two that would be on both networks,
‫or something like that.
‫And you can totally do that in Swarm.
‫Let's see this in action. What I want to do is I want to show you what it would be like if we deployed
‫the Drupal example from a previous assignment with the Postgres Database, as services, and then created
‫an Overlay network for them to talk to each other.
‫First we need to create a network, so docker network create, and this hasn't changed for Swarm
‫other than the fact that we're going to use a new driver.
‫I'm just going to call it mydrupal.
‫OK.
‫So, I'm going to do a docker network ls, and you'll see I've got a new overlay. The ingress one is there by default.
‫You'll also see a new one because a swarm, which is actually an outgoing network that we won't need to mess
‫with.
‫So, let's create our Postgres service...docker service create name...let's call it psql network.
‫We're going to connect it to mydrupal network.
‫There's actually tab completion on the network names.
‫Then remember I need to add in that environment variable for the Postgres password.
‫So we can connect to it later.
‫Then we're going to use the Postgres image. Looks good.
‫You'll notice here that we don't get the whole image downloading and all that, because services can't be
‫run in the foreground, because they have to go through the orchestrator and scheduler. So, we can do a
‫docker service ls, and we can see that one of one of them is running.
‫If we do a docker service ps psql, we can see that this is actually running on Node 1.
‫So I can do a docker container logs psql, and then I can see the logs of that.
‫Yep. Looks like it's running.
‫All right.
‫Let's create our other service so
‫I'll go ahead and just call it drupal.
‫We also need to attach to that same network. Then we want to publish the port 80:80 like we did with
‫a docker run command. And we'll use the Drupal image.
‫OK.
‫We can see that it's starting there.
‫There's a trick. You can actually use the watch command in Linux to watch. It'll
‫basically...what it does is it's rerunning a command over and over again. It's installed by default on
‫Ubuntu.
‫If you just put watch in front of the docker service ls, it will keep running it every two seconds
‫by default until...ah, see?
‫Now we have one by one.
‫All right, and then I'll just Control c out of that. We can do a docker service
‫ps drupal and see that drupal is actually running on Node 2.
‫We have the database running on Node 1.
‫We have the Drupal website running on Node 2.
‫So, how do they know how to talk to each other?
‫Well, using the service names.
‫If I paste in one of the IP addresses, it should come up on the Drupal install. And if I tell it the database
‫here, database name, we're just going to do the postgres again,
‫postgres, my password.
‫For the host, just like when we were with Compose, we made the service names in the Compose file,
‫here, we're going to use the service name that we created for the database server. So, that was
‫psql.
‫This screen is basically telling us that it's able to talk to the database across the nodes and
‫set up the system.
‫That's the great thing about Overlay is it really just acts like everything's on the same subnet.
‫All right.
‫So we don't need this anymore.
‫I'm going to finish up this example real quick just to show you the next cool thing about Swarm. OK.
‫Now, this site is running. But I have three nodes, so how do I know which node it's going to be on and
‫which port I need to make sure that my DNS points to when I create this website name?
‫In my case, I have three IP addresses, and I can copy all three of them and put all three of those
‫IP addresses in
‫and...oops...I got the wrong one there.
‫So, if you see, it appears like the website is running on all three nodes because I'm looking at
‫all three IP addresses.
‫But if I go over here and do this service ps drupal, I know it's only running node 2.
‫I can even do a docker service inspect, and see that indeed it's only got one IP address on that
‫overlay network...right here.
‫So, why is it responding on all three hosts?
‫That leads us to our next, new feature.