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

الدليل 8kubectl · Kubernetes

Install and set up kubectl

kubectl is the command-line tool for every Kubernetes cluster: EKS, AKS, GKE, k3s, OpenShift or your own lab. With it you deploy apps, read logs, open a shell in a container and fix problems. This guide installs kubectl on RHEL and Ubuntu (binary or package repository), sets up the kubeconfig, contexts, namespaces and shell completion, and shows the commands you use every day — plus the fixes for the first errors you will see.

  • 10 الدروس
  • 21 الأوامر

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

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

8.1

Know it before you install

kubectl talks to the API server of the cluster over HTTPS. Who you are and which cluster you use is written in the kubeconfig file. A context = one cluster + one user + one default namespace.

ItemValue
Programkubectl (one binary)
Config~/.kube/config, or the files in $KUBECONFIG
API server port6443/tcp (cloud clusters often 443)
Version rulekubectl may be one minor version older or newer than the cluster (cluster 1.33 → kubectl 1.32–1.34)
Packages repopkgs.k8s.io (one repo per minor version)

ملاحظة

kubectl is only the client. To run a cluster you need Kubernetes itself (kubeadm, k3s, minikube, kind or a cloud service).

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

8.2

Install the binary (any Linux)

This works on every distribution and always gets the newest stable version. Check the download with its checksum:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check
kubectl: OK
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client

For ARM servers write arm64 instead of amd64. For a fixed version, replace the $(curl … stable.txt) part with a version like v1.33.5.

8.3

Install on RHEL, Rocky, Alma and Fedora

With the package repository, dnf upgrade keeps kubectl updated. Write the minor version of your cluster in the two URLs (here 1.33):

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
EOF
dnf -y install kubectl

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

8.4

Install on Ubuntu and Debian

apt-get update
apt-get install -y apt-transport-https ca-certificates curl gnupg
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 kubectl

تذكّر

One repository = one minor version. When the cluster moves to 1.34, change v1.33 to v1.34 in the repo file and upgrade kubectl.

8.5

First configuration: kubeconfig and contexts

Get the kubeconfig of your cluster. Each platform has its own command:

aws eks update-kubeconfig --region eu-central-1 --name prod
az aks get-credentials --resource-group rg-prod --name aks-prod
gcloud container clusters get-credentials prod --region europe-west3
mkdir -p ~/.kube && sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config && sudo chown $USER: ~/.kube/config
chmod 600 ~/.kube/config

(The k3s line is for a k3s lab; a kubeadm cluster has /etc/kubernetes/admin.conf.) Now check and choose the context and default namespace:

kubectl config get-contexts
kubectl config use-context prod
kubectl config set-context --current --namespace=shop
kubectl cluster-info
kubectl get nodes

Turn on completion and a short alias — it saves hours:

echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -o default -F __start_kubectl k' >> ~/.bashrc
source ~/.bashrc

في العمل

Use different kubeconfig files for prod and test (export KUBECONFIG=~/.kube/prod), or tools like kubectx/kubens and a shell prompt that shows the context — so you never run a command on prod by mistake.

8.6

Daily use: the commands and the real uses

Look at what runs:

kubectl get pods -o wide
kubectl get deploy,svc,ingress
kubectl describe pod web-7d4b9c6f5-x2k8q
kubectl get events --sort-by=.lastTimestamp
kubectl top pods

Logs and a shell:

kubectl logs -f deploy/web
kubectl logs web-7d4b9c6f5-x2k8q --previous
kubectl exec -it deploy/web -- sh
kubectl port-forward svc/web 8080:80

Deploy and change:

kubectl apply -f k8s/
kubectl diff -f k8s/
kubectl rollout status deploy/web
kubectl rollout undo deploy/web
kubectl scale deploy/web --replicas=3
kubectl create deployment web --image=nginx:1.27 --dry-run=client -o yaml > web.yaml

Real uses at work:

  • Find why a Pod does not start (describe → Events, logs --previous).
  • Deploy YAML from Git (or check what Argo CD / Helm deployed).
  • Reach a database or an internal UI with port-forward, without opening it to the network.

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

8.7

Make it safe

Security checklist:

  • The kubeconfig is a password: chmod 600, never in Git, never in chat.
  • Use your own user with RBAC rights you need — not the cluster-admin file — in daily work.
  • Check rights: kubectl auth can-i delete pods -n shop.
  • Prefer short-lived logins (cloud SSO, OIDC) over static tokens.
  • Separate contexts or files for prod; kubectl diff before apply.
  • Secrets in YAML are only base64, not encrypted: use Sealed Secrets, SOPS or an external secret store.

8.8

Update and remove

dnf -y upgrade kubectl
apt-get update && apt-get install --only-upgrade -y kubectl
rm /usr/local/bin/kubectl

Upgrade kubectl when the cluster gets a new minor version (stay within ±1).

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

8.9

Fix common problems

ProblemCauseFix
The connection to the server localhost:8080 was refusedNo kubeconfig.Get it (see above), or export KUBECONFIG=….
error: You must be logged in to the server (Unauthorized)The token expired.Log in again (aws sso login, az login) and get new credentials.
Error from server (Forbidden)Your user has no RBAC right for that.kubectl auth can-i …; ask the cluster admin for a Role.
Unable to connect to the server: dial tcp … i/o timeoutNetwork, VPN or firewall to port 6443/443.Check the VPN; curl -k https://API:6443/version.
WARNING: version difference between client and server exceeds the supported minor version skewkubectl is too old or too new.Install the matching minor version.
Pod in CrashLoopBackOffThe app exits on start.kubectl logs POD --previous, kubectl describe pod POD.
Pod PendingNo node has enough CPU/RAM, or a PVC is not bound.kubectl describe pod → Events.
ImagePullBackOffWrong image name or tag, or no pull secret.Check the image; add imagePullSecrets.

8.10

Next steps

1Your cluster runs 1.33. Which kubectl versions are supported?

1.32, 1.33 and 1.34 (one minor version older or newer).

2A Pod is in CrashLoopBackOff. Which two commands do you run first?

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