0421.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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>Cluster VM Network Diagram</h2>
  78. <p><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;&lt;EOF | sudo tee /etc/sysctl.d/kubernetes.conf<br />
  100. net.bridge.bridge-nf-call-ip6tables = 1<br />
  101. net.bridge.bridge-nf-call-iptables = 1<br />
  102. EOF<eof><eof><br />
  103. sudo sysctl --system<br />
  104. # apply the changes<br />
  105. sudo sysctl -p</eof></eof></div>
  106. </li>
  107. </ol>
  108. <h2>Installing Docker</h2>
  109. <ol>
  110. <li>Continue with the following commands in terminal
  111. <div class="codeBlock"># install prerequisites<br />
  112. sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent -y<br />
  113. # add docker gpg key<br />
  114. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -<br />
  115. # add docker software repository<br />
  116. sudo add-apt-repository &quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot;<br />
  117. # install docker<br />
  118. sudo apt install docker-ce docker-compose containerd.io -y<br />
  119. # enable and start docker service<br />
  120. sudo systemctl enable docker &amp;&amp; sudo systemctl start docker<br />
  121. # add the current user to the docker group<br />
  122. sudo usermod -aG docker $USER<br />
  123. # reauthenticate for the new group membership to take effect<br />
  124. su - $USER</div>
  125. </li>
  126. </ol>
  127. <h2>Installing Kubernetes</h2>
  128. <ol>
  129. <li>Continue with the following commands in terminal
  130. <div class="codeBlock"># add kubernetes gpg key<br />
  131. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -<br />
  132. # add the kubernetes software repo<br />
  133. echo &#39;deb http://apt.kubernetes.io/ kubernetes-xenial main&#39; | sudo tee /etc/apt/sources.list.d/kubernetes.list<br />
  134. # update software repositories<br />
  135. sudo apt update<br />
  136. # install kubernetes<br />
  137. sudo apt install kubelet kubeadm kubectl -y<br />
  138. # shutdown the VM<br />
  139. sudo shutdown now</div>
  140. </li>
  141. </ol>
  142. <h2>Cloning the VirtualBox VM</h2>
  143. <ol>
  144. <li>In VirtualBox Manager, right click on the Kubernetes VM &gt; Clone...</li>
  145. <li>Name the cloned VM Docker1 &gt; Click Next</li>
  146. <li>Make sure Full Clone is selected &gt; Click Clone</li>
  147. <li>In VirtualBox Manager, right click on the Ubuntu VM again &gt; Clone...</li>
  148. <li>Name the cloned VM Docker2 &gt; Click Next</li>
  149. <li>Make sure Full Clone is selected &gt; Click Clone</li>
  150. <li>Right click the Kubernetes VM &gt; Start &gt; Normal Start</li>
  151. <li>Right click the Docker1 VM &gt; Start &gt; Normal Start</li>
  152. <li>Once the VM boots, log in and run the following commands
  153. <div class="codeBlock"># set the hostname<br />
  154. sudo hostnamectl set-hostname docker1<br />
  155. # update hosts file<br />
  156. sudo nano /etc/hosts</div>
  157. </li>
  158. <li>Update the 127.0.01 to the new hostname, docker1</li>
  159. <li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
  160. <li>Reboot the VM with the following command
  161. <div class="codeBlock">sudo reboot now</div>
  162. </li>
  163. <li>Back in VirtualBox Manager, right click the Docker2 VM &gt; Start &gt; Normal Start</li>
  164. <li>Once the VM boots, log in and run the following commands
  165. <div class="codeBlock"># set the hostname<br />
  166. sudo hostnamectl set-hostname docker2<br />
  167. # update hosts file<br />
  168. sudo nano /etc/hosts</div>
  169. </li>
  170. <li>Update the 127.0.01 to the new hostname, docker2</li>
  171. <li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
  172. <li>Reboot the VM with the following command
  173. <div class="codeBlock">sudo reboot now</div>
  174. </li>
  175. <li>At this point we have 3 Ubuntu VMs (kubernetes, docker1, docker2) running with swap disabled and docker and kubernetes installed</li>
  176. <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
  177. <div class="codeBlock">sudo nano /etc/netplan/01-installer-config.yaml</div>
  178. </li>
  179. <li>Set DHCP4 = no and update the IP address, gateway and nameservers
  180. <p>dhcp4: no<br />
  181. addresses: [192.168.100.100/24]<br />
  182. gateway4: 192.168.100.1<br />
  183. nameservers:<br />
  184. addresses: [192.168.100.1,8.8.8.8]</p>
  185. </li>
  186. </ol>
  187. <h2>Configuring the Kubernetes Cluster</h2>
  188. <p>Follow the steps below only on the kubernetes VM</p>
  189. <ol>
  190. <li>Log into the kubernetes VM and run the following commands in terminal
  191. <div class="codeBlock"># initialize the cluster, copy the kubeadm join command for use later when joining the docker hosts to the cluster<br />
  192. 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 />
  193. # create .kube config directory and copy configuration<br />
  194. mkdir -p $HOME/.kube<br />
  195. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config<br />
  196. sudo chown $(id -u):$(id -g) $HOME/.kube/config<br />
  197. # deploy the flannel network<br />
  198. kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml</div>
  199. </li>
  200. </ol>
  201. <h2>Joining Docker Nodes to the Cluster</h2>
  202. <p>Follow the steps below only on the docker1 and docker2 VMs</p>
  203. <ol>
  204. <li>Log into each of the Docker VMs and run the following commands in terminal
  205. <div class="codeBlock"># paste the kubeadm join command copied earlier, the command will look similar to the one below<br />
  206. sudo kubeadm join 10.10.27.100:6443 --token ua6bhl.wuzsqhnf0h40hgxr \<br />
  207. --discovery-token-ca-cert-hash sha256:69a89f8d81dbfc08e4098f3d43e42c78429369ca41cb2954bcbcbb15405d69ef</div>
  208. </li>
  209. </ol>
  210. <h2>Verifying the Cluster and Testing</h2>
  211. <p>Follow the steps below on the kubernetes VM</p>
  212. <ol>
  213. <li>Log into kubernetes VM and run the following commands in terminal
  214. <div class="codeBlock"><br />
  215. # list kubernetes nodes, all 3 VMs should be listed<br />
  216. kubectl get nodes<br />
  217. # deploy an example guestbook application<br />
  218. # deploy mongoDB<br />
  219. kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-deployment.yaml<br />
  220. # deploy mongoDB service<br />
  221. kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-service.yaml<br />
  222. # deploy guestbook frontend<br />
  223. kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-deployment.yaml<br />
  224. # deploy frontend service<br />
  225. kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml<br />
  226. # watch as the pods are created and start running, press ctrl+c once all pods are in running status<br />
  227. watch -n 3 kubectl get pods<br />
  228. # forward host port 8080 to container port 80<br />
  229. kubectl port-forward --address 0.0.0.0 svc/frontend 8080:80</div>
  230. </li>
  231. </ol>
  232. <h2>Removing the Test Deployments</h2>
  233. <p>Follow the steps below on the kubernetes VM</p>
  234. <ol>
  235. <li>Log into kubernetes VM and run the following commands in terminal
  236. <div class="codeBlock"># list the running pods<br />
  237. kubectl get pods<br />
  238. # delete the test pods created earlier<br />
  239. kubectl delete deployment -l app.kubernetes.io/name=mongo<br />
  240. kubectl delete service -l app.kubernetes.io/name=mongo<br />
  241. kubectl delete deployment -l app.kubernetes.io/name=guestbook<br />
  242. kubectl delete service -l app.kubernetes.io/name=guestbook<br />
  243. # watch as the pods are terminated and deleted, press ctrl+c to return to the terminal<br />
  244. watch -n 3 kubectl get pods</div>
  245. </li>
  246. </ol>
  247. </div>
  248. </div>
  249. </body>
  250. </html>