kubectl是什么
kubernetes API是管理其各种资源对象的唯一入口,它提供了一个RESTful风格的CRUD(create,read,Update,delete)接口用于查询和修改集群状态,并且将结果存储于etcd中,API Server也是用于更新ETCD中资源对象的唯一途径,整个集群的其他所有组件都需要通过它来完成查询和修改操作,kubectl的核心功能在于通过API Server操作k8s的各种资源对象,它支持三种操作方式,其中命令是的使用最为简单,是了解K8S集群管理的一种有效途径
kubectl常用的命令
命令语法格式
- kubectl [command] [TYPE] [NAME] [flags]
- 上面的命令是: kubectl命令行中,指定执行什么操作(command),指定什么类型资源对象(type),指定此类型的资源对象名称(name),指定可选参数(flags),后面的参数就是为了修饰那个唯一的对象
- 属于典型的英文语法,比如你是老师,你说,小明(kubectl)买(command)方便面(type)老坛酸菜方便面(name)桶装的(flag)。命令的根本原则是准确性,不能有异议!
查看集群信息
- kubectl cluster-info
[root@k8s-master01 nginx]# kubectl cluster-infoKubernetes master is running at https://172.18.15.116:6443CoreDNS is running at https://172.18.15.116:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.[root@k8s-master01 nginx]#
查看集群版本信息
- kubectl version
- kubectl version —short=tre
其中第二条命令能更精简的显示集群版本信息
[root@k8s-master01 nginx]# kubectl versionClient Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.9", GitCommit:"a17149e1a189050796ced469dbd78d380f2ed5ef", GitTreeState:"clean", BuildDate:"2020-04-16T11:44:51Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.9", GitCommit:"a17149e1a189050796ced469dbd78d380f2ed5ef", GitTreeState:"clean", BuildDate:"2020-04-16T11:36:15Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}[root@k8s-master01 nginx]# kubectl version --short=trueClient Version: v1.16.9Server Version: v1.16.9[root@k8s-master01 nginx]#
查看集群上运行了哪些资源
- 语法格式 kubectl get - “-“表示代表资源名称,其中资源名称后面可用跟上选项”-o wide”表示可用查看一些具体的信息
- 可以加上-n选项跟上pod所在的名称空间,如果不加则默认只会打印出default名称空间里面的Pod
- 也可以加上—all-namespaces选项打印出所有名称空间的pod资源信息
列出有哪些Pod资源
- -n选项可以打印出你指定的名称空间的资源
- —all-namespaces可以打印出所有的名称的资源
kubectl get pods
[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEbusybox 1/1 Running 0 31mmy-nginx-854bbd7557-phvsl 1/1 Running 0 2dmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d[root@k8s-master01 nginx]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESbusybox 1/1 Running 0 31m 10.244.59.2 172.18.15.113 <none> <none>my-nginx-854bbd7557-phvsl 1/1 Running 0 2d 10.244.38.3 172.18.15.114 <none> <none>my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d 10.244.38.7 172.18.15.114 <none> <none>[root@k8s-master01 nginx]##打印出所有名称空间的Pod资源信息[root@k8s-master01 nginx]# kubectl get pods --all-namespacesNAMESPACE NAME READY STATUS RESTARTS AGEdefault my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hdefault my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3hkube-system coredns-59c6ddbf5d-bdr92 1/1 Running 0 2d3h[root@k8s-master01 nginx]##只打印出kube-system名称空间的Pod[root@k8s-master01 nginx]# kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcoredns-59c6ddbf5d-bdr92 1/1 Running 0 2d3h[root@k8s-master01 nginx]#
列出有哪些deployment资源,默认也是打印出default名称空间的deployment资源
- -n选项可以打印出你指定的名称空间的资源
- —all-namespaces可以打印出所有的名称的资源
kubectl get deployment
[root@k8s-master01 nginx]# kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGEmy-nginx 2/2 2 2 2d2h[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl get deployment -o wideNAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTORmy-nginx 2/2 2 2 2d2h my-nginx daocloud.io/library/nginx:1.13.0-alpine app=my-nginx[root@k8s-master01 nginx]##打印出所有的名称空间的deployment[root@k8s-master01 nginx]# kubectl get deployment --all-namespacesNAMESPACE NAME READY UP-TO-DATE AVAILABLE AGEdefault my-nginx 2/2 2 2 2d3hkube-system coredns 1/1 1 1 2d3h[root@k8s-master01 nginx]#
列出有哪些Service资源,默认也是打印出default名称空间的
- -n选项可以打印出你指定的名称空间的资源
- —all-namespaces可以打印出所有的名称的资源
kubectl get svc
[root@k8s-master01 nginx]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8dmy-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d2h[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl get svc -o wideNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTORkubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d2h app=my-nginx[root@k8s-master01 nginx]##只打印出kube-system名称空间的资源[root@k8s-master01 nginx]# kubectl get svc -n kube-systemNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkube-dns ClusterIP 10.99.110.110 <none> 53/UDP,53/TCP,9153/TCP 2d3h[root@k8s-master01 nginx]##打印出所有名称空间的资源[root@k8s-master01 nginx]# kubectl get svc --all-namespacesNAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEdefault kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8ddefault my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3hkube-system kube-dns ClusterIP 10.99.110.110 <none> 53/UDP,53/TCP,9153/TCP 2d3h[root@k8s-master01 nginx]#
列出集群上面有哪些名称空间 kubectl get ns
[root@k8s-master01 nginx]# kubectl get nsNAME STATUS AGEdefault Active 8dkube-node-lease Active 8dkube-public Active 8dkube-system Active 8d[root@k8s-master01 nginx]#
查看某一个资源更详细的信息
- 语法格式:kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME) [options]
查看某一个Pod资源的详细信息 kubectl describe pod podname
[root@k8s-master01 nginx]# kubectl describe pod my-nginx-854bbd7557-phvslName: my-nginx-854bbd7557-phvslNamespace: defaultPriority: 0Node: 172.18.15.114/172.18.15.114Start Time: Mon, 01 Jun 2020 10:58:41 +0800Labels: app=my-nginxpod-template-hash=854bbd7557Annotations: <none>Status: RunningIP: 10.244.38.3IPs:IP: 10.244.38.3Controlled By: ReplicaSet/my-nginx-854bbd7557Containers:my-nginx:Container ID: docker://56d6ff87faf07a332dcf625b86301946c6f7ef956634ea1f0bf147767af6df7bImage: daocloud.io/library/nginx:1.13.0-alpineImage ID: docker-pullable://daocloud.io/library/nginx@sha256:5c36f962c506c379bd63884976489c9c5e700c1496a6e8ea13dc404b1d258f76Port: 80/TCPHost Port: 0/TCPState: RunningStarted: Mon, 01 Jun 2020 10:58:44 +0800Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from default-token-gvn55 (ro)Conditions:Type StatusInitialized TrueReady TrueContainersReady TruePodScheduled TrueVolumes:default-token-gvn55:Type: Secret (a volume populated by a Secret)SecretName: default-token-gvn55Optional: falseQoS Class: BestEffortNode-Selectors: <none>Tolerations: node.kubernetes.io/not-ready:NoExecute for 300snode.kubernetes.io/unreachable:NoExecute for 300sEvents: <none>[root@k8s-master01 nginx]#
查看某一个service资源的详细信息,如果不加Service资源明,则默认会把所有的service资源都显示出来 kubectl describe svc servicename
[root@k8s-master01 nginx]# kubectl describe svcName: kubernetesNamespace: defaultLabels: component=apiserverprovider=kubernetesAnnotations: <none>Selector: <none>Type: ClusterIPIP: 10.99.0.1Port: https 443/TCPTargetPort: 6443/TCPEndpoints: 172.18.15.116:6443Session Affinity: NoneEvents: <none>Name: my-nginxNamespace: defaultLabels: app=my-nginxAnnotations: kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...Selector: app=my-nginxType: NodePortIP: 10.99.44.234Port: http 80/TCPTargetPort: 80/TCPNodePort: http 13359/TCPEndpoints: 10.244.38.3:80,10.244.38.7:80Session Affinity: NoneExternal Traffic Policy: ClusterEvents: <none>[root@k8s-master01 nginx]## 加资源命访问[root@k8s-master01 nginx]# kubectl describe svc my-nginxName: my-nginxNamespace: defaultLabels: app=my-nginxAnnotations: kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...Selector: app=my-nginxType: NodePortIP: 10.99.44.234Port: http 80/TCPTargetPort: 80/TCPNodePort: http 13359/TCPEndpoints: 10.244.38.3:80,10.244.38.7:80Session Affinity: NoneExternal Traffic Policy: ClusterEvents: <none>[root@k8s-master01 nginx]#
打印出Pod容器的日志,默认就是Pod类型
- 语法格式:kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]
[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEbusybox 1/1 Running 0 164mmy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h[root@k8s-master01 nginx]# kubectl logs my-nginx-854bbd7557-phvsl10.244.38.1 - - [02/Jun/2020:03:32:22 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"[root@k8s-master01 nginx]#
如何进入到Pod的容器
- 语法格式:kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] — COMMAND [args…] [options]
[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEbusybox 1/1 Running 0 167mmy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h[root@k8s-master01 nginx]# kubectl exec -ti my-nginx-854bbd7557-phvsl /bin/sh/ #/ #/ #
如何删除一个Pod,删除的方法有多种,通常我们应该是怎么创建的Pod就怎么删除,因为Pod有两种运行方式,一种是基于命令创建的,一种是基于配置清单创建的,但是结果都是需要使用kubectl命令来删除,只是使用的选项不同而已,
- 语法格式:kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | —all)]) [options]
命令式创建的Pod删除方法,因为命令创建的Pod通常是没有配置文件清单的,所以需要先get出来需要删除的Pod
- kubectl delete Podname
[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEbusybox 1/1 Running 0 171mmy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl delete pod busyboxpod "busybox" deleted[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEmy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h[root@k8s-master01 nginx]#
配置清单式的Pod删除方法,这种方法删除Pod非常简单,怎么创建的就怎么删除,只需要把apply或者create换成delete
#我们先使用配置清单创建出一个基于配置清单的Pod[root@k8s-master01 nginx]# kubectl apply -f busybox.ymlpod/busybox created[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEbusybox 0/1 ContainerCreating 0 5smy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEbusybox 1/1 Running 0 10smy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h[root@k8s-master01 nginx]##删除基于配置清单的Pod[root@k8s-master01 nginx]# kubectl delete -f busybox.ymlpod "busybox" deleted[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEmy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h[root@k8s-master01 nginx]#
创建资源对象
- 语法格式:kubectl run NAME —image=image [—env=”key=value”] [—port=port] [—replicas=replicas] [—dry-run=bool] [—overrides=inline-json] [—command] — [COMMAND] [args…] [options]
- 创建资源对象有run,create,apply等命令
- 直接通过kubectl run命令及相关选项创建的资源对象被称为直接命令式操作
- 用户也可以直接根据配置清单来创建资源对象
下面是基于命令式创建一个名称为web的deployment和一个名称为web-svc的Service,镜像使用的是Nginx1.13的alpine版本,并且创建了5个副本的示例
[root@k8s-master01 nginx]# kubectl run web --image=nginx:1.13.0-alpine --replicas=5kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.deployment.apps/web created[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESmy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h 10.244.38.3 172.18.15.114 <none> <none>my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h 10.244.38.7 172.18.15.114 <none> <none>web-6ff4758d4d-5zkpb 1/1 Running 0 47s 10.244.38.4 172.18.15.114 <none> <none>web-6ff4758d4d-666wt 1/1 Running 0 47s 10.244.59.4 172.18.15.113 <none> <none>web-6ff4758d4d-n8plp 1/1 Running 0 47s 10.244.59.2 172.18.15.113 <none> <none>web-6ff4758d4d-qk4mv 1/1 Running 0 47s 10.244.59.3 172.18.15.113 <none> <none>web-6ff4758d4d-vmmbc 1/1 Running 0 47s 10.244.38.5 172.18.15.114 <none> <none>[root@k8s-master01 nginx]##创建一个名称为web-svc的Service资源,并且将web这个deployment里面的Pod资源包含进去[root@k8s-master01 nginx]# kubectl expose deployment/web --name=web-svc --port=80service/web-svc exposed[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8dmy-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3hweb-svc ClusterIP 10.99.221.103 <none> 80/TCP 72s[root@k8s-master01 nginx]##查看一下之前创建的5个Pod是否被包含到了这个Service资源里面[root@k8s-master01 nginx]# kubectl describe svc web-svcName: web-svcNamespace: defaultLabels: run=webAnnotations: <none>Selector: run=webType: ClusterIPIP: 10.99.221.103Port: <unset> 80/TCPTargetPort: 80/TCPEndpoints: 10.244.38.4:80,10.244.38.5:80,10.244.59.2:80 + 2 more...Session Affinity: NoneEvents: <none>[root@k8s-master01 nginx]##删除,因为我们没有配置清单,所以这里删除就比较烦,如果你的操作步骤多了,很有可能自己也会忘记自己创建的对象名称叫什么#先删除Pod相关资源步骤:#1.找出刚刚创建的deployment[root@k8s-master01 nginx]# kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGEmy-nginx 2/2 2 2 2d3hweb 5/5 5 5 15m[root@k8s-master01 nginx]##2.删除这个deployment[root@k8s-master01 nginx]# kubectl delete deployment webdeployment.apps "web" deleted[root@k8s-master01 nginx]##查看Pod,可以看到Pod资源已经是Terminating状态,虽然我这里比较方便,但是生产环境可能会有很多类似的资源,查找起来就没有这么方便了,所以推荐使用配置清单的方式去创建相关资源[root@k8s-master01 nginx]# kubectl get podsNAME READY STATUS RESTARTS AGEmy-nginx-854bbd7557-phvsl 1/1 Running 0 2d3hmy-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3hweb-6ff4758d4d-5zkpb 0/1 Terminating 0 16m[root@k8s-master01 nginx]##删除service[root@k8s-master01 nginx]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8dmy-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3hweb-svc ClusterIP 10.99.221.103 <none> 80/TCP 15m[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl delete svc web-svcservice "web-svc" deleted[root@k8s-master01 nginx]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8dmy-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h[root@k8s-master01 nginx]#
基于配置清单创建出一个名称为my-nginx的deployment,里面运行了5个副本,并且再创建出一个名称为my-nginx的Service资源
[root@k8s-master01 nginx]# cat my-nginx.ymlapiVersion: apps/v1kind: Deploymentmetadata:name: my-nginxspec:replicas: 5selector:matchLabels:app: my-nginxtemplate:metadata:labels:app: my-nginxspec:containers:- name: my-nginximage: daocloud.io/library/nginx:1.13.0-alpineports:- containerPort: 80---apiVersion: v1kind: Servicemetadata:name: my-nginxlabels:app: my-nginxspec:type: NodePortselector:app: my-nginxports:- name: httpport: 80targetPort: 80[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl apply -f my-nginx.ymldeployment.apps/my-nginx createdservice/my-nginx created[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESmy-nginx-854bbd7557-9c9tq 1/1 Running 0 23s 10.244.59.2 172.18.15.113 <none> <none>my-nginx-854bbd7557-rvtkb 1/1 Running 0 23s 10.244.38.5 172.18.15.114 <none> <none>my-nginx-854bbd7557-w676k 1/1 Running 0 23s 10.244.38.3 172.18.15.114 <none> <none>my-nginx-854bbd7557-wbvjf 1/1 Running 0 23s 10.244.38.4 172.18.15.114 <none> <none>my-nginx-854bbd7557-wfx8k 1/1 Running 0 23s 10.244.59.3 172.18.15.113 <none> <none>[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl get svc -o wideNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTORkubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>my-nginx NodePort 10.99.238.73 <none> 80:21954/TCP 43s app=my-nginx[root@k8s-master01 nginx]#[root@k8s-master01 nginx]# kubectl get svc -o wideNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTORkubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>my-nginx NodePort 10.99.238.73 <none> 80:21954/TCP 43s app=my-nginx[root@k8s-master01 nginx]# kubectl describe svc my-nginxName: my-nginxNamespace: defaultLabels: app=my-nginxAnnotations: kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...Selector: app=my-nginxType: NodePortIP: 10.99.238.73Port: http 80/TCPTargetPort: 80/TCPNodePort: http 21954/TCPEndpoints: 10.244.38.3:80,10.244.38.4:80,10.244.38.5:80 + 2 more...Session Affinity: NoneExternal Traffic Policy: ClusterEvents: <none>[root@k8s-master01 nginx]#关于如何删除前面有些,可以参考
小结
- 每个子命令下面还有众多的子命令,大家可以去网上找相关的资料查看,这里写的常用的也并不全,这里写的只是告诉大家一个基本的用法,使用的是实话可以使用kubectl Type —help来获取相关的帮助信息,一定要多动手去敲一敲才能深刻的理解
