示例

  1. # 创建
  2. [rancher@master1 test]$ kubectl create -f pod.yml
  3. pod/myapp-pod created
  4. # 查询
  5. [rancher@master1 test]$ kubectl get pod -o wide
  6. NAME READY STATUS RESTARTS AGE
  7. myapp-pod 1/1 Running 0 8s
  8. # 查询json
  9. [rancher@master1 test]$ kubectl get pod -n demo myapp-pod -o json |less
  10. # 查询详细
  11. [rancher@master1 test]$kubectl describe pod -n demo myapp-pod
  12. # 获取被创建时的yml文件内容
  13. [rancher@master1 test]$ kubectl get pod -n demo myapp-pod -o yaml
  14. # 删除
  15. [rancher@master1 test]$ kubectl delete -f .
  16. pod "myapp-pod" deleted
  17. # 创建和更新,apply大法好
  18. [rancher@master1 test]$ kubectl apply -f .
  19. pod/myapp-pod created
  20. # edit更新,如果只更新标签,那么元数据里的标签不会更新,,所以一般不用这种方法,除非要改全改了
  21. [rancher@master1 test]$ kubectl edit pod -n demo myapp-pod
  22. # patch替换,比较麻烦,效果跟edit一样
  23. [rancher@master1 test]$ kubectl patch pod -p '{"metadata":{"labels":{"app":"pod-nginx"}}}' myapp-pod -n demo
  24. pod/myapp-pod patched
  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: myapp-pod
  5. namespace: demo
  6. labels:
  7. app: myapp
  8. spec:
  9. containers:
  10. - name: myapp-container
  11. image: busybox
  12. command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
  13. - name: myapp-nginx
  14. image: nginx