kubectl命令

创建对象

bash
1
2
3
4
kubectl create -f <file...|dir|url>		# 创建资源(指定yaml文件或目录)

kubectl run nginx --image=nginx    		# 启动一个nginx实例
kubectl explain pods    			# 获取pod和svc的文档

查找资源

bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 以下命令查找资源时可能查不到的原因是需要指定namespace,通过 -n <namespace>指定即可,或者all
kubectl get pods --all-namespaces    # 列出所有namespace中的pod,也可以是services、deployment等
kubectl get pods -o wide    # 列出pod并显示详细信息
kubectl get deployment my-dep    #列出指定daployment
kubectl get pods --include-uninitialized    # 列出该namespace中的所有pod,包括未初始化的
# 使用详细输出来描述命令
kubectl describe nodes <my-node IP or name>    #查看node节点信息
kubectl describe pods <my-pod>    #查看pod详细信息
kubectl get services --sort-by=.metadata.name --all-namespaces    #l列出所有service并按名称排序
# 根据重启次数排序列出pod
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' --all-namespaces
# 获取所有具有app=cassandra的pod中的version标签
kubectl get pods --selector=app=cassandra rc -o jsonpath='{.items[*].metadata.labels.version}'
# 获取所有节点的ExternalIP
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternlIP")].address}'

kubectl get all								# 获取resource
kubectl get namespace						# 查看命名空间
kubectl get componentstatuses				# 查看组件状态 
kubectl get replicationcontroller <name>	# 查看复制控制器

kubectl get pods			# 列出所有的pods
kubectl get pods -o wide
kubectl get pod <podname> -o json   # 以json输出pod信息

kubectl get -f pod.yaml -o json		# 根据pod文件查找pod,并以json格式输出
kubectl get -o template pod/kube-dns-795f5f6f9c-ldxxs --template {{.status.phase}}	# 获取pod容器的状态
kubectl get rc,services # 同时获取所有的rc和service
kubectl get rc/web service/frontend pods/web-pod-13je7 # 获取符合条件的所有rc,svc,pod
 
kubectl get nodes
kubectl get namespace
kubectl get pod
kubectl get gs
kubectl get fleet
kubectl get fleetautoscaler
kubectl get svc -n httpserver
kubectl get deploy -n httpserver
kubectl get pod -n httpserver

kubectl get cs			#查看组件状态
kubectl cluster-info	# 查看环境信息
kubectl get nodes -o wide	#查看 Node
kubectl -n kube-system get cm kubeadm-config -oyaml #查看集群配置
kubectl run nginx --image=nginx --replicas=2 --port=80 #运行容器
kubectl expose deployment nginx --port=80 --type=LoadBalancer #暴露服务
kubectl get namespace #查看命名空间

#创建命名空间
apiVersion: v1
kind: Namespace
metadata:
  name: development
  
# 查看容器  
kubectl get pods -o wide
kubectl get deployment -o wide
kubectl get service -o wide #查看服务

# 查看详情
kubectl describe pod <Pod Name>
kubectl describe deployment <Deployment Name>
kubectl describe service <Service Name>

# 查看日志
kubectl logs -f <Pod Name>

# 删除容器和服务
kubectl delete deployment <Deployment Name>
kubectl delete service <Service Name>


kubectl create -f <YAML>		#配置方式删除

#查看配置
kubeadm config view
kubectl config view

kubectl get ingress	#查看 Ingress
kubectl get pv	#查看持久卷
kubectl get pvc	#查看持久卷消费者
kubectl get cm <ConfigMap Name>	#查看 ConfigMap
kubectl edit cm <ConfigMap Name> #修改 ConfigMap

编辑资源

bash
1
kubectl -n codeus edit svc/c    #编辑codeus命名空间下名称为c的service

Scale资源

bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 扩展pod下容器数量
kubectl scale --replicas=3 rs/foo    #扩展名称为foo的资源到3个,是否使用rs取决于yaml中的编写
# 例如yaml中kind: Deployment ,则应通过下面方法扩展
kubectl scale --replicas=3 deployment/foo
# 或者直接通过创建资源的yaml文件扩展
kubectl scale --replicas=3 -f foo.yaml
# 根据判断条件扩展
# 例如条件是:如果mysql的数量是2,则扩展到3
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql
# 同时扩展多个资源
kubectl scale --replicas=5 rc/foo rc/bar rc/baz

删除资源

bash
1
2
kubectl delete deployment <name>     # 删除指定deployment,此方法还可以删除service等
kubectl delete -f xxx.yaml    # 通过创建此pod的yaml文件删除pod

与pod交互

bash
1
2
3
kubectl -n <namespaces> logs my-podname    # 查看pod日志, -f 持续查看
kubectl port-forward my-podname 5000:6000    # 转发pod中的6000端口到本地的5000端口
kubectl exec my-podname -- ls /    # 在已存在的容器中执行命令

Reworld

bash
1
2
kubectl exec -it sg-beta-fleet-jtmfm-xl2wf -- /bin/bash
kubectl apply -f deployment.yaml
yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: sg-beta-http
  name: sg-beta-http
  namespace: httpserver
spec:
  replicas: 3
  selector:
    matchLabels:
      run: sg-beta-http
  template:
    metadata:
      labels:
        run: sg-beta-http
    spec:
      containers:
      - image: 492066437306.dkr.ecr.ap-southeast-1.amazonaws.com/sg-rd-prd-phys:sg-beta-http-43
        imagePullPolicy: IfNotPresent
        name: sg-beta-http

官方文档

https://www.jianshu.com/p/0ae3b2919481

shell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
kubectl controls the Kubernetes cluster manager.

查看更多帮助信息: https://kubernetes.io/zh/docs/reference/kubectl/overview/

Basic Commands (Beginner):
  create         从文件或标准输入创建资源.
  expose         Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
  run            Run a particular image on the cluster
  set            Set specific features on objects

Basic Commands (Intermediate):
  explain        Documentation of resources
  get            Display one or many resources
  edit           Edit a resource on the server
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout        Manage the rollout of a resource
  scale          Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
  autoscale      Auto-scale a Deployment, ReplicaSet, or ReplicationController

Cluster Management Commands:
  certificate    Modify certificate resources.
  cluster-info   Display cluster info
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         Mark node as unschedulable
  uncordon       Mark node as schedulable
  drain          Drain node in preparation for maintenance
  taint          Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe       Show details of a specific resource or group of resources
  logs           Print the logs for a container in a pod
  attach         Attach to a running container
  exec           Execute a command in a container
  port-forward   Forward one or more local ports to a pod
  restart        Restart a pod
  shutdown       Shutdown a pod
  start          UnShutdown a pod
  proxy          Run a proxy to the Kubernetes API server
  cp             Copy files and directories to and from containers.
  auth           Inspect authorization

Advanced Commands:
  diff           Diff live version against would-be applied version
  apply          Apply a configuration to a resource by filename or stdin
  patch          Update field(s) of a resource using strategic merge patch
  replace        Replace a resource by filename or stdin
  wait           Experimental: Wait for a specific condition on one or many resources.
  convert        Convert config files between different API versions
  kustomize      Build a kustomization target from a directory or a remote url.

Settings Commands:
  label          Update the labels on a resource
  annotate       Update the annotations on a resource
  completion     Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  api-resources  Print the supported API resources on the server
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         Modify kubeconfig files
  plugin         Provides utilities for interacting with plugins.
  version        Print the client and server version information

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

官方文档:https://kubernetes.io/zh/docs/reference/

https://www.jianshu.com/p/bc6d0b92e08c

Go客户端:https://github.com/kubernetes/client-go/


「真诚赞赏,手留余香」

Hollson码夫庄园

嗯,我的梦想是被读者的稿费包养~

使用微信扫描二维码完成支付

Hollson

评论