Tuesday, May 10, 2022

How to run Kubernetes clusters locally on Windows

Kubernetes production clusters are typically run on cloud platforms. However, running and deploying Kubernetes applications on cloud platforms such as Google Kubernetes Engine is costly. These high costs can restrict the Kubernetes learning process for beginners. However, running Kubernetes clusters locally helps you efficiently test applications without disrupting the production environment or paying for cloud services. 

To make things easier, the Kubernetes team developed two tools,  Minikube and Kind, that allow Kubernetes users to run clusters locally without spending a dollar. This article will cover how to do it with Kind. Kind is a command-line tool used to run Kubernetes clusters locally on your computer using Docker containers. Kind works with Docker by loading Docker containers into Kind clusters. A Kubernetes cluster is a group of nodes used to run containerized applications.

Kind uses a virtual machine on your pc to create nodes. Kind is also widely used to test and implement continuous integration tasks. Kind runs clusters locally from your computer making it easy to develop your Kubernetes applications without the need for a server.

Kind is good at creating clusters with multiple nodes. The more nodes you have, the more containerized applications you can run. These clusters created by Kind can be operated and interacted with using Kubectl. Kubectl is a command line that enables you to execute commands that communicate with your Kubernetes cluster and make changes.

In this article, you will learn how to install Kind on Windows. In addition, you will learn how to create a cluster using Kind and create a service.

Table of contents

  1. Prerequisites
  2. How to install Kind on Windows
  3. How to create and delete a cluster
  4. How to create a cluster with multiple nodes
  5. How to create a service
  6. How to export cluster logs
  7. Conclusion

Prerequisites

  • A Windows machine that has at least 8GB RAM. Using a machine that has less than 8GB of RAM will raise memory issues and complications when running a Kind cluster.
  • You need to have installed Kubectl before starting this tutorial; if you haven’t installed Kubectl, you install it from here.
  • Kind cannot function without Docker, install Docker from here.
  • In this tutorial, you will install Kind using Chocolatey. Download Chocolatey here.
  • Run Windows PowerShell as an Administrator whenever executing any Kind or Kubectl commands.

How to install Kind on Windows

In this article, you will learn how to download Kubernetes Kind using Chocolatey. Use the following command to download Kind using chocolatey:

choco install kind

Chocolatey will install 6 packages. After Kind has been successfully installed, you will get the list of the installed artifacts:

kb2919355 v1.0.20160915
dotnetfx v4.8.0.20190930
docker-desktop v4.4.4
kb2919442 v1.0.20160915
chocolatey-dotnetfx.extension v1.0.1
kind v0.11.1

To check if Kind has been installed successfully, execute the following command on Powershell:

kind version

If Kind has been installed successfully you will get the version output and your computer’s processor:

kind v0.11.1 go1.16.4 windows/amd64

Now you’re done installing Kind on Windows. You can now start creating clusters locally using kind. If you ever get stuck using Kind, use the command shown below to get all available commands:

kind -h

You will get the following list of commands as output:

Available Commands:
  build       Build one of [node-image]
  completion  Output shell completion code for the specified shell (bash, zsh or fish)
  create      Creates one of [cluster]
  delete      Deletes one of [cluster]
  export      Exports one of [kubeconfig, logs]
  get         Gets one of [clusters, nodes, kubeconfig]
  help        Help about any command
  load        Loads images into nodes
  version     Prints the kind CLI version

Flags:
  -h, --help              help for kind
      --loglevel string   DEPRECATED: see -v instead
  -q, --quiet             silence all stderr output
  -v, --verbosity int32   info log verbosity
      --version           version for kind

Use the -h flag to get more information about any command. For example, executing this command:

kind build -h

gives you the following detailed information about the build command:

Usage:
kind build [flags]
kind build [command]

Available Commands:
node-image Build the node image

Flags:
-h, --help help for build

Global Flags:
--loglevel string DEPRECATED: see -v instead
-q, --quiet silence all stderr output
-v, --verbosity int32 info log verbosity

