kubectl 命令tab自动补全配置
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
简单的kubectl 命令使用
获取节点
kubectl get nodes
创建
kubectl apply -f nginx.yaml | kubectl run nginx --image=nginx
查看创建的状态,状态为Pending【准备中】,Running状态【已经创建成功】
kubectl get pods
查看该容器在哪个节点
kubectl get pods -o wide
查看该容器的所有状态
kubectl get pods nginx -o yaml (json)
删除Pod
kubectl delete pod nginx
获取namespace信息
kubectl get namespace
条件过滤
kubectl get deployments nginx --output=jsonpath='{.spec.template.spec.containers[0].name}'
获取所有命名空间的所有资源
kubectl get all --all-namespaces
强制重启pod
kubectl replace --force -f nginx.yaml
滚动更新重启pod
kubectl rollout restart deploy nginx
更新pod的资源使用限制
kubectl set resources deploy nginx --limits=cpu=250m,memory=512Mi --requests=cpu=250m,memory=512Mi
配置基于cpu使用率的hpa自动伸缩器
kubectl autoscale deployment nginx--min=1 --max=100 --cpu-percent=70
自动刷新查看集群中所有为非Running 状态的pod
watch -n 1 "kubectl get pod --all-namespaces -o wide | grep -v Running"
pod 异常不正常启动时
kubectl describe pods nginx
kubectl 与awk、xargs 的组合命令使用
重启default命名空间下的所有无状态的pod
kubectl get deploy | grep -vE "NAME" | awk '{print $1}' | xargs kubectl rollout restart deploy
节点上下线维护
设置节点为维护不可调度状态
kubectl cordon node-1
驱逐该节点上的所有的pod
kubectl drain node-1 --ignore-daemonsets
驱逐该节点上指定标签为app:nginx的pod
kubectl drain node-1 --pod-selector="app=nginx"
设置该节点为可以调度状态
kubectl uncordon node-1
标签
添加标签
kubectl label nodes node-1 host=nginx
查看该节点上的标签
kubectl get nodes node-1 --show-labels
删除该节点上的指定标签
kubectl label nodes node-1 host-
污点与容忍度的使用
污点类型介绍:
NoSchedule:node添加这个effecf类型污点,新的不能容忍的pod不能再调度过来,但是老的运行在node上不受影响
NoExecute:K8Snode添加这个effecf类型污点,新的不能容忍的pod不能调度过来,老的pod也会被驱逐
PreferNoSchedule:pod会尝试将pod分配到该节点
添加污点
kubectl taint node node-1 host=nginx:NoSchedule
去除污点
kubectl taint node node-1 host:NoSchedule-