在命令行下对集群、节点、pod进行管理,kubernetes官方提供了一个管理命令:kubectl
kubectl作为客户端CLI工具,可以让用户通过命令行对Kubernetes集群进行操作。
格式:kubectl [command] [TYPE] [NAME] [flags]
在使用命令过程中出现问题,可以通过使用 --help来获取 kubectl 命令的使用说明:
获取 kubectl 的使用说明: kubectl --help
获取 kubectl 子命令的使用说明:kubectl get --help / kubectl delete --help
获取资源控制器说明: kubectl explain pod / rs / deployment / ·····
(1)基础命令
命令 | 作 用 |
create | 通过文件名或标准输入创建资源 |
expose | 将一个资源公开为一个新的service |
run | 在集群中运行一个特定的镜像 |
set | 在对象上设置特定的功能 |
get | 在显示一个或多个资源 |
explain | 文档参考资料 |
edit | 使用默认的编辑器编辑一个资源 |
delete | 通过文件名、标准输入、资源名称或标签选择器来删除资源 |
(2)部署命令
命令 | 作 用 |
rollout | 管理资源的发布 |
rolling-update | 对给定的复制控制器滚动更新 |
scale | 扩容或缩容pod数量,Deployment、ReplicaSet、RC或Job |
autoscale | 创建一个自动选择扩容或缩容并设置pod数量 |
(3)集群管理命令
命令 | 作 用 |
certificate | 修改证书资源 |
cluster-info | 显示集群信息 |
top | 显示资源(CPU/Memory/Storage)使用。需要Heapster运行 |
cordon | 标记节点不可调度 |
uncordon | 标记节点可调度 |
drain | 驱逐节点上的应用,准备下线维护 |
taint | 修改节点taint标记 |
(4)故障和调度命令
命令 | 作 用 |
describe | 显示特定资源或资源组的详细信息 |
logs | 在一个pod中打印一个容器日志。如果pod只有一个容器,容器名称是可选的 |
attach | 附加到一个运行的容器 |
exec | 执行命令到容器 |
port-forward |
转发一个或多个本地端口到一个pod |
proxy | 运行一个proxy到kubernetes API server |
cp | 拷贝文件或目录到容器中 |
auth | 检查授权 |
(5)高级命令
命令 | 作 用 |
apply | 通过文件名或标准输入对资源应用配置 |
patch | 使用补丁修改、更新资源的字段 |
replace | 通过文件名或标准输入替换一个资源 |
convert | 不同的API版本之间转换配置文件 |
(6)设置命令
命令 | 作 用 |
label | 更新资源上的标签 |
annotate | 更新资源上的注释 |
completion | 用于实现kubectl工具自动补全 |
(7)其他命令
命令 | 作 用 |
api-version | 打印受支持的API版本 |
config | 修改kubeconfig文件(用于访问API,比如配置认证信息) |
help | 所有命令的帮助 |
plugin | 运行一个命令行插件 |
version | 打印客户端和服务版本信息 |
[root@master ~]# kubectl create -f pod.yaml
pod/annotation-second-scheduler created
[root@master ~]# kubectl delete -f pod.yaml
pod "annotation-second-scheduler" deleted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 24s
[root@master ~]# kubectl delete pod nginx
pod "nginx" deleted
[root@master ~]# kubectl get pods
No resources found in default namespace.
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myweb-f64b97bf5-25c99 1/1 Running 0 4m28s 10.254.4.18 node02
myweb-f64b97bf5-dp962 1/1 Running 0 4m28s 10.254.4.16 node02
myweb-f64b97bf5-vk9m7 1/1 Running 0 4m28s 10.254.4.19 node02
myweb-f64b97bf5-zfsn7 1/1 Running 0 4m28s 10.254.4.17 node02
[root@master ~]# kubectl delete pod --all
pod "myweb-f64b97bf5-25c99" deleted
pod "myweb-f64b97bf5-dp962" deleted
pod "myweb-f64b97bf5-vk9m7" deleted
pod "myweb-f64b97bf5-zfsn7" deleted
[root@master ~]# kubectl get pods
No resources found in default namespace.
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 7h42m v1.23.0
node01 Ready 6h4m v1.23.0
node02 Ready 7h7m v1.23.0
node03 Ready 7h41m v1.23.0
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 13s 10.254.5.13 node01
[root@master ~]# kubectl get pod -o yaml
apiVersion: v1
items:
- apiVersion: v1
kind: Pod
metadata:
……………………
kubectl get all
[root@master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx 1/1 Running 0 89s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.244.0.1 443/TCP 7h44m
[root@master ~]# kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: node01/192.168.4.21
Start Time: Fri, 02 Sep 2022 16:49:25 +0800
Labels: env=test
Annotations:
Status: Running
IP: 10.254.5.13
IPs:
IP: 10.254.5.13
Containers:
nginx:
Container ID: docker://203ebc2c6371a2f1f9bffb522b07e3dc3686a2d7ca8292140b5459a667667d25
Image: nginx
Image ID: docker-pullable://nginx@sha256:b95a99feebf7797479e0c5eb5ec0bdfa5d9f504bc94da550c2f58e839ea6914f
Port:
Host Port:
State: Running
Started: Fri, 02 Sep 2022 16:49:26 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-x2gv6 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-x2gv6:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional:
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: disktype=ssd
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m32s default-scheduler Successfully assigned default/nginx to node01
Normal Pulled 2m31s kubelet Container image "nginx" already present on machine
Normal Created 2m31s kubelet Created container nginx
Normal Started 2m31s kubelet Started container nginx
[root@master ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-6d8c4cb4d-q89s6 1/1 Running 0 7h46m
coredns-6d8c4cb4d-xtdxr 1/1 Running 0 7h46m
etcd-master 1/1 Running 0 7h46m
kube-apiserver-master 1/1 Running 0 7h46m
kube-controller-manager-master 1/1 Running 0 7h46m
kube-proxy-lttsx 1/1 Running 0 7h46m
kube-proxy-mg9ng 1/1 Running 0 6h9m
kube-proxy-shjxm 1/1 Running 0 7h11m
kube-proxy-zzgzn 1/1 Running 0 7h46m
kube-scheduler-master 1/1 Running 0 7h46m
[root@master ~]# kubectl create deployment web --image=nginx -o yaml --dry-run=client > myweb.yaml
[root@master ~]# ls myweb.yaml
myweb.yaml
[root@master ~]# kubectl get deployments.apps nginx -o=yaml > myweb2.yaml
[root@master ~]# ls myweb*
myweb2.yaml myweb.yaml