CentOS FetchLinux如何安装Kubernetes
Installing Kubernetes on CentOS: A Step-by-Step Guide
Kubernetes is a powerful container orchestration platform, and CentOS is a popular Linux distribution for hosting it. Below is a structured guide to installing Kubernetes on CentOS, covering environment preparation, component installation, cluster initialization, and verification.
1. Environment Preparation
Before starting, ensure your CentOS system meets the following requirements:
- System Version: CentOS 7.9 or higher (CentOS 8+ requires additional steps, such as uninstalling
podman). - Hardware: At least 2 CPU cores and 2GB RAM (4GB+ recommended for production).
- Network: All nodes (Master/Worker) must be on the same network, with internet access for downloading images and packages.
Key Preparation Steps:
- Disable SELinux:
sudo setenforce 0 # Temporary disable sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config # Permanent disable - Disable Swap Partition:
sudo swapoff -a # Disable immediately sudo sed -i '/swap/s/^/#/' /etc/fstab # Remove swap entry from fstab (permanent) - Configure Hosts File: Add IP-hostname mappings for all nodes (edit
/etc/hosts):192.168.1.100 k8s-master 192.168.1.101 k8s-worker1 192.168.1.102 k8s-worker2 - Synchronize Time: Use
ntpdateto avoid certificate validation errors:sudo yum install -y ntpdate sudo ntpdate ntp.aliyun.com
These steps ensure compatibility with Kubernetes and prevent common issues during installation.
2. Install Docker (Container Runtime)
Kubernetes requires a container runtime to manage containers. Docker is the most widely used option.
Installation Steps:
- Install Dependencies:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 - Add Docker YUM Repository:
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo - Install Docker:
sudo yum install -y docker-ce docker-ce-cli containerd.io - Start and Enable Docker:
sudo systemctl start docker sudo systemctl enable docker - Configure Docker to Use Systemd:
Edit/etc/docker/daemon.jsonto avoid conflicts with Kubernetes:Restart Docker to apply changes:{ "exec-opts": ["native.cgroupdriver=systemd"] }sudo systemctl restart docker
Docker ensures that Kubernetes can run and manage containers efficiently.
3. Configure Kubernetes YUM Repository
Add the official Kubernetes YUM repository to install kubelet, kubeadm, and kubectl.
Repository Configuration:
cat <
<
EOF >
/etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
This repository provides stable Kubernetes packages optimized for CentOS.
4. Install Kubernetes Components
Install the core components required to manage the Kubernetes cluster:
Install Commands:
sudo yum install -y kubelet kubeadm kubectl
Enable and Start kubelet:
sudo systemctl enable kubelet
sudo systemctl start kubelet
kubelet runs on every node and communicates with the control plane. kubeadm initializes the cluster, and kubectl is the command-line tool for cluster management.
5. Initialize Master Node
The Master node manages the cluster’s control plane (API server, scheduler, controller manager).
Initialization Command:
sudo kubeadm init \
--apiserver-advertise-address=<
MASTER_IP>
\ # Replace with Master's IP
--pod-network-cidr=10.244.0.0/16 \ # CIDR for Pod network (matches network plugin)
--image-repository registry.aliyuncs.com/google_containers # Use Alibaba Cloud mirror for faster downloads
Output:
After successful initialization, kubeadm will output a kubeadm join command (save this—it’s required to add Worker nodes).
6. Configure kubectl
kubectl requires a configuration file to interact with the cluster.
Configuration Steps:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Verify the configuration:
kubectl version --client
This ensures kubectl can communicate with the Master node.
7. Deploy Network Plugin
Kubernetes requires a CNI (Container Network Interface) plugin for Pod-to-Pod communication. Popular options include Flannel and Calico.
Install Flannel:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Install Calico:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Verify the network plugin is running:
kubectl get pods -n kube-system
All pods should be in the Running state before proceeding.
8. Add Worker Nodes
Worker nodes run your application containers. Use the kubeadm join command from the Master node’s initialization output to add Workers.
Example Join Command:
sudo kubeadm join 192.168.1.100:6443 \
--token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
Replace placeholders (<
MASTER_IP>
, token, hash) with values from the Master’s kubeadm init output.
9. Verify Cluster Status
After adding all nodes, verify the cluster is healthy:
Check Node Status:
kubectl get nodes
Expected output:
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane 10m v1.28.2
k8s-worker1 Ready <
none>
5m v1.28.2
k8s-worker2 Ready <
none>
3m v1.28.2
Check Pod Status:
kubectl get pods -A
All pods should be Running or Completed.
By following these steps, you’ll have a fully functional Kubernetes cluster on CentOS. Adjust parameters (e.g., --pod-network-cidr) based on your network plugin and environment requirements.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: CentOS FetchLinux如何安装Kubernetes
本文地址: https://pptw.com/jishu/737145.html
