本文已参与「新人创作礼」活动,一起开启掘金创作之路
一、概述
kubectl 是 Kubernetes 集群的命令行工具, 通过 kubectl 能够对集群本身进行管理, 并能够在集群上进行容器化应用的安装部署。
二、kubectl 命令的语法
kubectl [command] [TYPE] [NAME] [flags]
(1)comand:指定要对资源执行的操作,例如 create、get、describe 和 delete
# create
# 创建一个名字为web,镜像是ginx的部署(又名deploy:是k8s中的一种资源,后面会讲到)
[root@k8smaster ~]# kubectl create deploy web --image=nginx
deployment.apps/web created
# get
# 获取这个部署的详情
[root@k8smaster ~]# kubectl get deploy web
NAME READY UP-TO-DATE AVAILABLE AGE
web 1/1 1 1 35s
# 获取所有的节点
[root@k8smaster ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 6d15h v1.21.9
k8s-slave001 Ready <none> 6d14h v1.21.9
k8s-slave002 Ready <none> 6d14h v1.21.9
k8s-slave003 Ready <none> 6d14h v1.21.9
# 获取kubectl get 帮助
[root@k8s-master ~]# kubectl get --help
#describe
# 输出资源的信息,包括基本信息和运行信息,比如镜像拉取情况,一般通过它去看
[root@k8smaster ~]# kubectl describe deploy web
Name: web
Namespace: default
CreationTimestamp: Thu, 24 Feb 2022 11:38:51 +0800
Labels: app=web
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=web
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=web
Containers:
nginx:
Image: nginx
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: web-96d5df5c8 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m38s deployment-controller Scaled up replica set web-96d5df5c8 to 1
# delete
# 删除之前创建的部署
[root@k8smaster ~]# kubectl delete deploy web
deployment.apps "web" deleted
# expose
# 将资源暴露为新的Kubernetes Service,Service,Service的功能是一组具有相同功能的容器应用提供一个统一的入口地址,并且将请求进行负载分发到后端的各个容器应用上
kubectl expose deploy web --port=80 --target-port=80
# 暴露后使用以下指令进行查询
[root@k8s-node4 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 39d
web ClusterIP 10.233.50.30 <none> 80/TCP 7m37s
# 可以通过ClUSTER-IP+端口进行访问应用
# run
# 启动一个pod
kubectl run nginx --image=nginx
# set 改变已经存在的资源,环境变量、镜像地址、限制、权限等
-c:设置镜像,--limits设置cpu和内存
kubectl set resources deployment web -c=nginx --limits=cpu=200m,memory=512Mi
# edit yaml的方式改变资源的信息
kubectl edit deploy web
# scale 扩容或缩容 Deployment、ReplicaSet、Replication Controller或 Job 中Pod数量
kubectl scale --replicas=3 deploy/web
其他命令使用使用kubectl --help
进行查询
(2)TYPE:指定资源类型,资源类型是大小写敏感的,开发者能够以单数、复数和缩略的 形式。例如:
# 首先把之前的部署在重新创建出来
kubectl create deploy web --image=nginx
kubectl get deploy
#全拼
kubectl get deployment
(3)NAME: 指定资源的名称,名称也大小写敏感的。如果省略名称,则会显示所有的资源, 例如:
kubectl get deploy web
(4)flags:指定可选的参数。例如,可用-s 或者–server 参数指定 Kubernetes API server 的地址和端口。
三、帮助
可以通过 kubectl --help
查看命令帮助
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的 Kubernetes
Service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
Basic Commands (Intermediate):
explain 查看资源的文档
get 显示一个或更多 resources
edit 在服务器上编辑一个资源
delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a Deployment, ReplicaSet or Replication Controller
autoscale Auto-scale a Deployment, ReplicaSet, StatefulSet, or ReplicationController
Cluster Management Commands:
certificate 修改 certificate 资源.
cluster-info 显示集群信息
top 显示 Resource (CPU/Memory) 使用.
cordon 标记 node 为 unschedulable
uncordon 标记 node 为 schedulable
drain Drain node in preparation for maintenance
taint 更新一个或者多个 node 上的 taints
Troubleshooting and Debugging Commands:
describe 显示一个指定 resource 或者 group 的 resources 详情
logs 输出容器在 pod 中的日志
attach Attach 到一个运行中的 container
exec 在一个 container 中执行一个命令
port-forward Forward one or more local ports to a pod
proxy 运行一个 proxy 到 Kubernetes API server
cp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff live version against would-be applied version
apply 通过文件名或标准输入流(stdin)对资源进行配置
patch Update field(s) of a resource
replace 通过 filename 或者 stdin替换一个资源
wait Experimental: Wait for a specific condition on one or many resources.
kustomize Build a kustomization target from a directory or URL.
Settings Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config 修改 kubeconfig 文件
plugin Provides utilities for interacting with plugins.
version 输出 client 和 server 的版本信息