给你来一份Kubernetes常用命令备忘录

本文主要整理了Kubernetes常用命令,给朋友们一个备忘录。

查看K8S的帮助命令

复制

kubectl --help
  • 1.

切换被操作的集群

默认情况下会在.kube目录下的config文件里的证书去操作K8S集群。如果碰到需要切换访问别的K8S集群的场景,可以使用kubectl --kubeconfig xxxxxx去指定某个证书文件,比如:

复制

kubectl --kubeconfig (xxxx.json | xxxxconfig) get node
  • 1.
创建资源对象

复制

kubectl apply -f xxx.yaml
  • 1.
查找资源对象

复制

# 查到node
kubectl get node
# 查到namespace
kubectl get ns
# 查找service
kubectl get svc -n demo
# 查找部署资源
kubectl get deployment -n demo
# 查找pod
kubectl get pods -n demo
# 查找pod,展示更多信息
kubectl get pods -n demo -o wide
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
描述资源对象

describe命令非常重要,当你部署资源出现问题时,可以通过这个命令去查看原因。

复制

# 描述pod
kubectl describe pods webapp -n demo
# 描述node
kubectl describe node k8s-worker-2
# 描述ingress
kubectl describe ingress xxxxxx-ingress -n demo
# 描述service
kubectl describe svc web-service -n demo
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
伸缩资源

复制

扩展副本数到4:
kubectl scale --replicas=4 deployment/xxxxxx -n demo
重新缩减副本数到2:
kubectl scale --replicas=2 deployment/xxxxxx -n demo
根据情况自动缩放:
kubectl autoscale deployment xxxxxx --min=1 --max=4 -n demo
kubectl autoscale deployment ehome-cloud-server --min=1 --max=3 -n test1 --cpu-percent=80

获取水平自动缩放器
kubectl get hpa -n test1
删除水平自动缩放器
kubectl delete hpa ehome-cloud-server -n test1
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
操作label

label是集群内部用来查找和筛选的标签,非常重要。

复制

kubectl label node k8s-worker-3 app=backend
kubectl label node k8s-worker-3 app-
kubectl get nodes --show-labels
kubectl get nodes k8s-worker-3 --show-labels
kubectl label pods xxxxxx addlabel=addlabelval -n demo
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
添加污点

复制

# 添加污点
kubectl taint nodes node01 key=value:effect
# 删除污点
kubectl taint nodes node01 key[:effect]-
  • 1.
  • 2.
  • 3.
  • 4.
删除资源

复制

kubectl delete -f xxx.yaml
kubectl delete pod xxx -n demo
  • 1.
  • 2.
查看日志

复制

kubectl logs -f xxxxxx-9d8695998-s9cwk -n demo
kubectl logs xxxxxx-9d8695998-s9cwk -n demo
  • 1.
  • 2.
进入容器内部

复制

kubectl exec -it webapp -n demo -- /bin/sh
  • 1.

查看使用资源的情况

复制

kubectl top pod
kubectl top node
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值