linux

Kubernetes在Linux上的安装指南

小樊
43
2025-10-01 15:03:17
栏目: 智能运维

Kubernetes on Linux: Installation Guide
Kubernetes is a container orchestration platform widely used to manage cloud-native applications. Installing it on Linux requires careful preparation and sequential steps to ensure a functional cluster. Below is a structured guide covering prerequisites, component installation, and cluster setup.

1. Prerequisites

Before starting, verify the following:

2. Prepare All Nodes

Perform these steps on every node (Master and Workers) to ensure consistency:

2.1 Disable Swap

Kubernetes requires swap to be disabled for proper pod scheduling. Run the following commands:

sudo swapoff -a  # Disable swap temporarily
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab  # Disable swap permanently in fstab

2.2 Configure Kernel Parameters

Enable IP forwarding and bridge networking (critical for pod communication):

cat <<EOF | sudo tee /etc/modules-load.d/kubernetes.conf
overlay
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sudo sysctl --system  # Apply settings immediately

2.3 Install Container Runtime (Docker)

Kubernetes uses a container runtime to manage containers. Docker is the most common choice:

2.4 Install Kubernetes Components (kubelet, kubeadm, kubectl)

These tools manage the cluster lifecycle:

3. Initialize the Master Node

The Master node orchestrates the cluster. Run this command on the designated Master:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

3.1 Configure kubectl for Local Access

Set up kubectl (the Kubernetes CLI) to communicate with the cluster:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

4. Join Worker Nodes

On each Worker node, run the kubeadm join command copied from the Master initialization step. For example:

sudo kubeadm join 192.168.1.100:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef

This command installs necessary components (kubelet, kube-proxy) and registers the node with the Master.

5. Install a Network Plugin

Kubernetes requires a network plugin to enable pod-to-pod communication. Popular choices include:

Verify network connectivity with:

kubectl get pods -n kube-system  # Ensure all pods are "Running"

6. Verify Cluster Health

Check the status of nodes and core components:

kubectl get nodes  # Should show all nodes in "Ready" state
kubectl get pods --all-namespaces  # Core components (e.g., kube-apiserver, kube-controller-manager) should be "Running"

Optional: Simplify Installation with microk8s

For local development or testing, use microk8s (a lightweight, single-node Kubernetes cluster):

sudo snap install microk8s --classic
sudo usermod -aG microk8s $USER  # Add current user to microk8s group
newgrp microk8s  # Apply group changes
microk8s status --wait-ready  # Verify cluster is running

Enable additional features (e.g., dashboard, DNS) with:

microk8s enable dashboard dns

By following these steps, you’ll have a functional Kubernetes cluster on Linux. Adjust commands based on your distribution (Ubuntu/CentOS) and hardware requirements. Always refer to the official Kubernetes documentation for version-specific updates.

0
看了该问题的人还看了