How to install K8s and setup a cluster
Installing Kubernetes (K8s) and using it typically involves setting up a cluster, installing necessary tools, and deploying applications. Here’s a step-by-step guide for setting up a Kubernetes cluster using Minikube and managing it with kubectl.
Prerequisites
- A computer with at least 2 CPUs, 2GB of free memory, 20GB of free disk space, and a container or virtual machine manager (like Docker).
- Administrative privileges on the machine.
Step 1: Install Docker
Minikube requires a container or VM manager. Docker is a common choice.
- Install Docker:
- Windows/Mac: Docker Desktop is recommended. Download and follow the installation instructions.
- Linux: Follow the official Docker installation guide.
Step 2: Install Minikube
Minikube sets up a local Kubernetes cluster.
-
Download Minikube:
- Windows:
choco install minikube
- Mac:
brew install minikube
- Linux:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 sudo install minikube-linux-amd64 /usr/local/bin/minikube
- Windows:
-
Start Minikube:
minikube start
Step 3: Install kubectl
kubectl is the command-line tool for interacting with Kubernetes clusters.
-
Download kubectl:
- Windows:
choco install kubernetes-cli
- Mac:
brew install kubectl
- Linux:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" sudo install kubectl /usr/local/bin/kubectl
- Windows:
-
Verify Installation:
kubectl version --client
Step 4: Use Minikube and kubectl
-
Check Minikube Status:
minikube status
-
Interact with the Cluster:
- Get Cluster Info:
kubectl cluster-info
- List Nodes:
kubectl get nodes
- Deploy an Application:
kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
- Expose the Application:
kubectl expose deployment hello-minikube --type=NodePort --port=8080
- Get the URL of the Service:
minikube service hello-minikube --url
- Get Cluster Info:
Step 5: Access the Kubernetes Dashboard
- Launch the Dashboard:
minikube dashboard
Step 6: Clean Up
-
Stop Minikube:
minikube stop
-
Delete Minikube Cluster:
minikube delete
Using Kubernetes
Once your Kubernetes cluster is set up, you can deploy, manage, and scale your applications. Here are some additional commands and steps:
-
Deploy a YAML file:
kubectl apply -f <file>.yaml
-
Check Pod Status:
kubectl get pods
-
View Logs of a Pod:
kubectl logs <pod-name>
-
Execute a Command in a Pod:
kubectl exec -it <pod-name> -- /bin/bash