0421.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. <link rel="icon" type="image/x-icon" href="includes/favicon.ico">
  12. <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  13. <script type="text/javascript" src="includes/js/steps.js"></script>
  14. <link href="css/steps.css" rel="stylesheet" type="text/css" />
  15. </head>
  16. <body>
  17. <div id="gridContainer">
  18. <div class="topMargin"></div>
  19. <div id="listName" class="topMargin">
  20. <h1>Create a Kubernetes Docker Cluster on Ubuntu Server</h1>
  21. </div>
  22. <div></div>
  23. <div id="content">
  24. <h2 class="hidden">Cluster VM Network Diagram</h2>
  25. <p class="hidden"><img alt="Kubernetes Cluster Diagram" src="attachments/0421/diagram.jpg" title="Kubernetes Cluster Diagram" /></p>
  26. <h2>Preparing the VMs</h2>
  27. <ol>
  28. <li>Log into a freshly installed Ubuntu VM</li>
  29. <li>Run the following commands
  30. <div class="codeBlock"># set the hostname<br />
  31. sudo hostnamectl set-hostname kubernetes<br />
  32. # update hosts file<br />
  33. sudo nano /etc/hosts</div>
  34. </li>
  35. <li>Update the 127.0.01 to the new hostname, kubernetes</li>
  36. <li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
  37. <li>Continue with the following commands
  38. <div class="codeBlock"># disable the firewall<br />
  39. sudo ufw disable<br />
  40. # disable the swap file<br />
  41. sudo swapoff -a<br />
  42. # disable swap partition<br />
  43. sudo sed -i &#39;/ swap / s/^\(.*\)$/#\1/g&#39; /etc/fstab<br />
  44. sudo echo &quot;vm.swappiness=0&quot; | sudo tee --append /etc/sysctl.conf<br />
  45. # update sysctl networking<br />
  46. cat &lt;<eof> net.bridge.bridge-nf-call-ip6tables = 1<br />
  47. net.bridge.bridge-nf-call-iptables = 1<br />
  48. EOF<eof><eof><br />
  49. sudo sysctl --system<br />
  50. # apply the changes<br />
  51. sudo sysctl -p</eof></eof></eof></div>
  52. </li>
  53. </ol>
  54. <h2>Installing Docker</h2>
  55. <ol>
  56. <li>Continue with the following commands in terminal
  57. <div class="codeBlock"># install prerequisites<br />
  58. sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent -y<br />
  59. # add docker gpg key<br />
  60. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -<br />
  61. # add docker software repository<br />
  62. sudo add-apt-repository &quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot;<br />
  63. # install docker<br />
  64. sudo apt install docker-ce docker-compose containerd.io -y<br />
  65. # enable and start docker service<br />
  66. sudo systemctl enable docker &amp;&amp; sudo systemctl start docker<br />
  67. # add the current user to the docker group<br />
  68. sudo usermod -aG docker $USER<br />
  69. # reauthenticate for the new group membership to take effect<br />
  70. su - $USER</div>
  71. </li>
  72. </ol>
  73. <h2>Installing Kubernetes</h2>
  74. <ol>
  75. <li>Continue with the following commands in terminal
  76. <div class="codeBlock"># add kubernetes gpg key<br />
  77. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -<br />
  78. # add the kubernetes software repo<br />
  79. echo &#39;deb http://apt.kubernetes.io/ kubernetes-xenial main&#39; | sudo tee /etc/apt/sources.list.d/kubernetes.list<br />
  80. # update software repositories<br />
  81. sudo apt update<br />
  82. # install kubernetes<br />
  83. sudo apt install kubelet kubeadm kubectl -y<br />
  84. # shutdown the VM<br />
  85. sudo shutdown now</div>
  86. </li>
  87. </ol>
  88. <h2>Cloning the VirtualBox VM</h2>
  89. <ol>
  90. <li>In VirtualBox Manager, right click on the Kubernetes VM &gt; Clone...</li>
  91. <li>Name the cloned VM Docker1 &gt; Click Next</li>
  92. <li>Make sure Full Clone is selected &gt; Click Clone</li>
  93. <li>In VirtualBox Manager, right click on the Ubuntu VM again &gt; Clone...</li>
  94. <li>Name the cloned VM Docker2 &gt; Click Next</li>
  95. <li>Make sure Full Clone is selected &gt; Click Clone</li>
  96. <li>Right click the Kubernetes VM &gt; Start &gt; Normal Start</li>
  97. <li>Right click the Docker1 VM &gt; Start &gt; Normal Start</li>
  98. <li>Once the VM boots, log in and run the following commands
  99. <div class="codeBlock"># set the hostname<br />
  100. sudo hostnamectl set-hostname docker1<br />
  101. # update hosts file<br />
  102. sudo nano /etc/hosts</div>
  103. </li>
  104. <li>Update the 127.0.01 to the new hostname, docker1</li>
  105. <li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
  106. <li>Reboot the VM with the following command
  107. <div class="codeBlock">sudo reboot now</div>
  108. </li>
  109. <li>Back in VirtualBox Manager, right click the Docker2 VM &gt; Start &gt; Normal Start</li>
  110. <li>Once the VM boots, log in and run the following commands
  111. <div class="codeBlock"># set the hostname<br />
  112. sudo hostnamectl set-hostname docker2<br />
  113. # update hosts file<br />
  114. sudo nano /etc/hosts</div>
  115. </li>
  116. <li>Update the 127.0.01 to the new hostname, docker2</li>
  117. <li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
  118. <li>Reboot the VM with the following command
  119. <div class="codeBlock">sudo reboot now</div>
  120. </li>
  121. <li>At this point we have 3 Ubuntu VMs (kubernetes, docker1, docker2) running with swap disabled and docker and kubernetes installed</li>
  122. <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
  123. <div class="codeBlock">sudo nano /etc/netplan/01-installer-config.yaml</div>
  124. </li>
  125. <li>Set DHCP4 = no and update the IP address, gateway and nameservers
  126. <p>dhcp4: no<br />
  127. addresses: [192.168.100.100/24]<br />
  128. gateway4: 192.168.100.1<br />
  129. nameservers:<br />
  130. addresses: [192.168.100.1,8.8.8.8]</p>
  131. </li>
  132. </ol>
  133. <h2>Configuring the Kubernetes Cluster</h2>
  134. <p>Follow the steps below only on the kubernetes VM</p>
  135. <ol>
  136. <li>Log into the kubernetes VM and run the following commands in terminal
  137. <div class="codeBlock"># initialize the cluster, copy the kubeadm join command for use later when joining the docker hosts to the cluster<br />
  138. 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 />
  139. # create .kube config directory and copy configuration<br />
  140. mkdir -p $HOME/.kube<br />
  141. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config<br />
  142. sudo chown $(id -u):$(id -g) $HOME/.kube/config<br />
  143. # deploy the flannel network<br />
  144. kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml</div>
  145. </li>
  146. </ol>
  147. <h2>Joining Docker Nodes to the Cluster</h2>
  148. <p>Follow the steps below only on the docker1 and docker2 VMs</p>
  149. <ol>
  150. <li>Log into each of the Docker VMs and run the following commands in terminal
  151. <div class="codeBlock"># paste the kubeadm join command copied earlier, the command will look similar to the one below<br />
  152. sudo kubeadm join 10.10.27.100:6443 --token ua6bhl.wuzsqhnf0h40hgxr \<br />
  153. --discovery-token-ca-cert-hash sha256:69a89f8d81dbfc08e4098f3d43e42c78429369ca41cb2954bcbcbb15405d69ef</div>
  154. </li>
  155. </ol>
  156. <h2>Verifying the Cluster and Testing</h2>
  157. <p>Follow the steps below on the kubernetes VM</p>
  158. <ol>
  159. <li>Log into kubernetes VM and run the following commands in terminal
  160. <div class="codeBlock"><br />
  161. # list kubernetes nodes, all 3 VMs should be listed<br />
  162. kubectl get nodes<br />
  163. # deploy an example guestbook application<br />
  164. # deploy mongoDB<br />
  165. kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-deployment.yaml<br />
  166. # deploy mongoDB service<br />
  167. kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-service.yaml<br />
  168. # deploy guestbook frontend<br />
  169. kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-deployment.yaml<br />
  170. # deploy frontend service<br />
  171. kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml<br />
  172. # watch as the pods are created and start running, press ctrl+c once all pods are in running status<br />
  173. watch -n 3 kubectl get pods<br />
  174. # forward host port 8080 to container port 80<br />
  175. kubectl port-forward --address 0.0.0.0 svc/frontend 8080:80</div>
  176. </li>
  177. </ol>
  178. <h2>Removing the Test Deployments</h2>
  179. <p>Follow the steps below on the kubernetes VM</p>
  180. <ol>
  181. <li>Log into kubernetes VM and run the following commands in terminal
  182. <div class="codeBlock"># list the running pods<br />
  183. kubectl get pods<br />
  184. # delete the test pods created earlier<br />
  185. kubectl delete deployment -l app.kubernetes.io/name=mongo<br />
  186. kubectl delete service -l app.kubernetes.io/name=mongo<br />
  187. kubectl delete deployment -l app.kubernetes.io/name=guestbook<br />
  188. kubectl delete service -l app.kubernetes.io/name=guestbook<br />
  189. # watch as the pods are terminated and deleted, press ctrl+c to return to the terminal<br />
  190. watch -n 3 kubectl get pods</div>
  191. </li>
  192. </ol>
  193. </div>
  194. </div>
  195. </body>
  196. </html>