AC CLI 开发者命令参考

本参考提供了 AC CLI 开发者命令的描述和示例命令。有关管理员命令,请参见 AC CLI 管理员命令参考。

运行 ac help 可列出所有命令,或运行 ac <command> --help 获取特定命令的详细信息。

目录

ac annotate示例用法ac api-resources示例用法ac api-versions示例用法ac apply示例用法ac apply edit-last-applied示例用法ac apply set-last-applied示例用法ac apply view-last-applied示例用法ac attach示例用法ac authac auth can-i示例用法ac auth reconcile示例用法ac auth whoami示例用法ac autoscale示例用法ac cluster-info示例用法ac cluster-info dump示例用法ac completion示例用法ac configac config current-context示例用法ac config delete-cluster示例用法ac config delete-context示例用法ac config delete-user示例用法ac config get-clusters示例用法ac config get-contexts示例用法ac config get-sessions示例用法ac config get-users示例用法ac config rename-context示例用法ac config set示例用法ac config set-cluster示例用法ac config set-context示例用法ac config set-credentials示例用法ac config sync示例用法ac config unset示例用法ac config use-cluster示例用法ac config use-context示例用法ac config use-session示例用法ac config view示例用法ac cp示例用法ac create示例用法ac create clusterrole示例用法ac create clusterrolebinding示例用法ac create configmap示例用法ac create cronjob示例用法ac create deployment示例用法ac create ingress示例用法ac create job示例用法ac create namespace示例用法ac create poddisruptionbudget示例用法ac create priorityclass示例用法ac create quota示例用法ac create role示例用法ac create rolebinding示例用法ac create secretac create secret docker-registry示例用法ac create secret generic示例用法ac create secret tls示例用法ac create serviceac create service clusterip示例用法ac create service externalname示例用法ac create service loadbalancer示例用法ac create service nodeport示例用法ac create serviceaccount示例用法ac create token示例用法ac delete示例用法ac describe示例用法ac diff示例用法ac edit示例用法ac events示例用法ac exec示例用法ac explain示例用法ac expose示例用法ac get示例用法ac kustomize示例用法ac label示例用法ac login示例用法ac logout示例用法ac logs示例用法ac namespace示例用法ac patch示例用法ac plugin示例用法ac plugin list示例用法ac port-forward示例用法ac process示例用法ac proxy示例用法ac replace示例用法ac rollout示例用法ac rollout history示例用法ac rollout pause示例用法ac rollout restart示例用法ac rollout resume示例用法ac rollout status示例用法ac rollout undo示例用法ac run示例用法ac scale示例用法ac setac set env示例用法ac set image示例用法ac set resources示例用法ac set selector示例用法ac set serviceaccount示例用法ac set subject示例用法ac version示例用法ac wait示例用法

ac annotate

更新资源的注解

示例用法

# 更新 pod 'foo' 的注解 'description',值为 'my frontend'
# 如果同一注解被多次设置,只有最后一个值会生效
ac annotate pods foo description='my frontend'

# 更新通过类型和名称在 "pod.json" 中标识的 pod
ac annotate -f pod.json description='my frontend'

# 更新 pod 'foo' 的注解 'description',值为 'my frontend running nginx',覆盖任何已有值
ac annotate --overwrite pods foo description='my frontend running nginx'

# 更新命名空间中的所有 pod
ac annotate pods --all description='my frontend running nginx'

# 仅当资源版本为 1 时更新 pod 'foo'
ac annotate pods foo description='my frontend running nginx' --resource-version=1

# 删除 pod 'foo' 中名为 'description' 的注解(如果存在)
# 不需要 --overwrite 标志
ac annotate pods foo description-

ac api-resources

打印服务器支持的 API 资源

示例用法

# 打印支持的 API 资源
ac api-resources

# 打印带更多信息的支持的 API 资源
ac api-resources -o wide

# 按列排序打印支持的 API 资源
ac api-resources --sort-by=name

# 打印支持的命名空间资源
ac api-resources --namespaced=true

# 打印支持的非命名空间资源
ac api-resources --namespaced=false

# 打印特定 APIGroup 的支持资源
ac api-resources --api-group=rbac.authorization.k8s.io

ac api-versions

打印服务器支持的 API 版本,格式为 "group/version"

示例用法

# 打印支持的 API 版本
ac api-versions

ac apply

通过文件名或标准输入应用配置到资源

示例用法

# 将 pod.json 中的配置应用到 pod
ac apply -f ./pod.json

# 从包含 kustomization.yaml 的目录应用资源 - 例如 dir/kustomization.yaml
ac apply -k dir/

# 将通过 stdin 传入的 JSON 应用到 pod
cat pod.json | ac apply -f -

# 应用所有以 '.json' 结尾的文件中的配置
ac apply -f '*.json'

# 注意:--prune 仍处于 Alpha 阶段
# 应用 manifest.yaml 中匹配标签 app=nginx 的配置,并删除文件中未包含且匹配标签 app=nginx 的其他资源
ac apply --prune -f manifest.yaml -l app=nginx

# 应用 manifest.yaml 中的配置,并删除文件中未包含的所有其他 config maps
ac apply --prune -f manifest.yaml --all --prune-allowlist=core/v1/ConfigMap

ac apply edit-last-applied

编辑资源/对象的最新 last-applied-configuration 注解

示例用法

# 以 YAML 格式编辑类型/名称指定的 last-applied-configuration 注解
ac apply edit-last-applied deployment/nginx

# 以 JSON 格式编辑文件中的 last-applied-configuration 注解
ac apply edit-last-applied -f deploy.yaml -o json

ac apply set-last-applied

设置 live 对象的 last-applied-configuration 注解以匹配文件内容

示例用法

# 将资源的 last-applied-configuration 设置为匹配文件内容
ac apply set-last-applied -f deploy.yaml

# 对目录中的每个配置文件执行 set-last-applied
ac apply set-last-applied -f path/

# 将资源的 last-applied-configuration 设置为匹配文件内容;如果注解不存在则创建
ac apply set-last-applied -f deploy.yaml --create-annotation=true

ac apply view-last-applied

查看资源/对象的最新 last-applied-configuration 注解

示例用法

# 以 YAML 格式查看类型/名称指定的 last-applied-configuration 注解
ac apply view-last-applied deployment/nginx

# 以 JSON 格式查看文件中的 last-applied-configuration 注解
ac apply view-last-applied -f deploy.yaml -o json

ac attach

附加到正在运行的容器

示例用法

# 获取运行中 pod mypod 的输出;使用 'ac.kubernetes.io/default-container' 注解选择附加的容器,否则选择 pod 中的第一个容器
ac attach mypod

# 获取 pod mypod 中 ruby-container 的输出
ac attach mypod -c ruby-container

# 切换到原始终端模式;将 stdin 发送到 pod mypod 中 ruby-container 的 'bash',并将 'bash' 的 stdout/stderr 返回给客户端
ac attach mypod -c ruby-container -i -t

# 获取名为 nginx 的 replica set 的第一个 pod 的输出
ac attach rs/nginx

