k8s常用命令

相关服务及用途节点名称服务名称用途管理节点kubctl-apiserver提供HTTP Rest统一接口服务,处理和验证REST请求和更新etcd中API对象的状态管理节点kubectl-controller-manager资源控制管理同步管理节点kube-scheduler负责资源调度(调度Pod)工作节点kubelet管理Pod的生命周期,创建、启停等任务工作节点kube-proxy负责网络通信及负载均衡### 1.重启服务kubectl patch deployment app名称 -p {"spec":{"template":{"metadata":{"labels":{"date":"date +'%s'"}}}}} -n kube-system

2.重新分配pod数量kubectl scale deployment kubernetes-dashboard --replicas=1 -n kube-system

3.查看部署应用kubectl get deploy -n kube-system

4.获得容器前一次运行的日志内容kubectl logs --previous

kubectl logs --previous l7-lb-controller-74c67fb85d-5b5cg -n kube-system

5.查看描述kubectl describe svc kubernetes-dashboard -n kube-system

kubectl describe pod kubernetes-dashboard -n kube-system

6.查看pod部署节点及IPkubectl get pods -n kube-system -o wide

7.查看集群中服务kubectl get services --all-namespaces

kubectl get svc kubernetes-dashboard -n kube-system

8.查看暴露端口kubectl get endpoints -n kube-system kubernetes-dashboard

kubectl get ep -n kube-system kubernetes-dashboard

9.标签管理添加标签kubectl label node 192.168.61.66 nodetype=node --overwrite

lsmod | grep ip_vs
查看标签kubectl get node --show-labels -n kube-system
kubectl get pods --show-labels kube-sytem
kubectl get pod --show-labels kubernetes-dashboard-6845889d64-25f9x -n kube-system

10.查看svc/pod描述kubectl describe 类型 资源名称 命名空间

kubectl describe svc kubernetes-dashboard -n kube-system
kubectl describe pod kubernetes-dashboard -n kube-system

11.查询日志检查某种Pod的日志(标准输出)kubectl logs -f pod名称 -n kube-system

在容器内执行命令kubectl exec -ti pod名称 /bin/bash -n kube-system

12.以yaml/json方式显示pod信息kubectl get deployment -o yaml kubernetes-dashboard -n kube-system

kubectl get daemonset -o yaml -n kube-system calico-node
kubectl get svc -o json pod名称 -n kube-system

13.显示所有的服务kubectl get deployments -n kube-system

kubectl get daemonsets -n kube-system

14.创建命名空间通过命令创建kubectl create namespace new-namespace

通过文件创建cat > my-namespace.yaml << EOF
apiVersion: v1
kind: Namespace
metadata:
name: new-namespace
EOF
kubectl create -f ./my-namespace.yaml
删除命令空间kubectl delete namespaces new-namespace
删除一个namespace会自动删除所有属于该namespace的资源。default 和 kube-system 命名空间不可删除。### 15.创建/查看 Deployment 记录版本号kubectl create -f nginx-deployment.yaml --record

16.实现水平扩展或收缩kubectl scale deployment kubernetes-dashboard --replicas=2 -n kube-system

重新分配pod数量kubectl scale deployment kubernetes-dashboard --replicas=1 -n kube-system

17.版本回退查看回退状态kubectl rollout status deployment/kubernetes-dashboard -n kube-system

查看升级历史记录kubectl rollout history deployment/kubernetes-dashboard -n kube-system
查看单个revision的详细信息kubectl rollout history deployment/kubernetes-dashboard --revision=1 -n kube-system
回滚到上一个版本kubectl rollout undo deployment/kubernetes-dashboard -n kube-system
指定回滚某个历史版本kubectl rollout undo deployment/kubernetes-dashboard --to-revision=1 -n kube-system

18.自动扩展集群支持 horizontal pod autoscaling,可以为Deployment设置自动扩展kubectl autoscale deployment kubernetes-dashboard --min=1 --max=5 --cpu-percent=80 -n kube-system

查看hap状态kubectl get hpa kubernetes-dashboard -n kube-system
查看hpa详细状态kubectl describe hpa kubernetes-dashboard -n kube-system
删除hpakubectl delete hpa kubernetes-dashboard -n kube-system

19.更新Deployment镜像kubectl set image deployment kubernetes-dashboard nginx=nginx:1.9.1

20.编辑 Deployment pod配置kubectl edit deployment kubernetes-dashboard -n kube-system

21.升级资源暂停状态下执行kubectl rollout pause deployment kubernetes-dashboard -n kube-system

kubectl set image deploy kubernetes-dashboard nginx=nginx:1.9.1
更新升级资源kubectl set resources deployment kubernetes-dashboard -c=kubernetes-dashboard --limits=cpu=200m,memory=512Mi -n kube-system
恢复kubectl rollout resume deploy kubernetes-dashboard -n kube-system

22.查看CIDR划分kubectl get nodes -o json | jq '.items[] | .spec'## 23.授权管理查看授权node节点kubectl get certificatesigningrequests

kubectl get csr
添加新工作节点kubectl get csr|grep 'Pending' | awk 'NR>0{print 1}';do kubectl certificate approve 1}';do kubectl certificate deny 1}';do kubectl delete csr $i;done

24.集群信息查看下载 kubeadmcurl -sSL kubeadm > /usr/bin/kubeadm

查看集群访问连接kubectl cluster-info
查看权限kubectl describe clusterrole cluster-admin -n kube-system
查看kubectl版本kubectl version
查看支持api版本kubectl api-version
查看当前kubectl配置kubectl config view
查看集群状态kubectl get componentstatuses
kubectl get rs
查看集群节点kubectl get nodes
查看命名空间kubectl get namespaces
Node状态维护每个Node都包括以下状态信息
地址:包括hostname、外网IP和内网IP
条件(Condition):包括OutOfDisk、Ready、MemoryPressure和DiskPressure
容量(Capacity):Node上的可用资源,包括CPU、内存和Pod总数
基本信息(Info):包括内核版本、容器引擎版本、OS类型等
维护状态/取消维护模式kubectl cordon NodeName
kubectl uncordon NodeName

你可能感兴趣的:(k8s常用命令)