咸宁制作网站,dedecms英文外贸网站企业模板下载,网站建设功能表,网页版微信官方一、简介 kubernetes#xff0c;简称K8s#xff0c;是用8代替名字中间的8个字符“ubernete”而成的缩写#xff0c;是一个开源的#xff0c;用于管理云平台中多个主机上的容器化的应用。kubernetes是基于容器技术的分布式架构解决方案#xff0c;具有完备的集群管理能力简称K8s是用8代替名字中间的8个字符“ubernete”而成的缩写是一个开源的用于管理云平台中多个主机上的容器化的应用。kubernetes是基于容器技术的分布式架构解决方案具有完备的集群管理能力包括多层次的安全防护和准入机制多租户应用支持能力透明的服务注册和服务发现机制内建智能负载均衡器强大的故障发现和自我修复能力服务滚动升级和在线扩容能力可拓展的资源自动调度机制以及多粒度的资源配额管理能力。同时K8s提供了完善的管理工具这些工具涵盖了包括开发部署测试运维监控在内的各个环节。K8s的目标是让部署容器化的应用简单并且高效它提供了应用部署、规划、更新、维护的一种机制。
K8s中的大部分概念如Node、Pod、ReplicationController、Service等都可以看作一种“资源对象”几乎所有的资源对象都可以通过K8s提供的kubectl工具执行增、删、改、查等操作井将其保存在etcd中持久化存储。这篇文章就整理了常用的kubectl命令供大家参考。
二、通过kubectl查看资源信息
2.1. 节点资源名称: nodes, 缩写: no
$ 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[?(.typeExternalIP)].address} # 输出jsonpath表达式定义的字段信息
$ kubectl top node [node_name] # 显示节点(CPU/内存/存储)使用情况
2.2. 容器组资源名称: pods, 缩写: po
$ kubectl get po # 显示所有容器组信息
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels # 查看容器组的labels
$ kubectl get po -l appnginx
$ 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.phaseRunning # 使用字段选择器筛选出容器组信息
2.3. 命名空间资源名称: namespaces, 缩写: ns
$ kubectl get ns
$ kubectl get ns -o yaml
$ kubectl describe ns
2.4. 无状态应用资源名称: deployments, 缩写: deploy
$ kubectl get deploy
$ kubectl describe deploy
$ kubectl get deploy -o wide
$ kubectl get deploy -o yaml
2.5. 服务资源名称: services, 缩写: svc
$ 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
$ 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
$ kubectl get events
$ kubectl get events -n kube-system
$ kubectl get events -w
2.8. 日志
$ kubectl logs [pod_name]
$ kubectl logs --since1h [pod_name]
$ kubectl logs --tail20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name]
$ kubectl logs [pod_name] pod.log
2.9. 服务帐户资源名称: serviceaccounts, 缩写: sa
$ 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. 角色
$ kubectl get roles --all-namespaces
$ kubectl get roles --all-namespaces -o yaml
2.11. 配置项资源名称: configmaps, 缩写: cm
$ kubectl get cm
$ kubectl get cm --all-namespaces
$ kubectl get cm --all-namespaces -o yaml
2.12. 路由资源名称: ingresses, 缩写: ing
$ kubectl get ing
$ kubectl get ing --all-namespaces
2.13. 副本集资源名称: replicasets, 缩写: rs
$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide
$ kubectl get rs -o yaml
三、通过kubectl变更资源属性
# 标签变更
$ kubectl label nodes node-name label-keylabel-value #增加
$ kubectl label nodes node-name label-key- #删除
$ kubectl label nodes node-name label-keylabel-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] --port80 --typeNodePort
$ kubectl scale deploy [deploy_name] --replicas5
$ 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. 创建容器
$ kubectl create -f [name_of_file]
$ kubectl apply -f [name_of_file]
$ kubectl run [pod_name] --imagenginx --restartNever
$ kubectl run [pod_name] --generatorrun-pod/v1 --imagenginx
$ kubectl run [pod_name] --imagenginx --restartNever
4.2. 创建服务
$ kubectl create svc nodeport [svc_name] --tcp8080:80
4.3. 创建无状态应用
$ kubectl create -f [name_of_file]
$ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --imagenginx
4.4. 输出YAML文件
$ kubectl create deploy [deploy_name] --imagenginx --dry-run -o yaml deploy.yaml
$ kubectl get po [pod_name] -o yaml --export pod.yaml
$ kubectl run nginx --imagenginx:alpine --dry-run -o -yaml deploy.yaml
五、其他命令
# 获取帮助
$ 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/