Skip to content

Kubernetes kubectl API

每个 Kubernetes 资源对象共享的 YAML 清单字段。

1 class · 6 methods

Resource Fields

6 methods

每个 Kubernetes 清单暴露的顶层字段。

kubectl apply -f file [-n namespace]

资源的 API 组和版本,如 apps/v1 或核心资源的 v1。

Parameters

NameTypeDescription
filestringManifest file path or URL; '-' reads from stdin.
namespacestringOptional target namespace.

Returns

void

Example

kubernetes
kubectl apply -f deployment.yaml -n production
kubectl get type [name] [-o format] [-n namespace]

资源类型,如 Pod、Deployment、Service、ConfigMap。

Parameters

NameTypeDescription
typestringResource type, e.g. pods, svc, deployments.
namestringOptional resource name.
formatstringOutput format: wide, yaml, json, jsonpath.

Returns

void

Example

kubernetes
kubectl get pods -o wide -n kube-system
kubectl describe type name [-n namespace]

资源的标识和组织数据:名称、命名空间、标签、注解。

Parameters

NameTypeDescription
typestringResource type.
namestringResource name.

Returns

void

Example

kubernetes
kubectl describe pod nginx-6799fc88d8-abc12
kubectl delete (-f file | type name) [--grace-period=0]

资源的期望状态。Schema 取决于类型。

Parameters

NameTypeDescription
filestringManifest path declaring resources to delete.
typestringResource type when deleting by name.
namestringResource name when deleting by name.

Returns

void

Example

kubernetes
kubectl delete pod buggy-pod --grace-period=0 --force
kubectl logs [-f] pod [-c container] [-n namespace]

Prints container logs. Follows logs with -f; selects a container when a pod has several.

Parameters

NameTypeDescription
podstringPod name.
containerstringContainer name inside the pod.

Returns

void

Example

kubernetes
kubectl logs -f api-7d8-xyz -c app
kubectl exec [-it] pod -- command [args...]

Executes a command inside a container in a pod.

Parameters

NameTypeDescription
podstringPod name.
commandstringCommand to execute.

Returns

void

Example

kubernetes
kubectl exec -it web-pod -- /bin/sh