kubectl

kubectl是kubernetes的管理工具.

https://github.com/cloudnativelabs/kube-shell

https://github.com/jonmosco/kube-ps1

https://github.com/ahmetb/kubectx

在master上通过kubectl命令管理集群.

1
kubectl options # 查看所有命令可用选项

配置:

1
kubectl --kubeconfig=$HOME/.kube.config (default)

basic command

 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
# 通过yaml或json文件创建pod/container:
$ kubectl create -f FILENAME [options]

$ kubectl delete -f FILENAME

# 创建serivce:
$ kubectl expose

# 创建pod/container:
$ kubectl run (docker run)
$ kubectl run <name> --image=<image> --labels=<key=value> ...

$ kubectl set

// 获取resource信息:
$ kubectl get (docker ps)
kubectl get nodes/no # 获取node节点信息
kubectl get namespace/ns # 获取namespace信息
kubectl get componentstatuses/cs

kubectl --all-namespaces get pod 
kubectl --all-namespaces get pods -o wide
kubectl -n kube-system get all 
kubectl -n kube-system get pods/po
kubectl -n kube-system get deployments/deploy
kubectl -n kube-system get services/svc

$ kubectl explain

$ kubectl edit

advanced commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ kubectl apply -f FILENAME

$ kubectl diff

$ kubectl patch

$ kubectl replace

$ kubectl wait

$ kubectl convert

$ kubectl kustomize

deploy command

1
2
3
4
5
6
7
8
// scale service.
$ kubectl scale

$ kubectl autoscale

$ kubectl rollout

$ kubectl rolling-update

debug&troubleshoot

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// like docker inspect
$ kubectl describe
//查看pod详细信息
$ kubectl -n kube-system describe pod <pod-name> 

// 获取k8s-dashboard的token
$ kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

# like docker logs
$ kubectl logs

# like docker exec
$ kubectl exec

$ kubectl attach

$ kubectl port-forward

$ kubectl proxy

$ kubectl cp

$ kubectl auth

cluster management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# top command
$ kubectl top

$ kubectl cluster-info

$ kubectl certificate

$ kubectl cordon

$ kubectl uncordon

$ kubectl drain

$ kubectl taint

Settings Commands

1
2
3
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)

others

1
2
3
4
5
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.