ac auth

检查授权

ac auth can-i

检查某个操作是否被允许

示例用法

# 检查是否可以在所有命名空间创建 pods
ac auth can-i create pods --all-namespaces

# 检查是否可以在当前命名空间列出 deployments
ac auth can-i list deployments.apps

# 检查命名空间 "dev" 中的服务账户 "foo" 是否可以在命名空间 "prod" 中列出 pods
# 需要允许使用全局选项 "--as" 的模拟权限
ac auth can-i list pods --as=system:serviceaccount:dev:foo -n prod

# 检查是否可以在当前命名空间执行所有操作("*" 表示全部)
ac auth can-i '*' '*'

# 检查是否可以获取命名空间 "foo" 中名为 "bar" 的 job
ac auth can-i list jobs.batch/bar -n foo

# 检查是否可以读取 pod 日志
ac auth can-i get pods --subresource=log

# 检查是否可以访问 URL /logs/
ac auth can-i get /logs/

# 检查是否可以批准 certificates.k8s.io
ac auth can-i approve certificates.k8s.io

# 列出命名空间 "foo" 中所有允许的操作
ac auth can-i --list --namespace=foo

ac auth reconcile

对 RBAC 角色、角色绑定、集群角色和集群角色绑定对象进行规则对齐

示例用法

# 从文件对齐 RBAC 资源
ac auth reconcile -f my-rbac-rules.yaml

ac auth whoami

实验性功能:检查自身主体属性

示例用法

# 获取你的主体属性
ac auth whoami

# 以 JSON 格式获取你的主体属性
ac auth whoami -o json

ac autoscale

自动扩缩部署、ReplicaSet、StatefulSet 或 ReplicationController

示例用法

# 自动扩缩部署 "foo",pod 数量在 2 到 10 之间,未指定目标 CPU 利用率,使用默认自动扩缩策略
ac autoscale deployment foo --min=2 --max=10

# 自动扩缩 ReplicationController "foo",pod 数量在 1 到 5 之间,目标 CPU 利用率为 80%
ac autoscale rc foo --max=5 --cpu-percent=80

ac cluster-info

显示集群信息

示例用法

# 打印控制平面和集群服务的地址
ac cluster-info

ac cluster-info dump

导出调试和诊断相关信息

示例用法

# 将当前集群状态导出到标准输出
ac cluster-info dump

# 将当前集群状态导出到 /path/to/cluster-state
ac cluster-info dump --output-directory=/path/to/cluster-state

# 导出所有命名空间到标准输出
ac cluster-info dump --all-namespaces

# 导出指定命名空间集合到 /path/to/cluster-state
ac cluster-info dump --namespaces default,kube-system --output-directory=/path/to/cluster-state

ac completion

输出指定 shell(bash、zsh、fish 或 powershell)的补全代码

示例用法

# 在 macOS 上使用 homebrew 安装 bash 补全
## 如果使用 macOS 自带的 Bash 3.2
brew install bash-completion
## 或者,如果使用 Bash 4.1+
brew install bash-completion@2
## 如果通过 homebrew 安装 ac,补全应立即生效
## 如果通过其他方式安装,可能需要将补全脚本添加到补全目录
ac completion bash > $(brew --prefix)/etc/bash_completion.d/ac


# 在 Linux 上安装 bash 补全
## 如果未安装 bash-completion,请通过发行版包管理器安装 'bash-completion' 包
## 将 ac 的 bash 补全代码加载到当前 shell
source <(ac completion bash)
## 将 bash 补全代码写入文件,并从 .bash_profile 中 source
ac completion bash > ~/.kube/completion.bash.inc
printf "
# ac shell completion
source '$HOME/.kube/completion.bash.inc'
" >> $HOME/.bash_profile
source $HOME/.bash_profile

# 将 ac 的 zsh[1] 补全代码加载到当前 shell
source <(ac completion zsh)
# 设置 ac 的 zsh[1] 补全代码开机自动加载
ac completion zsh > "${fpath[1]}/_ac"


# 将 ac 的 fish[2] 补全代码加载到当前 shell
ac completion fish | source
# 若要每个会话都加载补全,执行一次:
ac completion fish > ~/.config/fish/completions/ac.fish

# 将 ac 的 powershell 补全代码加载到当前 shell
ac completion powershell | Out-String | Invoke-Expression
# 设置 ac 补全代码开机自动运行
## 保存补全代码到脚本并在配置文件中执行
ac completion powershell > $HOME\.kube\completion.ps1
Add-Content $PROFILE "$HOME\.kube\completion.ps1"
## 在配置文件中执行补全代码
Add-Content $PROFILE "if (Get-Command ac -ErrorAction SilentlyContinue) {
ac completion powershell | Out-String | Invoke-Expression
}"
## 直接将补全代码添加到 $PROFILE 脚本
ac completion powershell >> $PROFILE

ac config

修改 kubeconfig 文件

ac config current-context

显示当前上下文

示例用法

# 显示当前上下文
ac config current-context

ac config delete-cluster

从 kubeconfig 中删除指定集群

示例用法

# 删除 minikube 集群
ac config delete-cluster minikube

ac config delete-context

从 kubeconfig 中删除指定上下文

示例用法

# 删除 minikube 集群的上下文
ac config delete-context minikube

ac config delete-user

从 kubeconfig 中删除指定用户

示例用法

# 删除 minikube 用户
ac config delete-user minikube

ac config get-clusters

显示 kubeconfig 中定义的集群

示例用法

# 列出 ac 知道的集群
ac config get-clusters

ac config get-contexts

描述一个或多个上下文

示例用法

# 列出 kubeconfig 文件中的所有上下文
ac config get-contexts

# 描述 kubeconfig 文件中的一个上下文
ac config get-contexts my-context

ac config get-sessions

列出所有配置的 ACP 平台会话

示例用法

# 列出所有配置的 ACP 会话
ac config get-sessions

ac config get-users

显示 kubeconfig 中定义的用户

示例用法

# 列出 ac 知道的用户
ac config get-users

ac config rename-context

重命名 kubeconfig 文件中的上下文

示例用法

# 将上下文 'old-name' 重命名为 'new-name'
ac config rename-context old-name new-name

ac config set

设置 kubeconfig 文件中的单个值

示例用法

# 将 my-cluster 集群的 server 字段设置为 https://1.2.3.4
ac config set clusters.my-cluster.server https://1.2.3.4

# 设置 my-cluster 集群的 certificate-authority-data 字段
ac config set clusters.my-cluster.certificate-authority-data $(echo "cert_data_here" | base64 -i -)

# 将 my-context 上下文的 cluster 字段设置为 my-cluster
ac config set contexts.my-context.cluster my-cluster

# 使用 --set-raw-bytes 选项设置 cluster-admin 用户的 client-key-data 字段
ac config set users.cluster-admin.client-key-data cert_data_here --set-raw-bytes=true

ac config set-cluster

设置 kubeconfig 中的集群条目

示例用法

# 仅设置 e2e 集群条目的 server 字段,不修改其他值
ac config set-cluster e2e --server=https://1.2.3.4

