هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
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.
| Item | Value |
|---|---|
| Program | kubectl (one binary) |
| Config | ~/.kube/config, or the files in $KUBECONFIG |
| API server port | 6443/tcp (cloud clusters often 443) |
| Version rule | kubectl may be one minor version older or newer than the cluster (cluster 1.33 → kubectl 1.32–1.34) |
| Packages repo | pkgs.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).
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 --clientFor ARM servers write arm64 instead of amd64. For a fixed version, replace the $(curl … stable.txt) part with a version like v1.33.5.
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):
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.
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 nodesTurn 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.
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 podsLogs 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:80Deploy 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.yamlReal 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.
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 diffbeforeapply. - Secrets in YAML are only base64, not encrypted: use Sealed Secrets, SOPS or an external secret store.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
The connection to the server localhost:8080 was refused | No 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 timeout | Network, 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 skew | kubectl is too old or too new. | Install the matching minor version. |
Pod in CrashLoopBackOff | The app exits on start. | kubectl logs POD --previous, kubectl describe pod POD. |
Pod Pending | No node has enough CPU/RAM, or a PVC is not bound. | kubectl describe pod → Events. |
ImagePullBackOff | Wrong image name or tag, or no pull secret. | Check the image; add imagePullSecrets. |
Next steps
- Quick how-tos: Find why a Pod does not start, Logs of a crashed Pod, Shell in a Pod, Port-forward, Roll back, Scale, Switch clusters and namespaces, CPU and memory of Pods and nodes, Probes, ConfigMaps, CronJobs.
- Command pages: kubectl, helm, k9s, kustomize.
- A full Kubernetes course: CKA summary — Workloads, Troubleshooting.
- Practise: Lab: troubleshoot a cluster.
- kubectl at work: kubectl — talk to the cluster.
1Your cluster runs 1.33. Which kubectl versions are supported?
1.32, 1.33 and 1.34 (one minor version older or newer).