Leo Zhang
菩提本无树,明镜亦非台!

# 创建/删除/修改配置

# 通过文件创建cm资源
kubectl create configmap appservice --from-file=application.properties
 
# 以json形式修改资源
kubectl edit configmaps/appservice -o json
 
# 删除指定名称pod
kubectl delete pod $(kubectl get pod | grep appservice | awk '{print $1}')
 
# 删除指定标签所有资源
kubectl -n user-100003 delete all -lappId=54359 --force
 
# 删除驱逐残留pod
kubectl get pods -n user-shanglu| grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n user-shanglu
 
# 强制删除pod
kubectl delete pod POD_NAME --force --grace-period=0

 

# 滚动更新

# 设置指定资源镜像版本
kubectl set image deployment/appservice test-deploy=hub.jhmy.com/test/appservice:latest
 
# 为指定资源添加补丁
kubectl patch deployment/appservice --patch '{"spec": {"template": {"metadata": {"annotations":{"version": "2020" }}}}}'
 
# 回滚到上个版本
kubectl rollout undo deploy/appservice
 
# 扩展副本数
kubectl scale --replicas=2 deploy/appservice

 

# 使用kubeadm命令删除节点

# 驱逐节点上服务
kubectl drain k8s-master02 --delete-local-data --force --ignore-daemonsets
 
# 删除节点
kubectl delete node k8s-master02

 

# 查看ingress配置

kubectl exec -t daemonset/nginx-ingress -n nginx-ingress cat /etc/nginx/nginx.conf
 
#查看服务全部容器日志
kubectl -n nginx-ingress logs -f -lapp=nginx-ingress --all-containers=true --max-log-requests=10

 

# 批量部署和删除

kubectl apply -Rf manifests
kubectl delete --ignore-not-found=true -Rf manifests/

 

# 节点标记

# 去除k8s-master节点污点
kubectl taint node k8s-master node-role.kubernetes.io/master-
 
# 标记节点不可部署
kubectl cordon node
 
# 标记节点可部署
kubectl uncordon node
 
# 给节点增加标签
kubectl label nodes xxx suanpan.xuelangyun.com/gpu=available

 

# 所有证书有效期查询

for tls in `find /etc/kubernetes/pki -maxdepth 2 -name "*.crt"`; \
do echo ===============$tls===============; \
openssl x509 -in $tls -text| grep Not; \
done
 
新版本>>> kubeadm alpha certs check-expiration

 

# 统计pod内存使用

for pod in $(kubectl -n user-1000327 top pod | awk '{print $1}')
do
echo `kubectl -n user-1000327 top pod $pod | grep -v NAME | awk '{print $1,$2,$3}'` >>pod.txt
kubectl -n user-1000327 get pod $pod -oyaml |grep image: | tail -n1 >>pod.txt
done

 

# 查看集群信息

[root@k8s-master wenruo]# kubectl cluster-info
Kubernetes master is running at https://192.168.73.131:6443
Heapster is running at https://192.168.73.131:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://192.168.73.131:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
 
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

 

# 批量清理操作

# 这个是删所有crash的pod
kubectl get po -A --no-headers | awk '{if ($4 == "CrashLoopBackOff") system ("kubectl -n " $1 " delete pods " $2 " --grace-period=0 " " --force ")}'
 
# 这个是重启所有deploy的
kubectl get deploy -A --no-headers | awk '{system ("kubectl rollout restart deploy -n" $1 " " $2 )}'ce

 

posted on 2022-09-06 09:57  LeoZhanggg  阅读(85)  评论(0编辑  收藏  举报