# 为 e2e 集群条目嵌入证书颁发机构数据
ac config set-cluster e2e --embed-certs --certificate-authority=~/.kube/e2e/kubernetes.ca.crt

# 禁用 e2e 集群条目的证书检查
ac config set-cluster e2e --insecure-skip-tls-verify=true

# 设置 e2e 集群条目用于验证的自定义 TLS 服务器名称
ac config set-cluster e2e --tls-server-name=my-cluster-name

# 设置 e2e 集群条目的代理 URL
ac config set-cluster e2e --proxy-url=https://1.2.3.4

ac config set-context

设置 kubeconfig 中的上下文条目

示例用法

# 仅设置 gce 上下文条目的 user 字段,不修改其他值
ac config set-context gce --user=cluster-admin

ac config set-credentials

设置 kubeconfig 中的用户条目

示例用法

# 仅设置 "cluster-admin" 条目的 "client-key" 字段,不修改其他值
ac config set-credentials cluster-admin --client-key=~/.kube/admin.key

# 为 "cluster-admin" 条目设置基本认证
ac config set-credentials cluster-admin --username=admin --password=uXFGweU9l35qcif

# 在 "cluster-admin" 条目中嵌入客户端证书数据
ac config set-credentials cluster-admin --client-certificate=~/.kube/admin.crt --embed-certs=true

# 为 "cluster-admin" 条目启用 Google Compute Platform 认证提供者
ac config set-credentials cluster-admin --auth-provider=gcp

# 为 "cluster-admin" 条目启用 OpenID Connect 认证提供者并添加额外参数
ac config set-credentials cluster-admin --auth-provider=oidc --auth-provider-arg=client-id=foo --auth-provider-arg=client-secret=bar

# 移除 "cluster-admin" 条目中 OpenID Connect 认证提供者的 "client-secret" 配置值
ac config set-credentials cluster-admin --auth-provider=oidc --auth-provider-arg=client-secret-

# 为 "cluster-admin" 条目启用新的 exec 认证插件
ac config set-credentials cluster-admin --exec-command=/path/to/the/executable --exec-api-version=client.authentication.k8s.io/v1beta1

# 为 "cluster-admin" 条目启用新的 exec 认证插件并设置交互模式
ac config set-credentials cluster-admin --exec-command=/path/to/the/executable --exec-api-version=client.authentication.k8s.io/v1beta1 --exec-interactive-mode=Never

# 为 "cluster-admin" 条目定义新的 exec 认证插件参数
ac config set-credentials cluster-admin --exec-arg=arg1 --exec-arg=arg2

# 为 "cluster-admin" 条目创建或更新 exec 认证插件环境变量
ac config set-credentials cluster-admin --exec-env=key1=val1 --exec-env=key2=val2

# 移除 "cluster-admin" 条目的 exec 认证插件环境变量
ac config set-credentials cluster-admin --exec-env=var-to-remove-

ac config sync

同步 kubeconfig 与 ACP 平台状态

示例用法

# 基于当前活动上下文同步当前会话
ac config sync

# 同步指定会话
ac config sync --session prod

# 同步所有会话
ac config sync --all

ac config unset

取消设置 kubeconfig 文件中的单个值

示例用法

# 取消设置 current-context
ac config unset current-context

# 取消设置 foo 上下文中的 namespace
ac config unset contexts.foo.namespace

ac config use-cluster

通过集群名称切换到指定 ACP 集群

示例用法

# 切换到 workload-a 集群的现有上下文
ac config use-cluster workload-a

# 为 workload-b 创建带命名空间的新上下文
ac config use-cluster workload-b --namespace my-app

# 切换到 global 集群
ac config use-cluster global

ac config use-context

设置 kubeconfig 文件中的 current-context

示例用法

# 使用 minikube 集群的上下文
ac config use-context minikube

ac config use-session

切换到指定的 ACP 会话,支持智能上下文选择

示例用法

# 切换到 staging 会话(默认 global 集群)
ac config use-session staging

# 切换到指定集群的 production 会话
ac config use-session prod --cluster workload-b

# 切换到指定集群和命名空间的 staging 会话
ac config use-session staging --cluster workload-a --namespace my-app

ac config view

显示合并的 kubeconfig 设置或指定的 kubeconfig 文件

示例用法

# 显示合并的 kubeconfig 设置
ac config view

# 显示合并的 kubeconfig 设置,包含原始证书数据和暴露的密钥
ac config view --raw

# 获取 e2e 用户的密码
ac config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'

ac cp

在容器和本地之间复制文件和目录

示例用法

# !!!重要提示!!!
# 需要容器镜像中存在 'tar' 二进制文件
# 如果没有 'tar','ac cp' 将失败
#
# 对于高级用例,如符号链接、通配符展开或文件模式保留,建议使用 'ac exec'

# 将本地文件 /tmp/foo 复制到命名空间 <some-namespace> 中远程 pod 的 /tmp/bar
tar cf - /tmp/foo | ac exec -i -n <some-namespace> <some-pod> -- tar xf - -C /tmp/bar

# 将远程 pod 中的 /tmp/foo 复制到本地 /tmp/bar
ac exec -n <some-namespace> <some-pod> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar

# 将本地目录 /tmp/foo_dir 复制到默认命名空间中远程 pod 的 /tmp/bar_dir
ac cp /tmp/foo_dir <some-pod>:/tmp/bar_dir

# 将本地文件 /tmp/foo 复制到远程 pod 中指定容器的 /tmp/bar
ac cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>

# 将本地文件 /tmp/foo 复制到命名空间 <some-namespace> 中远程 pod 的 /tmp/bar
ac cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar

# 将远程 pod 中的 /tmp/foo 复制到本地 /tmp/bar
ac cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

ac create

从文件或标准输入创建资源

示例用法

# 使用 pod.json 中的数据创建 pod
ac create -f ./pod.json

# 基于通过 stdin 传入的 JSON 创建 pod
cat pod.json | ac create -f -

# 以 JSON 格式编辑 registry.yaml 中的数据,然后使用编辑后的数据创建资源
ac create -f registry.yaml --edit -o json

ac create clusterrole

创建集群角色

示例用法

# 创建名为 "pod-reader" 的集群角色,允许用户对 pods 执行 "get"、"watch" 和 "list"
ac create clusterrole pod-reader --verb=get,list,watch --resource=pods

# 创建名为 "pod-reader" 的集群角色,指定 ResourceName
ac create clusterrole pod-reader --verb=get --resource=pods --resource-name=readablepod --resource-name=anotherpod

# 创建名为 "foo" 的集群角色,指定 API Group
ac create clusterrole foo --verb=get,list,watch --resource=rs.apps

# 创建名为 "foo" 的集群角色,指定 SubResource
ac create clusterrole foo --verb=get,list,watch --resource=pods,pods/status

