Kubernetes kubectl 常用操作
kubectl create
从file或stdin中创建资源。
// Create a service using the definition in example-service.yaml.
$ kubectl create -f example-service.yaml
// Create a replication controller using the definition in example-controller.yaml.
$ kubectl create -f example-controller.yaml
// Create the objects that are defined in any .yaml, .yml, or .json file within the <directory> directory.
$ kubectl create -f <directory>
kubectl get
列出一个或多个资源。
// List all pods in plain-text output format.
$ kubectl get pods
// List all pods in plain-text output format and includes additional information (such as node name).
$ kubectl get pods -o wide
// List the replication controller with the specified name in plain-text output format. Tip: You can shorten and replace the 'replicationcontroller' resource type with the alias 'rc'.
$ kubectl get replicationcontroller <rc-name>
// List all replication controllers and services together in plain-text output format.
$ kubectl get rc,services
kubectl describe
显示一个或多个资源的详细状态
// Display the details of the node with name <node-name>.
$ kubectl describe nodes <node-name>
// Display the details of the pod with name <pod-name>.
$ kubectl describe pods/<pod-name>
// Display the details of all the pods that are managed by the replication controller named <rc-name>.
// Remember: Any pods that are created by the replication controller get prefixed with the name of the replication controller.
$ kubectl describe pods <rc-name>
kubectl delete
从file、stdin或指定label选择器,names,resource选择器或resources中删除resources资源
// Delete a pod using the type and name specified in the pod.yaml file.
$ kubectl delete -f pod.yaml
// Delete all the pods and services that have the label name=<label-name>.
$ kubectl delete pods,services -l name=<label-name>
// Delete all pods.
$ kubectl delete pods --all
kubectl exec
对pod中的容器执行命令。
// Get output from running 'date' from pod <pod-name>. By default, output is from the first container.
$ kubectl exec <pod-name> date
// Get output from running 'date' in container <container-name> of pod <pod-name>.
$ kubectl exec <pod-name> -c <container-name> date
// Get an interactive TTY and run /bin/bash from pod <pod-name>. By default, output is from the first container.
$ kubectl exec -ti <pod-name> /bin/bash
kubectl logs
打印pod中的容器的日志。
// Return a snapshot of the logs from pod <pod-name>.
$ kubectl logs <pod-name>
// Start streaming the logs from pod <pod-name>. This is similar to the 'tail -f' Linux command.
$ kubectl logs -f <pod-name>