0421.html 12 KB

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