# 创建名为 "foo" 的集群角色,指定 NonResourceURL
ac create clusterrole "foo" --verb=get --non-resource-url=/logs/*

# 创建名为 "monitoring" 的集群角色,指定 AggregationRule
ac create clusterrole monitoring --aggregation-rule="rbac.example.com/aggregate-to-monitoring=true"

ac create clusterrolebinding

为特定集群角色创建集群角色绑定

示例用法

# 使用 cluster-admin 集群角色为 user1、user2 和 group1 创建集群角色绑定
ac create clusterrolebinding cluster-admin --clusterrole=cluster-admin --user=user1 --user=user2 --group=group1

ac create configmap

从本地文件、目录或字面值创建 config map

示例用法

# 基于目录 bar 创建名为 my-config 的新 config map
ac create configmap my-config --from-file=path/to/bar

# 创建名为 my-config 的新 config map,指定键而非磁盘上的文件名
ac create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt

# 创建名为 my-config 的新 config map,包含 key1=config1 和 key2=config2
ac create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

# 从文件中的键值对创建名为 my-config 的新 config map
ac create configmap my-config --from-file=path/to/bar

# 从 env 文件创建名为 my-config 的新 config map
ac create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env

ac create cronjob

创建指定名称的定时任务

示例用法

# 创建定时任务
ac create cronjob my-job --image=busybox --schedule="*/1 * * * *"

# 创建带命令的定时任务
ac create cronjob my-job --image=busybox --schedule="*/1 * * * *" -- date

ac create deployment

创建指定名称的部署

示例用法

# 创建名为 my-dep 的部署,运行 busybox 镜像
ac create deployment my-dep --image=busybox

# 创建带命令的部署
ac create deployment my-dep --image=busybox -- date

# 创建名为 my-dep 的部署,运行 nginx 镜像,副本数为 3
ac create deployment my-dep --image=nginx --replicas=3

# 创建名为 my-dep 的部署,运行 busybox 镜像,暴露端口 5701
ac create deployment my-dep --image=busybox --port=5701

# 创建名为 my-dep 的部署,运行多个容器
ac create deployment my-dep --image=busybox:latest --image=ubuntu:latest --image=nginx

ac create ingress

创建指定名称的 ingress

示例用法

# 创建名为 'simple' 的单一 ingress,将 foo.com/bar 的请求转发到 svc1:8080,使用 TLS 密钥 "my-cert"
ac create ingress simple --rule="foo.com/bar=svc1:8080,tls=my-cert"

# 创建 catch-all ingress,路径为 "/path",指向服务 svc:port,Ingress Class 为 "otheringress"
ac create ingress catch-all --class=otheringress --rule="/path=svc:port"

# 创建带有两个注解 ingress.annotation1 和 ingress.annotation2 的 ingress
ac create ingress annotated --class=default --rule="foo.com/bar=svc:port" \
--annotation ingress.annotation1=foo \
--annotation ingress.annotation2=bla

# 创建同一主机多个路径的 ingress
ac create ingress multipath --class=default \
--rule="foo.com/=svc:port" \
--rule="foo.com/admin/=svcadmin:portadmin"

# 创建多个主机且路径类型为 Prefix 的 ingress
ac create ingress ingress1 --class=default \
--rule="foo.com/path*=svc:8080" \
--rule="bar.com/admin*=svc2:http"

# 创建启用 TLS 的 ingress,使用默认 ingress 证书和不同路径类型
ac create ingress ingtls --class=default \
--rule="foo.com/=svc:https,tls" \
--rule="foo.com/path/subpath*=othersvc:8080"

# 创建启用 TLS 的 ingress,使用指定密钥且路径类型为 Prefix
ac create ingress ingsecret --class=default \
--rule="foo.com/*=svc:8080,tls=secret1"

# 创建带默认后端的 ingress
ac create ingress ingdefault --class=default \
--default-backend=defaultsvc:http \
--rule="foo.com/*=svc:8080,tls=secret1"

ac create job

创建指定名称的作业

示例用法

# 创建作业
ac create job my-job --image=busybox

# 创建带命令的作业
ac create job my-job --image=busybox -- date

# 从名为 "a-cronjob" 的定时任务创建作业
ac create job test-job --from=cronjob/a-cronjob

ac create namespace

创建指定名称的命名空间

示例用法

# 创建名为 my-namespace 的新命名空间
ac create namespace my-namespace

ac create poddisruptionbudget

创建指定名称的 pod 中断预算

示例用法

# 创建名为 my-pdb 的 pod 中断预算,选择所有带有 app=rails 标签的 pod,要求任意时刻至少有一个可用
ac create poddisruptionbudget my-pdb --selector=app=rails --min-available=1

# 创建名为 my-pdb 的 pod 中断预算,选择所有带有 app=nginx 标签的 pod,要求任意时刻至少有一半可用
ac create pdb my-pdb --selector=app=nginx --min-available=50%

ac create priorityclass

创建指定名称的优先级类

示例用法

# 创建名为 high-priority 的优先级类
ac create priorityclass high-priority --value=1000 --description="high priority"

# 创建名为 default-priority 的优先级类,作为全局默认优先级
ac create priorityclass default-priority --value=1000 --global-default=true --description="default priority"

# 创建名为 high-priority 的优先级类,禁止抢占优先级较低的 pod
ac create priorityclass high-priority --value=1000 --description="high priority" --preemption-policy="Never"

ac create quota

创建指定名称的资源配额

示例用法

# 创建名为 my-quota 的新资源配额
ac create quota my-quota --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10

# 创建名为 best-effort 的新资源配额
ac create quota best-effort --hard=pods=100 --scopes=BestEffort

ac create role

创建单条规则的角色

示例用法

# 创建名为 "pod-reader" 的角色,允许用户对 pods 执行 "get"、"watch" 和 "list"
ac create role pod-reader --verb=get --verb=list --verb=watch --resource=pods

# 创建名为 "pod-reader" 的角色,指定 ResourceName
ac create role pod-reader --verb=get --resource=pods --resource-name=readablepod --resource-name=anotherpod

# 创建名为 "foo" 的角色,指定 API Group
ac create role foo --verb=get,list,watch --resource=rs.apps

# 创建名为 "foo" 的角色,指定 SubResource
ac create role foo --verb=get,list,watch --resource=pods,pods/status

ac create rolebinding

为特定角色或集群角色创建角色绑定

示例用法

# 使用 admin 集群角色为 user1、user2 和 group1 创建角色绑定
ac create rolebinding admin --clusterrole=admin --user=user1 --user=user2 --group=group1

# 使用 admin 角色为服务账户 monitoring:sa-dev 创建角色绑定
ac create rolebinding admin-binding --role=admin --serviceaccount=monitoring:sa-dev

ac create secret

使用指定子命令创建 secret

ac create secret docker-registry

为 Docker 注册表创建 secret

示例用法

# 如果没有 .dockercfg 文件,直接创建 dockercfg secret
ac create secret docker-registry my-secret --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL

# 从 ~/.docker/config.json 创建名为 my-secret 的新 secret
ac create secret docker-registry my-secret --from-file=path/to/.docker/config.json

ac create secret generic

从本地文件、目录或字面值创建 secret

