目录

kubectl

1
2
3
4
5
6
7
8
9
kubectl
kubectl version
kubectl api-versions
kubectl api-resources
kubectl create -h
kubectl create namespace -h

# 自动补全 setup autocomplete in zsh
source <(kubectl completion zsh)

上下文和配置

1
2
3
4
5
6
7
8
9
# 显示合并后的 kubeconfig 配置
kubectl config view 
# 同时使用多个 kubeconfig 文件并查看合并后的配置
KUBECONFIG=~/.kube/config:~/.kube/kubconfig2
kubectl config view
# 显示当前的上下文
kubectl config current-context    
# 设置默认上下文为 my-cluster-name
kubectl config use-context my-cluster-name  

显示与查找资源

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
kubectl get nodes -o wide
# kubectl get namespaces
kubectl get ns
kubectl -n kube-system get pods -o wide
kubectl -n kube-system get pods etcd-docker-desktop -o yaml
kubectl -n kube-system get pods etcd-docker-desktop -o yaml | grep image
kubectl -n kube-system get all
kubectl -n kube-system get po,svc

# -c 容器名
kubectl -n ynthm exec -it myblog -c myblog bash
env
1
2
kubectl -n kube-system describe pods/etcd-docker-desktop
kubectl -n kube-system describe pods etcd-docker-desktop
1
2
3
kubectl explain -h
kubectl explain pods
kubectl explain pods.spec.containers
1
kubectl -n kube-system logs -f --tail=100 myblog -c mysql
1
2
3
4
5
6
7
vim myblog-deploy.yaml
# 修改配置 应用yaml
kubectl apply -f myblog-deploy.yaml
kubectl -n ynthm scale deployments myblog --replicas=2
kubectl -n ynthm edit deploy myblog
kubectl -n ynthm describe deploy myblog
kubectl -n ynthm set image deploy myblog myblog=10.0.0.110:5000/myblog:v2 --record
1
2
docker build . -t 10.0.0.110:5000/myblog:v2 -f Dockfile
dokcer push 10.0.0.110:5000/myblog:v2

pod 更新策略

1
kubectl -n kube-system get deploy coredns -o yaml |grep -n5 strategy

pod 回滚

1
2
kubectl -n ynthm rollout history deploy myblog
kubectl -n ynthm rollout undo deploy myblog --to-revision=1

服务发现

service 名称可以作为pod内部域名访问相应服务。

1
2
3
4
5
6
kubectl get svc -A
kubectl -n kube-system describe svc kube-dns
kubectl -n kube-system get po -l k8s-app=kube-dns
# 进入pod 内部
kubectl -n ynthm exec -it myblog bash
cat /etc/resolv.conf

Scale 资源

1
2
3
4
5
6
7
8
# Scale a replicaset named 'foo' to 3
kubectl scale --replicas=3 rs/foo       
# Scale a resource specified in "foo.yaml" to 3
kubectl scale --replicas=3 -f foo.yaml     
# If the deployment named mysql's current size is 2, scale mysql to 3
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql  
# Scale multiple replication controllers
kubectl scale --replicas=5 rc/foo rc/bar rc/baz

删除资源

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 删除 pod.json 文件中定义的类型和名称的 pod
kubectl delete -f ./pod.json        
# 删除名为“baz”的 pod 和名为“foo”的 service
kubectl delete pod,service baz foo      
# 删除具有 name=myLabel 标签的 pod 和 serivce
kubectl delete pods,services -l name=myLabel       
# 删除具有 name=myLabel 标签的 pod 和 service,包括尚未初始化的
kubectl delete pods,services -l name=myLabel --include-uninitialized   
# 删除 my-ns namespace 下的所有 pod 和 serivce包
kubectl -n my-ns delete po,svc --all

与运行中的 Pod 交互

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# dump 输出 pod 的日志(stdout)
kubectl logs my-pod  
 # dump 输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)
kubectl logs my-pod -c my-container      
# 流式输出 pod 的日志(stdout)
kubectl logs -f my-pod             
# 流式输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)
kubectl logs -f my-pod -c my-container
# 交互式 shell 的方式运行 pod
kubectl run -i --tty busybox --image=busybox -- sh  
 # 连接到运行中的容器
kubectl attach my-pod -i    
# 转发 pod 中的 6000 端口到本地的 5000 端口
kubectl port-forward my-pod 5000:6000   
  # 在已存在的容器中执行命令(只有一个容器的情况下)
kubectl exec my-pod -- ls /          
# 在已存在的容器中执行命令(pod 中有多个容器的情况下)
kubectl exec my-pod -c my-container -- ls /    
# 显示指定 pod 和容器的指标度量
kubectl top pod <POD_NAME> --containers

与节点和集群交互

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 标记 my-node 不可调度
kubectl cordon my-node       
# 清空 my-node 以待维护
kubectl drain my-node    
# 标记 my-node 可调度
kubectl uncordon my-node             
# 显示 my-node 的指标度量
kubectl top node my-node
# 将当前集群状态输出到 stdout   
kubectl cluster-infoa                                     
# 将当前集群状态输出到 /path/to/cluster-state
kubectl cluster-info dump --output-directory=/path/to/cluster-state  
# 如果该键和影响的污点(taint)已存在,则使用指定的值替换
kubectl taint nodes foo dedicated=special-user:NoSchedule

资源类型

资源类型 缩写别名
clusters
componentstatuses cs
configmaps cm
daemonsets ds
deployments deploy
endpoints ep
event ev
horizontalpodautoscalers hpa
ingresses ing
jobs
limitranges limits
namespaces ns
networkpolicies
nodes no
statefulsets sts
persistentvolumeclaims pvc
persistentvolumes pv
pods po
podsecuritypolicies psp
podtemplates
replicasets rs
replicationcontrollers rc
resourcequotas quota
cornjob
secrets
serviceaccount sa
services svc
storageclasses
thirdpartyresources