Kubernetes Tutorial: Learn the Basics and Get Started
Tags: tutorial, kubernetes
Published: 2019-10-29 13:55:19

<!-- wp:paragraph -->
<p>It's been over five years since Google open-sourced the Kubernetes project. Even so, you might still be wondering what <a href="https://www.scalyr.com/blog/kubernetes-the-next-vmware/">Kubernetes </a>is and how you can get started using it. Well, you're in the right place! In this post, I'm going to explain the basics you need to know to get started with Kubernetes. And I won't just be throwing concepts at you—I'll <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="7" data-gr-id="7">give</g> you real code examples that will help you get a better idea of why you might need to use Kubernetes if you're thinking about using containers. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2148,"align":"center","width":217,"height":217} -->
<div class="wp-block-image"><figure class="aligncenter is-resized"><img src="https://library.scalyr.com/2019/01/14223934/Boat_wheel_with_Scalyr_colors_signifying_Kubernetes_Tutorial.png" alt="" class="wp-image-2148" width="217" height="217"/></figure></div>
<!-- /wp:image -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading -->
<h2>Getting Started With Hands-On Exercises</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before we dive into the core concepts of Kubernetes, you're going to want to brew a pot of coffee.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now, let's get started by setting up your local environment. I'm not sure about you, but I learn more quickly by doing rather than just reading or memorizing concepts. Kubernetes's learning curve is significant, and sometimes you can get overwhelmed with all the ecosystem that surrounds this popular container-orchestration system. But once you have a solid knowledge base, you'll see that Kubernetes only happens to have a lot of pieces that you need to put together to create something cool.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you don't want to go through the process of installing Kubernetes right away, there's a playground you can use called <a href="https://labs.play-with-k8s.com/">Play with Kubernetes</a>&nbsp;(PWK). I'd recommend you take the PWK approach only if installing Kubernetes becomes complicated with your current computer. There are a lot of <a href="https://v1-13.docs.kubernetes.io/docs/setup/pick-right-solution/">options to install and use Kubernetes</a>, but I'll focus only on the local environment.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Installing the Kubernetes Command Line</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before installing Kubernetes, you need to install the command line (CLI) tool called <a href="https://kubernetes.io/docs/reference/kubectl/overview/">kubectl</a>&nbsp;(pronounced: cube-cuttle). Kubectl is the CLI you'll use to interact with any Kubernetes cluster, not just the local one. If you don't want to have a local Kubernetes installation, but you want to operate a Kubernetes cluster, you'll only use kubectl.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Installing kubectl is very simple, and the instructions vary depending on your OS. It's supported for Linux, Windows, or Mac. You can follow the detailed<a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/"> instructions in the official docs page</a>. Run the following command to verify the installation:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">kubectl version</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Now continue with the local installation of Kubernetes.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Installing Kubernetes Locally</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You start by <a href="https://docs.docker.com/install/#nightly-builds">installing Docker</a>. Kubernetes does support other container systems, but I think using Docker is the easiest way to learn Kubernetes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Depending on your OS, I'd recommend a specific install method:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>If <strong>Linux</strong>, the preferred method is to&nbsp;<a href="https://kubernetes.io/docs/tasks/tools/install-minikube/">install&nbsp;minikube</a>. You'll need a hypervisor&nbsp;like <a href="https://www.virtualbox.org/wiki/Downloads">VirtualBox</a> or <a href="http://www.linux-kvm.org/">KVM</a>.</li><li>If <strong>Windows</strong> or <strong>Mac</strong>, the preferred method is to install the latest version of <a href="https://blog.docker.com/2018/07/kubernetes-is-now-available-in-docker-desktop-stable-channel/">Docker for desktop</a>. You might need to enable Kubernetes if you are already using the newest version.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>The installation process might take a while, so take this time to grab another cup of coffee.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Verify that Kubernetes is running with the following command:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">kubectl get svc</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If you don't have an error, that's it! You now have Kubernetes running locally. If you want to have a graphic interface, you can <a href="https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/">install it very quickly</a>. In this guide, I'll only use the CLI, but it's good to have the GUI just in case you want to have a graphic view of all the components.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Kubernetes Core Concepts</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You interact with Kubernetes using the API (typically via the kubectl CLI) with the purpose of setting up the desired state of your applications. That means that you tell Kubernetes how your application should run. Kubernetes will make sure that the application is running in the state you specify, even if there was a failure in the cluster or the load increase and you need to scale out the resources.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The way you set the desired state is by creating objects in Kubernetes. We'll take a look at some of the core objects that you'll need to deploy with zero-downtime a resilient application. You define these <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/">objects in a YAML format</a>, and that's what I'll use to practice the concepts.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Pods</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/">pod</a> is the smallest compute unit object you create in Kubernetes, and it's how you group one or more <a href="https://www.scalyr.com/blog/containers-benefits-and-making-a-business-case/">containers</a>. You might know that containers have only one responsibility. But the reason why a pod has more than one container is for performance and co-scheduling purposes. Containers in a pod share the same networking, the same storage, and are scheduled in the same host.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, a web app could be a container that writes the logs to a storage device. Another container that collects logs in files could use the same storage device as the web app container. That way, you don't have more than one responsibility into a container. So you should think of a pod as if it was a representation of a virtual machine, but it isn't.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You create a pod object with a YAML file definition named pod.yaml, like the following:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">apiVersion: v1
kind: Pod
metadata:
    name: helloworld
    labels:
        app: helloworld