示例用法

# 创建名为 my-secret 的新 secret,包含目录 bar 中每个文件的键
ac create secret generic my-secret --from-file=path/to/bar

# 创建名为 my-secret 的新 secret,指定键而非磁盘上的名称
ac create secret generic my-secret --from-file=ssh-privatekey=path/to/id_rsa --from-file=ssh-publickey=path/to/id_rsa.pub

# 创建名为 my-secret 的新 secret,包含 key1=supersecret 和 key2=topsecret
ac create secret generic my-secret --from-literal=key1=supersecret --from-literal=key2=topsecret

# 使用文件和字面值组合创建名为 my-secret 的新 secret
ac create secret generic my-secret --from-file=ssh-privatekey=path/to/id_rsa --from-literal=passphrase=topsecret

# 从 env 文件创建名为 my-secret 的新 secret
ac create secret generic my-secret --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env

ac create secret tls

创建 TLS secret

示例用法

# 创建名为 tls-secret 的新 TLS secret,使用指定的密钥对
ac create secret tls tls-secret --cert=path/to/tls.crt --key=path/to/tls.key

ac create service

使用指定子命令创建服务

ac create service clusterip

创建 ClusterIP 服务

示例用法

# 创建名为 my-cs 的新 ClusterIP 服务
ac create service clusterip my-cs --tcp=5678:8080

# 创建名为 my-cs 的新 ClusterIP 服务(无头模式)
ac create service clusterip my-cs --clusterip="None"

ac create service externalname

创建 ExternalName 服务

示例用法

# 创建名为 my-ns 的新 ExternalName 服务
ac create service externalname my-ns --external-name bar.com

ac create service loadbalancer

创建 LoadBalancer 服务

示例用法

# 创建名为 my-lbs 的新 LoadBalancer 服务
ac create service loadbalancer my-lbs --tcp=5678:8080

ac create service nodeport

创建 NodePort 服务

示例用法

# 创建名为 my-ns 的新 NodePort 服务
ac create service nodeport my-ns --tcp=5678:8080

ac create serviceaccount

创建指定名称的服务账户

示例用法

# 创建名为 my-service-account 的新服务账户
ac create serviceaccount my-service-account

ac create token

请求服务账户令牌

示例用法

# 请求当前命名空间中服务账户 "myapp" 的令牌,用于认证 kube-apiserver
ac create token myapp

# 请求自定义命名空间中服务账户的令牌
ac create token myapp --namespace myns

# 请求带自定义过期时间的令牌
ac create token myapp --duration 10m

# 请求带自定义受众的令牌
ac create token myapp --audience https://example.com

# 请求绑定到 Secret 对象实例的令牌
ac create token myapp --bound-object-kind Secret --bound-object-name mysecret

# 请求绑定到具有特定 UID 的 Secret 对象实例的令牌
ac create token myapp --bound-object-kind Secret --bound-object-name mysecret --bound-object-uid 0d4691ed-659b-4935-a832-355f77ee47cc

ac delete

通过文件名、标准输入、资源和名称,或资源和标签选择器删除资源

示例用法

# 使用 pod.json 中指定的类型和名称删除 pod
ac delete -f ./pod.json

# 从包含 kustomization.yaml 的目录删除资源 - 例如 dir/kustomization.yaml
ac delete -k dir

# 删除所有以 '.json' 结尾的文件中的资源
ac delete -f '*.json'

# 基于通过 stdin 传入的 JSON 删除 pod
cat pod.json | ac delete -f -

# 删除名称为 "baz" 和 "foo" 的 pods 和 services
ac delete pod,service baz foo

# 删除标签为 name=myLabel 的 pods 和 services
ac delete pods,services -l name=myLabel

# 立即删除 pod foo
ac delete pod foo --now

# 强制删除死节点上的 pod foo
ac delete pod foo --force

# 删除所有 pods
ac delete pods --all

# 仅在用户确认删除时删除所有 pods
ac delete pods --all --interactive

ac describe

显示特定资源或资源组的详细信息

示例用法

# 描述节点
ac describe nodes kubernetes-node-emt8.c.myproject.internal

# 描述 pod
ac describe pods/nginx

# 描述通过类型和名称在 "pod.json" 中标识的 pod
ac describe -f pod.json

# 描述所有 pods
ac describe pods

# 按标签 name=myLabel 描述 pods
ac describe pods -l name=myLabel

# 描述所有由 'frontend' replication controller 管理的 pods
# (rc 创建的 pods 名称以 rc 名称为前缀)
ac describe pods frontend

ac diff

比较 live 版本与将要应用的版本差异

示例用法

# 比较 pod.json 中的资源差异
ac diff -f pod.json

# 比较从 stdin 读取的文件差异
cat service.yaml | ac diff -f -

ac edit

编辑服务器上的资源

示例用法

# 编辑名为 'registry' 的服务
ac edit svc/registry

# 使用替代编辑器
KUBE_EDITOR="nano" ac edit svc/registry

# 以 JSON 格式使用 v1 API 编辑名为 'myjob' 的 job
ac edit job.v1.batch/myjob -o json

# 以 YAML 格式编辑名为 'mydeployment' 的部署,并将修改后的配置保存到注解
ac edit deployment/mydeployment -o yaml --save-config

# 编辑名为 'mydeployment' 的部署的 'status' 子资源
ac edit deployment mydeployment --subresource='status'

ac events

列出事件

示例用法

# 列出默认命名空间中的最近事件
ac events

# 列出所有命名空间中的最近事件
ac events --all-namespaces

# 列出指定 pod 的最近事件,然后等待更多事件并实时列出
ac events --for pod/web-pod-13je7 --watch

# 以 YAML 格式列出最近事件
ac events -oyaml

# 仅列出类型为 'Warning' 或 'Normal' 的最近事件
ac events --types=Warning,Normal

ac exec

在容器中执行命令

示例用法

# 获取 pod mypod 中运行的 'date' 命令的输出,默认使用第一个容器
ac exec mypod -- date

# 获取 pod mypod 中 ruby-container 运行的 'date' 命令的输出
ac exec mypod -c ruby-container -- date

# 切换到原始终端模式;将 stdin 发送到 pod mypod 中 ruby-container 的 'bash',并将 'bash' 的 stdout/stderr 返回给客户端
ac exec mypod -c ruby-container -i -t -- bash -il

# 列出 pod mypod 第一个容器中 /usr 目录内容并按修改时间排序
# 如果要执行的命令与 ac exec 的参数有冲突(如 -i),必须使用两个破折号 (--) 分隔命令参数
# 注意,不要用引号包裹命令及其参数,除非你通常这样执行(例如,使用 ls -t /usr,而非 "ls -t /usr")
ac exec mypod -i -t -- ls -t /usr

# 获取部署 mydeployment 第一个 pod 中运行的 'date' 命令的输出,默认使用第一个容器
ac exec deploy/mydeployment -- date

# 获取服务 myservice 第一个 pod 中运行的 'date' 命令的输出,默认使用第一个容器
ac exec svc/myservice -- date

