0421.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Create a Kubernetes Docker Cluster on Ubuntu Server</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta charset="UTF-8">
  7. <meta name="keywords" content="Kubernetes Installation,Install Kubernetes On Ubuntu,Kubernetes Tutorial,Kubernetes,VirtualBox,Docker,Docker Made Easy,Install Guide,Home Lab,Containerization,Orchestration,Self-Hosted,Virtual Machines,Ubuntu,Ubuntu Server,Cluster,SSH,Command Line,CLI,How To,Tutorial,i12bretro">
  8. <meta name="author" content="i12bretro">
  9. <meta name="description" content="Create a Kubernetes Docker Cluster on Ubuntu Server">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  12. <script type="text/javascript">
  13. $(function(){
  14. $('textarea').each(function(i,e){
  15. theTextarea = $(this);
  16. theTextarea.height((theTextarea[0].scrollHeight-5) +'px');
  17. });
  18. $('li').each(function(i,e){
  19. if(!$(this).hasClass('noCheckbox')){
  20. var uuid = 'li_' + Math.floor(Math.random() * Math.floor(1000000)).toString() + '_' + i.toString();
  21. $(this).contents().wrap('<span id="'+ uuid +'"><label for="cb_'+ uuid +'"></label></span>');
  22. $(this).prepend('<input type="checkbox" class="completeBox" id="cb_' + uuid +'" rel="'+ uuid +'" />')
  23. }
  24. });
  25. $('code,div.codeBlock,textarea.codeBlock').each(function(i,e){
  26. theElement = $(this);
  27. var lines = theElement.html().split("\n");
  28. theElement.empty();
  29. for(l=0;l<lines.length;l++){
  30. if($.trim(lines[l]) != '' && $.trim(lines[l]).substr(0,1) != '#' && $.trim(lines[l]).indexOf(' #') == -1 && lines[l].substr(0, 4).toUpperCase() != 'REM '){
  31. theElement.append('<input type="image" src="images/clipboard.png" value="" class="copy-text" rel="copy_'+ i +'_'+ l +'" data-clipboard-text="'+ $.trim(lines[l].replace(/"/g, '&quot;')) +'" /><span id="copy_'+ i +'_'+ l +'">'+ lines[l] +'</span>');
  32. } else {
  33. theElement.append(lines[l]);
  34. }
  35. }
  36. });
  37. $(document).on('click','input.copy-text',function(){
  38. theButton = $(this);
  39. $('input.copy-text').attr('src','images/clipboard.png');
  40. $('span.copy-animation,span.copy-animation-ps').removeClass('copy-animation copy-animation-ps');
  41. try {
  42. if($('#'+ theButton.attr('rel')).parent('div').hasClass('PS')){
  43. $('#'+ theButton.attr('rel')).addClass('copy-animation-ps');
  44. } else if($('#'+ theButton.attr('rel')).parent('div').hasClass('CMD')){
  45. $('#'+ theButton.attr('rel')).addClass('copy-animation-cmd');
  46. } else {
  47. $('#'+ theButton.attr('rel')).addClass('copy-animation');
  48. }
  49. navigator.clipboard.writeText(theButton.data('clipboard-text').replace(/<[^>]*>?/gm, ''));
  50. theButton.attr('src','images/clipboard_active.png');
  51. } catch(err) {
  52. }
  53. return false;
  54. });
  55. $(document).on('click','input.completeBox',function(){
  56. theBox = $(this);
  57. $('#'+ theBox.attr('rel')).addClass('strikethrough');
  58. theBox.prop('disabled',true);
  59. theBox.parent('li').prevAll().each(function(i,e){
  60. theLI = $(this);
  61. if(theLI.find('input[type=checkbox]').not(':checked')){
  62. $('#'+ theLI.find('input[type=checkbox]').attr('rel')).addClass('strikethrough');
  63. theLI.find('input[type=checkbox]').prop('checked',true).prop('disabled',true);
  64. }
  65. });
  66. });
  67. if(window.self !== window.top){
  68. window.parent.$('iframe.stepsFrame').height((this['scrollingElement']['scrollHeight']+20) +'px');
  69. }
  70. });
  71. </script>
  72. <link href="css/steps.css" rel="stylesheet" type="text/css" />
  73. </head>
  74. <body>
  75. <div id="gridContainer">
  76. <div class="topMargin"></div>
  77. <div id="listName" class="topMargin">
  78. <h1>Create a Kubernetes Docker Cluster on Ubuntu Server</h1>
  79. </div>
  80. <div></div>
  81. <div id="content">
  82. <h2 class="hidden">Cluster VM Network Diagram</h2>
  83. <p class="hidden"><img alt="Kubernetes Cluster Diagram" src="attachments/0421/diagram.jpg" title="Kubernetes Cluster Diagram" /></p>
  84. <h2>Preparing the VMs</h2>
  85. <ol>
  86. <li>Log into a freshly installed Ubuntu VM</li>
  87. <li>Run the following commands
  88. <div class="codeBlock"># set the hostname<br />
  89. sudo hostnamectl set-hostname kubernetes<br />
  90. # update hosts file<br />
  91. sudo nano /etc/hosts</div>
  92. </li>
  93. <li>Update the 127.0.01 to the new hostname, kubernetes</li>
  94. <li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
  95. <li>Continue with the following commands
  96. <div class="codeBlock"># disable the firewall<br />
  97. sudo ufw disable<br />
  98. # disable the swap file<br />
  99. sudo swapoff -a<br />
  100. # disable swap partition<br />
  101. sudo sed -i &#39;/ swap / s/^\(.*\)$/#\1/g&#39; /etc/fstab<br />
  102. sudo echo &quot;vm.swappiness=0&quot; | sudo tee --append /etc/sysctl.conf<br />
  103. # update sysctl networking<br />
  104. cat &lt;<eof> net.bridge.bridge-nf-call-ip6tables = 1<br />
  105. net.bridge.bridge-nf-call-iptables = 1<br />
  106. EOF<eof><eof><br />
  107. sudo sysctl --system<br />
  108. # apply the changes<br />
  109. sudo sysctl -p</eof></eof></eof></div>
  110. </li>
  111. </ol>
  112. <h2>Installing Docker</h2>
  113. <ol>
  114. <li>Continue with the following commands in terminal
  115. <div class="codeBlock"># install prerequisites<br />
  116. sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent -y<br />
  117. # add docker gpg key<br />
  118. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -<br />
  119. # add docker software repository<br />
  120. sudo add-apt-repository &quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot;<br />
  121. # install docker<br />
  122. sudo apt install docker-ce docker-compose containerd.io -y<br />
  123. # enable and start docker service<br />
  124. sudo systemctl enable docker &amp;&amp; sudo systemctl start docker<br />
  125. # add the current user to the docker group<br />
  126. sudo usermod -aG docker $USER<br />
  127. # reauthenticate for the new group membership to take effect<br />
  128. su - $USER</div>
  129. </li>
  130. </ol>
  131. <h2>Installing Kubernetes</h2>
  132. <ol>
  133. <li>Continue with the following commands in terminal
  134. <div class="codeBlock"># add kubernetes gpg key<br />
  135. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -<br />
  136. # add the kubernetes software repo<br />
  137. echo &#39;deb http://apt.kubernetes.io/ kubernetes-xenial main&#39; | sudo tee /etc/apt/sources.list.d/kubernetes.list<br />
  138. # update software repositories<br />
  139. sudo apt update<br />
  140. # install kubernetes<br />
  141. sudo apt install kubelet kubeadm kubectl -y<br />
  142. # shutdown the VM<br />
  143. sudo shutdown now</div>
  144. </li>
  145. </ol>
  146. <h2>Cloning the VirtualBox VM</h2>
  147. <ol>
  148. <li>In VirtualBox Manager, right click on the Kubernetes VM &gt; Clone...</li>
  149. <li>Name the cloned VM Docker1 &gt; Click Next</li>
  150. <li>Make sure Full Clone is selected &gt; Click Clone</li>
  151. <li>In VirtualBox Manager, right click on the Ubuntu VM again &gt; Clone...</li>
  152. <li>Name the cloned VM Docker2 &gt; Click Next</li>
  153. <li>Make sure Full Clone is selected &gt; Click Clone</li>
  154. <li>Right click the Kubernetes VM &gt; Start &gt; Normal Start</li>
  155. <li>Right click the Docker1 VM &gt; Start &gt; Normal Start</li>
  156. <li>Once the VM boots, log in and run the following commands
  157. <div class="codeBlock"># set the hostname<br />
  158. sudo hostnamectl set-hostname docker1<br />
  159. # update hosts file<br />
  160. sudo nano /etc/hosts</div>
  161. </li>
  162. <li>Update the 127.0.01 to the new hostname, docker1</li>
  163. <li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
  164. <li>Reboot the VM with the following command
  165. <div class="codeBlock">sudo reboot now</div>
  166. </li>
  167. <li>Back in VirtualBox Manager, right click the Docker2 VM &gt; Start &gt; Normal Start</li>
  168. <li>Once the VM boots, log in and run the following commands
  169. <div class="codeBlock"># set the hostname<br />
  170. sudo hostnamectl set-hostname docker2<br />
  171. # update hosts file<br />
  172. sudo nano /etc/hosts</div>
  173. </li>
  174. <li>Update the 127.0.01 to the new hostname, docker2</li>
  175. <li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
  176. <li>Reboot the VM with the following command
  177. <div class="codeBlock">sudo reboot now</div>
  178. </li>
  179. <li>At this point we have 3 Ubuntu VMs (kubernetes, docker1, docker2) running with swap disabled and docker and kubernetes installed</li>
  180. <li>All 3 VMs will need static IP addresses, I prefer to create static DHCP leases on my router for a single point of configuration, but the VM IP addresses can be set with the following command if needed
  181. <div class="codeBlock">sudo nano /etc/netplan/01-installer-config.yaml</div>
  182. </li>
  183. <li>Set DHCP4 = no and update the IP address, gateway and nameservers
  184. <p>dhcp4: no<br />
  185. addresses: [192.168.100.100/24]<br />
  186. gateway4: 192.168.100.1<br />
  187. nameservers:<br />
  188. addresses: [192.168.100.1,8.8.8.8]</p>
  189. </li>
  190. </ol>
  191. <h2>Configuring the Kubernetes Cluster</h2>
  192. <p>Follow the steps below only on the kubernetes VM</p>
  193. <ol>
  194. <li>Log into the kubernetes VM and run the following commands in terminal
  195. <div class="codeBlock"># initialize the cluster, copy the kubeadm join command for use later when joining the docker hosts to the cluster<br />
  196. sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.10.27.100 --kubernetes-version &quot;1.20.4&quot;<br />
  197. # create .kube config directory and copy configuration<br />
  198. mkdir -p $HOME/.kube<br />
  199. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config<br />
  200. sudo chown $(id -u):$(id -g) $HOME/.kube/config<br />
  201. # deploy the flannel network<br />
  202. kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml</div>
  203. </li>
  204. </ol>
  205. <h2>Joining Docker Nodes to the Cluster</h2>
  206. <p>Follow the steps below only on the docker1 and docker2 VMs</p>
  207. <ol>
  208. <li>Log into each of the Docker VMs and run the following commands in terminal
  209. <div class="codeBlock"># paste the kubeadm join command copied earlier, the command will look similar to the one below<br />
  210. sudo kubeadm join 10.10.27.100:6443 --token ua6bhl.wuzsqhnf0h40hgxr \<br />
  211. --discovery-token-ca-cert-hash sha256:69a89f8d81dbfc08e4098f3d43e42c78429369ca41cb2954bcbcbb15405d69ef</div>
  212. </li>
  213. </ol>
  214. <h2>Verifying the Cluster and Testing</h2>
  215. <p>Follow the steps below on the kubernetes VM</p>
  216. <ol>
  217. <li>Log into kubernetes VM and run the following commands in terminal
  218. <div class="codeBlock"><br />
  219. # list kubernetes nodes, all 3 VMs should be listed<br />
  220. kubectl get nodes<br />
  221. # deploy an example guestbook application<br />
  222. # deploy mongoDB<br />
  223. kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-deployment.yaml<br />
  224. # deploy mongoDB service<br />
  225. kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-service.yaml<br />
  226. # deploy guestbook frontend<br />
  227. kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-deployment.yaml<br />
  228. # deploy frontend service<br />
  229. kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml<br />
  230. # watch as the pods are created and start running, press ctrl+c once all pods are in running status<br />
  231. watch -n 3 kubectl get pods<br />
  232. # forward host port 8080 to container port 80<br />
  233. kubectl port-forward --address 0.0.0.0 svc/frontend 8080:80</div>
  234. </li>
  235. </ol>
  236. <h2>Removing the Test Deployments</h2>
  237. <p>Follow the steps below on the kubernetes VM</p>
  238. <ol>
  239. <li>Log into kubernetes VM and run the following commands in terminal
  240. <div class="codeBlock"># list the running pods<br />
  241. kubectl get pods<br />
  242. # delete the test pods created earlier<br />
  243. kubectl delete deployment -l app.kubernetes.io/name=mongo<br />
  244. kubectl delete service -l app.kubernetes.io/name=mongo<br />
  245. kubectl delete deployment -l app.kubernetes.io/name=guestbook<br />
  246. kubectl delete service -l app.kubernetes.io/name=guestbook<br />
  247. # watch as the pods are terminated and deleted, press ctrl+c to return to the terminal<br />
  248. watch -n 3 kubectl get pods</div>
  249. </li>
  250. </ol>
  251. </div>
  252. </div>
  253. </body>
  254. </html>