k8s common commands
1. View all pod lists
kubectl get pod
kubectl get pod -o wide
kubectl get pod -A
kubectl get pod -n tione-demo
-o wide View details
-A View all namespace pods
-n followed by namespace
2. View the rc and service lists
kubectl get rc,svc
kubectl get pod,svc -o wide
3. Display detailed information of Node
kubectl describe node 192.168.0.212
4. Display the detailed information of the pod, especially check the log when the pod cannot be created.
kubectl describe pod <pod-name>
5. Create resources based on yaml. Apply can be executed repeatedly, but create cannot.
kubectl create -f pod.yaml
kubectl apply -f pod.yaml
6. Delete the pod based on the name defined in pod.yaml
kubectl delete -f pod.yaml
7. Delete a certain pod
kubectl delete pod pod_name -n tione-demo
8. Delete all pods (use with caution)
kubectl delete pod --all
9. View the endpoint list
kubectl get endpoints
10. Execute the date command of the pod
kubectl exec <pod-name> -- date
kubectl exec <pod-name> -- bash
kubectl exec <pod-name> -- ping 10.24.51.9
11. Obtain the TTY of a container in the pod through bash, which is equivalent to logging in to the container.
kubectl exec -it <pod-name> -c <container-name> -- bash
eg:
kubectl exec -it redis-master-cln81 -- bash
12. View the container’s logs
kubectl logs <pod-name>
kubectl logs -f <pod-name> # 实时查看日志
kubectl log <pod-name> -c <container_name> # 若 pod 只有一个容器,可以不加 -c
kubectl logs -l app=frontend # 返回所有标记为 app=frontend 的 pod 的合并日志。
eg:
Kubectl logs pod/ms-348-6d7dd57c75-vnvkb -n demo-task
13. View comments
kubectl explain pod
kubectl explain pod.apiVersion
14. View node labels
kubectl get node --show-labels
15. Restart pod
kubectl get pod <POD名称> -n <NAMESPACE名称> -o yaml | kubectl replace --force -f
16. Modify network type
kubectl patch service istio-ingressgateway -n istio-system -p '{"spec":{"type":"NodePort"}}'
17. Scaling pod replicas
Can be used to shrink a Deployment and its Pods to zero replicas, effectively killing all replicas. When you scale it back to 1/1, a new Pod will be created, restarting your application.
kubectl scale deploy/nginx-1 --replicas=0
kubectl scale deploy/nginx-1 --replicas=1
18. View the log of the previous pod, logs -p option
kubectl logs --tail 100 -p user-klvchen-v1.0-6f67dcc46b-5b4qb > pre.log
19. Get all nodes
kubectl get nodes
20. The node status changes to unschedulable
kubectl cordon hostname
21.The node status changes to schedulable
kubectl uncordon hostname
22. Modify the basic image configuration of the image’s dependencies
kubectl edit deployment nginx -n tione-demo
23. Get configmap configuration
kubectl get configmap -n ti-base
24. Modify configmap configuration
kubectl edit configmap -n ti-base ti-gateway-config