kubernetes 使用jq命令對資源配置查看方式
使用jq命令對資源配置查看
有圖形化的直接從圖形化可以看到各種資源,如Deployment、Pod等資源的配置
這里寫一個 jq 命令
jq命令允許針對json進行操作,如過濾
jq命令centos環(huán)境下安裝
# yum -y install jq
假設(shè)我們有個文件
# cat pod-yaml
?
{
? ? "apiVersion": "v1",
? ? "kind": "Pod",
? ? "metadata": {
? ? ? ? "name": "nginx-pod",
? ? ? ? "namespace": "default"
? ? },
? ? "spec": {
? ? ? ? "containers": [
? ? ? ? ? ? {
? ? ? ? ? ? ? ? "image": "nginx:1.20",
? ? ? ? ? ? ? ? "imagePullPolicy": "IfNotPresent",
? ? ? ? ? ? ? ? "name": "nginx-pod",
? ? ? ? ? ? ? ? "resources": {
? ? ? ? ? ? ? ? ? ? "limits": {
? ? ? ? ? ? ? ? ? ? ? ? "cpu": "20m",
? ? ? ? ? ? ? ? ? ? ? ? "memory": "120Mi"
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? "requests": {
? ? ? ? ? ? ? ? ? ? ? ? "cpu": "10m",
? ? ? ? ? ? ? ? ? ? ? ? "memory": "100Mi"
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ]
? ? }
}我們要直接取出下面這一段
? "limits": {
? ? "cpu": "20m",
? ? "memory": "120Mi"
? },
? "requests": {
? ? "cpu": "10m",
? ? "memory": "100Mi"
? }可以這么執(zhí)行
# cat pod-yaml | jq .spec.containers[].resources
?
輸出:
{
? "limits": {
? ? "cpu": "20m",
? ? "memory": "120Mi"
? },
? "requests": {
? ? "cpu": "10m",
? ? "memory": "100Mi"
? }
}規(guī)律很容易看出來,就是取key的value
同樣,在查看資源時也可以這樣使用
例如查看一個pod資源限制:
查看pod名稱
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 22s
查看該pod的資源限制
# kubectl get pod/nginx-pod -o json | jq .spec.containers[].resources
{
"limits": {
"cpu": "20m",
"memory": "120Mi"
},
"requests": {
"cpu": "10m",
"memory": "100Mi"
}
}
查看該Pod的容器重啟策略
# kubectl get pod/nginx-pod -o json | jq .spec.restartPolicy
"Always"kubernetes常用命令總結(jié)
k8s常用命令
kubectl常用命令
創(chuàng)建資源對象
kubectl create -f xxx.yaml(文件)、kubectl create -f <directory>(目錄下所有文件)
查看資源對象
kubectl get nodes kubectl get pods -n <namespace> -o wide
描述資源對象
kubectl describe nodes <node-name> kubectl describe pods -n <namespace> kubectl describe <pod-name> kubectl describe pods <rc-name>
刪除資源對象
kubectl delete -f <filename> kubectl delete pods,services -l name=<label-name> kubectl delete pods --all(生產(chǎn)環(huán)境慎用)
執(zhí)行容器的命令
kubectl exec <pod-name> date(默認使用第一個容器執(zhí)行Pod的date命令) kubectl exec <pod-name> -c <container-name> date(指定Pod中的某個容器執(zhí)行date命令) kubectl exec -it <pod-name> -c <container-name> /bin/bash (相當與docker exec -it <container-name> /bin/bash)
查看容器的日志
kubectl logs <pod-name> kubectl logs -f <pod-name> -c <container-name> (相當于tail -f 命令)
kubectl格式化輸出
顯示Pod的更多信息
kubectl get pods -n <namespace> -o wide
以yaml格式顯示
kubectl get pods -n <namespace> -o yaml
以自定義列明顯示Pod信息
kubectl get pod <pod-name> -o =custom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion
基于文件的自定義列名輸出
kubectl get pods <pod-name> -o=custom-columns-file=template.txt
輸出結(jié)果排序
kubectl get pods --sort-by=.metadata.name
kubernetes集群管理指南
node的管理
命令:
kubectl replace -f xxx.yaml kubectl patch kubectl cordon <node_name> kubectl uncordon <node_name>(對node節(jié)點的隔離和恢復(fù))
刪除節(jié)點:
kubectl drain swarm1 --delete-local-data --force --ignore-daemonsets kubectl delete node swarm1
使用:
kubectl get nodes kubectl cordon <node_name> kubectl uncordon <node_name>
Label的管理:
給node設(shè)置標簽
- 添加:kubectl label node lustre-manager-1 node-role.kubernetes.io/minion-1=
- 刪除:kubectl label node lustre-manager-1 node-role.kubernetes.io/minion-1-
- 修改: kubectl label node lustre-manager-1 node-role.kubernetes.io/minion-1= --overwrite
給pod設(shè)置標簽
把node改成pod即可
其他命令:
node節(jié)點加入master:
kubeadm join 192.168.138.131:6443 --token zlk694.ev3odwj7rbyaggz6 --discovery-token-ca-cert-hash sha256:eefe51ccf1c54149f5ce89423c100b1e0de8f8081c7c2c0e07a7613ef2025146
生成加入master的命令:kubeadm token create --print-join-command
刪除node節(jié)點:1)kubectl drain swarm1 --delete-local-data --force --ignore-daemonsets 2)kubectl delete node swarm1
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
k8s部署Pyroscope并分析golang性能瓶頸(最新推薦)
這篇文章主要介紹了k8s部署Pyroscope并分析golang性能瓶頸,Pyroscope支持多種編程語言并提供了豐富的性能數(shù)據(jù),可以幫助我們跟蹤應(yīng)用程序的執(zhí)行情況,并根據(jù)收集到的數(shù)據(jù)來識別性能瓶頸,需要的朋友可以參考下2023-04-04
kubernetes需要默認的serviceaccount的原因解析
這篇文章主要介紹了kubernetes為何需要默認的serviceaccount,ServiceAccount 是 Kubernetes 中的一種重要概念,它的實際使用場景包括很多,本文給大家講解的非常詳細,需要的朋友可以參考下2023-04-04

