Setting Up a Local Kubernetes Environment with Minikube on CentOS
This guide provides a step-by-step process to install Minikube and kubectl on CentOS for creating a local single-node Kubernetes cluster.
Prerequisites and References
Official Documentation:
Install kubectl
-
Download the Binary Obtain the kubectl binary for Linux.
-
Grant Execution Permissions
chmod +x ./kubectl -
Add to System Path
sudo mv ./kubectl /usr/local/bin/kubectl -
Verify Installation
kubectl version
Install Minikube
-
Download the Minikube Binary Download the Linux AMD64 version of Minikube.
-
Configure System Path
sudo mv minikube-linux-amd64 minikube && chmod +x minikube && mv minikube /usr/local/bin/ -
Verify Installation
minikube version
Initialize a Single-Node Kubernetes Cluster
Start Minikube using the none driver, which runs Kubernetes components directly on the host rather than in a VM, and specify a mirror repository.
minikube start --vm-driver=none --image-repository=gcr.azk8s.cn/google-containers
Troubleshooting Commmon Issues
Issue: Unable to Pull Container Images
Solution: Manually pull required images from an accessible registry like Alibaba Cloud and tag them appropriately.
# Example for the kube-proxy image
# First, pull the image from the mirror.
docker pull registry.cn-hangzhou.aliyuncs.com/snail-gao/k8s:kube-proxy
# Then, retag it to match the expected name.
docker tag registry.cn-hangzhou.aliyuncs.com/snail-gao/k8s:kube-proxy gcr.azk8s.cn/google-containers/kube-proxy:v1.16.2
Issue: Network Bridge Configuration
Solution: Enable bridged traffic to be processed by iptables.
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
Exploring the Kubernetes Environment
After a successful Minikube start, you can interact with your local cluster.
Check Cluster Configuration and Status
# View current kubectl configuration
kubectl config view
# List available contexts
kubectl config get-contexts
# Display cluster endpoints
kubectl cluster-info
Working with Pods: A Practical Example
1. Create a Pod Manifest File
Create a file named nginx-pod.yaml with the following content:
apiVersion: v1
kind: Pod
metadata:
name: web-server
labels:
app: webserver
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
2. Deploy the Pod
kubectl apply -f nginx-pod.yaml
3. Inspect the Pod
# List pods
kubectl get pods
# List pods with additional details (IP, Node)
kubectl get pods -o wide
# Get detailed description of a specific pod
kubectl describe pod web-server
4. Access the Pod's Container
# Access via kubectl exec
kubectl exec -it web-server -- /bin/bash
# Alternatively, access via Docker (if using the 'none' driver)
minikube ssh
docker ps
docker exec -it <CONTAINER_ID> /bin/bash
5. Forward Local Port to Pod
To access the NGINX server running in the pod from your local machine:
kubectl port-forward web-server 8080:80
You can now access the pod at http://localhost:8080.
6. Delete the Pod
kubectl delete -f nginx-pod.yaml
Minikube Management Commands
# Start the local Kubernetes cluster
minikube start
# Completely remove the Minikube VM and all data
minikube delete
# SSH into the Minikube node (VM or host)
minikube ssh
# Check the status of the Minikube cluster
minikube status
# Open the Kubernetes dashboard in your default browser
minikube dashboard
Minikube essentially provisions a lightweight environment (a VM or directly on the host) that contains a fully functional, single-node Kubernetes cluster, allowing you to interact with it via kubectl.