命令简化

将以下内容追加到环境变量中,可以简化运维操作

往~/.bashrc文件加入以下内容:

alias k=kubectl
source <(kubectl completion bash | sed s/kubectl/k/g)
alias kcd='kubectl config set-context $(kubectl config current-context) --namespace'
KUBE_EDITOR=/usr/bin/vim

依赖以下包

# yum install bash-completion -y

关于查询短暂存储位置

K8S 会在当前的 Node 自动创建一个目录来实际承载这个卷,目录的位置在 Node 的 /var/lib/kubelet/pods 路径下。要查看这个目录中的内容,需要先找到 Pod Id 和对应的 Node,然后登录到这个 Node,就能找到这个目录了,POD ID使用以下命令可以对应上:

$ kubectl get pods -o custom-columns=PodName:.metadata.name,PodUID:.metadata.uid,PodNode:.spec.nodeName

删除非运行状态的Pod

$ export ns=XXX
$ kubectl -n $ns get pod --field-selector status.phase!=Running -o name | xargs $ kubectl  -n $ns delete

查看以带状态的Pod

打印出非Running状态的Pod

$ kubectl get pods --field-selector status.phase!=Running

进化版

$ kubectl get pod --field-selector status.phase!=Running -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NAMEDPACE:.metadata.namespace

高阶进化版

$ kubectl get pod --field-selector status.phase!=Running -o=custom-columns=POD_NAME:.metadata.name,STATUS:.status.phase,NAMEDPACE:.metadata.namespace,CONTAINER_NAME:.spec.containers[*].name

输出所有非Running状态的Pod

$ kubectl get pods -A --field-selector status.phase!="Running" -o=jsonpath='{.items[*].metadata.name}'

查看当前节点CIDR

$ kubectl get nodes -o jsonpath='{.items[*].spec.podCIDR}'

查看节点配置

$ kubectl get --raw "/api/v1/nodes/kube-master-1/proxy/configz" | jq .

创建命名空间,不存在则创建,存在则忽略

kubernetes 1.19以上

$ kubectl create namespace <add-namespace-here> --dry-run=client -o yaml | kubectl apply -f -

$ kubernetes 1.18以下

$ kubectl create namespace <add-namespace-here> --dry-run -o yaml | kubectl apply -f -

删除可能不存在的资源

$ kubectl delete ns istio-system --ignore-not-found=true

SVC更新类型

$ kubectl -n istio-system patch svc istio-ingressgateway -p '{"spec": {"ports": [{"nodePort": 443, "port": 443,"targetPort": 8443,"name": "https"},{"nodePort": 80, "port": 80,"targetPort": 8080,"name": "http2"}],"type": "NodePort"}}'

打印集群中的污点信息

  • 1
$ kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints --no-headers
  • 2
$ kubectl get nodes -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.taints[*].key}{"\n"}{end}' 
  • 3

只打印有污点的节点

$ kubectl get nodes -o go-template='{{printf "%-50s %-12s\n" "Node" "Taint"}}{{- range .items}}{{- if $taint := (index .spec "taints") }}{{- .metadata.name }}{{ "\t" }}{{- range $taint }}{{- .key }}={{ .value }}:{{ .effect }}{{ "\t" }}{{- end }}{{- "\n" }}{{- end}}{{- end}}'

打印集群中的节点资源

$ kubectl get nodes -o custom-columns=NAME:.metadata.name,ARCH:.status.nodeInfo.architecture,KERNEL:.status.nodeInfo.kernelVersion,KUBLET:.status.nodeInfo.kubeletVersion,CPU:.status.capacity.cpu,RAM:.status.capacity.memory

打印集群所有的节点

$ kubectl get nodes -o jsonpath='{range.items[*]}{.metadata.name}'

集体排水:慎用

$ for node in `kubectl get nodes -o jsonpath='{range.items[*]}{.metadata.name}'`
do
	drain="kubectl drain $node --ignore-daemonsets &"
	eval $drain
done

Node

新节点加入

$ kubeadm token create --print-join-comman

节点维护或排水

$ kubectl drain nodename --force --ignore-daemonsets --delete-local-data
$ kubectl cordon node-name
$ kubectl drain node-name 
$ kubectl uncordon node-name

命名空间

当前所在的命名空间

方法1:

$ kubectl config get-contexts | grep -e "^\*" |  awk '{print $5}' 

方法2:

$ kubectl config view --minify | awk '/namespace:/{print $2}'

切换命名空间

$ kubectl config set-context --current --namespace=default

--current 可以使用kubectl config current-context

滚动重启应用

$ kubectl rollout restart deployment nginx-deploy

**提示:**由于大多数容器仍在运行,因此整个过程是感知的。

按标签输出pod

$ kubectl get pod -l environment=dev,tier=backend
$ kubectl get pod -l 'environment in (dev,prod)'

输出pod名称

第一个

$ kubectl get pods -o=jsonpath='{.items[0].metadata.name}'

全部

$ kubectl get pods -o=jsonpath='{.items[*].metadata.name}'

//格式化
$ kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'

输出节点与pod名

$ kubectl get po -o json | jq -r '.items | sort_by(.spec.nodeName)[] | [.spec.nodeName,.metadata.name] | @tsv'

自定义排序

$ kubectl get po -o wide --sort-by=.spec.nodeName

查看运行的容器镜像

$ kubectl -n gitee get po -o jsonpath={..image}|tr -s '[[:space:]]' '\n'|sort -ru

查看指定Pod 地址

$ kubectl get pods pod_name  -o jsonpath --template={.status.podIP}

查看Pod资源声明情况

$ kubectl get pods -A -o  go-template --template='
{{ range $i, $pod := .items -}}
  {{range .spec.containers -}}
    {{ $pod.metadata.namespace }} {{ $pod.metadata.name }} {{.resources.requests.cpu}} {{.resources.requests.memory}} {{.resources.limits.cpu}} {{.resources.limits.memory}}
  {{end -}}
{{end}}'

使用到的镜像

$ kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c

对应Pod使用的镜像

$ kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\
sort

输出PV资源

$ kubectl get pv -o json | jq -r '.items | sort_by(.spec.capacity.storage)[]|[.metadata.name,.spec.capacity.storage]| @tsv'