kubernetes命令(一)

 

版权声明:本文为博主原创文章,支持原创,转载请附上原文出处链接和本声明。

本文链接地址:https://www.cnblogs.com/wannengachao/p/12073552.html

 

1、查看pod

格式:kubectl get pod

2、查看node

格式:kubectl get node

3、查看pod详细信息

格式:kubectl describe pod pod名

[root@chushi ~]# kubectl describe pod redis
Name: redis
Namespace: default
Priority: 0
Node: kelong1/192.168.80.135
Start Time: Tue, 10 Dec 2019 11:48:09 +0800
Labels: db=redis
Annotations: <none>
Status: Running
IP: 10.244.1.12
Containers:
redis:
Container ID: docker://344d4b8888224c2eab6cc206a0fc73d1e1484f11de1293477663d0f4c768f5eb
Image: redis:latest
Image ID: docker-pullable://redis@sha256:1eedfc017b0cd3e232878ce38bd9328518219802a8ef37fe34f58dcf591688ef
Port: 6379/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 13 Dec 2019 15:55:52 +0800
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 10 Dec 2019 11:48:19 +0800
Finished: Wed, 11 Dec 2019 16:16:44 +0800
Ready: True
Restart Count: 1
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gzslg (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady True
PodScheduled True
Volumes:
default-token-gzslg:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gzslg
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>  

 

4、查看node详细信息

格式:kubectl describe node node名

[root@chushi ~]# kubectl describe node chushi
Name: chushi
Roles: master
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/arch=amd64
kubernetes.io/hostname=chushi
kubernetes.io/os=linux
node-role.kubernetes.io/master=
Annotations: flannel.alpha.coreos.com/backend-data: {"VtepMAC":"66:ce:a3:7b:91:ad"}
flannel.alpha.coreos.com/backend-type: vxlan
flannel.alpha.coreos.com/kube-subnet-manager: true
flannel.alpha.coreos.com/public-ip: 192.168.80.128
kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
node.alpha.kubernetes.io/ttl: 0
volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Mon, 25 Nov 2019 15:30:54 +0800
Taints: node-role.kubernetes.io/master:NoSchedule
Unschedulable: false
Conditions:
Type Status LastHeartbeatTime LastTransitionTime Reason Message
---- ------ ----------------- ------------------ ------ -------
MemoryPressure False Fri, 20 Dec 2019 15:28:04 +0800 Mon, 25 Nov 2019 15:30:50 +0800 KubeletHasSufficientMemory kubelet has sufficient memory available
DiskPressure False Fri, 20 Dec 2019 15:28:04 +0800 Mon, 25 Nov 2019 15:30:50 +0800 KubeletHasNoDiskPressure kubelet has no disk pressure
PIDPressure False Fri, 20 Dec 2019 15:28:04 +0800 Mon, 25 Nov 2019 15:30:50 +0800 KubeletHasSufficientPID kubelet has sufficient PID available
Ready True Fri, 20 Dec 2019 15:28:04 +0800 Thu, 28 Nov 2019 22:27:54 +0800 KubeletReady kubelet is posting ready status
Addresses:
InternalIP: 192.168.80.128
Hostname: chushi
Capacity:
cpu: 2
ephemeral-storage: 28289540Ki
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 1867048Ki
pods: 110
Allocatable:
cpu: 2
ephemeral-storage: 26071640021
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 1764648Ki
pods: 110
System Info:
Machine ID: d6ee34812c7e4a918a4ac716500ada7d
System UUID: 3AF84D56-1EE2-5D07-0CA4-99F1BE843902
Boot ID: e2273d82-8657-474e-b866-47fb57772939
Kernel Version: 3.10.0-693.el7.x86_64
OS Image: CentOS Linux 7 (Core)
Operating System: linux
Architecture: amd64
Container Runtime Version: docker://18.6.1
Kubelet Version: v1.15.0
Kube-Proxy Version: v1.15.0
PodCIDR: 10.244.0.0/24
Non-terminated Pods: (9 in total)
Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits AGE
--------- ---- ------------ ---------- --------------- ------------- ---
kube-system coredns-bccdc95cf-bgw9p 100m (5%) 0 (0%) 70Mi (4%) 170Mi (9%) 24d
kube-system coredns-bccdc95cf-wpbjz 100m (5%) 0 (0%) 70Mi (4%) 170Mi (9%) 24d
kube-system etcd-chushi 0 (0%) 0 (0%) 0 (0%) 0 (0%) 24d
kube-system kube-apiserver-chushi 250m (12%) 0 (0%) 0 (0%) 0 (0%) 24d
kube-system kube-controller-manager-chushi 200m (10%) 0 (0%) 0 (0%) 0 (0%) 24d
kube-system kube-flannel-ds-amd64-2dsdr 100m (5%) 100m (5%) 50Mi (2%) 50Mi (2%) 24d
kube-system kube-proxy-t2dm5 0 (0%) 0 (0%) 0 (0%) 0 (0%) 24d
kube-system kube-scheduler-chushi 100m (5%) 0 (0%) 0 (0%) 0 (0%) 24d
kube-system kubernetes-dashboard-79ddd5-x4rkc 0 (0%) 0 (0%) 0 (0%) 0 (0%) 23d
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 850m (42%) 100m (5%)
memory 190Mi (11%) 390Mi (22%)
ephemeral-storage 0 (0%) 0 (0%)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Starting 4m2s kubelet, chushi Starting kubelet.
Normal NodeHasSufficientMemory 4m2s (x8 over 4m2s) kubelet, chushi Node chushi status is now: NodeHasSufficientMemory
Normal NodeHasNoDiskPressure 4m2s (x8 over 4m2s) kubelet, chushi Node chushi status is now: NodeHasNoDiskPressure
Normal NodeHasSufficientPID 4m2s (x7 over 4m2s) kubelet, chushi Node chushi status is now: NodeHasSufficientPID
Normal NodeAllocatableEnforced 4m2s kubelet, chushi Updated Node Allocatable limit across pods
Normal Starting 3m50s kube-proxy, chushi Starting kube-proxy.

 

5、查看命名空间下所有的pod

格式:kubectl get pod --all-namespaces

 

 6、查看一个命名空间下的所有pod

格式:kubectl get pod -n 命名空间name

 

7、查看某个命名空间下的某个pod

格式:kubectl describe pod pod名 -n 命名空间名

[root@chushi ~]# kubectl describe pod kube-flannel-ds-amd64-2dsdr -n kube-system
Name: kube-flannel-ds-amd64-2dsdr
Namespace: kube-system
Priority: 0
Node: chushi/192.168.80.128
Start Time: Mon, 25 Nov 2019 15:39:24 +0800
Labels: app=flannel
controller-revision-hash=68bc74f4cc
pod-template-generation=1
tier=node
Annotations: <none>
Status: Running
IP: 192.168.80.128
Controlled By: DaemonSet/kube-flannel-ds-amd64
Init Containers:
install-cni:
Container ID: docker://2c00be015b66b9e32e9b67f68975b375713ba13b4633b135a2cb3980fa6ccbf7
Image: quay.io/coreos/flannel:v0.11.0-amd64
Image ID: docker-pullable://lizhenliang/flannel@sha256:bd76b84c74ad70368a2341c2402841b75950df881388e43fc2aca000c546653a
Port: <none>
Host Port: <none>
Command:
cp
Args:
-f
/etc/kube-flannel/cni-conf.json
/etc/cni/net.d/10-flannel.conflist
State: Terminated
Reason: Completed
Exit Code: 0
Started: Fri, 20 Dec 2019 15:25:08 +0800
Finished: Fri, 20 Dec 2019 15:25:08 +0800
Ready: True
Restart Count: 6
Environment: <none>
Mounts:
/etc/cni/net.d from cni (rw)
/etc/kube-flannel/ from flannel-cfg (rw)
/var/run/secrets/kubernetes.io/serviceaccount from flannel-token-pvw94 (ro)
Containers:
kube-flannel:
Container ID: docker://831b8254f9e489d70865c7d0c44c881c9833967dcb5fdf0decfa9cce2012980b
Image: quay.io/coreos/flannel:v0.11.0-amd64
Image ID: docker-pullable://lizhenliang/flannel@sha256:bd76b84c74ad70368a2341c2402841b75950df881388e43fc2aca000c546653a
Port: <none>
Host Port: <none>
Command:
/opt/bin/flanneld
Args:
--ip-masq
--kube-subnet-mgr
State: Running
Started: Fri, 20 Dec 2019 15:25:09 +0800
Last State: Terminated
Reason: Error
Exit Code: 137
Started: Fri, 13 Dec 2019 15:55:36 +0800
Finished: Mon, 16 Dec 2019 10:38:32 +0800
Ready: True
Restart Count: 6
Limits:
cpu: 100m
memory: 50Mi
Requests:
cpu: 100m
memory: 50Mi
Environment:
POD_NAME: kube-flannel-ds-amd64-2dsdr (v1:metadata.name)
POD_NAMESPACE: kube-system (v1:metadata.namespace)
Mounts:
/etc/kube-flannel/ from flannel-cfg (rw)
/run/flannel from run (rw)
/var/run/secrets/kubernetes.io/serviceaccount from flannel-token-pvw94 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
run:
Type: HostPath (bare host directory volume)
Path: /run/flannel
HostPathType:
cni:
Type: HostPath (bare host directory volume)
Path: /etc/cni/net.d
HostPathType:
flannel-cfg:
Type: ConfigMap (a volume populated by a ConfigMap)
Name: kube-flannel-cfg
Optional: false
flannel-token-pvw94:
Type: Secret (a volume populated by a Secret)
SecretName: flannel-token-pvw94
Optional: false
QoS Class: Guaranteed
Node-Selectors: <none>
Tolerations: :NoSchedule
node.kubernetes.io/disk-pressure:NoSchedule
node.kubernetes.io/memory-pressure:NoSchedule
node.kubernetes.io/network-unavailable:NoSchedule
node.kubernetes.io/not-ready:NoExecute
node.kubernetes.io/pid-pressure:NoSchedule
node.kubernetes.io/unreachable:NoExecute
node.kubernetes.io/unschedulable:NoSchedule
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedMount 17m kubelet, chushi MountVolume.SetUp failed for volume "flannel-token-pvw94" : couldn't propagate object cache: timed out waiting for the condition
Warning FailedMount 17m kubelet, chushi MountVolume.SetUp failed for volume "flannel-cfg" : couldn't propagate object cache: timed out waiting for the condition
Normal SandboxChanged 17m kubelet, chushi Pod sandbox changed, it will be killed and re-created.
Normal Pulled 17m kubelet, chushi Container image "quay.io/coreos/flannel:v0.11.0-amd64" already present on machine
Normal Created 17m kubelet, chushi Created container install-cni
Normal Started 17m kubelet, chushi Started container install-cni
Normal Pulled 17m kubelet, chushi Container image "quay.io/coreos/flannel:v0.11.0-amd64" already present on machine
Normal Created 17m kubelet, chushi Created container kube-flannel
Normal Started 17m kubelet, chushi Started container kube-flannel

 

8、查看service

格式:kubectl get svc

 

9、查看pvc

格式:kubectl get pvc

10、查看deploy

格式:kubectl get deploy

11、查看svc下的某个service详细信息

格式:kubectl describe svc service名

 

 12、查看集群信息

格式:kubectl cluster-info

第一项输出是APIserver地址

第二项输出是外部访问集群DNS的地址

 

13、删除pod

格式:kubectl delete pod pod名

14、强制删除pod

格式:kubectl delete pod pod名 --force --grace-period=0 -n 命名空间name

 

15、简易列出pod运行所在的节点

kubectl get pod pod名 -o wide

简易列出namespaces下所有pod运行所在的节点

格式:kubectl get pod --all-namespaces -o wide

图上NODE项为所在节点,此处的IP是pod的IP

 

16、查看pod的yaml信息

格式: kubectl get pod pod名 -o yaml

[root@chushi ~]# kubectl get pod redis -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2019-12-20T08:21:47Z"
labels:
db: redis
name: redis
namespace: default
resourceVersion: "265437"
selfLink: /api/v1/namespaces/default/pods/redis
uid: 77764369-49af-4106-b916-b9394afc5386
spec:
containers:
- image: redis:latest
imagePullPolicy: Always
name: redis
ports:
- containerPort: 6379
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-gzslg
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-gzslg
secret:
defaultMode: 420
secretName: default-token-gzslg
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2019-12-20T08:21:47Z"
message: '0/3 nodes are available: 3 node(s) had taints that the pod didn''t tolerate.'
reason: Unschedulable
status: "False"
type: PodScheduled
phase: Pending
qosClass: BestEffort

 

17、编辑某个pod yaml信息

kubectl edit pod pod名

[root@chushi ~]# kubectl edit pod redis

dnsPolicy: ClusterFirst
enableServiceLinks: true
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-gzslg
secret:
defaultMode: 420
secretName: default-token-gzslg
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2019-12-20T08:21:47Z"
message: '0/3 nodes are available: 3 node(s) had taints that the pod didn''t tolerate.'
reason: Unschedulable
status: "False"
type: PodScheduled
phase: Pending
qosClass: BestEffort

 

18、查看pod的labels信息

kubectl get pod pod名 --show-labels

若想查看所有的pod的labels信息 kubectl get pod --show-labels

 

 19、查看node的labels信息

kubectl get node node名 --show-labels

若想查看所有的node的labels信息 kubectl get node --show-labels

 

 20、实时监控pod创建

kubect get pod -w

 

21、输出服务端和客户端的版本信息

kubectl versions

-c, --client[=false]: 仅输出客户端版本(无需连接服务器)。

命令的选项

      --alsologtostderr[=false]: 同时输出日志到标准错误控制台和文件。
      --api-version="": 和服务端交互使用的API版本。
      --certificate-authority="": 用以进行认证授权的.cert文件路径。
      --client-certificate="": TLS使用的客户端证书路径。
      --client-key="": TLS使用的客户端密钥路径。
      --cluster="": 指定使用的kubeconfig配置文件中的集群名。
      --context="": 指定使用的kubeconfig配置文件中的环境名。
      --insecure-skip-tls-verify[=false]: 如果为true,将不会检查服务器凭证的有效性,这会导致你的HTTPS链接变得不安全。
      --kubeconfig="": 命令行请求使用的配置文件路径。
      --log-backtrace-at=:0: 当日志长度超过定义的行数时,忽略堆栈信息。
      --log-dir="": 如果不为空,将日志文件写入此目录。
      --log-flush-frequency=5s: 刷新日志的最大时间间隔。
      --logtostderr[=true]: 输出日志到标准错误控制台,不输出到文件。
      --match-server-version[=false]: 要求服务端和客户端版本匹配。
      --namespace="": 如果不为空,命令将使用此namespace
      --password="": API Server进行简单认证使用的密码。
  -s, --server="": Kubernetes API Server的地址和端口号。
      --stderrthreshold=2: 高于此级别的日志将被输出到错误控制台。
      --token="": 认证到API Server使用的令牌。
      --user="": 指定使用的kubeconfig配置文件中的用户名。
      --username="": API Server进行简单认证使用的用户名。
      --v=0: 指定输出日志的级别。
      --vmodule=: 指定输出日志的模块,格式如下:pattern=N,使用逗号分隔。

 

22、查看组件信息

kubectl get cs

 

 23、主节点重置

kubeadm reset

 

24、查看replication controllers

kubectl get rc

 

25、查看未授权CSR请求(未授权的节点)

kubectl get csr        ###kubelet 首次启动时向 kube-apiserver 发送CA(证书)签名请求,必须通过后 kubernetes系统才会将该 Node 加入到cluster。 自动生成 kubelet kubeconfig 文件和公私钥

 

26、将节点加入TLS认证

kubectl certificate approve {NodeName}

 

27、查看pod日志

kubectl logs pod名                            ###查看容器输出的日志;
kubectl logs -f pod名  -c 容器名        ### -f 动态查看容器的日志  不加 -f 非动态查看;
kubectl exec pod-name cmd            ###在podname中执行cmd命令,该命令用‘’扩好;
kubectl exec pod名  -c 容器名          ###在pod中的容器containername中执行命令;
kubectl exec -it pod名 -c 容器名 /bin/sh -n ns-2          ###进入pod 的容器中,执行一些命令;

logs一些选项:

  -c, --container="": 容器名。
  -f, --follow[=false]: 指定是否持续输出日志。
      --interactive[=true]: 如果为true,当需要时提示用户进行输入。默认为true。
      --limit-bytes=0: 输出日志的最大字节数。默认无限制。
  -p, --previous[=false]: 如果为true,输出pod中曾经运行过,但目前已终止的容器的日志。
      --since=0: 仅返回相对时间范围,如5s、2m或3h,之内的日志。默认返回所有日志。只能同时使用since和since-time中的一种。
      --since-time="": 仅返回指定时间(RFC3339格式)之后的日志。默认返回所有日志。只能同时使用since和since-time中的一种。
      --tail=-1: 要显示的最新的日志条数。默认为-1,显示所有的日志。
      --timestamps[=false]: 在日志中包含时间戳。

 

28、进入pod中的某个容器

kubectl exec -it pod名 --container 容器名 -- /bin/bash

kubectl exec -it pod名 -c 容器名 -- /bin/bash

 

29、免交互执行pod中的命令

kubectl exec -n 命名空间   pod名字 ls

 

30、显示kubernet集群的配置设置信息

kubectl config view

 

31、通过验证添加一个新分支进集群

kubectl config set-credentials kubeuser/foo.kubernetes.com --username=$kube用户 --password=$kube密码

 

32、详细查看Pod的事件

kubectl get events

 

posted @ 2019-12-20 17:08  万能阿超  阅读(4814)  评论(0编辑  收藏  举报