|NO.Z.00018|——————————|^^ 标准 ^^|——|KuberNetes&标准.V17|——|常用操作.V03|创建资源|查看资源|

一、创建命令
### --- 创建命令
~~~     # 创建资源
[root@k8s-master01 ~]#   kubectl apply -f ./my-manifest.yaml
~~~     # 使用多个文件创建
[root@k8s-master01 ~]#   kubectl apply -f ./my1.yaml -f ./my2.yaml   
~~~     # 基于目录下的所有清单文件创建资源
[root@k8s-master01 ~]#   kubectl apply -f ./dir            
~~~     # 从 URL 中创建资源
[root@k8s-master01 ~]#   kubectl apply -f https://git.io/vPieo      
~~~     # 启动单实例 nginx
[root@k8s-master01 ~]#   kubectl create deployment nginx --image=nginx 
~~~     # 获取 pod 清单的文档说明
[root@k8s-master01 ~]#   kubectl explain pods,svc      
~~~     # 从标准输入创建多个 YAML 对象

[root@k8s-master01 ~]#   cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000000"
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep-less
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000"
EOF
~~~     # 创建有多个 key 的 Secret

[root@k8s-master01 ~]#   cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  password: $(echo -n "s33msi4" | base64 -w0)
  username: $(echo -n "jane" | base64 -w0)
EOF

二、查看和查找创建的资源
### --- 查看和查找创建的资源
~~~     # get 命令的基本输出

[root@k8s-master01 ~]#   kubectl get services                 // 列出当前命名空间下的所有 services
[root@k8s-master01 ~]#   kubectl get pods --all-namespaces    // 列出所有命名空间下的全部的 Pods
[root@k8s-master01 ~]#   kubectl get pods -o wide             // 列出当前命名空间下的全部 Pods,并显示更详细的信息
[root@k8s-master01 ~]#   kubectl get deployment my-dep        // 列出某个特定的 Deployment
[root@k8s-master01 ~]#   kubectl get pods                     // 列出当前命名空间下的全部 Pods
[root@k8s-master01 ~]#   kubectl get pod my-pod -o yaml       // 获取一个 pod 的 YAML
~~~     # describe 命令的详细输出

[root@k8s-master01 ~]#  kubectl describe nodes my-node
[root@k8s-master01 ~]#  kubectl describe pods my-pod
~~~     # 列出当前名字空间下所有 Services,按名称排序
[root@k8s-master01 ~]#  kubectl get services --sort-by=.metadata.name

~~~     # 列出 Pods,按重启次数排序
[root@k8s-master01 ~]#  kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
~~~     # 列举所有 PV 持久卷,按容量排序
[root@k8s-master01 ~]#  kubectl get pv --sort-by=.spec.capacity.storage

~~~     # 获取包含 app=cassandra 标签的所有 Pods 的 version 标签
[root@k8s-master01 ~]#  kubectl get pods --selector=app=cassandra -o \
  jsonpath='{.items[*].metadata.labels.version}'
~~~     # 获取所有工作节点(使用选择器以排除标签名称为 'node-role.kubernetes.io/master' 的结果)
[root@k8s-master01 ~]#  kubectl get node --selector='!node-role.kubernetes.io/master'

~~~     # 获取当前命名空间中正在运行的 Pods
kubectl get pods --field-selector=status.phase=Running
~~~     # 获取全部节点的 ExternalIP 地址
[root@k8s-master01 ~]#  kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

~~~     # 列出属于某个特定 RC 的 Pods 的名称
~~~     # 在转换对于 jsonpath 过于复杂的场合,"jq" 命令很有用;
~~~     可以在 https://stedolan.github.io/jq/ 找到它。
[root@k8s-master01 ~]#  sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?}
[root@k8s-master01 ~]#  echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})
~~~     # 显示所有 Pods 的标签(或任何其他支持标签的 Kubernetes 对象)
[root@k8s-master01 ~]#  kubectl get pods --show-labels

~~~     # 检查哪些节点处于就绪状态
[root@k8s-master01 ~]#  JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
 && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
~~~     # 列出被一个 Pod 使用的全部 Secret
[root@k8s-master01 ~]#  kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

~~~     # 列举所有 Pods 中初始化容器的容器 ID(containerID)
~~~     Helpful when cleaning up stopped containers, while avoiding removal of initContainers.
[root@k8s-master01 ~]#  kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3
~~~     # 列出事件(Events),按时间戳排序
[root@k8s-master01 ~]#  kubectl get events --sort-by=.metadata.creationTimestamp

~~~     # 比较当前的集群状态和假定某清单被应用之后的集群状态
[root@k8s-master01 ~]#  kubectl diff -f ./my-manifest.yaml

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-03-29 13:10  yanqi_vip  阅读(11)  评论(0)    收藏  举报

导航