ac explain

获取资源文档

示例用法

# 获取资源及其字段的文档
ac explain pods

# 获取资源中的所有字段
ac explain pods --recursive

# 获取支持的 api 版本中 deployment 的说明
ac explain deployments --api-version=apps/v1

# 获取资源特定字段的文档
ac explain pods.spec.containers

# 以不同格式获取资源文档
ac explain deployment --output=plaintext-openapiv2

ac expose

将 replication controller、service、deployment 或 pod 公开为新的 Kubernetes 服务

示例用法

# 为复制的 nginx 创建服务,服务端口为 80,连接容器端口为 8000
ac expose rc nginx --port=80 --target-port=8000

# 为通过类型和名称在 "nginx-controller.yaml" 中指定的 replication controller 创建服务,服务端口为 80,连接容器端口为 8000
ac expose -f nginx-controller.yaml --port=80 --target-port=8000

# 为 pod valid-pod 创建服务,服务端口为 444,名称为 "frontend"
ac expose pod valid-pod --port=444 --name=frontend

# 基于上述服务创建第二个服务,将容器端口 8443 公开为端口 443,名称为 "nginx-https"
ac expose service nginx --port=443 --target-port=8443 --name=nginx-https

# 为复制的流媒体应用创建服务,端口为 4100,负载均衡 UDP 流量,名称为 'video-stream'
ac expose rc streamer --port=4100 --protocol=UDP --name=video-stream

# 为复制的 nginx 使用 replica set 创建服务,服务端口为 80,连接容器端口为 8000
ac expose rs nginx --port=80 --target-port=8000

# 为 nginx 部署创建服务,服务端口为 80,连接容器端口为 8000
ac expose deployment nginx --port=80 --target-port=8000

ac get

显示一个或多个资源

示例用法

# 以 ps 输出格式列出所有 pods
ac get pods

# 以 ps 输出格式列出所有 pods,显示更多信息(如节点名称)
ac get pods -o wide

# 以 ps 输出格式列出指定名称的单个 replication controller
ac get replicationcontroller web

# 以 JSON 输出格式列出 "apps" API 组 "v1" 版本的 deployments
ac get deployments.v1.apps -o json

# 以 JSON 输出格式列出单个 pod
ac get -o json pod web-pod-13je7

# 以 JSON 输出格式列出通过类型和名称在 "pod.yaml" 中指定的 pod
ac get -f pod.yaml -o json

# 从包含 kustomization.yaml 的目录获取资源 - 例如 dir/kustomization.yaml
ac get -k dir/

# 仅返回指定 pod 的 phase 值
ac get -o template pod/web-pod-13je7 --template={{.status.phase}}

# 以自定义列格式列出资源信息
ac get pod test-pod -o custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image

# 以 ps 输出格式同时列出所有 replication controllers 和 services
ac get rc,services

# 按类型和名称列出一个或多个资源
ac get rc/web service/frontend pods/web-pod-13je7

# 列出单个 pod 的 'status' 子资源
ac get pod web-pod-13je7 --subresource status

# 列出命名空间 'backend' 中的所有 deployments
ac get deployments.apps --namespace backend

# 列出所有命名空间中的所有 pods
ac get pods --all-namespaces

ac kustomize

从目录或 URL 构建 kustomization 目标

示例用法

# 构建当前工作目录
ac kustomize

# 构建共享配置目录
ac kustomize /home/config/production

# 从 github 构建
ac kustomize https://github.com/kubernetes-sigs/kustomize.git/examples/helloWorld?ref=v1.0.6

ac label

更新资源的标签

示例用法

# 更新 pod 'foo' 的标签 'unhealthy',值为 'true'
ac label pods foo unhealthy=true

# 更新 pod 'foo' 的标签 'status',值为 'unhealthy',覆盖任何已有值
ac label --overwrite pods foo status=unhealthy

# 更新命名空间中的所有 pod
ac label pods --all status=unhealthy

# 更新通过类型和名称在 "pod.json" 中标识的 pod
ac label -f pod.json status=unhealthy

# 仅当资源版本为 1 时更新 pod 'foo'
ac label pods foo status=unhealthy --resource-version=1

# 删除 pod 'foo' 中名为 'bar' 的标签(如果存在)
# 不需要 --overwrite 标志
ac label pods foo bar-

ac login

登录 ACP 平台

示例用法

# 交互式登录(提示缺失参数)
ac login https://example.com --name prod

# 通过标志传入所有参数登录
ac login https://example.com --name prod --username=myuser --password=mypassword

# 使用环境变量登录(适合自动化)
AC_LOGIN_PLATFORM_URL=https://example.com AC_LOGIN_SESSION=prod \
AC_LOGIN_USERNAME=myuser AC_LOGIN_PASSWORD=mypassword ac login

# 使用特定身份提供者登录
ac login https://example.com --name prod --idp ldap-test

# 登录并设置特定集群和命名空间
ac login https://example.com --name prod --cluster=my-cluster --namespace=my-namespace

# 使用自定义 kubeconfig 文件登录
ac login https://example.com --name prod --kubeconfig=/path/to/kubeconfig

ac logout

结束当前 ACP 平台会话

示例用法

# 登出当前 ACP 平台会话
ac logout

# 登出指定会话
ac logout --session prod

# 登出所有会话
ac logout --all

ac logs

打印 pod 中容器的日志

示例用法

# 返回只有一个容器的 pod nginx 的快照日志
ac logs nginx

# 返回 pod nginx 的快照日志,每行前缀为源 pod 和容器名称
ac logs nginx --prefix

# 返回 pod nginx 的快照日志,限制输出为 500 字节
ac logs nginx --limit-bytes=500

# 返回 pod nginx 的快照日志,等待最多 20 秒直到 pod 运行
ac logs nginx --pod-running-timeout=20s

# 返回多容器 pod nginx 的快照日志
ac logs nginx --all-containers=true

# 返回部署 nginx 中所有 pod 的快照日志
ac logs deployment/nginx --all-pods=true

# 返回标签 app=nginx 定义的所有 pods 中所有容器的快照日志
ac logs -l app=nginx --all-containers=true

# 返回标签 app=nginx 定义的所有 pods 的快照日志,限制并发日志请求为 10 个 pod
ac logs -l app=nginx --max-log-requests=10

# 返回 pod web-1 中已终止的 ruby 容器的前一个快照日志
ac logs -p -c ruby web-1

# 开始流式输出 pod nginx 的日志,即使发生错误也继续
ac logs nginx -f --ignore-errors=true

# 开始流式输出 pod web-1 中 ruby 容器的日志
ac logs -f -c ruby web-1

# 开始流式输出标签 app=nginx 定义的所有 pods 中所有容器的日志
ac logs -f -l app=nginx --all-containers=true

# 仅显示 pod nginx 中最近 20 行日志
ac logs --tail=20 nginx

# 显示 pod nginx 中过去一小时内的所有日志
ac logs --since=1h nginx