spec:
    containers:
    - name: helloworld
      image: christianhxc/helloworld:1.0
      ports:
      - containerPort: 80
      resources:
        requests:
            cpu: 50m
        limits:
            cpu: 100m</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Now you create the pod by running the following command:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">kubectl apply -f pod.yaml</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>You then verify that the pod is running with this command:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">$ kubectl get pods
NAME         READY     STATUS    RESTARTS   AGE
helloworld   1/1       Running   0          41s</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If for some reason the pod dies, you don't have to tell Kubernetes to create the pod again. Kubernetes will recreate one automatically because the pod's state is not the same as the one you defined. Let's take a look at the following object then.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Deployments</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Pods are mortal, and that means that your application is not resilient. A <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/">deployment</a> object is how you can give immortality to pods. You define how many pods you want to have running all the time, the scaling policies, and the policy for zero-downtime deployments. If a pod dies, Kubernetes will spin up a new pod. Kubernetes continually verifies that the desired state matches with the object's definition.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's create a deployment.yaml file to say that we want to have two pods running all the time, like this:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld
  labels:
    app: helloworld
spec:
  replicas: 3
  selector:
    matchLabels:
      app: helloworld
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      containers:
      - name: helloworld
        image: christianhxc/helloworld:1.0
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 50m
          limits:
            cpu: 100m</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Now let's tell Kubernetes that we want to create the deployment object by running the following command:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">$ kubectl apply -f deployment.yaml
deployment.apps/helloworld created</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Verify that the deployment exists by running the following command:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">$ kubectl get deployments
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
helloworld   3         3         3            3           40s</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>You'll see that now you have three pods running plus the one we created previously.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">$ kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
helloworld                    1/1       Running   0          8h
helloworld-75d8567b94-7lmvc   1/1       Running   0          24s
helloworld-75d8567b94-n7spd   1/1       Running   0          24s
helloworld-75d8567b94-z9tjq   1/1       Running   0          24s</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>If you want to try killing a pod, run the command "kubectl delete podhelloworld-75d8567b94-7lmvc". You'll see how Kubernetes creates a new pod so that the desired state matches with the deployment definition. Every time you need to update the de container image, you'll change the deployment.yaml file and run the "apply" command. Then, Kubernetes will create new pods with the updated container image version. Until they're running, the pods with the previous version will be terminated. You can configure in much more detail how you want this update to happen, but for now the default behavior works for learning purposes.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Services</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>At the moment, you can't access the pods through a web browser to verify that they're working—you need to expose them. The recommended way to expose pods is via the <a href="https://kubernetes.io/docs/concepts/services-networking/service/">service object</a>. Still, other pods in the cluster can communicate between each other by using the internal IP address. But you can't rely on an IP to communicate with pods for its dynamism, which is why the solution is still a service.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A service then is a representation of a load balancer for pods. You can configure the load balancing algorithm, and if Kubernetes is integrated with a cloud provider, you'll use the native load balancers from the cloud provider.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The most straightforward way to define your services is as the following service.yaml:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">apiVersion: v1
kind: Service
metadata:
  name: helloworld
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: helloworld</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>You might notice that we've been using <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/">labels and selectors</a>, and that's because that's how a service knows to which pods redirect the requests. In the objects you've been creating, the label chosen is "app: helloworld" in the pod's metadata definition. It doesn't matter how a pod was created. As long as it has that label, it will be "registered" in the service.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's run the following command to create the service:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">$ kubectl apply -f service.yaml
service/helloworld created</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>To verify that the service is running, and to get the IP address to test it, run the following command:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">$ kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
helloworld   LoadBalancer   10.105.139.72   localhost     80:30299/TCP   21s
kubernetes   ClusterIP      10.96.0.1       &lt;none&gt;        443/TCP        12h</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Use the value given for "EXTERNAL-IP" and the "PORT" to test it. In this case, the URL should be <a href="http://localhost/api/values">http://localhost/api/values</a>&nbsp;for the container image we're using. You might receive different values depending on where you're running Kubernetes. If for some reason port 80 is taken, you can change the port in the service.yaml file and run the "apply" command again.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>And that's it! Just by using pods, deployments, and services, you can deploy a resilient stateless app in your local environment.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>What's Next?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The next step is to move forward by deploying the same app to the following environment, like the cloud. The process and flow are the same, at least with a simple app like the one I used in the examples.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Even though this&nbsp;guide is a little more advanced than a typical "hello world" app, the app is still straightforward. But that's not usually the reality in today's complex and distributed world. I just scratched the surface—Kubernetes has a lot more concepts. (For example, I didn't talk about other core concepts like <a href="https://kubernetes.io/docs/concepts/storage/volumes/">volumes</a>, <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/">namespaces</a>, <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/">ingress</a>, <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/">configmap</a>, <a href="https://kubernetes.io/docs/concepts/configuration/secret/">secrets</a>, <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/">jobs</a>, <a href="https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/">daemonset</a>, and <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/">statefulset</a>.) But the concepts I explained and demonstrated here should be a good foundation for your Kubernetes journey.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you want to continue learning more, the official documentation is a pretty good source, and here are some useful links to start:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><a href="https://kubernetes.io/docs/tutorials/kubernetes-basics/">Kubernetes basics from the official docs</a></li><li><a href="https://kubernetes.io/docs/tutorials/">Tutorials for common apps and workflows</a></li><li><a href="https://kubernetespodcast.com/">Kubernetes podcast from Google</a></li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Get your hands-on experience by practicing; it's the best way you'll learn Kubernetes.</p>
<!-- /wp:paragraph -->