متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 11Kubernetes (kubeadm) · Kubernetes

Install a Kubernetes cluster with kubeadm

kubeadm builds a real Kubernetes cluster on your own servers: one control plane node and any number of worker nodes. It is the way the CKA exam and many on-prem companies do it. This guide prepares every node (kernel settings, swap, containerd), installs kubeadm, kubelet and kubectl on RHEL and Ubuntu, creates the cluster, adds a pod network and workers, and shows backups, upgrades and the fixes for nodes that stay NotReady.

  • 12 الدروس
  • 26 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

11.1

Know it before you install

Every node runs a container runtime (containerd) and the kubelet. On the control plane, kubeadm init starts the API server, etcd, the scheduler and the controller manager as static Pods. Workers join with a token. A CNI plugin (Flannel, Calico, Cilium) gives the Pods their network.

ItemValue
Packageskubeadm, kubelet, kubectl (from pkgs.k8s.io), containerd
Serviceskubelet.service, containerd.service
Cluster config/etc/kubernetes/ (admin.conf, manifests/, pki/)
etcd data/var/lib/etcd/
kubelet/var/lib/kubelet/config.yaml, logs: journalctl -u kubelet
Control plane ports6443 (API), 2379–2380 (etcd), 10250 (kubelet), 10257, 10259
Worker ports10250 (kubelet), 30000–32767 (NodePort services)

What you need (each node):

  • RHEL / Rocky / Alma 9–10 or Ubuntu 22.04 / 24.04, at least 2 CPU and 2 GB RAM (control plane), a unique hostname, MAC and product_uuid.
  • Full network between all nodes; a fixed IP for the control plane.
  • The same Kubernetes minor version on all nodes (this guide: 1.33 — change v1.33 in the URLs to the version you want).

11.2

Prepare every node

Do these steps on all nodes (control plane and workers). Turn off swap (the kubelet does not start with swap by default):

swapoff -a
sed -i '/\sswap\s/ s/^/#/' /etc/fstab

Load the kernel modules and set the network settings that Kubernetes needs:

cat > /etc/modules-load.d/k8s.conf <<'EOF'
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat > /etc/sysctl.d/k8s.conf <<'EOF'
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF
sysctl --system

Give every node a name in DNS or in /etc/hosts on all nodes:

/etc/hosts
192.0.2.10  cp1
192.0.2.21  worker1
192.0.2.22  worker2

11.3

Install containerd

On RHEL, use the containerd.io package from the Docker repository; on Ubuntu, the containerd package of Ubuntu is fine:

dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
dnf -y install containerd.io
apt-get update && apt-get install -y containerd

Make the default config and switch on the systemd cgroup driver — the kubelet uses systemd, and both must match:

mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
systemctl enable --now containerd
systemctl restart containerd

انتبه

The containerd.io package writes a config that turns off the CRI plugin. The containerd config default step above replaces it — do not skip it.

11.4

Install kubeadm, kubelet and kubectl on RHEL

cat > /etc/yum.repos.d/kubernetes.repo <<'EOF'
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.33/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.33/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
dnf -y install kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet

Open the firewall (control plane; on workers open 10250 and 30000–32767):

firewall-cmd --permanent --add-port={6443,2379-2380,10250,10257,10259}/tcp
firewall-cmd --reload

ملاحظة

The exclude= line stops a normal dnf upgrade from changing the Kubernetes packages — cluster upgrades must be done on purpose with kubeadm. The Kubernetes docs set SELinux to permissive for kubeadm clusters.

11.5

Install kubeadm, kubelet and kubectl on Ubuntu

apt-get install -y apt-transport-https ca-certificates curl gpg
mkdir -p -m 755 /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.33/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.33/deb/ /' > /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
systemctl enable --now kubelet

تذكّر

The kubelet now restarts every few seconds. That is normal: it waits for kubeadm init or kubeadm join to give it a config.

11.6

Create the cluster

On the control plane only. The Pod network range must match the CNI plugin (Flannel uses 10.244.0.0/16):

kubeadm config images pull
kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.0.2.10
Your Kubernetes control-plane has initialized successfully!
...
kubeadm join 192.0.2.10:6443 --token abcdef.0123456789abcdef \
        --discovery-token-ca-cert-hash sha256:1f2e...

Give your normal user the admin kubeconfig:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
NAME   STATUS     ROLES           AGE   VERSION
cp1    NotReady   control-plane   1m    v1.33.5

