温馨提示×

Kubernetes在Linux上的安装指南

小樊
45
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:

  • Hardware: Minimum 2GB RAM, 2 CPU cores, and 20GB available disk space per node.
  • OS Compatibility: Supported distributions include Ubuntu (22.04/20.04), CentOS Stream 8/9, or RHEL 8/9.
  • Node Roles: Designate one node as the Master (manages the cluster) and others as Workers (run workloads).
  • Internet Access: Nodes must have internet access to download packages and container images.

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:

  • Ubuntu/Debian:
    sudo apt update
    sudo apt install -y docker.io
    sudo systemctl enable --now docker
    
  • CentOS/RHEL:
    sudo yum install -y docker
    sudo systemctl enable --now docker
    

2.4 Install Kubernetes Components (kubelet, kubeadm, kubectl)

These tools manage the cluster lifecycle:

  • Ubuntu/Debian:
    sudo apt update && sudo apt install -y apt-transport-https curl
    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    sudo apt update
    sudo apt install -y kubelet kubeadm kubectl
    sudo systemctl enable --now kubelet
    
  • CentOS/RHEL:
    sudo yum install -y epel-release
    sudo yum update -y
    sudo yum install -y kubelet kubeadm kubectl
    sudo systemctl enable --now kubelet
    

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
  • Key Output: The command generates a kubeadm join command (e.g., kubeadm join 192.168.1.100:6443 --token ...). Save this—you’ll need it to add Worker nodes.

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:

  • Flannel (lightweight, simple):
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
    
  • Calico (advanced features like network policies):
    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
    

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