Use "kind build [command] --help" for more information about a command.

How to create and delete a cluster

Clusters are the holy grail of Kubernetes. Clusters comprise containers with all the runtime resources needed to run applications in different operating systems.

Use the following command to create a default Kind cluster:

kind create cluster

The above command will create a default cluster called kind. If you want to use a specific name, add the --name flag to the kind create cluster command. Here is an example:

kind create cluster --name simple-cluster

The Kind create command has the following arguments and options

  • --quiet: this flag silences all stderr output.
  • --verbosity int32: This flag will log verbosity information.

When your cluster has been created successfully, you will get the following output:

Creating cluster "simple-cluster" ...
• Ensuring node image (kindest/node:v1.21.1) 🖼 ...
✓ Ensuring node image (kindest/node:v1.21.1) 🖼
• Preparing nodes 📦 ...
✓ Preparing nodes 📦
• Writing configuration 📜 ...
✓ Writing configuration 📜
• Starting control-plane 🕹 ...
✓ Starting control-plane 🕹
• Installing CNI 🔌 ...
✓ Installing CNI 🔌
• Installing StorageClass 💾 ...
✓ Installing StorageClass 💾
Set kubectl context to "kind-simple-cluster"

After you have created your cluster, use the following command to check if the cluster has been created successfully or get the list of all Kind clusters running:

kind get clusters

If you want to interact with a specific cluster locally. Use the following command and specify the name of the cluster (kind-<cluster-name>):

kubectl cluster-info --context kind-simple-cluster

You will get the following output which shows you where the Kubernetes control plane and CoreDNS are running at:

Kubernetes control plane is running at https://127.0.0.1:2450
CoreDNS is running at https://127.0.0.1:2450/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

The cluster’s configuration access will be stored in the ${HOME}/.kube/config file. This file acts as your cluster’s manifest. Here is the configuration access of the default Kind cluster created earlier:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data:
    server: https://127.0.0.1:2474
  name: kind-kind
contexts:
- context:
    cluster: kind-kind
    user: kind-kind
  name: kind-kind
current-context: kind-kind
kind: Config
preferences: {}
users:
- name: kind-kind
  user:
    client-certificate-data:
    client-key-data:

A corresponding Docker container will be created automatically when creating a Kind cluster. Use the following docker command to see the corresponding docker container:

docker ps

You will get the following information:

CONTAINER ID: 22add82af339
IMAGE: kindest/node:v1.21.1
COMMAND:"/usr/local/bin/entr..."
CREATED: About an hour ago
STATUS: Up About an hour
PORTS: 127.0.0.1:2450->6443/tcp NAMES:simple-cluster-control-plane

To view nodes created by the cluster, use the following command to view them:

kubectl get nodes

You will get the following information:

NAME                         STATUS ROLES               AGE VERSION
simple-cluster-control-plane Ready control-plane,master 89m v1.21.1

Use the following command to delete a cluster:

kind delete cluster

After the cluster has been successfully deleted, you will get the following information:

Deleting cluster "kind" ...

To delete a cluster without the default name, use the –name flag.

How to create a cluster with multiple nodes

A Kubernetes node is a virtual machine that runs a cluster. There are two types of nodes which are:

  • Worker node: A worker node executes and runs applications and containers assigned to it.
  • control-plane: A control-plane controls all the worker nodes.

You’ll need a YAML file that defines the nodes when creating a multi-node cluster.

A node can also be a physical machine. To add more nodes to your cluster, start by creating a cluster and adding different roles of nodes you want to add to your cluster. Save the following in kind-config.yaml.

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker

After saving the YAML file that has the above contents. Use the following command to apply changes:

kind create cluster --name best-cluster --config kind-config.yaml

You will get the following output; this time the output will show that Kind is creating multiple nodes and show that it is joining worker nodes at the bottom of the output:

