参考文档

https://blog.51cto.com/zhangxueliang/2968407

指令帮助

  1. student@master01:~$ kubectl run -h
  2. Create and run a particular image in a pod.
  3. Examples:
  4. # Start a nginx pod
  5. kubectl run nginx --image=nginx
  6. # Start a hazelcast pod and let the container expose port 5701
  7. kubectl run hazelcast --image=hazelcast/hazelcast --port=5701

指令作用

创建并运行一个特定镜像的Pod

历史版本默认创建的是一个Deployment,新版本中创建的Pod

常用参数

—image=nginx 指定镜像
—replicas=5 指定副本数量
—dry-run 打印对象不创建
—restart Pod的重启策略
—env=”POD_NAMESPACE=default” 设置环境变量
—port=5701 暴露端口
—expose如果为true,会创建有ClusterIP的service此时需要搭配参数—port,默认为false;
—labels=”app=nginx,env=prod” 指定Pod的标签
—image-pull-policy 指定镜像的拉取策略

历史版本—generator参数已经废弃,新版本已经无此参数

指令议题

https://github.com/kubernetes/kubernetes/pull/87077

指令样例

对象打印

  1. student@master01:~$ kubectl run podname --image=nginx --dry-run=client -o yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. creationTimestamp: null
  6. labels:
  7. run: podname
  8. name: podname
  9. spec:
  10. containers:
  11. - image: nginx
  12. name: podname
  13. resources: {}
  14. dnsPolicy: ClusterFirst
  15. restartPolicy: Always
  16. status: {}
  17. student@master01:~$

进入终端

  1. student@master01:~$ kubectl run podname2 -it --image=nginx -- /bin/bash
  2. root@podname2:/# exit
  3. exit
  4. Session ended, resume using 'kubectl attach podname2 -c podname2 -i -t' command when the pod is running