温馨提示×

k8s centos安装资源推荐

小樊
38
2025-10-26 06:47:05
栏目: 智能运维

Kubernetes on CentOS: Essential Installation Resources & Requirements

Installing Kubernetes (K8s) on CentOS requires careful preparation of system resources, configuration adjustments, and sequential execution of steps. Below are critical resources and recommendations to ensure a successful deployment.

1. System Requirements

Before starting, verify your CentOS system meets the minimum hardware and software specifications:

  • Operating System: CentOS 7.x or 8.x (64-bit). Higher versions (e.g., CentOS Stream 8/9) may require additional compatibility checks.
  • Hardware:
    • CPU: At least 2 physical cores (4+ cores recommended for production workloads with pods).
    • Memory: Minimum 2GB RAM (4GB+ recommended to avoid performance bottlenecks).
    • Storage: At least 20GB available disk space (30GB+ recommended for storing container images, logs, and persistent volumes).
  • Network: All nodes (Master/Worker) must be on the same network, with stable internet access to pull container images (e.g., from Docker Hub).

2. Pre-Installation Configuration

Preparation is key to avoiding conflicts during installation. Follow these critical steps:

  • Disable SELinux: Kubernetes does not support SELinux in enforcing mode. Run the following commands to disable it permanently:
    sudo setenforce 0 # Temporary disable sudo sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config # Permanent disable 
  • Disable Swap: Kubernetes requires swap to be turned off to manage memory efficiently. Execute:
    sudo swapoff -a # Temporary disable sudo sed -i '/swap/s//#/' /etc/fstab # Comment out swap entry in fstab (permanent disable) 
  • Configure Kernel Parameters: Adjust sysctl settings to enable bridge networking (required for pod communication). Create/Edit /etc/sysctl.d/k8s.conf and add:
    net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 
    Load the changes with sudo sysctl --system.
  • Set Hostnames & Update Hosts File: Assign unique hostnames to each node (e.g., master-node, worker-node1) using hostnamectl set-hostname <name>. Then, edit /etc/hosts on all nodes to map IP addresses to hostnames (e.g., 192.168.1.10 master-node).
  • Synchronize Time: Use ntpdate or chrony to sync system time across nodes. For example:
    sudo yum install -y ntpdate sudo ntpdate time.windows.com # Or use a local NTP server 

These steps ensure system stability and compatibility with Kubernetes.

3. Installing Prerequisite Software

Kubernetes relies on Docker (or another compatible container runtime) and its own components (kubelet, kubeadm, kubectl). Follow these steps to install them:

  • Install Docker:
    1. Add the Docker YUM repository:
      sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 
    2. Install Docker Engine and containerd:
      sudo yum install -y docker-ce docker-ce-cli containerd.io 
    3. Start and enable Docker:
      sudo systemctl enable docker && sudo systemctl start docker 
  • Add Kubernetes YUM Repository: Create a file /etc/yum.repos.d/kubernetes.repo with the following content:
    [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg 
  • Install Kubernetes Components: Install kubelet, kubeadm, and kubectl:
    sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes 
    Enable and start kubelet:
    sudo systemctl enable kubelet && sudo systemctl start kubelet 

Docker is the most commonly used container runtime, but you can also use containerd or CRI-O if preferred.

4. Initializing the Kubernetes Cluster

The kubeadm tool simplifies cluster setup. Here’s how to initialize a Master node:

  • Initialize Master: Run the following command (replace <Master_IP> with your Master node’s IP and adjust --pod-network-cidr if needed):
    sudo kubeadm init --apiserver-advertise-address=<Master_IP> --pod-network-cidr=10.244.0.0/16 
    This command generates a kubeadm join command (save it—you’ll need it for Worker nodes) and sets up the Master node.
  • Configure kubectl: Set up the kubeconfig file for your user:
    mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config 
  • Install Network Plugin: A network plugin (e.g., Flannel, Calico) is required for pod communication. For Flannel:
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml 
    For Calico (recommended for production):
    kubectl apply -f https://docs.projectcalico.org/manifests/tigera-operator.yaml kubectl apply -f https://docs.projectcalico.org/manifests/custom-resources.yaml 

The Master node is now ready. Verify its status with kubectl get nodes (it should show “NotReady” until Worker nodes join).

5. Adding Worker Nodes

To scale your cluster, join Worker nodes using the kubeadm join command generated during Master initialization. On each Worker node:

sudo kubeadm join <Master_IP>:6443 --token <token> --discovery-token-ca-cert-hash <hash> 

Replace <token> and <hash> with values from the Master’s kubeadm init output. After joining, run kubectl get nodes on the Master to confirm the Worker node status (should change to “Ready”).

6. Post-Installation Verification

Check the health of your cluster with these commands:

  • List Nodes:
    kubectl get nodes 
  • Check Pod Status:
    kubectl get pods -A 
  • Test Deployment: Deploy a sample application (e.g., Nginx) to verify functionality:
    kubectl create deployment nginx --image=nginx kubectl expose deployment nginx --type=NodePort --port=80 kubectl get svc nginx 

Access the Nginx service using the NodePort (e.g., http://<Master_IP>:<NodePort>).

By following these recommendations and steps, you can successfully install Kubernetes on CentOS. Always refer to the official Kubernetes documentation for the latest updates and troubleshooting tips.

0