# 显示 pod nginx 中从 2024 年 8 月 30 日 06:00:00 UTC 开始的所有带时间戳日志
ac logs nginx --since-time=2024-08-30T06:00:00Z --timestamps=true

# 显示 kubelet 的日志,跳过过期的服务证书验证
ac logs --insecure-skip-tls-verify-backend nginx

# 返回名为 hello 的 job 的第一个容器的快照日志
ac logs job/hello

# 返回名为 nginx 的部署中容器 nginx-1 的快照日志
ac logs deployment/nginx -c nginx-1

ac namespace

显示或切换当前命名空间上下文

示例用法

# 显示当前命名空间和上下文信息
ac namespace

# 切换到不同的命名空间
ac namespace my-namespace

# 切换到默认命名空间
ac namespace default

ac patch

更新资源字段

示例用法

# 使用战略合并补丁部分更新节点,补丁为 JSON 格式
ac patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'

# 使用战略合并补丁部分更新节点,补丁为 YAML 格式
ac patch node k8s-node-1 -p $'spec:\n unschedulable: true'

# 使用战略合并补丁部分更新通过类型和名称在 "node.json" 中指定的节点
ac patch -f node.json -p '{"spec":{"unschedulable":true}}'

# 更新容器镜像;spec.containers[*].name 是合并键
ac patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'

# 使用带位置数组的 JSON 补丁更新容器镜像
ac patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'

# 通过 'scale' 子资源使用合并补丁更新部署副本数
ac patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'

ac plugin

提供与插件交互的工具

示例用法

# 列出所有可用插件
ac plugin list

# 仅列出可用插件的二进制名称,不含路径
ac plugin list --name-only

ac plugin list

列出用户 PATH 中所有可见的插件可执行文件

示例用法

# 列出所有可用插件
ac plugin list

# 仅列出可用插件的二进制名称,不含路径
ac plugin list --name-only

ac port-forward

将一个或多个本地端口转发到 pod

示例用法

# 本地监听端口 5000 和 6000,转发数据到 pod 中的 5000 和 6000 端口
ac port-forward pod/mypod 5000 6000

# 本地监听端口 5000 和 6000,转发数据到由部署选择的 pod 中的 5000 和 6000 端口
ac port-forward deployment/mydeployment 5000 6000

# 本地监听端口 8443,转发到由服务选择的 pod 中名为 "https" 的服务端口的 targetPort
ac port-forward service/myservice 8443:https

# 本地监听端口 8888,转发到 pod 中的 5000 端口
ac port-forward pod/mypod 8888:5000

# 本地监听所有地址的 8888 端口,转发到 pod 中的 5000 端口
ac port-forward --address 0.0.0.0 pod/mypod 8888:5000

# 本地监听 localhost 和指定 IP 的 8888 端口,转发到 pod 中的 5000 端口
ac port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000

# 本地监听随机端口,转发到 pod 中的 5000 端口
ac port-forward pod/mypod :5000

ac process

将模板处理为资源列表

示例用法

# 将 template.json 文件转换为资源列表并传递给 create
ac process -f template.json | ac apply -f -

# 本地处理文件而非联系服务器
ac process -f template.json -o yaml

# 处理模板时传递用户定义的标签
ac process -f template.json -l name=mytemplate

# 将存储的模板转换为资源列表
ac process foo

# 通过设置/覆盖参数值将存储的模板转换为资源列表
ac process foo -p PARM1=VALUE1 -p PARM2=VALUE2

# 将存储在不同命名空间的模板转换为资源列表
ac process cpaas-system//foo

# 将 template.json 转换为资源列表
cat template.json | ac process -f -

ac proxy

运行 Kubernetes API 服务器代理

示例用法

# 代理整个 Kubernetes API,无其他内容
ac proxy --api-prefix=/

# 代理部分 Kubernetes API 及一些静态文件
# 可通过 'curl localhost:8001/api/v1/pods' 获取 pods 信息
ac proxy --www=/my/files --www-prefix=/static/ --api-prefix=/api/

# 以不同根路径代理整个 Kubernetes API
# 可通过 'curl localhost:8001/custom/api/v1/pods' 获取 pods 信息
ac proxy --api-prefix=/custom/

# 在端口 8011 运行 Kubernetes API 服务器代理,静态内容来自 ./local/www/
ac proxy --port=8011 --www=./local/www/

# 在任意本地端口运行 Kubernetes API 服务器代理
# 服务器选择的端口将输出到标准输出
ac proxy --port=0

# 运行 Kubernetes API 服务器代理,修改 API 前缀为 k8s-api
# 例如 pods API 可通过 localhost:8001/k8s-api/v1/pods/ 访问
ac proxy --api-prefix=/k8s-api

ac replace

通过文件名或标准输入替换资源

示例用法

# 使用 pod.json 中的数据替换 pod
ac replace -f ./pod.json

# 基于通过 stdin 传入的 JSON 替换 pod
cat pod.json | ac replace -f -

# 将单容器 pod 的镜像版本(标签)更新为 v4
ac get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | ac replace -f -

# 强制替换,先删除再重新创建资源
ac replace --force -f ./pod.json

ac rollout

管理资源的滚动更新

示例用法

# 回滚到上一个部署版本
ac rollout undo deployment/abc

# 检查 daemonset 的滚动状态
ac rollout status daemonset/foo

# 重启部署
ac rollout restart deployment/abc

# 重启带有标签 'app=nginx' 的部署
ac rollout restart deployment --selector=app=nginx

ac rollout history

查看滚动更新历史

示例用法

# 查看部署的滚动历史
ac rollout history deployment/abc

# 查看 daemonset 第 3 版的详细信息
ac rollout history daemonset/abc --revision=3

ac rollout pause

将指定资源标记为暂停

示例用法

# 将 nginx 部署标记为暂停
# 部署的当前状态将继续运行;只要部署处于暂停状态,新更新将不会生效
ac rollout pause deployment/nginx

ac rollout restart

重启资源

示例用法

# 重启 test-namespace 命名空间中的所有部署
ac rollout restart deployment -n test-namespace

# 重启部署
ac rollout restart deployment/nginx

# 重启 daemonset
ac rollout restart daemonset/abc

# 重启带有标签 app=nginx 的部署
ac rollout restart deployment --selector=app=nginx

ac rollout resume

恢复已暂停的资源

示例用法

# 恢复已暂停的部署
ac rollout resume deployment/nginx

ac rollout status

显示滚动更新状态

示例用法

# 监视部署的滚动状态
ac rollout status deployment/nginx

ac rollout undo

撤销之前的滚动更新

示例用法

# 回滚到上一个部署版本
ac rollout undo deployment/abc

# 回滚到 daemonset 第 3 版
ac rollout undo daemonset/abc --to-revision=3

# 以 dry-run 模式回滚到上一个部署版本
ac rollout undo --dry-run=server deployment/abc

ac run

在集群上运行特定镜像

示例用法

# 启动 nginx pod
ac run nginx --image=nginx

# 启动 hazelcast pod,并让容器暴露端口 5701
ac run hazelcast --image=hazelcast/hazelcast --port=5701

