使用jq命令对资源配置查看
有图形化的直接从图形化可以看到各种资源,如Deployment、Pod等资源的配置
这里写一个 jq 命令
jq命令允许针对json进行操作,如过滤
jq命令centos环境下安装
1
|
# yum -y install jq |
假设我们有个文件
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
|
# 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" } } } ] } } |
我们要直接取出下面这一段
1
2
3
4
5
6
7
8
|
"limits" : { "cpu" : "20m" , "memory" : "120Mi" }, "requests" : { "cpu" : "10m" , "memory" : "100Mi" } |
可以这么执行
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# cat pod-yaml | jq .spec.containers[].resources 输出: { "limits" : { "cpu" : "20m" , "memory" : "120Mi" }, "requests" : { "cpu" : "10m" , "memory" : "100Mi" } } |
规律很容易看出来,就是取key的value
同样,在查看资源时也可以这样使用
例如查看一个pod资源限制:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
查看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常用命令总结
k8s常用命令
kubectl常用命令
创建资源对象
1
|
kubectl create -f xxx.yaml(文件)、kubectl create -f <directory>(目录下所有文件) |
查看资源对象
1
2
|
kubectl get nodes kubectl get pods -n <namespace> -o wide |
描述资源对象
1
2
3
|
kubectl describe nodes <node-name> kubectl describe pods -n <namespace> kubectl describe <pod-name> kubectl describe pods <rc-name> |
删除资源对象
1
2
3
|
kubectl delete -f <filename> kubectl delete pods,services -l name=<label-name> kubectl delete pods --all(生产环境慎用) |
执行容器的命令
1
2
3
|
kubectl exec <pod-name> date (默认使用第一个容器执行Pod的 date 命令) kubectl exec <pod-name> -c <container-name> date (指定Pod中的某个容器执行 date 命令) kubectl exec -it <pod-name> -c <container-name> /bin/bash (相当与docker exec -it <container-name> /bin/bash ) |
查看容器的日志
1
2
|
kubectl logs <pod-name> kubectl logs -f <pod-name> -c <container-name> (相当于 tail -f 命令) |
kubectl格式化输出
显示Pod的更多信息
1
|
kubectl get pods -n <namespace> -o wide |
以yaml格式显示
1
|
kubectl get pods -n <namespace> -o yaml |
以自定义列明显示Pod信息
1
|
kubectl get pod <pod-name> -o =custom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion |
基于文件的自定义列名输出
1
|
kubectl get pods <pod-name> -o=custom-columns- file =template.txt |
输出结果排序
1
|
kubectl get pods -- sort -by=.metadata.name |
kubernetes集群管理指南
node的管理
命令:
1
2
3
|
kubectl replace -f xxx.yaml kubectl patch kubectl cordon <node_name> kubectl uncordon <node_name>(对node节点的隔离和恢复) |
删除节点:
1
2
|
kubectl drain swarm1 --delete- local -data --force --ignore-daemonsets kubectl delete node swarm1 |
使用:
1
2
|
kubectl get nodes kubectl cordon <node_name> kubectl uncordon <node_name> |
Label的管理:
给node设置标签
- 添加: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设置标签
把node改成pod即可
其他命令:
node节点加入master:
1
2
3
|
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节点:1)kubectl drain swarm1 --delete-local-data --force --ignore-daemonsets 2)kubectl delete node swarm1
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_38367535/article/details/123500595