Selaa lähdekoodia

0421: Create a Kubernetes Docker Cluster on Ubuntu Server

i12bretro 3 vuotta sitten
vanhempi
commit
fd5b7a90fe
1 muutettua tiedostoa jossa 277 lisäystä ja 0 poistoa
  1. 277 0
      0421.html

+ 277 - 0
0421.html

@@ -0,0 +1,277 @@
+    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
+      <head>
+        <title>Create a Kubernetes Docker Cluster on Ubuntu Server</title>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+								<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
+				        <script type="text/javascript">
+          $(function(){
+            $('textarea').each(function(i,e){
+              theTextarea = $(this);
+              theTextarea.height((theTextarea[0].scrollHeight-5) +'px');
+            });
+
+            $('li').each(function(i,e){
+							if(!$(this).hasClass('noCheckbox')){
+								var uuid = 'li_' + Math.floor(Math.random() * Math.floor(1000000)).toString() + '_' + i.toString();
+								$(this).contents().wrap('<span id="'+ uuid +'"><label for="cb_'+ uuid +'"></label></span>');
+								$(this).prepend('<input type="checkbox" class="completeBox" id="cb_' + uuid +'" rel="'+ uuid +'" />')
+							}
+            });
+
+            $('code,div.codeBlock,textarea.codeBlock').each(function(i,e){
+              theElement = $(this);
+              var lines = theElement.html().split("\n");
+              theElement.empty();
+              for(l=0;l<lines.length;l++){
+                if($.trim(lines[l]) != '' && $.trim(lines[l]).substr(0,1) != '#' && $.trim(lines[l]).indexOf(' #') == -1 && lines[l].substr(0, 4).toUpperCase() != 'REM '){
+									                  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>');
+									                } else {
+                  theElement.append(lines[l]);
+                }
+              }
+            });
+
+            $(document).on('click','input.copy-text',function(){
+              theButton = $(this);
+														$('input.copy-text').attr('src','images/clipboard.png');
+							              $('span.copy-animation,span.copy-animation-ps').removeClass('copy-animation copy-animation-ps');
+              try {
+                if($('#'+ theButton.attr('rel')).parent('div').hasClass('PS')){
+                  $('#'+ theButton.attr('rel')).addClass('copy-animation-ps');
+								} else if($('#'+ theButton.attr('rel')).parent('div').hasClass('CMD')){
+									$('#'+ theButton.attr('rel')).addClass('copy-animation-cmd');
+                } else {
+                  $('#'+ theButton.attr('rel')).addClass('copy-animation');
+                }
+                navigator.clipboard.writeText(theButton.data('clipboard-text').replace(/<[^>]*>?/gm, ''));
+																theButton.attr('src','images/clipboard_active.png');
+								              } catch(err) {
+              }
+              return false;
+            });
+
+            $(document).on('click','input.completeBox',function(){
+              theBox = $(this);
+              $('#'+ theBox.attr('rel')).addClass('strikethrough');
+              theBox.prop('disabled',true);
+              theBox.parent('li').prevAll().each(function(i,e){
+                theLI = $(this);
+                if(theLI.find('input[type=checkbox]').not(':checked')){
+                  $('#'+ theLI.find('input[type=checkbox]').attr('rel')).addClass('strikethrough');
+                  theLI.find('input[type=checkbox]').prop('checked',true).prop('disabled',true);
+                }
+              });
+            });
+
+            if(window.self !== window.top){
+															window.parent.$('iframe.stepsFrame').height((this['scrollingElement']['scrollHeight']+20) +'px');
+							            }
+          });
+        </script>
+        <link href="css/steps.css" rel="stylesheet" type="text/css" />
+      </head>
+      <body>
+        <div id="gridContainer">
+          <div class="topMargin"></div>
+          <div id="listName" class="topMargin">
+            <h1>Create a Kubernetes Docker Cluster on Ubuntu Server</h1>
+          </div>
+          <div></div>
+          <div id="content">
+          <h2>Cluster VM Network Diagram</h2>
+
+<p><img alt="Kubernetes Cluster Diagram" src="attachments/0421/diagram.jpg" title="Kubernetes Cluster Diagram" /></p>
+
+<h2>Preparing the VMs</h2>
+
+<ol>
+	<li>Log into a freshly installed Ubuntu VM</li>
+	<li>Run the following commands
+	<div class="codeBlock"># set the hostname<br />
+	sudo hostnamectl set-hostname kubernetes<br />
+	# update hosts file<br />
+	sudo nano /etc/hosts</div>
+	</li>
+	<li>Update the 127.0.01 to the new hostname, kubernetes</li>
+	<li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
+	<li>Continue with the following commands
+	<div class="codeBlock"># disable the firewall<br />
+	sudo ufw disable<br />
+	# disable the swap file<br />
+	sudo swapoff -a<br />
+	# disable swap partition<br />
+	sudo sed -i &#39;/ swap / s/^\(.*\)$/#\1/g&#39; /etc/fstab<br />
+	sudo echo &quot;vm.swappiness=0&quot; | sudo tee --append /etc/sysctl.conf<br />
+	# update sysctl networking<br />
+	cat &lt;&lt;EOF | sudo tee /etc/sysctl.d/kubernetes.conf<br />
+	net.bridge.bridge-nf-call-ip6tables = 1<br />
+	net.bridge.bridge-nf-call-iptables = 1<br />
+	EOF<eof><eof><br />
+	sudo sysctl --system<br />
+	# apply the changes<br />
+	sudo sysctl -p</eof></eof></div>
+	</li>
+</ol>
+
+<h2>Installing Docker</h2>
+
+<ol>
+	<li>Continue with the following commands in terminal
+	<div class="codeBlock"># install prerequisites<br />
+	sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent -y<br />
+	# add docker gpg key<br />
+	curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -<br />
+	# add docker software repository<br />
+	sudo add-apt-repository &quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot;<br />
+	# install docker<br />
+	sudo apt install docker-ce docker-compose containerd.io -y<br />
+	# enable and start docker service<br />
+	sudo systemctl enable docker &amp;&amp; sudo systemctl start docker<br />
+	# add the current user to the docker group<br />
+	sudo usermod -aG docker $USER<br />
+	# reauthenticate for the new group membership to take effect<br />
+	su - $USER</div>
+	</li>
+</ol>
+
+<h2>Installing Kubernetes</h2>
+
+<ol>
+	<li>Continue with the following commands in terminal
+	<div class="codeBlock"># add kubernetes gpg key<br />
+	curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -<br />
+	# add the kubernetes software repo<br />
+	echo &#39;deb http://apt.kubernetes.io/ kubernetes-xenial main&#39; | sudo tee /etc/apt/sources.list.d/kubernetes.list<br />
+	# update software repositories<br />
+	sudo apt update<br />
+	# install kubernetes<br />
+	sudo apt install kubelet kubeadm kubectl -y<br />
+	# shutdown the VM<br />
+	sudo shutdown now</div>
+	</li>
+</ol>
+
+<h2>Cloning the VirtualBox VM</h2>
+
+<ol>
+	<li>In VirtualBox Manager, right click on the Kubernetes VM &gt; Clone...</li>
+	<li>Name the cloned VM Docker1 &gt; Click Next</li>
+	<li>Make sure Full Clone is selected &gt; Click Clone</li>
+	<li>In VirtualBox Manager, right click on the Ubuntu VM again &gt; Clone...</li>
+	<li>Name the cloned VM Docker2 &gt; Click Next</li>
+	<li>Make sure Full Clone is selected &gt; Click Clone</li>
+	<li>Right click the Kubernetes VM &gt; Start &gt; Normal Start</li>
+	<li>Right click the Docker1 VM &gt; Start &gt; Normal Start</li>
+	<li>Once the VM boots, log in and run the following commands
+	<div class="codeBlock"># set the hostname<br />
+	sudo hostnamectl set-hostname docker1<br />
+	# update hosts file<br />
+	sudo nano /etc/hosts</div>
+	</li>
+	<li>Update the 127.0.01 to the new hostname, docker1</li>
+	<li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
+	<li>Reboot the VM with the following command
+	<div class="codeBlock">sudo reboot now</div>
+	</li>
+	<li>Back in VirtualBox Manager, right click the Docker2 VM &gt; Start &gt; Normal Start</li>
+	<li>Once the VM boots, log in and run the following commands
+	<div class="codeBlock"># set the hostname<br />
+	sudo hostnamectl set-hostname docker2<br />
+	# update hosts file<br />
+	sudo nano /etc/hosts</div>
+	</li>
+	<li>Update the 127.0.01 to the new hostname, docker2</li>
+	<li>Press CTRL+O, Enter, CTRL+X to write the changes to hosts</li>
+	<li>Reboot the VM with the following command
+	<div class="codeBlock">sudo reboot now</div>
+	</li>
+	<li>At this point we have 3 Ubuntu VMs (kubernetes, docker1, docker2) running with swap disabled and docker and kubernetes installed</li>
+	<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
+	<div class="codeBlock">sudo nano /etc/netplan/01-installer-config.yaml</div>
+	</li>
+	<li>Set DHCP4 = no and update the IP address, gateway and nameservers
+	<p>dhcp4: no<br />
+	addresses: [192.168.100.100/24]<br />
+	gateway4: 192.168.100.1<br />
+	nameservers:<br />
+	addresses: [192.168.100.1,8.8.8.8]</p>
+	</li>
+</ol>
+
+<h2>Configuring the Kubernetes Cluster</h2>
+
+<p>Follow the steps below only on the kubernetes VM</p>
+
+<ol>
+	<li>Log into the kubernetes VM and run the following commands in terminal
+	<div class="codeBlock"># initialize the cluster, copy the kubeadm join command for use later when joining the docker hosts to the cluster<br />
+	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 />
+	# create .kube config directory and copy configuration<br />
+	mkdir -p $HOME/.kube<br />
+	sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config<br />
+	sudo chown $(id -u):$(id -g) $HOME/.kube/config<br />
+	# deploy the flannel network<br />
+	kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml</div>
+	</li>
+</ol>
+
+<h2>Joining Docker Nodes to the Cluster</h2>
+
+<p>Follow the steps below only on the docker1 and docker2 VMs</p>
+
+<ol>
+	<li>Log into each of the Docker VMs and run the following commands in terminal
+	<div class="codeBlock"># paste the kubeadm join command copied earlier, the command will look similar to the one below<br />
+	sudo kubeadm join 10.10.27.100:6443 --token ua6bhl.wuzsqhnf0h40hgxr \<br />
+	--discovery-token-ca-cert-hash sha256:69a89f8d81dbfc08e4098f3d43e42c78429369ca41cb2954bcbcbb15405d69ef</div>
+	</li>
+</ol>
+
+<h2>Verifying the Cluster and Testing</h2>
+
+<p>Follow the steps below on the kubernetes VM</p>
+
+<ol>
+	<li>Log into kubernetes VM and run the following commands in terminal
+	<div class="codeBlock"><br />
+	# list kubernetes nodes, all 3 VMs should be listed<br />
+	kubectl get nodes<br />
+	# deploy an example guestbook application<br />
+	# deploy mongoDB<br />
+	kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-deployment.yaml<br />
+	# deploy mongoDB service<br />
+	kubectl apply -f https://k8s.io/examples/application/guestbook/mongo-service.yaml<br />
+	# deploy guestbook frontend<br />
+	kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-deployment.yaml<br />
+	# deploy frontend service<br />
+	kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml<br />
+	# watch as the pods are created and start running, press ctrl+c once all pods are in running status<br />
+	watch -n 3 kubectl get pods<br />
+	# forward host port 8080 to container port 80<br />
+	kubectl port-forward --address 0.0.0.0 svc/frontend 8080:80</div>
+	</li>
+</ol>
+
+<h2>Removing the Test Deployments</h2>
+
+<p>Follow the steps below on the kubernetes VM</p>
+
+<ol>
+	<li>Log into kubernetes VM and run the following commands in terminal
+	<div class="codeBlock"># list the running pods<br />
+	kubectl get pods<br />
+	# delete the test pods created earlier<br />
+	kubectl delete deployment -l app.kubernetes.io/name=mongo<br />
+	kubectl delete service -l app.kubernetes.io/name=mongo<br />
+	kubectl delete deployment -l app.kubernetes.io/name=guestbook<br />
+	kubectl delete service -l app.kubernetes.io/name=guestbook<br />
+	# watch as the pods are terminated and deleted, press ctrl+c to return to the terminal<br />
+	watch -n 3 kubectl get pods</div>
+	</li>
+</ol>
+          </div>
+        </div>
+      </body>
+    </html>
+