# 启动 hazelcast pod,在容器中设置环境变量 "DNS_DOMAIN=cluster" 和 "POD_NAMESPACE=default"
ac run hazelcast --image=hazelcast/hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"

# 启动 hazelcast pod,在容器中设置标签 "app=hazelcast" 和 "env=prod"
ac run hazelcast --image=hazelcast/hazelcast --labels="app=hazelcast,env=prod"

# dry run;打印对应的 API 对象但不创建
ac run nginx --image=nginx --dry-run=client

# 启动 nginx pod,但用部分 JSON 解析的值覆盖 spec
ac run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'

# 启动 busybox pod,保持前台运行,退出时不重启
ac run -i -t busybox --image=busybox --restart=Never

# 启动 nginx pod,使用默认命令,但为该命令使用自定义参数 (arg1 .. argN)
ac run nginx --image=nginx -- <arg1> <arg2> ... <argN>

# 启动 nginx pod,使用不同命令和自定义参数
ac run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>

ac scale

为部署、ReplicaSet 或 ReplicationController 设置新规模

示例用法

# 将名为 'foo' 的 replica set 扩缩到 3
ac scale --replicas=3 rs/foo

# 将通过类型和名称在 "foo.yaml" 中指定的资源扩缩到 3
ac scale --replicas=3 -f foo.yaml

# 如果名为 mysql 的部署当前规模为 2,则扩缩为 3
ac scale --current-replicas=2 --replicas=3 deployment/mysql

# 扩缩多个 replication controllers
ac scale --replicas=5 rc/example1 rc/example2 rc/example3

# 将名为 'web' 的 stateful set 扩缩到 3
ac scale --replicas=3 statefulset/web

ac set

设置对象的特定功能

ac set env

更新 pod 模板中的环境变量

示例用法

# 更新部署 'registry',添加新环境变量
ac set env deployment/registry STORAGE_DIR=/local

# 列出部署 'sample-build' 中定义的环境变量
ac set env deployment/sample-build --list

# 列出所有 pods 中定义的环境变量
ac set env pods --all --list

# 输出修改后的部署 YAML,不修改服务器上的对象
ac set env deployment/sample-build STORAGE_DIR=/data -o yaml

# 更新项目中所有 replication controllers 的所有容器,设置 ENV=prod
ac set env rc --all ENV=prod

# 从 secret 导入环境变量
ac set env --from=secret/mysecret deployment/myapp

# 从 config map 导入环境变量并添加前缀
ac set env --from=configmap/myconfigmap --prefix=MYSQL_ deployment/myapp

# 从 config map 导入特定键
ac set env --keys=my-example-key --from=configmap/myconfigmap deployment/myapp

# 从所有部署配置中移除容器 'c1' 的环境变量 ENV
ac set env deployments --all --containers="c1" ENV-

# 从磁盘上的部署定义中移除环境变量 ENV,并更新服务器上的部署配置
ac set env -f deploy.json ENV-

# 将本地 shell 环境中的部分变量导入部署配置
env | grep RAILS_ | ac set env -e - deployment/registry

ac set image

更新 pod 模板的镜像

示例用法

# 设置部署中 nginx 容器的镜像为 'nginx:1.9.1',busybox 容器的镜像为 'busybox'
ac set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

# 更新所有部署和 rc 中 nginx 容器的镜像为 'nginx:1.9.1'
ac set image deployments,rc nginx=nginx:1.9.1 --all

# 更新 daemonset abc 中所有容器的镜像为 'nginx:1.9.1'
ac set image daemonset abc *=nginx:1.9.1

# 从本地文件打印更新 nginx 容器镜像的结果(yaml 格式),不访问服务器
ac set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

ac set resources

更新带 pod 模板对象的资源请求/限制

示例用法

# 设置部署中 nginx 容器的 cpu 限制为 "200m",内存限制为 "512Mi"
ac set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi

# 设置 nginx 中所有容器的资源请求和限制
ac set resources deployment nginx --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi

# 移除 nginx 中容器的资源请求
ac set resources deployment nginx --limits=cpu=0,memory=0 --requests=cpu=0,memory=0

# 从本地文件打印更新 nginx 容器限制的结果(yaml 格式),不访问服务器
ac set resources -f path/to/file.yaml --limits=cpu=200m,memory=512Mi --local -o yaml

ac set selector

设置资源的选择器

示例用法

# 在创建部署/服务对之前设置标签和选择器
ac create service clusterip my-svc --clusterip="None" -o yaml --dry-run=client | ac set selector --local -f - 'environment=qa' -o yaml | ac create -f -
ac create deployment my-dep -o yaml --dry-run=client | ac label --local -f - environment=qa -o yaml | ac create -f -

ac set serviceaccount

更新资源的服务账户

示例用法

# 将部署 nginx-deployment 的服务账户设置为 serviceaccount1
ac set serviceaccount deployment nginx-deployment serviceaccount1

# 从本地文件打印更新服务账户的结果(YAML 格式),不访问 API 服务器
ac set sa -f nginx-deployment.yaml serviceaccount1 --local --dry-run=client -o yaml

ac set subject

更新角色绑定或集群角色绑定中的用户、组或服务账户

示例用法

# 更新 clusterrolebinding 中的服务账户 serviceaccount1
ac set subject clusterrolebinding admin --serviceaccount=namespace:serviceaccount1

# 更新 rolebinding 中的用户 user1、user2 和组 group1
ac set subject rolebinding admin --user=user1 --user=user2 --group=group1

# 从本地打印更新 rolebinding 主体的结果(YAML 格式),不访问服务器
ac create rolebinding admin --role=admin --user=admin -o yaml --dry-run=client | ac set subject --local -f - --user=foo -o yaml

ac version

打印客户端和服务器版本信息

示例用法

# 打印客户端和服务器版本信息
ac version

# 仅打印客户端版本
ac version --client

# 以 JSON 格式打印版本信息
ac version -o json

ac wait

实验性功能:等待一个或多个资源满足特定条件

示例用法

# 等待 pod "busybox1" 包含类型为 "Ready" 的状态条件
ac wait --for=condition=Ready pod/busybox1

# 状态条件的默认值为 true;可等待其他目标,等号后比较时忽略大小写
ac wait --for=condition=Ready=false pod/busybox1

# 等待 pod "busybox1" 的状态 phase 为 "Running"
ac wait --for=jsonpath='{.status.phase}'=Running pod/busybox1

# 等待 pod "busybox1" 的 Ready 条件为 True
ac wait --for='jsonpath={.status.conditions[?(@.type=="Ready")].status}=True' pod/busybox1

# 等待服务 "loadbalancer" 拥有 ingress
ac wait --for=jsonpath='{.status.loadBalancer.ingress}' service/loadbalancer

# 创建 secret "busybox1",等待其创建,超时 30 秒
ac create secret generic busybox1
ac wait --for=create secret/busybox1 --timeout=30s

# 删除 pod "busybox1",等待其删除,超时 60 秒
ac delete pod/busybox1
ac wait --for=delete pod/busybox1 --timeout=60s