Creating cluster "best-cluster" ...
• Ensuring node image (kindest/node:v1.21.1) 🖼 ...
✓ Ensuring node image (kindest/node:v1.21.1) 🖼
• Preparing nodes 📦 📦 📦 ...
✓ Preparing nodes 📦 📦 📦
• Writing configuration 📜 ...
✓ Writing configuration 📜
• Starting control-plane 🕹 ...
✓ Starting control-plane 🕹
• Installing CNI 🔌 ...
✓ Installing CNI 🔌
• Installing StorageClass 💾 ...
✓ Installing StorageClass 💾
• Joining worker nodes 🚜 ...
✓ Joining worker nodes 🚜

Set kubectl context to "kind-best-cluster"

You can now use your cluster with:

kubectl cluster-info --context kind-best-cluster
Thanks for using kind! 😊

To check if the nodes have been created successfully and are ready, execute the following command:

kubectl get nodes

You will get the following output:

NAME                       STATUS   ROLES                AGE   VERSION
best-cluster-control-plane Ready    control-plane,master 4m59s v1.21.1
best-cluster-worker        NotReady <none>               90s   v1.21.1
best-cluster-worker2       NotReady <none>               90s   v1.21.1

How to create a service

A Kubernetes service is used to expose your cluster and connect with authorized traffic outside your cluster. An ingress controller is a type of load balancer service that routes traffic to your service.

The following YAML file creates a pod, service, and the ingress controller objects. The service will receive traffic through the 5678 port. The ingress controller version is set to networking.k8s.io/v1.

Start by creating a YAML file that will define and configure the service. Add the following content to the YAML file:

kind: Pod
apiVersion: v1
metadata:
  name: best-cluster
  labels:
    app: best-cluster
spec:
  containers:
  - name: best-cluster
    image: hashicorp/http-echo:0.2.3
    args:
    - "-You just deployed your first service"
---
kind: Service
apiVersion: v1
metadata:
  name: simple-service
spec:
  selector:
    app: best-cluster
  ports:
  # Default port used by the image
  - port: 5678
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: simple-ingress
spec:
  rules:
  - http:
      paths:
      - pathType: Prefix
        path: "/simple"
        backend:
          service:
            name: simple-service
            port:
              number: 5678
---

After saving your YAML file, use the following command to deploy the service:

kubectl apply -f simple-service.yaml

Execute the following command to check if the service is running:

kubectl get services

You will get the following output:

NAME              TYPE      CLUSTER-IP    EXTERNAL-IP   PORT(S)  AGE
kubernetes        ClusterIP 10.96.0.1     <none>        443/TCP  72m
simple-service    ClusterIP 10.96.245.41  <none>        5678/TCP 56s

The above output shows that your cluster is now ready to receive traffic from outside the cluster through the service you just created.

How to export cluster logs

Kubernetes logs give you visibility on how your cluster performs. Some vulnerabilities and defects will be identified when inspecting and auditing logs. Kind has the following command that is used to export cluster logs to your desired directory on your pc:

kind export logs C:\Users\redgate

The above command will give you the following output, exporting logs of the default cluster. Use the –name flag if you have named the cluster something else:

Exporting logs for cluster “kind” to:
C:\Users\redgate

The structure of the logs will look like this in the folder you exported them to

├── docker-info.txt
└── kind-control-plane/     
         ├── containers     
         ├── docker.log     
         ├── inspect.json     
         ├── journal.log     
         ├── kubelet.log     
         ├── kubernetes-
version.txt
        └── pods/

Conclusion

In this article you have learned:

  1. How to install Kind on Windows.
  2. How to create a cluster using Kind.
  3. How to create a cluster with multiple nodes.
  4. How to create a service.

The above skills are enough to get you started with the journey of creating Kubernetes clusters using Kind. You can advance your Kubernetes skills on the Kubernetes official page here.

 

The post How to run Kubernetes clusters locally on Windows appeared first on Simple Talk.



from Simple Talk https://ift.tt/29nW1CD
via

No comments:

Post a Comment