k8s必须知道的好用命令

  1. 获取k8s节点及内存容量,-o jsonpath还可以替代jq命令
kubectl get no -o json | \
  jq -r '.items | sort_by(.status.capacity.memory)[]|[.metadata.name,.status.capacity.memory]| @tsv'
  1. k8s get显示自定义信息
kubectl get pods -A -o custom-columns=NodeName:.spec.nodeName,PodName:.metadata.name,PodUID:.metadata.uid

  1. k8s --dry-run命令,不真正执行,只输出yaml
kubectl create secret docker-registry  --dry-run=client  harbor-ld  --docker-server=192.168.12.12:8888 --docker-username=admin --docker-password=1234qwer   -o yaml > docker-secret.yaml

  1. kubectl patch命令,修改资源配置
kubectl patch vs test-101-vs  --type='json' -p='[{"op": "replace","path": "/spec/hosts/0", "value": "test.maple.com"}]' -n test
kubectl patch svc istio-ingressgateway -n istio-system -p '{"spec": {"externalIPs": ["xx.xx.xx.xx"]}}'
  1. 查看指定namespace下还有哪些资源没有删除
kubectl api-resources --verbs=list --namespaced -o name   | xargs -n 1 kubectl get --show-kind --ignore-not-found -n nightly

  1. 获取指定资源的描述清单
# kubectl explain deployment
# kubectl explain deployment.spec
# kubectl explain deployment.spec.template
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: template <Object>

DESCRIPTION:
     Template describes the pods that will be created.

     PodTemplateSpec describes the data a pod should have when created from a
     template

FIELDS:
   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
  1. 获取pod尾部日志
kubectl logs -f --tail=100 istio-ingressgateway-787c7bd99b-p97q2 -n istio-system
  1. pod如果崩溃重启,获取重启前的日志
kubectl logs -p istio-ingressgateway-787c7bd99b-p97q2 -n istio-system
  1. secret复制到其它命名空间
kubectl get secrets -o json --namespace namespace-old | \
  jq '.items[].metadata.namespace = "namespace-new"' | \
  kubectl create-f  -
  1. 生成测试自签发证书
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=maple Inc./CN=*.maple.com' -keyout maple.com.key -out maple.com.crt

kubectl create -n istio-system secret tls istio-ingressgateway-certs-host --key=maple.com.key --cert=maple.com.crt
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