Created
February 21, 2021 14:41
-
-
Save iamabhishek-dubey/3d913b705e663370241600fdc4157e3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1 id="k8s-kubectl-lab">K8s | Kubectl Lab</h1> | |
<p>Let's try to run an elasticsearch container on Kubernetes.</p> | |
<h2 id="installation">Installation</h2> | |
<p>Kubectl is a command-line tool for controlling Kubernetes clusters.</p> | |
<h3 id="linux">Linux</h3> | |
<p>Download the latest release with the command:</p> | |
<pre><code class="lang-shell">curl -LO https:<span class="hljs-regexp">//</span>storage.googleapis.com<span class="hljs-regexp">/kubernetes-release/</span>release<span class="hljs-regexp">/`curl -s https:/</span><span class="hljs-regexp">/storage.googleapis.com/</span>kubernetes-release<span class="hljs-regexp">/release/</span>stable.txt`<span class="hljs-regexp">/bin/</span>linux<span class="hljs-regexp">/amd64/</span>kubectl | |
</code></pre> | |
<p>Make the kubectl binary executable.</p> | |
<pre><code class="lang-shell"><span class="hljs-keyword">chmod</span> +<span class="hljs-keyword">x</span> ./kubectl | |
</code></pre> | |
<p>Move the binary into your PATH.</p> | |
<pre><code class="lang-shell">sudo mv .<span class="hljs-regexp">/kubectl /u</span>sr<span class="hljs-regexp">/local/</span>bin<span class="hljs-regexp">/kubectl</span> | |
</code></pre> | |
<h3 id="macos">macOS</h3> | |
<p>Download the latest release with the command:</p> | |
<pre><code class="lang-shell">curl -LO "https://storage.googleapis.com/kubernetes-<span class="hljs-keyword">release</span>/<span class="hljs-keyword">release</span>/$(curl -s https://storage.googleapis.com/kubernetes-<span class="hljs-keyword">release</span>/<span class="hljs-keyword">release</span>/stable.txt)/<span class="hljs-keyword">bin</span>/darwin/amd64/kubectl<span class="hljs-string">"</span> | |
</code></pre> | |
<p>Make the kubectl binary executable.</p> | |
<pre><code class="lang-shell"><span class="hljs-keyword">chmod</span> +<span class="hljs-keyword">x</span> ./kubectl | |
</code></pre> | |
<p>Move the binary into your PATH.</p> | |
<pre><code class="lang-shell">sudo mv .<span class="hljs-regexp">/kubectl /u</span>sr<span class="hljs-regexp">/local/</span>bin<span class="hljs-regexp">/kubectl</span> | |
</code></pre> | |
<h4 id="run-command">Run Command</h4> | |
<pre><code class="lang-shell">kubectl <span class="hljs-built_in">run</span> elasticsearch <span class="hljs-comment">--image=opstree/empms-es:1.0 --replicas=1</span> | |
</code></pre> | |
<p>In this command, we are using the run command to create an elasticsearch container on Kubernetes.</p> | |
<ul> | |
<li><code>--image</code> flag is used to provide the name of the image</li> | |
<li><code>--replicas</code> is the count of container which will run</li> | |
</ul> | |
<h4 id="logs-and-get-command">Logs and Get-Command</h4> | |
<p>Let's validate the elasticsearch by reading its stdout logs, but for that, we have to get the name of the container, because Kubernetes appends its own id with the container as well.</p> | |
<p>To list the containers, we can use this command:-</p> | |
<pre><code class="lang-shell">kubectl <span class="hljs-keyword">get</span> pods | |
</code></pre> | |
<p>Once we get the name of the pod/container we can read their logs:-</p> | |
<pre><code class="lang-shell"><span class="hljs-attribute">kubectl logs elasticsearch</span> | |
</code></pre> | |
<h4 id="exec-command">Exec Command</h4> | |
<p>Let's try to go inside the running container of elasticsearch and validate its functionality.</p> | |
<pre><code class="lang-shell">kubectl exec -<span class="hljs-keyword">it</span> elasticsearch bash | |
</code></pre> | |
<p>Just like docker, we will use <code>exec</code> with <code>it</code> mode to access the container</p> | |
<p>Let's put a curl request to validate it.</p> | |
<pre><code class="lang-shell">curl -XGET -u <span class="hljs-string">elastic:</span>elastic <span class="hljs-string">http:</span><span class="hljs-comment">//localhost:9200</span> | |
</code></pre> | |
<h4 id="delete-command">Delete Command</h4> | |
<p>Let's delete the complete elasticsearch setup</p> | |
<pre><code class="lang-shell">kubectl <span class="hljs-keyword">delete</span> pod elasticsearch | |
</code></pre> | |
<h2 id="mysql-setup">MySQL Setup</h2> | |
<p>Now let's try to setup MySQL on Kubernetes with kubectl client</p> | |
<pre><code class="lang-shell"><span class="hljs-comment">kubectl</span> <span class="hljs-comment">run</span> <span class="hljs-comment">mysql</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">image=opstree/empms</span><span class="hljs-literal">-</span><span class="hljs-comment">db:1</span><span class="hljs-string">.</span><span class="hljs-comment">0</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">env</span> <span class="hljs-comment">MYSQL_DATABASE=attendancedb</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">replicas=1</span> | |
</code></pre> | |
<pre><code class="lang-shell">kubectl <span class="hljs-keyword">get</span> pods | |
</code></pre> | |
<h4 id="cp-command">CP Command</h4> | |
<p>Let's download a dummy SQL file from <a href="https://sp.mysqltutorial.org/wp-content/uploads/2018/03/mysqlsampledatabase.zip">here</a></p> | |
<pre><code class="lang-shell"><span class="hljs-selector-tag">unzip</span> <span class="hljs-selector-tag">mysqlsampledatabase</span><span class="hljs-selector-class">.zip</span> | |
</code></pre> | |
<p>Once the SQL file is downloaded, we can copy that file to MySQL container</p> | |
<pre><code class="lang-shell">kubectl cp mysqlsampledatabase.sql <span class="hljs-string">mysql:</span><span class="hljs-regexp">/tmp/</span> | |
</code></pre> | |
<p>Let's try to create the table using this SQL file</p> | |
<pre><code class="lang-shell">kubectl exec -<span class="hljs-keyword">it</span> mysql bash | |
</code></pre> | |
<pre><code class="lang-shell">cd /tmp | |
mysql -u root -<span class="hljs-selector-tag">p</span> attendancedb < mysqlsampledatabase.sql | |
</code></pre> | |
<h4 id="port-forward-command">Port-Forward Command</h4> | |
<p>Now let's try to access MySQL from localhost</p> | |
<pre><code class="lang-shell"><span class="hljs-selector-tag">kubectl</span> <span class="hljs-selector-tag">port-forward</span> <span class="hljs-selector-tag">mysql</span> 3306<span class="hljs-selector-pseudo">:3306</span> | |
</code></pre> | |
<p>Install mysql-client on your local system</p> | |
<p>Try to connect to MySQL from the host machine</p> | |
<pre><code class="lang-shell"><span class="hljs-attribute">mysql -u root -p</span> | |
</code></pre> | |
<p>Let's delete the setup</p> | |
<pre><code class="lang-shell">kubectl <span class="hljs-keyword">delete</span> pod mysql | |
</code></pre> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment