码迷,mamicode.com
首页 > Web开发 > 详细

Kubernetes-常用命令

时间:2019-12-03 23:29:03      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:特殊用法   amp   exp   accounts   flags   sql   cap   configure   url   

语法:kubectl 【command】【type】【name】【flags】
 
①command:子命令,用于操作Kubernetes集群资源对象的命令,如create, delete, describe, get, apply等
②type:资源对象的类型,如pod, service, rc, deployment, node等,可以单数、复数以及简写(pod, pods, po/service,services, svc)
③name:资源对象的名称,不指定则返回所有,如get pod 会返回所有pod, get pod nginx, 只返回nginx这个pod
④flags:kubectl子命令的可选参数,例如-n 指定namespace,-s 指定apiserver的URL
 
示例:kubectl get pod tomcat-p9x0h
[root@k8s ~]# kubectl get pod tomcat-p9x0h
NAME           READY     STATUS    RESTARTS   AGE
tomcat-p9x0h   1/1       Running      0       1h

 一、资源对象类型列表

 1:获取命令
kubectl explain kubectl api-resources
名称                                      简写
componentsstatuses                        cs
daemonsets                                ds
deployment                                deploy
events                                    ev
endpoints                                 ep
horizontalpodautoscalers                  hpa
ingresses                                 ing
jobs
limitranges                               limits
nodes                                    no
namspaces                                ns
pods                                     po
persistentvolumes                        pv
persistentvolumeclaims                   pvc
resourcequotas                           quota
replicationcontrollers                   rc
secrets
serviceaccounts                          sa
services                                 svc

 

二、特殊用法

1:查询多个Pod信息
kubectl get pods 【pod1】【pod2】
示例:
[root@k8s ~]# kubectl get pods tomcat-p9x0h mysql-19h65
NAME           READY     STATUS    RESTARTS   AGE
tomcat-p9x0h   1/1       Running      0       1h
mysql-19h65    1/1       Running      0       2h

 2:同时查询pod、rc信息

kubectl get pod/pod1 rc/rc1
示例:
[root@k8s ~]# kubectl get pod/tomcat-p9x0h rc/mysql
NAME              READY     STATUS    RESTARTS   AGE
po/tomcat-p9x0h    1/1      Running      0       1h

NAME       DESIRED   CURRENT   READY     AGE
rc/mysql     1         1         1       2h

 3:同时创建rc、svc

kubectl create -f 【rc1.yaml】-f 【service1.yaml】
示例:
[root@k8s ~]# kubectl create -f mysql-rc.yaml -f mysql-svc.yaml 
replicationcontroller "mysql" created
service "mysql" created

 

三、kubectl子命令
 
主要包括对资源的创建、删除、查看、修改、配置、运行等
 
1:查看所有子命令
kubectl --help
 
2:kubectl参数
kubectl options 可以查看支持的参数,例如--namespace指定所在namespace
 
3:kubectl输出格式
kubectl命令可以用多种格式对结果进行显示,输出格式通过-o参数指定:-o支持的格式有
输出格式                                              说明
custom-columns=<spec>                 根据自定义列名进行输出,逗号分隔
custom-columns-file=<filename>        从文件中获取自定义列名进行输出
json                                  以JSON格式显示结果
jsonpath=<template>                   输出jasonpath表达式定义的字段信息
jasonpath-file=<filename>             输出jsonpath表达式定义的字段信息,来源于文件
name                                  仅输出资源对象的名称
wide                                  输出更多信息,比如会输出node名
yaml                                  以yaml格式输出

 

2:举例
kubectl get pod -o wide
kubectl get pod -o yaml
kubectl get pod -o custom-columns=NAME:.metadata.name,RESC:.metadata.resourceVersion
kubectl get pod --sort-by=.metadata.name //name排序
 
四、创建资源对象
 
1:根据yaml文件创建service和deployment
kubectl create -f 【rc-yaml-name】 -f 【svc-yaml-name】
示例
[root@k8s ~]# kubectl create -f mysql-rc.yaml -f mysql-svc.yaml 
replicationcontroller "mysql" created
service "mysql" created

2:可以指定一个目录,这样可以一次性根据该目录下所有yaml或json文件定义资源

kubectl create -f 【directory】
示例
[root@k8s ~]# kubectl create -f /data/k8s-yaml-root/
replicationcontroller "mysql" created
service "mysql" created

 

五、查看资源对象
 
1:查看所有pod
kubectl get pods
示例:
[root@k8s ~]# kubectl get pods
NAME           READY     STATUS    RESTARTS   AGE
mysql-512rz    1/1       Running   0          2m
mysql-5kmht    1/1       Running   0          2m
tomcat-p9x0h   1/1       Running   0          2h

 2:查看rc和service

kubectl get rc,svc
示例:
[root@k8s ~]# kubectl get rc,svc
NAME       DESIRED   CURRENT   READY     AGE
rc/mysql   10        10        10        4m

NAME             CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
svc/kubernetes   10.254.0.1       <none>        443/TCP          4h
svc/mysql        10.254.244.102   <none>        3306/TCP         4m
svc/tomcat       10.254.250.150   <nodes>       8080:30001/TCP   2h

 

六、描述资源对象
 
1:显示node的详细信息
kubectl describe nodes 【node-name】
示例:
[root@k8s ~]# kubectl describe nodes 127.0.0.1
Name:			127.0.0.1
Role:			
Labels:			beta.kubernetes.io/arch=amd64
			beta.kubernetes.io/os=linux
			kubernetes.io/hostname=127.0.0.1
Taints:			<none>
CreationTimestamp:	Fri, 29 Nov 2019 05:46:33 +0800
Phase:			
Conditions:
  Type			Status	LastHeartbeatTime			LastTransitionTime			Reason				Message
  ----			------	-----------------			------------------			------				-------
  OutOfDisk 		False 	Fri, 29 Nov 2019 09:47:58 +0800 	Fri, 29 Nov 2019 05:46:33 +0800 	KubeletHasSufficientDisk 	kubelet has sufficient disk space available
  MemoryPressure 	False 	Fri, 29 Nov 2019 09:47:58 +0800 	Fri, 29 Nov 2019 05:46:33 +0800 	KubeletHasSufficientMemory 	kubelet has sufficient memory available
  DiskPressure 		False 	Fri, 29 Nov 2019 09:47:58 +0800 	Fri, 29 Nov 2019 05:46:33 +0800 	KubeletHasNoDiskPressure 	kubelet has no disk pressure
  Ready 		True 	Fri, 29 Nov 2019 09:47:58 +0800 	Fri, 29 Nov 2019 05:46:33 +0800 	KubeletReady 			kubelet is posting ready status
Addresses:		127.0.0.1,127.0.0.1,127.0.0.1
Capacity:
 alpha.kubernetes.io/nvidia-gpu:	0
 cpu:					1
 memory:				2902952Ki
 pods:					110
Allocatable:
 alpha.kubernetes.io/nvidia-gpu:	0
 cpu:					1
 memory:				2902952Ki
 pods:					110
System Info:
 Machine ID:			c1cc26a06e9e405aa56fa81c7fcc0ca7
 System UUID:			564DEF43-F784-4106-FBC8-E81C9DC9216F
 Boot ID:			9df7d476-41e9-412d-bde4-9b9ba119ba25
 Kernel Version:		3.10.0-123.el7.x86_64
 OS Image:			CentOS Linux 7 (Core)
 Operating System:		linux
 Architecture:			amd64
 Container Runtime Version:	docker://1.13.1
 Kubelet Version:		v1.5.2
 Kube-Proxy Version:		v1.5.2
