Kubectl命令行工具是与Kubernetes交互的主要工具,你可以使用Kubectl在Kubernetes集群中创建、更新和管理资源,例如Pod,Service和Deployment等。接下来我们将了解Kubernetes运维必须掌握的12个Kubectl命令。
1.kubectl cluster-info
使用以下命令提供有关Kubernetes集群当前状态信息,包括API服务器地址、集群状态等信:
$ kubectl cluster-info
Kubernetes control plane is running at https://10.0.42.142:6443
metrics-server is running at https://10.0.42.142:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://10.0.42.142:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
2.kubectl version
使用以下命令显示系统上当前安装的 kubectl 版本,以及它连接到的 Kubernetes 集群的版本:
$ kubectl version
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short. Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"24+", GitVersion:"v1.24.6-aliyun.1", GitCommit:"a45529632bcea0750cf04a1122c73f0fc97d26b0", GitTreeState:"clean", BuildDate:"2023-03-08T03:10:49Z", GoVersion:"go1.18.6", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.4
Server Version: version.Info{Major:"1", Minor:"24+", GitVersion:"v1.24.6-aliyun.1", GitCommit:"5296768e052ba56e92b5d5bf7b52d33973a34c6f", GitTreeState:"clean", BuildDate:"2023-04-19T06:36:28Z", GoVersion:"go1.18.6", Compiler:"gc", Platform:"linux/amd64"}
3.kubectl get
此命令将提供 Kubernetes 集群中可用资源的列表,常用资源列表如下:
- Namespace
- Deployment
- Pod
- Node
- Service
- ReplicaSets
使用以下命令查看当前default命名空间的所有可用资源:
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/mysql-tnj2g 1/1 Running 0 37d
pod/oom-cb7dbb86d-tkw5d 1/1 Running 0 9d
NAME DESIRED CURRENT READY AGE
replicationcontroller/mysql 1 1 1 3y83d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 172.25.0.1 <none> 443/TCP 4y115d
service/oom-svc ClusterIP 172.25.14.133 <none> 8080/TCP 224d
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/willie-oom 1/1 1 1 224d
NAME DESIRED CURRENT READY AGE
replicaset.apps/oom-55b85549b6 0 0 0 50d
replicaset.apps/oom-6b7cf75c98 0 0 0 9d
使用以下命令查看当前default命名空间的deployment资源:
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
oom 1/1 1 1 224d
指定特定的命名空间:(-n 参数是指定特定命名空间 -namespace 的缩写形式):
$ kubectl get deployments -n kube-system
NAME READY UP-TO-DATE AVAILABLE AGE
ags-metrics-collector 1/1 1 1 3y111d
alibaba-log-controller 1/1 1 1 49d
alicloud-disk-controller 1/1 1 1 4y115d
查看指定资源的更多详细信息:(-o参数是查看更多详细信息):
$ kubectl get deployments -n kube-system -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
ags-metrics-collector 1/1 1 1 3y111d ags-metrics-collector registry.cn-hangzhou.aliyuncs.com/gene-aliyun/ags-metrics-collector:v1.0.0.2-cc3b2d6-aliyun app=ags-metrics-collector,owner=aliyun
alibaba-log-controller 1/1 1 1 49d alibaba-log-controller registry-cn-shanghai-vpc.ack.aliyuncs.com/acs/log-controller:0.3.3.0-d37b3ff-aliyun k8s-app=alibaba-log-controller
4.kubectl create
Kubernetes 命令 kubectl create 用于向集群添加新资源。用户可以使用此命令创建 Pod、Service和Deployment等资源。
使用以下命令,将使用 nginx 映像创建一个名为my-nginx 的新deployment:
$ kubectl create deployment my-nginx --image=nginx
deployment.apps/my-nginx created
使用以下命令创建新的 cronjob 的另一个示例:
$ kubectl create job my-cronjob --schedule="*/5 * * * *" --image=busybox -- command -- args="echo This is a cron job!"
命令参数:
- --schedule 指定cron语法中任务计划
- --image 指定运行容器镜像
- --command 执行容器运行的命令
5.kubectl edit
kubectl edit 命令,可以编辑集群中的现有资源对象。您可以使用 kubectl edit 直接修改资源的配置,这样您就无需手动生成新的 YAML 文件。以下命令修改名为my-nginx的deployment的资源配置:
$ kubectl edit deployments my-nginx
deployment.apps/my-nginx edited
6.kubectl delete
Kubectl delete 命令将帮助您删除 Kubernetes 集群中的任何资源,例如 pod、deployment、service、cornjob等资源。使用以下命令删除名为my-nginx的deplyment资源:
$ kubectl delete deployments.apps my-nginx
deployment.apps "my-nginx" deleted
7.kubectl apply
kubectl apply命令使您能够通过YAML编排文件,在集群中创建或修改资源:
$ kubectl apply -f deployment.yaml
8.kubectl config
在 Kubernetes 中,命令 kubectl config 允许您管理 kubectl 客户端的配置。config 命令可用于查看、编辑或在多个集群配置之间切换,以及管理用户凭据和上下文设置:
$ kubectl config set-context --current --namespace=NAMESPACE
kubectl config set-context 是 Kubernetes 中的一个命令,允许您修改 kubectl 配置的上下文。上下文定义了 kubectl 命令操作的集群、用户和命名空间。在此示例中,此命令将当前命名空间设置为“NAMESPACE”。
9.kubectl describe
kubectl describe 提供了一种快速方法来获取有关资源的全面信息,从而更轻松地了解资源的当前状态并发现任何问题。它显示有关资源状态、事件和元数据的详细信 使用以下命令可以查看Pod名称为metrics-server-85bd976946-rlk6c的详细信息:
$ kubectl describe -n kube-system pod metrics-server-85bd976946-rlk6c
Name: metrics-server-85bd976946-rlk6c
Namespace: kube-system
Priority: 2000001000
Priority Class Name: system-node-critical
Node: cn-shanghai.i-uf64cmp52ak0xgn2l0aa/10.0.41.110
Start Time: Mon, 08 May 2023 23:30:47 +0800
Labels: k8s-app=metrics-server
pod-template-hash=85bd976946
Annotations: kubernetes.io/psp: ack.privileged
Status: Running
IP: 172.24.3.211
IPs:
IP: 172.24.3.211
Controlled By: ReplicaSet/metrics-server-85bd976946
Containers:
metrics-server:
Container ID: containerd://aae2f389294e5000222e6e015df2df2283ffe91d5cd4243d2cf7a60479f7e666
Image: registry-vpc.cn-shanghai.aliyuncs.com/acs/metrics-server:v0.3.9-d554015-aliyun
Image ID: registry-vpc.cn-shanghai.aliyuncs.com/acs/metrics-server@sha256:c920f8733e79e06aaab201eb58a5d3ebdac5e87fe1cbb4ecb5b32af843d39a04
Port: <none>
Host Port: <none>
Command:
/metrics-server
--source=kubernetes.hybrid:''
--sink=socket:tcp://monitor.csk.cn-shanghai.aliyuncs.com:8093?clusterId=c76c5be3d8fe647699cc2a480bf870d5a&public=true
State: Running
Started: Tue, 06 Jun 2023 16:59:21 +0800
Last State: Terminated
Reason: Unknown
Exit Code: 255
Started: Tue, 06 Jun 2023 16:57:55 +0800
Finished: Tue, 06 Jun 2023 16:58:55 +0800
Ready: True
Restart Count: 3
Limits:
cpu: 4
memory: 8Gi
Requests:
cpu: 100m
memory: 200Mi
Liveness: tcp-socket :443 delay=0s timeout=1s period=10s #success=1 #failure=3
Readiness: tcp-socket :443 delay=0s timeout=1s period=10s #success=1 #failure=3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-nx95f (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-nx95f:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: Burstable
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
10.kubectl logs
kubectl logs 获取 pod 中容器的日志,可用于跟踪容器的问题或解决容器的问题。执行以下命令可以查看Pod名称为metrics-server-85bd976946-rlk6c的日志:
$ kubectl logs -n kube-system metrics-server-85bd976946-rlk6c
I0714 08:49:52.202911 1 socket.go:39] Start ExportData 156 metrics.
I0714 08:49:52.206977 1 socket.go:175] Successful write 260741 bytes metrics to monitor server
I0714 08:49:52.410389 1 socket.go:175] Successful write 241216 bytes metrics to monitor server
I0714 08:49:52.613469 1 socket.go:175] Successful write 226287 bytes metrics to monitor server
I0714 08:49:52.816824 1 socket.go:175] Successful write 240725 bytes metrics to monitor server
11.kubectl exec
kubectl exec 在 pod 的正在运行的容器中执行命令。它对于调试、故障排除和监视应用程序的状态很有帮助 执行以下命令可以进入Pod命令为metrics-server-85bd976946-rlk6c容器中:
$ kubectl exec -it -n kube-system metrics-server-85bd976946-rlk6c sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/go #
12.kubectl cp
kubectl cp 允许您在本地文件系统和 pod 中的容器之间或同一 pod 中的两个容器之间复制文件和目录。这对于在主机和容器之间传输文件,或者在 pod 内的容器之间复制文件非常有用:
$ kubectl cp <local-file-path> <pod-name>:<container-destination-path>
参数:
- kubectl cp 用于将本地文件复制到 pod 中的容器中。
- local-file-path 指定本地文件系统上文件的路径。
- pod-name 和 container-destination-path 指定容器内文件的目的地。