『运维备忘录』之 Kubernetes(K8S) 常用命令速查

一、简介

kubernetes,简称K8s,是用8代替名字中间的8个字符"ubernete"而成的缩写,是一个开源的,用于管理云平台中多个主机上的容器化的应用。kubernetes是基于容器技术的分布式架构解决方案 ,具有完备的集群管理能力,包括多层次的安全防护和准入机制,多租户应用支持能力,透明的服务注册和服务发现机制,内建智能负载均衡器,强大的故障发现和自我修复能力,服务滚动升级和在线扩容能力,可拓展的资源自动调度机制,以及多粒度的资源配额管理能力。同时,K8s提供了完善的管理工具,这些工具涵盖了包括开发,部署测试,运维监控在内的各个环节。K8s的目标是让部署容器化的应用简单并且高效,它提供了应用部署、规划、更新、维护的一种机制。

K8s中的大部分概念如Node、Pod、ReplicationController、Service等都可以看作一种**"资源对象"**,几乎所有的资源对象都可以通过K8s提供的kubectl工具执行增、删、改、查等操作井将其保存在etcd中持久化存储。这篇文章就整理了常用的kubectl命令供大家参考。

二、通过kubectl查看资源信息

2.1. 节点(资源名称: nodes, 缩写: no)

bash 复制代码
$ kubectl get no          # 显示所有节点信息
$ kubectl get no -o wide  # 显示所有节点的更多信息
$ kubectl describe no     # 显示节点详情
$ kubectl get no -o yaml  # 以yaml格式,显示节点详情
$ kubectl get node --selector=[label_name] # 筛选指定标签的节点
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}' # 输出jsonpath表达式定义的字段信息
$ kubectl top node [node_name] # 显示节点(CPU/内存/存储)使用情况

2.2. 容器组(资源名称: pods, 缩写: po)

bash 复制代码
$ kubectl get po         # 显示所有容器组信息
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels   # 查看容器组的labels
$ kubectl get po -l app=nginx
$ kubectl get po -o yaml
$ kubectl get pod [pod_name] -o yaml --export
$ kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml   # 以yaml格式导出容器组信息到yaml文件
$ kubectl get pods --field-selector status.phase=Running    # 使用字段选择器筛选出容器组信息

2.3. 命名空间(资源名称: namespaces, 缩写: ns)

bash 复制代码
$ kubectl get ns
$ kubectl get ns -o yaml
$ kubectl describe ns

2.4. 无状态应用(资源名称: deployments, 缩写: deploy)

bash 复制代码
$ kubectl get deploy
$ kubectl describe deploy
$ kubectl get deploy -o wide 
$ kubectl get deploy -o yaml

2.5. 服务(资源名称: services, 缩写: svc)

bash 复制代码
$ kubectl get svc
$ kubectl describe svc
$ kubectl get svc -o wide 
$ kubectl get svc -o yaml
$ kubectl get svc --show-labels

2.6. 守护进程(资源名称: daemonsets, 缩写: ds)

bash 复制代码
$ kubectl get ds
$ kubectl describe ds --all-namespaces
$ kubectl describe ds [daemonset_name] -n [namespace_name]
$ kubectl get ds [ds_name] -n [ns_name] -o yaml

2.7. 事件(资源名称: events, 缩写: ev)

bash 复制代码
$ kubectl get events 
$ kubectl get events -n kube-system
$ kubectl get events -w

2.8. 日志

bash 复制代码
$ kubectl logs [pod_name]
$ kubectl logs --since=1h [pod_name]
$ kubectl logs --tail=20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name]
$ kubectl logs [pod_name] > pod.log

2.9. 服务帐户(资源名称: serviceaccounts, 缩写: sa)

bash 复制代码
$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
$ kubectl replace serviceaccount default -f ./sa.yaml

2.10. 角色

bash 复制代码
$ kubectl get roles --all-namespaces
$ kubectl get roles --all-namespaces -o yaml

2.11. 配置项(资源名称: configmaps, 缩写: cm)

bash 复制代码
$ kubectl get cm
$ kubectl get cm --all-namespaces
$ kubectl get cm --all-namespaces -o yaml

2.12. 路由(资源名称: ingresses, 缩写: ing)

bash 复制代码
$ kubectl get ing
$ kubectl get ing --all-namespaces

2.13. 副本集(资源名称: replicasets, 缩写: rs)

bash 复制代码
$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide 
$ kubectl get rs -o yaml

三、通过kubectl变更资源属性

bash 复制代码
# 标签变更
$ kubectl label nodes <node-name> <label-key>=<label-value>  #增加
$ kubectl label nodes <node-name> <label-key>- #删除
$ kubectl label nodes <node-name> <label-key>=<label-value> --overwrite #修改

# 维护/可调度
$ kubectl cordon [node_name]   # 节点维护
$ kubectl uncordon [node_name] # 节点可调度

# 节点/容器组变更
$ kubectl delete node [node_name] 
$ kubectl delete pod [pod_name]
$ kubectl edit node [node_name]
$ kubectl edit pod [pod_name]
$ kubectl drain [node_name]    # 清空节点

# 无状态/命名空间变更
$ kubectl edit deploy [deploy_name]
$ kubectl delete deploy [deploy_name]
$ kubectl expose deploy [deploy_name] --port=80 --type=NodePort
$ kubectl scale deploy [deploy_name] --replicas=5
$ kubectl delete ns
$ kubectl edit ns [ns_name]

# 服务变更
$ kubectl edit svc [svc_name]
$ kubectl delete svc [svc_name]

# 守护进程变更
$ kubectl edit ds [ds_name] -n kube-system 
$ kubectl delete ds [ds_name]

# 注释
$ kubectl annotatepo [pod_name] [annotation]
$ kubectl annotateno [node_name]

四、通过kubectl添加资源

4.1. 创建容器

bash 复制代码
$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl run [pod_name] --image=nginx --restart=Never
$ kubectl run [pod_name] --generator=run-pod/v1 --image=nginx
$ kubectl run [pod_name] --image=nginx --restart=Never

4.2. 创建服务

bash 复制代码
$ kubectl create svc nodeport [svc_name] --tcp=8080:80

4.3. 创建无状态应用

bash 复制代码
$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --image=nginx

4.4. 输出YAML文件

bash 复制代码
$ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml
$ kubectl get po [pod_name] -o yaml --export > pod.yaml
$ kubectl run nginx --image=nginx:alpine --dry-run -o -yaml > deploy.yaml

五、其他命令

bash 复制代码
# 获取帮助
$ kubectl -h
$ kubectl create -h
$ kubectl run -h
$ kubectl explain deploy.spec

# API调用
$ kubectl get --raw /apis/metrics.k8s.io/

# 获取集群信息
$ kubectl config
$ kubectl cluster-info
$ kubectl get componentstatus

参考资料:https://kubernetes.io/zh-cn/docs/reference/kubectl/

相关推荐
资源开发与学习9 小时前
Kubernetes集群核心概念 Service
kubernetes
少妇的美梦10 小时前
logstash教程
运维
容器魔方10 小时前
Bloomberg 正式加入 Karmada 用户组!
云原生·容器·云计算
chen94511 小时前
k8s集群部署vector日志采集器
运维
chen94511 小时前
aws ec2部署harbor,使用s3存储
运维
muyun280016 小时前
Docker 下部署 Elasticsearch 8 并集成 Kibana 和 IK 分词器
elasticsearch·docker·容器
東雪蓮☆16 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_2642208916 小时前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
乌萨奇也要立志学C++16 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
雨落Liy16 小时前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