1 00:00:04,820 --> 00:00:11,510 ‫This is going to be the first lecture where we're actually going to use multiple hosts or nodes, 2 00:00:11,570 --> 00:00:17,900 ‫or instances, or whatever you want...multiple OS's. We're going to set up a 3-node swarm across 3 00:00:17,900 --> 00:00:19,630 ‫all three of those nodes. 4 00:00:19,670 --> 00:00:24,620 ‫We unfortunately can't do this part with the built-in Docker on your machine because that's only going to 5 00:00:24,620 --> 00:00:31,150 ‫provide us one OS. We need three Linux OS's to play with this next set of features. 6 00:00:31,190 --> 00:00:35,600 ‫The first option you're going to have is using play-with-docker.com. 7 00:00:35,630 --> 00:00:37,900 ‫It's absolutely the easiest way to get started. 8 00:00:37,910 --> 00:00:43,400 ‫It comes with Docker pre-installed. It takes you seconds to provision three different machines, and it requires 9 00:00:43,430 --> 00:00:50,160 ‫no investment on your end. But it does mean that every four hours it will wipe all the work you've done. 10 00:00:50,180 --> 00:00:54,590 ‫So if you think you're going to do the rest of this section over the course of days, then this may not 11 00:00:54,590 --> 00:00:59,210 ‫be the best option for you. But if you're going to focus for a few hours, you can definitely get through 12 00:00:59,210 --> 00:01:05,030 ‫most, if not all, of the multi node swarm stuff using Play With Docker in one sitting. 13 00:01:05,030 --> 00:01:09,380 ‫Another option you have is you can use docker-machine, which is not something we've talked about yet, 14 00:01:09,530 --> 00:01:14,570 ‫but it's a command line tool that actually gets installed with Docker for Windows and Docker for Mac. 15 00:01:14,720 --> 00:01:22,040 ‫You can download it for Linux, and it is basically an automation tool for provisioning virtual machines 16 00:01:22,040 --> 00:01:26,960 ‫locally, and on the Internet, and then automatically setting up Docker for you. 17 00:01:26,960 --> 00:01:31,850 ‫The default way that it works is with VirtualBox, which you can install free. But it does mean that if 18 00:01:31,850 --> 00:01:37,340 ‫you're going to run three virtual machines locally, each one of them probably needs a gig of RAM or so 19 00:01:37,340 --> 00:01:42,440 ‫to run through a lot of these examples. So you'll need a machine with a good amount of RAM in it. Another 20 00:01:42,530 --> 00:01:48,350 ‫option, and the one that I'm going to default to for a lot of these demos and walk throughs, is DigitalOcean. 21 00:01:48,350 --> 00:01:52,910 ‫I just recommend DigitalOcean just because they have the cheapest and easiest service to 22 00:01:52,910 --> 00:01:58,760 ‫get started, and they run everything on SSD so it's nice and fast. You can actually use this in a 23 00:01:58,760 --> 00:02:04,130 ‫very, very similar set up to what production would be because you're using three actual virtual machines, 24 00:02:04,190 --> 00:02:07,810 ‫in the cloud, and it takes about a minute to launch each virtual machine. 25 00:02:07,910 --> 00:02:12,050 ‫If you're thinking that that might be an option, you can use my referral code and the references to 26 00:02:12,050 --> 00:02:15,630 ‫this section to get your first $10 free. 27 00:02:15,650 --> 00:02:21,740 ‫If you're only going to take a few days to get through this swarm stuff, you can provision three, $10 a 28 00:02:21,740 --> 00:02:25,480 ‫month nodes and have them up for days or a week. 29 00:02:25,520 --> 00:02:30,280 ‫And that might fit within the $10 budget so technically wouldn't cost you anything. 30 00:02:30,290 --> 00:02:35,270 ‫And then lastly, there are so many ways to get Docker installed, and that's really all you need is three 31 00:02:35,270 --> 00:02:39,900 ‫machines with Docker installed on them. You can use Docker Machine. 32 00:02:40,010 --> 00:02:47,120 ‫It has built in drivers that allow you to use it to provision Amazon instances, or Azure instances, or 33 00:02:47,120 --> 00:02:54,170 ‫DigitalOcean Droplets, or Google Compute nodes or any place you can get a Linux virtual machine, you 34 00:02:54,170 --> 00:02:58,460 ‫can actually just install Docker with an automated script from get.docker.com. 35 00:02:58,520 --> 00:03:04,640 ‫Frankly, I prefer the get.docker.com version only because Docker Machine may save you a few steps, 36 00:03:04,640 --> 00:03:09,230 ‫especially for locations like Amazon and Azure that require you to provision a lot of different things 37 00:03:09,230 --> 00:03:13,760 ‫before you can get to just provisioning a virtual machine. But you're probably not going to be using 38 00:03:13,760 --> 00:03:15,580 ‫Docker Machine in production. 39 00:03:15,590 --> 00:03:20,960 ‫It is a tool to simply automate dev and test environments. It was never really designed to set up all 40 00:03:20,960 --> 00:03:24,500 ‫of the production settings you might need for a multi-node swarm. 41 00:03:24,530 --> 00:03:30,170 ‫There's nothing inherently wrong with it, but typically get.docker.com works on all the major 42 00:03:30,170 --> 00:03:35,140 ‫Linux distributions and doesn't take any longer than actually using Docker Machine itself. 43 00:03:35,210 --> 00:03:41,390 ‫I'm going to give you a really quick example of just getting any of these scenarios to the point 44 00:03:41,390 --> 00:03:46,540 ‫where Docker is installed. Then they all will be very similar in the way we actually create the 45 00:03:46,540 --> 00:03:48,850 ‫swarm and do our services. 46 00:03:48,860 --> 00:03:52,250 ‫So don't think that you have to do it the way I do it. 47 00:03:52,250 --> 00:03:56,600 ‫Really we're just trying to get Docker info to actually bring back information, and then just to make 48 00:03:56,600 --> 00:04:01,400 ‫sure that the three nodes have networking access to each other and they're on a well connected network. 49 00:04:01,400 --> 00:04:04,630 ‫So ideally they should be in the same network. 50 00:04:04,670 --> 00:04:09,560 ‫If you were going to use Play With Docker, you actually just go to play-with-docker.com and check 51 00:04:09,560 --> 00:04:11,430 ‫on the box for I'm not a robot. 52 00:04:11,540 --> 00:04:15,580 ‫And if it gives you the CAPTCHA screen, just follow along. 53 00:04:17,720 --> 00:04:20,290 ‫All right. You'll see that it's counting down by four hours. 54 00:04:20,310 --> 00:04:26,130 ‫But if I just click the creating instance, and then I click it two more times, it'll create three virtual 55 00:04:26,130 --> 00:04:27,480 ‫environments very quickly. 56 00:04:27,570 --> 00:04:31,680 ‫As a side note, this is actually using Docker in Docker, which is a pretty neat technology where we're 57 00:04:31,680 --> 00:04:34,480 ‫actually running Docker inside a Docker container. 58 00:04:34,500 --> 00:04:39,870 ‫But in this set up with Play With Docker, which was created by other Docker captains and the Docker team, 59 00:04:40,020 --> 00:04:41,450 ‫it's really hard to tell a difference. 60 00:04:41,580 --> 00:04:51,240 ‫If I go to node1, I can already see that Docker info gives me the latest version. And if I even tried 61 00:04:51,240 --> 00:04:58,200 ‫to ping node2, it actually recognizes friendly names, so it will make it easier to communicate back and 62 00:04:58,200 --> 00:04:59,220 ‫forth between them. 63 00:04:59,260 --> 00:05:03,990 ‫So if this is something you want to try, it's a great and easy way on either low-powered machines 64 00:05:04,020 --> 00:05:07,290 ‫or if you just don't want to take the time to set up an environment for the next four hours. 65 00:05:08,180 --> 00:05:15,290 ‫Option two is to use docker-machine. If you have it installed, it should be at least version 10 66 00:05:15,380 --> 00:05:16,610 ‫if not newer. 67 00:05:16,670 --> 00:05:20,450 ‫You'll know if it's installed because you'll type the docker-machine command from your command line and 68 00:05:20,450 --> 00:05:22,340 ‫it'll either work or it won't. 69 00:05:22,430 --> 00:05:28,370 ‫If you need to download it for your particular Linux OS, then you can hop over to the Docker Documentation, 70 00:05:28,370 --> 00:05:31,580 ‫which will actually give you a quick curl line to download it. 71 00:05:31,730 --> 00:05:37,840 ‫If you're using the ToolBox on Windows 7 and 8, or Docker for Windows on Windows 10, or Docker for 72 00:05:37,840 --> 00:05:42,040 ‫Mac on Mac OS, it's already going to be up to date on those OS's for you. 73 00:05:42,260 --> 00:05:48,260 ‫If you want to use the VirtualBox option, which is the default, you just need to jump over to 74 00:05:48,260 --> 00:05:54,170 ‫virtualbox.org and download the latest version for your OS. And you don't even have to do anything with 75 00:05:54,170 --> 00:05:54,500 ‫VirtualBox. 76 00:05:54,500 --> 00:06:02,150 ‫It just needs to be installed. And then from your command line, you can just type docker-machine create 77 00:06:02,510 --> 00:06:09,530 ‫node1, and this will actually run through creating a virtual machine with BusyBox, which is a very 78 00:06:09,530 --> 00:06:12,430 ‫lightweight Linux distribution. 79 00:06:12,640 --> 00:06:18,160 ‫This will actually run through creating a very lightweight Linux machine, and you'll just repeat those 80 00:06:18,160 --> 00:06:20,920 ‫two commands for node2 and 3. 81 00:06:21,070 --> 00:06:26,590 ‫If you need more information on a Docker Machine, you can actually look at my reference section 82 00:06:26,620 --> 00:06:30,130 ‫for tutorial links. 83 00:06:30,390 --> 00:06:34,400 ‫Once your machines are created, you really have two options for how you can access those machines. 84 00:06:34,410 --> 00:06:41,580 ‫You can either use docker-machine ssh, and then the name of the node you created and that'll hop you right 85 00:06:41,660 --> 00:06:42,630 ‫into that machine. 86 00:06:42,960 --> 00:06:45,870 ‫Or you can use the environment command. 87 00:06:45,960 --> 00:06:54,030 ‫So the command docker-machine env, and then the name of the machine you want, will give you some output, 88 00:06:54,180 --> 00:06:59,760 ‫and you can either cut and paste these options to your command line or you can copy and paste the bottom 89 00:06:59,760 --> 00:07:05,280 ‫option, and that will actually create the environment variable so that when I run my docker command, 90 00:07:05,640 --> 00:07:10,830 ‫because again, the Docker CLI just needs to know what Docker host to talk to. And by default, it's talking 91 00:07:10,830 --> 00:07:11,850 ‫to your local machine. 92 00:07:11,880 --> 00:07:14,530 ‫But that just reprogrammed it to talk to the Docker Machine 93 00:07:14,540 --> 00:07:15,560 ‫node1. 94 00:07:15,630 --> 00:07:20,550 ‫If I do docker info, you'll see that its name is node1 because that's the one I'm actually now operating 95 00:07:20,550 --> 00:07:20,920 ‫on. 96 00:07:22,460 --> 00:07:27,010 ‫For the last example, I'm going to use DigitalOcean, which I have created an account on. 97 00:07:27,080 --> 00:07:33,830 ‫Here, they call servers Droplets. I recommend if you don't have a specific reason to run another 98 00:07:33,830 --> 00:07:41,900 ‫OS, to start with Ubuntu 16.04 because that tends to be the first Linux distribution to support the 99 00:07:41,900 --> 00:07:43,430 ‫newer Docker features. 100 00:07:43,430 --> 00:07:44,870 ‫So that's the default there. 101 00:07:44,870 --> 00:07:50,990 ‫Then I would recommend for the remainder of this course for us to use the $10 a month server. You 102 00:07:50,990 --> 00:07:55,850 ‫might be able to get away with the $5 a month server, but it might get a little bit slow during some of 103 00:07:55,850 --> 00:08:01,860 ‫the later demos when we do monitoring and logging containers on top of our applications. 104 00:08:01,910 --> 00:08:06,860 ‫Then you can pick the data center closest to you. Then the only other option is to add an SSH key. 105 00:08:07,430 --> 00:08:12,690 ‫If you're not familiar with how to create your own SSH key locally and then upload the public into 106 00:08:12,710 --> 00:08:13,830 ‫DigitalOcean, 107 00:08:13,830 --> 00:08:17,930 ‫there's a great tutorial that they have for it that you can find in the resources of this section. 108 00:08:19,140 --> 00:08:27,320 ‫Down at the bottom I'm going to create three machines, and I'm going to call them node1, 2 and 3. 109 00:08:27,520 --> 00:08:34,570 ‫Then I need to wait probably about 60 seconds and they'll be ready for me. 110 00:08:34,620 --> 00:08:38,040 ‫You could just use the IP addresses here...jump on these machines. 111 00:08:38,090 --> 00:08:43,440 ‫Clicking the copy button and then ssh root @ the IP address. 112 00:08:43,560 --> 00:08:49,230 ‫And if your SSH key is properly set up on your machine, then it'll automatically log in with that 113 00:08:49,230 --> 00:08:49,870 ‫key. 114 00:08:49,950 --> 00:08:56,070 ‫But in the resources section, I've given you an example of how to set up a .ssh config file that will 115 00:08:56,070 --> 00:09:00,800 ‫allow you to have these three nodes in a config so you can just simply call them node1, node2, 116 00:09:00,830 --> 00:09:04,980 ‫node3, instead of having to remember their IP addresses, in case you're going to be doing this over multiple 117 00:09:04,980 --> 00:09:10,650 ‫days. That's really an easy way to get in and out of these servers. 118 00:09:10,660 --> 00:09:15,550 ‫If you've used Docker Machine, or if you used Play With Docker, then Docker is already installed 119 00:09:15,850 --> 00:09:17,760 ‫and you're getting ready to create your swarm. 120 00:09:17,830 --> 00:09:22,400 ‫But if you use DigitalOcean, or you roll your own VMs, then you need to get Docker installed. 121 00:09:22,540 --> 00:09:28,180 ‫The easiest way to do that, that I like to use, is to use the script over at get.docker.com. 122 00:09:28,660 --> 00:09:33,520 ‫You can see examples up here of how to use this script to get the current production release of 123 00:09:33,520 --> 00:09:34,850 ‫Docker on your machine. 124 00:09:39,470 --> 00:09:45,020 ‫Now, all three of my servers have the latest version of Docker installed and we're ready to set 125 00:09:45,020 --> 00:09:46,400 ‫up our swarm. 126 00:09:46,400 --> 00:09:50,960 ‫Again, all that's required is that these three nodes have solid networking to each other and specific 127 00:09:50,960 --> 00:09:51,760 ‫ports open. 128 00:09:51,830 --> 00:09:56,360 ‫If you're curious about the ports of swarm, look in the resources of this section where I actually 129 00:09:56,360 --> 00:10:01,220 ‫have a link to my own list of the firewall ports necessary for Swarm to work. 130 00:10:01,220 --> 00:10:08,630 ‫If you remember before, we did docker swarm init. Now, in this case, which is pretty common in cloud 131 00:10:08,630 --> 00:10:14,510 ‫servers, it wants us to specify an IP address to advertise the Swarm service on. 132 00:10:14,510 --> 00:10:19,240 ‫You want to use an IP address that is accessible from the other servers. 133 00:10:19,250 --> 00:10:23,570 ‫In this case, I'm going to use the public IP address for my DigitalOcean 134 00:10:23,580 --> 00:10:24,050 ‫host. 135 00:10:29,750 --> 00:10:30,650 ‫And there we go. 136 00:10:30,700 --> 00:10:33,100 ‫So far, this is just like on our local machine. 137 00:10:33,170 --> 00:10:39,530 ‫This time, though, I'm going to copy the swarm join command and go over to node2 and add it in. 138 00:10:39,890 --> 00:10:40,720 ‫And there we go. 139 00:10:41,000 --> 00:10:42,940 ‫This node is now a part of the swarm. 140 00:10:43,010 --> 00:10:50,300 ‫So we go back to node1, docker node ls, and now we have two. 141 00:10:50,510 --> 00:10:58,070 ‫You'll notice the second node is only a worker. Because as it says, if we wanted it to be a manager, 142 00:10:58,070 --> 00:11:05,090 ‫we should have gotten this manager token. But no worries. We can actually update this worker to promote 143 00:11:05,090 --> 00:11:06,160 ‫it to being a manager. 144 00:11:06,200 --> 00:11:15,500 ‫But if we're over on node2, notice that I can't use Swarm commands. Because workers aren't really privileged. 145 00:11:15,860 --> 00:11:18,720 ‫They don't have access to control the swarm. 146 00:11:18,830 --> 00:11:23,510 ‫So they're not going to be able to use any of the Swarm commands that we're used to using on the managers. 147 00:11:23,510 --> 00:11:28,280 ‫Back on node1 and the manager, I'm going to do a docker node update... 148 00:11:37,470 --> 00:11:42,120 ‫What I was doing there was hitting Tab for tab completion, which is always a fantastic feature in 149 00:11:42,120 --> 00:11:47,520 ‫Docker, especially with so many features. It was walking me through each step of how to use that update 150 00:11:47,520 --> 00:11:48,240 ‫command. 151 00:11:48,240 --> 00:11:53,390 ‫Now I've got this final update finished and it has changed node2. 152 00:11:53,430 --> 00:11:59,930 ‫Now if I do my docker node ls, you'll see that now it's considered reachable. 153 00:12:00,030 --> 00:12:04,920 ‫But the original node is still the leader. By the way, the little asterisk? That just means that's the 154 00:12:04,920 --> 00:12:06,770 ‫node you're currently talking to. 155 00:12:07,020 --> 00:12:10,750 ‫For node3, let's add it as a manager by default. 156 00:12:10,890 --> 00:12:18,130 ‫We need to go back to our original command of docker swarm, and then we need to get the join-token 157 00:12:20,740 --> 00:12:22,400 ‫manager. 158 00:12:22,450 --> 00:12:25,430 ‫You can get these tokens at any time. 159 00:12:25,510 --> 00:12:27,500 ‫You don't have to write them down or save them. 160 00:12:27,520 --> 00:12:33,100 ‫They're part of the swarm configuration and stored encrypted on disk, so don't worry about that. 161 00:12:33,220 --> 00:12:38,620 ‫You can also change these in case they possibly get exposed or you maybe get a server that might have 162 00:12:38,620 --> 00:12:43,900 ‫had a vulnerability on it, and you want to make sure that no nodes can join the swarm from the old key, 163 00:12:43,900 --> 00:12:45,790 ‫you can actually rotate these keys. 164 00:12:45,910 --> 00:12:50,270 ‫I'm going to copy this, paste it into node3. 165 00:12:50,770 --> 00:12:53,820 ‫And there we go. Back on node1. 166 00:12:53,830 --> 00:13:00,670 ‫If I do a docker node ls again, you'll see I've got three nodes. And they all have a manager status indicating 167 00:13:00,670 --> 00:13:03,540 ‫that they are managers. 168 00:13:03,560 --> 00:13:11,600 ‫So now we have a 3-node, redundant swarm, with three managers. Let's just do the same thing 169 00:13:11,600 --> 00:13:16,540 ‫we did earlier, docker service create alpine 170 00:13:19,150 --> 00:13:22,420 ‫ping 8.8.8.8 171 00:13:22,690 --> 00:13:23,770 ‫Only this time, 172 00:13:27,350 --> 00:13:29,040 ‫let's give it three replicas. 173 00:13:31,690 --> 00:13:33,920 ‫And then do a docker service 174 00:13:34,010 --> 00:13:36,820 ‫ls, and we'll see 175 00:13:36,820 --> 00:13:39,350 ‫we've got three of three already running. 176 00:13:39,760 --> 00:13:50,050 ‫If I did a docker node ps, I can actually see my local node is actually running this task or container. 177 00:13:50,320 --> 00:13:59,740 ‫I can actually specify the node, node2, and it'll give me just the containers running on node2. 178 00:13:59,980 --> 00:14:07,900 ‫If I wanted to get a full list, I could do a docker service ps, and then sleepy brown is the name that 179 00:14:07,900 --> 00:14:13,350 ‫it gave our service. And you'll see here..let me expand the screen a little bit. 180 00:14:13,510 --> 00:14:17,210 ‫You can see here that it's listing all three of our tasks. 181 00:14:17,470 --> 00:14:22,450 ‫Once you've got this swarm created, you normally don't need to be typing commands into all the different 182 00:14:22,450 --> 00:14:26,910 ‫nodes. You can really operate the whole swarm, for most things, from node1. 183 00:14:26,950 --> 00:14:33,250 ‫So we'll be doing a lot of our work from node1 in future lectures, but for now, pat yourself on the back 184 00:14:33,280 --> 00:14:36,420 ‫because you have a fully operational swarm cluster.