ExternalID:			127.0.0.1
Non-terminated Pods:		(11 in total)
  Namespace			Name			CPU Requests	CPU Limits	Memory Requests	Memory Limits
  ---------			----			------------	----------	---------------	-------------
  default			mysql-05ktg		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-5tvkd		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-dccp5		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-f3195		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-h6h4h		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-hbhhl		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-hws8f		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-jt4sf		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-m8z1d		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			mysql-xkgcn		0 (0%)		0 (0%)		0 (0%)		0 (0%)
  default			tomcat-c5995		0 (0%)		0 (0%)		0 (0%)		0 (0%)
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.
  CPU Requests	CPU Limits	Memory Requests	Memory Limits
  ------------	----------	---------------	-------------
  0 (0%)	0 (0%)		0 (0%)		0 (0%)
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath	Type		Reason			Message
  ---------	--------	-----	----			-------------	--------	------			-------
  32m		32m		1	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-vgtbf_default(ae73965a-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-twqs3_default(ae73c4c4-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-x61sl_default(ae7378c1-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-5kmht_default(ae7440a0-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		1	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-rp3pz_default(ae744a31-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-8znqq_default(ae73a01f-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-512rz_default(ae7386c6-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-6vf6j_default(ae73b850-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-cwmlr_default(ae73a8e3-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		32m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-8739b_default(ae73b0a3-1245-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  28m		28m		1	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-j8s9g_default(3708c328-1246-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  28m		28m		1	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-rlprq_default(376b8d4e-1246-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-hbhhl_default(b0441080-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-dccp5_default(b043a874-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-05ktg_default(b0442004-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-hws8f_default(b0441a6c-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-xkgcn_default(b04429a8-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-jt4sf_default(b0508921-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-f3195_default(b0759f67-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-5tvkd_default(b0e0a048-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  4m		4m		2	{kubelet 127.0.0.1}			Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. pod: "mysql-m8z1d_default(b0c00ce6-1249-11ea-ac12-000c29c9216f)". Falling back to DNSDefault policy.
  32m		4m		24	{kubelet 127.0.0.1}	

 

2:显示pod的详细信息
kubectl describe pods【pod-name】
示例:
[root@k8s ~]# kubectl describe pods mysql-f3195 
Name:		mysql-f3195
Namespace:	default
Node:		127.0.0.1/127.0.0.1
Start Time:	Fri, 29 Nov 2019 09:43:49 +0800
Labels:		app=mysql
Status:		Running
IP:		172.17.0.4
Controllers:	ReplicationController/mysql
Containers:
  mysql:
    Container ID:	docker://25d5f66e45a3650e8dc054ab3644f73a726b008d2160c8496d72a09832242662
    Image:		mysql:5.6
    Image ID:		docker-pullable://docker.io/mysql@sha256:5345afaaf1712e60bbc4d9ef32cc62acf41e4160584142f8d73115f16ad94af4
    Port:		3306/TCP
    State:		Running
      Started:		Fri, 29 Nov 2019 09:44:03 +0800
    Ready:		True
    Restart Count:	0
    Volume Mounts:	<none>
    Environment Variables:
      MYSQL_ROOT_PASSWORD:	123456
Conditions:
  Type		Status
  Initialized 	True 
  Ready 	True 
  PodScheduled 	True 
No volumes.
QoS Class:	BestEffort
Tolerations:	<none>
Events:
  FirstSeen	LastSeen	Count	From			SubObjectPath		Type		Reason			Message
  ---------	--------	-----	----			-------------		--------	------			-------
  3m		3m		1	{default-scheduler }				Normal		Scheduled		Successfully assigned mysql-f3195 to 127.0.0.1
  3m		3m		2	{kubelet 127.0.0.1}				Warning		MissingClusterDNS	kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  3m		3m		1	{kubelet 127.0.0.1}	spec.containers{mysql}	Normal		Pulled			Container image "mysql:5.6" already present on machine
  3m		3m		1	{kubelet 127.0.0.1}	spec.containers{mysql}	Normal		Created			Created container with docker id 25d5f66e45a3; Security:[seccomp=unconfined]
  3m		3m		1	{kubelet 127.0.0.1}	spec.containers{mysql}	Normal		Started			Started container with docker id 25d5f66e45a3

 3:显示deployment管理的pod信息

kubectl describe pods 【deployment-name】
 
七、删除资源对象
 
1:基于yaml文件删除,-f指定文件
kubectl delete -f 【pod.yaml】
示例:
[root@k8s ~]# kubectl delete -f mysql-rc.yaml 
replicationcontroller "mysql" deleted
[root@k8s ~]# kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
tomcat    1         1         1         2h

 2:删除所有包含某个label的pod和service

kubectl delete po,svc -l name=【lable-name】
示例:
[root@k8s ~]# kubectl delete po,svc -l app=mysql
pod "mysql-0f7b6" deleted
pod "mysql-0fs9s" deleted
pod "mysql-1zmvc" deleted
pod "mysql-9d6vk" deleted
pod "mysql-gbtvh" deleted

 3:删除所有pod

kubectl delete pods --all
示例:
[root@k8s ~]# kubectl delete pods --all
pod "mysql-512rz" deleted
pod "mysql-5kmht" deleted
pod "tomcat-p9x0h" deleted

 

八、执行容器的命令
 
1:指定pod的某个容器执行命令
kubectl exec 【pod-name】 uname
示例:
[root@k8s ~]# kubectl exec mysql-0f7b6 uname
Linux

 2:进入到pod的容器里

kubectl exec-it 【pod-name】 bash

示例:
[root@k8s ~]# kubectl exec -it mysql-0f7b6 bash
root@mysql-0f7b6:/# ls
bin  boot  dev	docker-entrypoint-initdb.d  entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

 

九、查看日志命令
 
1:查看某个容器日志
kubectl logs 【pod-name】
示例:
[root@k8s ~]# kubectl logs mysql-0f7b6 
2019-11-29 01:19:10+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.6.46-1debian9 started.
2019-11-29 01:19:10+00:00 [Note] [Entrypoint]: Switching to dedicated user ‘mysql‘
2019-11-29 01:19:10+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.6.46-1debian9 started.
2019-11-29 01:19:10+00:00 [Note] [Entrypoint]: Initializing database files
2019-11-29 01:19:11 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-11-29 01:19:11 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-11-29 01:19:11 0 [Note] /usr/sbin/mysqld (mysqld 5.6.46) starting as process 47 ...
2019-11-29 01:19:11 47 [Note] InnoDB: Using atomics to ref count buffer pool pages

 2:动态查看日志,类似于tail-f

kubectl logs -f 【pod-name】
示例:
[root@k8s ~]# kubectl logs -f  mysql-0f7b6 
2019-11-29 01:20:40 1 [Warning] ‘proxies_priv‘ entry ‘@ root@mysql-0f7b6‘ ignored in --skip-name-resolve mode.
2019-11-29 01:20:41 1 [Note] Event Scheduler: Loaded 0 events
2019-11-29 01:20:41 1 [Note] mysqld: ready for connections.
Version: ‘5.6.46‘  socket: ‘/var/run/mysqld/mysqld.sock‘  port: 3306  MySQL Community Server (GPL)

Kubernetes-常用命令

标签:特殊用法   amp   exp   accounts   flags   sql   cap   configure   url   

原文地址:https://www.cnblogs.com/douyi/p/11980060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!