NotReady is expected now: there is no Pod network yet. Install Flannel:

kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
kubectl get pods -A
kubectl get nodes
NAME   STATUS   ROLES           AGE   VERSION
cp1    Ready    control-plane   3m    v1.33.5

في العمل

Flannel is the simplest CNI, but it does not enforce NetworkPolicies. For real clusters choose Calico or Cilium (with the Pod CIDR they expect) when you need network policies.

11.7

Join the worker nodes

On each worker, run the kubeadm join line from the init output. The token is valid 24 hours; make a new line any time on the control plane:

kubeadm token create --print-join-command
kubeadm join 192.0.2.10:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:1f2e...
kubectl get nodes -o wide
kubectl label node worker1 node-role.kubernetes.io/worker=

Test the cluster with a small app:

kubectl create deployment web --image=nginx:1.27 --replicas=2
kubectl expose deployment web --port=80 --type=NodePort
kubectl get svc web
curl -I http://worker1:31234

(Use the NodePort that kubectl get svc shows.)

11.8

Daily work: backups, certificates and upgrades

Back up etcd — the whole cluster state — often, and copy the file off the node:

ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key snapshot save /backup/etcd-$(date +%F).db
kubeadm certs check-expiration

Certificates last one year; kubeadm upgrade renews them. Upgrade one minor version at a time (1.33 → 1.34): change the repo URL to the new version, then on the control plane:

dnf -y install kubeadm-1.34.* --disableexcludes=kubernetes
kubeadm upgrade plan
kubeadm upgrade apply v1.34.1
kubectl drain cp1 --ignore-daemonsets
dnf -y install kubelet-1.34.* kubectl-1.34.* --disableexcludes=kubernetes
systemctl daemon-reload && systemctl restart kubelet
kubectl uncordon cp1

On each worker: install the new kubeadm, run kubeadm upgrade node, drain the node, upgrade kubelet, restart it and uncordon. (Ubuntu: apt-mark unhold, apt-get install kubeadm=1.34.1-1.1, then hold again.)

11.9

Make it safe

Security checklist:

  • admin.conf is the master key: keep it on the control plane, give people their own RBAC users.
  • Back up etcd (encrypted, off the node) and test the restore.
  • Use a CNI with NetworkPolicy support; start with a default-deny policy per namespace.
  • Pod Security Admission: label namespaces with pod-security.kubernetes.io/enforce=restricted (or baseline).
  • Encrypt Secrets at rest (EncryptionConfiguration for the API server).
  • Firewall: 6443 only from admin and node networks; etcd ports only between control plane nodes.
  • Upgrade at least once a year (each minor version gets about 14 months of fixes). Check with kube-bench.

11.10

Remove a node or the cluster

kubectl drain worker2 --ignore-daemonsets --delete-emptydir-data
kubectl delete node worker2
kubeadm reset -f
rm -rf /etc/cni/net.d $HOME/.kube/config

kubeadm reset on a node undoes init or join. Clean the iptables/nftables rules or reboot after it.

الأوامر في هذا الدرس

11.11

Fix common problems

ProblemCauseFix
Node stays NotReadyNo CNI plugin, or it fails.kubectl get pods -n kube-flannel; journalctl -u kubelet; install the CNI.
[ERROR Swap] or kubelet will not startSwap is on.swapoff -a and comment the swap line in /etc/fstab.
container runtime is not runningcontainerd stopped or its CRI plugin is off.Make the default config (see above), restart containerd.
Pods restart again and again, API server unstablecgroup driver mismatch.SystemdCgroup = true in containerd config; restart containerd and kubelet.
[ERROR FileContent--proc-sys-net-ipv4-ip_forward]IP forwarding is off.The sysctl file above, then sysctl --system.
kubeadm join fails with token errorThe token expired (24 h).kubeadm token create --print-join-command.
x509: certificate has expiredCertificates are older than one year.kubeadm certs renew all, then restart the control plane Pods.
CoreDNS PendingNo CNI yet.Install the CNI; CoreDNS starts after it.

11.12

Next steps

1A new node is NotReady right after kubeadm init. Is something broken?

No. The Pod network (CNI) is missing. Install it, and the node becomes Ready.

2Why must containerd use SystemdCgroup = true?

The kubelet uses the systemd cgroup driver. Both must use the same driver, or Pods become unstable.

كل الأوامر في هذا الفصل