up:: [[Computing MOC]] tags:: #note/boat #note/develop #on/kubernetes # Kubernetes Kubernetes is an open-source container orchestration engine. It allows for automating deployment, scaling, and management of containerized applications. ## Getting Started There are various ways to set up and run Kubernetes. Several components can also be deployed as container images within the cluster. It is **recommended** to run Kubernetes components as container images whenever possible. However, components that run containers are an exception. For a learning environment, one can use the tools supported by the community or tools in the ecosystem to set up a cluster on a local machine. For running in production, consider which aspects of operating a cluster you want to manage yourself and which you prefer to hand off to a provider. For a cluster you're managing yourself, the officially supported tool for deploying Kubernetes is [kubeadm](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/). More info about planning a production environment can be found in the [official Kubernetes documentation](https://kubernetes.io/docs/setup/production-environment/). The notes below assume Kubernetes is being run locally in a learning environment, such as on a laptop or desktop computer. ### List of Tools - **kubectl**: allows you to run commands against Kubernetes clusters. Use to deploy applications, inspect and manage cluster resources, and view logs. - [kind](https://kind.sigs.k8s.io/): tool for running local Kubernetes clusters using Docker container "nodes" - [minikube](https://minikube.sigs.k8s.io/docs/start/): tool for setting up local Kubernetes clusters, focusing on making it easy to learn and develop for Kubernetes ### Using Minikube to Create a Kubernetes Cluster **Minikube** is a lightweight Kubernetes implementation that creates a VM on a local machine and deploys a simple cluster containing only one node. To create a minikube cluster: ```bash minikube start ``` #### Opening the Kubernetes Dashboard There are two ways to open the Kubernetes dashboard when using minikube: ##### Method 1: Launch A Browser from the Command Line Run the following command to open the Kubernetes dashboard in your default browser: ```bash minikube dashboard ``` By default, the dashboard is only accessible from within the internal Kubernetes virtual network. The `dashboard` command creates a temporary proxy to make the dashboard accessible from outside the Kubernetes virtual network. After the command exits, the dashboard remains running in the cluster. Rerun the `dashboard` command to create another proxy to access the dashboard. ##### Method 2: URL Copy and Paste To get a URL for the dashboard that can be copied and pasted, run the `dashboard` subcommand with the `--url` flag. This will output a URL that can be pasted into any browser. ```bash minikube dashboard --url ``` ## Clusters - Kubernetes coordinates a highly available cluster of computers that are connected to work as a single unit - Abstractions allow you to deploy containerized applications to a cluster without tying them specifically to individual machines - Kubernetes clusters consist of two types of resources - The **Control Plane**, which coordinates the cluster - Includes activities such as scheduling applications, maintaining applications' desired state, scaling applications, and rolling out updates - **Nodes**, which are the workers that run applications - VM or physical computer that serves as a worker machine - Each node has a **Kubelet** - Agent for managing the node and communicating with the Kubernetes control plane - Nodes also have tools for handling container operations - **A Kubernetes cluster handling production traffic should have a minimum of three nodes** - If you have less than three and a node goes down, then you've lost both an etcd member and a control place instance, meaning redundancy is compromised - You can mitigate risk by adding more control plane nodes - Can be deployed on either physical or virtual machines ## Nodes - Pods run on nodes - A **Node** is a worker machine in Kubernetes; it is either a virtual or physical machine, depending on the cluster - Nodes are managed by the control plane - A node can have multiple Pods - Every Kubernetes node runs at least: - Kubelet (manages the Pods and the containers running on a machine, responsible for communication between the Kubernetes control plane and the node) - A container runtime (like Docker) responsible for pulling the container image from a registry, unpacking the container, and running the application ## Deploying Applications - Kubernetes **Deployments** are instructions for how to create and update instances of applications - Once a deployment is created, the Kubernetes control plane schedules the application instances included in that deployment to run on individual Nodes in the cluster - Once application instances are created, a Deployment Controller continuously monitors those instances - If a node goes down, the Deployment Controller replaces the instance with an instance on another node ## Services In Kubernetes, a **Service** is an abstraction that defines a logical set of [[Kubernetes Pods|Pods]] and a policy by which to access them. Services enable loose coupling between dependent Pods. Services route traffic across a set of Pods. Services are defined using YAML or JSON. Services allow applications to receive traffic. Pod IP addresses are not exposed outside the cluster without a service. Services can be exposed in different ways by specifying a `type` in the service's spec. Some different types are: - *Cluster IP*: this is the default. It exposes the service on an internal IP in the cluster. This type makes the service only reachable from within the cluster. - *NodePort*: Exposes the service on the same port of each selected node in the cluster using NAT. Makes a service accessible from outside the cluster using `<NodeIP>:<NodePort>`. Superset of ClusterIP. - *LoadBalancer*: Creates an external load balancer and assigns a fixed, external IP to the Service. Superset of NodePort. - *ExternalName*: Maps the service to the contents of the `externalName` field by returning a CNAME record with its value. No proxying is set up. Requires v1.7 or higher of `kube-dns` or CoreDNS v0.0.8 or higher. Services match a set of Pods using labels and selectors. Labels are key/value pairs attached to objects and can be used in any number of ways. ## References - [Kubernetes Documentation](https://kubernetes.io/docs/home/)