centos

centos k8s安装最新版本

小樊
46
2025-10-26 06:45:59
栏目: 智能运维

Installing the Latest Kubernetes Version on CentOS
This guide provides a step-by-step workflow to install the latest stable Kubernetes version (e.g., 1.28 as of July 2025) on CentOS 7/8/9, covering environment preparation, container runtime setup, Kubernetes component installation, and cluster initialization.

1. Environment Preparation

Before starting, ensure your CentOS nodes meet the following requirements:

Execute the following commands on all nodes to configure prerequisites:

# Disable SELinux (Kubernetes requires this for pod network communication)
sudo setenforce 0
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

# Disable swap (Kubernetes does not support swap by default)
sudo swapoff -a
sudo sed -i '/swap/s/^/#/' /etc/fstab

# Configure kernel parameters for bridge networking
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
sudo modprobe br_netfilter
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system

# Install essential utilities
sudo yum install -y conntrack ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git

2. Install Container Runtime (Containerd)

Kubernetes recommends containerd as the default container runtime. Follow these steps to install it:

# Create containerd configuration directory
sudo mkdir -p /etc/containerd

# Generate default containerd configuration
sudo containerd config default | sudo tee /etc/containerd/config.toml

# Modify the configuration to use systemd as cgroup driver (required for Kubernetes)
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml

# Restart containerd to apply changes
sudo systemctl restart containerd
sudo systemctl enable containerd

3. Add Kubernetes YUM Repository

To install the latest Kubernetes components (kubelet, kubeadm, kubectl), add the official Kubernetes YUM repository:

# Create Kubernetes repository file
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/repodata/repomd.xml.key
EOF

4. Install Kubernetes Components

Install the latest versions of kubelet, kubeadm, and kubectl (replace 1.28 with your desired version if needed):

# Install components (ignore GPG errors if using Alibaba Cloud mirrors)
sudo yum install -y --nogpgcheck kubelet-1.28.3 kubeadm-1.28.3 kubectl-1.28.3

# Enable and start kubelet (required for cluster operation)
sudo systemctl enable kubelet
sudo systemctl start kubelet

5. Initialize Master Node

On the master node, use kubeadm to initialize the cluster. This command sets up the control plane (API server, scheduler, controller manager) and generates a token for worker nodes to join:

# Initialize the cluster with Flannel network plugin (recommended for beginners)
sudo kubeadm init \
  --kubernetes-version=v1.28.3 \
  --pod-network-cidr=10.244.0.0/16 \
  --cri-socket=unix:///var/run/containerd/containerd.sock \
  --apiserver-advertise-address=<MASTER_IP>  # Replace with your master node's IP

After initialization, kubeadm will output a join command (e.g., kubeadm join <MASTER_IP>:6443 --token <TOKEN> --discovery-token-ca-cert-hash <HASH>). Save this command—you’ll need it to add worker nodes later.

Configure kubectl to connect to the cluster:

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

6. Install Network Plugin (Flannel)

Kubernetes requires a network plugin for pod-to-pod communication. Flannel is a popular choice for its simplicity:

# Apply Flannel manifest (downloads and installs the plugin)
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml

7. Join Worker Nodes

On each worker node, execute the join command obtained from the master node initialization step. For example:

sudo kubeadm join 192.168.1.10:6443 --token abcdef.1234567890abcdef \
  --discovery-token-ca-cert-hash sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef

8. Verify Cluster Status

On the master node, run the following commands to confirm the cluster is healthy:

# Check node status (should show "Ready" for all nodes)
kubectl get nodes

# Check pod status (all pods should be "Running" or "Completed")
kubectl get pods -A

Troubleshooting Tips

For further assistance, refer to the official Kubernetes documentation or community forums.

0
看了该问题的人还看了