第 6 章 OpenShift CLI 命令参考
OpenShift CLI (oc
) 命令的描述和示例命令包括在此参考文档中。您必须具有 cluster-admin
或同等权限才能使用这些命令。要列出管理员命令及其信息,请使用以下命令:
输入
oc adm -h
命令来列出所有管理员命令:命令语法
oc adm -h
$ oc adm -h
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 输入
oc <command> --help
命令以获取特定命令的更多详情:命令语法
oc <command> --help
$ oc <command> --help
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
使用 oc <command> --help
列出所有 oc
命令的详情。并非所有 oc
命令都适用于使用 MicroShift 的红帽构建。
6.1. OpenShift CLI(oc)开发人员命令 复制链接链接已复制到粘贴板!
6.1.1. oc annotate 复制链接链接已复制到粘贴板!
更新资源上的注解
用法示例
6.1.2. oc api-resources 复制链接链接已复制到粘贴板!
在服务器上显示支持的 API 资源
用法示例
6.1.3. oc api-versions 复制链接链接已复制到粘贴板!
以"group/version"的形式输出服务器上支持的 API 版本。
用法示例
Print the supported API versions
# Print the supported API versions
oc api-versions
6.1.4. oc apply 复制链接链接已复制到粘贴板!
通过文件名或 stdin 将配置应用到资源
用法示例
6.1.5. oc apply edit-last-applied 复制链接链接已复制到粘贴板!
编辑资源/对象的最新 last-applied-configuration 注解
用法示例
Edit the last-applied-configuration annotations by type/name in YAML
# Edit the last-applied-configuration annotations by type/name in YAML
oc apply edit-last-applied deployment/nginx
# Edit the last-applied-configuration annotations by file in JSON
oc apply edit-last-applied -f deploy.yaml -o json
6.1.6. oc apply set-last-applied 复制链接链接已复制到粘贴板!
设置 live 对象上的 last-applied-configuration 注释,以匹配文件的内容。
用法示例
6.1.7. oc apply view-last-applied 复制链接链接已复制到粘贴板!
查看资源/对象最新的最后应用配置注解
用法示例
View the last-applied-configuration annotations by type/name in YAML
# View the last-applied-configuration annotations by type/name in YAML
oc apply view-last-applied deployment/nginx
# View the last-applied-configuration annotations by file in JSON
oc apply view-last-applied -f deploy.yaml -o json
6.1.8. oc attach 复制链接链接已复制到粘贴板!
附加到正在运行的容器
用法示例
6.1.9. oc auth can-i 复制链接链接已复制到粘贴板!
检查是否允许操作
用法示例
6.1.10. oc auth reconcile 复制链接链接已复制到粘贴板!
协调 RBAC 角色、角色绑定、集群角色和集群角色绑定对象的规则
用法示例
Reconcile RBAC resources from a file
# Reconcile RBAC resources from a file
oc auth reconcile -f my-rbac-rules.yaml
6.1.11. oc auth whoami 复制链接链接已复制到粘贴板!
实验性:检查自我主题属性
用法示例
Get your subject attributes.
# Get your subject attributes.
oc auth whoami
# Get your subject attributes in JSON format.
oc auth whoami -o json
6.1.12. oc cluster-info 复制链接链接已复制到粘贴板!
显示集群信息
用法示例
Print the address of the control plane and cluster services
# Print the address of the control plane and cluster services
oc cluster-info
6.1.13. oc cluster-info dump 复制链接链接已复制到粘贴板!
转储用于调试和诊断的相关信息
用法示例
6.1.14. oc completion 复制链接链接已复制到粘贴板!
输出指定 shell 的 shell 完成代码 (bash、zsh、fish 或 powershell)
用法示例
6.1.15. oc config current-context 复制链接链接已复制到粘贴板!
显示 current-context
用法示例
Display the current-context
# Display the current-context
oc config current-context
6.1.16. oc config delete-cluster 复制链接链接已复制到粘贴板!
从 kubeconfig 删除指定的集群
用法示例
Delete the minikube cluster
# Delete the minikube cluster
oc config delete-cluster minikube
6.1.17. oc config delete-context 复制链接链接已复制到粘贴板!
从 kubeconfig 删除指定的上下文
用法示例
Delete the context for the minikube cluster
# Delete the context for the minikube cluster
oc config delete-context minikube
6.1.18. oc config delete-user 复制链接链接已复制到粘贴板!
从 kubeconfig 删除指定用户
用法示例
Delete the minikube user
# Delete the minikube user
oc config delete-user minikube
6.1.19. oc config get-clusters 复制链接链接已复制到粘贴板!
显示 kubeconfig 中定义的集群
用法示例
List the clusters that oc knows about
# List the clusters that oc knows about
oc config get-clusters
6.1.20. oc config get-contexts 复制链接链接已复制到粘贴板!
描述一个或多个上下文
用法示例
List all the contexts in your kubeconfig file
# List all the contexts in your kubeconfig file
oc config get-contexts
# Describe one context in your kubeconfig file
oc config get-contexts my-context
6.1.21. oc config get-users 复制链接链接已复制到粘贴板!
显示 kubeconfig 中定义的用户
用法示例
List the users that oc knows about
# List the users that oc knows about
oc config get-users
6.1.22. oc config new-admin-kubeconfig 复制链接链接已复制到粘贴板!
生成,使服务器信任并显示新的 admin.kubeconfig。
用法示例
Generate a new admin kubeconfig
# Generate a new admin kubeconfig
oc config new-admin-kubeconfig
6.1.23. oc config new-kubelet-bootstrap-kubeconfig 复制链接链接已复制到粘贴板!
生成,使服务器信任并显示新的 kubelet /etc/kubernetes/kubeconfig。
用法示例
Generate a new kubelet bootstrap kubeconfig
# Generate a new kubelet bootstrap kubeconfig
oc config new-kubelet-bootstrap-kubeconfig
6.1.24. oc config refresh-ca-bundle 复制链接链接已复制到粘贴板!
通过联系 apiserver 来更新 OpenShift CA 捆绑包。
用法示例
6.1.25. oc config rename-context 复制链接链接已复制到粘贴板!
从 kubeconfig 文件中重命名上下文
用法示例
Rename the context 'old-name' to 'new-name' in your kubeconfig file
# Rename the context 'old-name' to 'new-name' in your kubeconfig file
oc config rename-context old-name new-name
6.1.26. oc config set 复制链接链接已复制到粘贴板!
在 kubeconfig 文件中设置单个值
用法示例
6.1.27. oc config set-cluster 复制链接链接已复制到粘贴板!
在 kubeconfig 中设置集群条目
用法示例
6.1.28. oc config set-context 复制链接链接已复制到粘贴板!
在 kubeconfig 中设置上下文条目
用法示例
Set the user field on the gce context entry without touching other values
# Set the user field on the gce context entry without touching other values
oc config set-context gce --user=cluster-admin
6.1.29. oc config set-credentials 复制链接链接已复制到粘贴板!
在 kubeconfig 中设置用户条目
用法示例
6.1.30. oc config unset 复制链接链接已复制到粘贴板!
在 kubeconfig 文件中取消设置单个值
用法示例
Unset the current-context
# Unset the current-context
oc config unset current-context
# Unset namespace in foo context
oc config unset contexts.foo.namespace
6.1.31. oc config use-context 复制链接链接已复制到粘贴板!
在 kubeconfig 文件中设置 current-context
用法示例
Use the context for the minikube cluster
# Use the context for the minikube cluster
oc config use-context minikube
6.1.32. oc config view 复制链接链接已复制到粘贴板!
显示合并的 kubeconfig 设置或指定的 kubeconfig 文件
用法示例
6.1.33. oc cp 复制链接链接已复制到粘贴板!
将文件和目录复制到容器或从容器中复制
用法示例
6.1.34. oc create 复制链接链接已复制到粘贴板!
从文件或 stdin 创建资源
用法示例
6.1.35. oc create clusterrole 复制链接链接已复制到粘贴板!
创建集群角色
用法示例
6.1.36. oc create clusterrolebinding 复制链接链接已复制到粘贴板!
为特定集群角色创建集群角色绑定
用法示例
Create a cluster role binding for user1, user2, and group1 using the cluster-admin cluster role
# Create a cluster role binding for user1, user2, and group1 using the cluster-admin cluster role
oc create clusterrolebinding cluster-admin --clusterrole=cluster-admin --user=user1 --user=user2 --group=group1
6.1.37. oc create configmap 复制链接链接已复制到粘贴板!
从本地文件、目录或字面值创建配置映射
用法示例
6.1.38. oc create cronjob 复制链接链接已复制到粘贴板!
使用指定名称创建 cron 作业
用法示例
Create a cron job
# Create a cron job
oc create cronjob my-job --image=busybox --schedule="*/1 * * * *"
# Create a cron job with a command
oc create cronjob my-job --image=busybox --schedule="*/1 * * * *" -- date
6.1.39. oc create deployment 复制链接链接已复制到粘贴板!
使用指定名称创建部署
用法示例
6.1.40. oc create ingress 复制链接链接已复制到粘贴板!
使用指定名称创建入口
用法示例
6.1.41. oc create job 复制链接链接已复制到粘贴板!
使用指定名称创建作业
用法示例
6.1.42. oc create namespace 复制链接链接已复制到粘贴板!
使用指定名称创建命名空间
用法示例
Create a new namespace named my-namespace
# Create a new namespace named my-namespace
oc create namespace my-namespace
6.1.43. oc create poddisruptionBudget 复制链接链接已复制到粘贴板!
使用指定名称创建 pod 中断预算
用法示例
6.1.44. oc create priorityclass 复制链接链接已复制到粘贴板!
创建具有指定名称的优先级类
用法示例
6.1.45. oc create quota 复制链接链接已复制到粘贴板!
使用指定名称创建配额
用法示例
Create a new resource quota named my-quota
# Create a new resource quota named my-quota
oc create quota my-quota --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10
# Create a new resource quota named best-effort
oc create quota best-effort --hard=pods=100 --scopes=BestEffort
6.1.46. oc create role 复制链接链接已复制到粘贴板!
创建具有单一规则的角色
用法示例
6.1.47. oc create rolebinding 复制链接链接已复制到粘贴板!
为特定角色或集群角色创建角色绑定
用法示例
Create a role binding for user1, user2, and group1 using the admin cluster role
# Create a role binding for user1, user2, and group1 using the admin cluster role
oc create rolebinding admin --clusterrole=admin --user=user1 --user=user2 --group=group1
# Create a role binding for serviceaccount monitoring:sa-dev using the admin role
oc create rolebinding admin-binding --role=admin --serviceaccount=monitoring:sa-dev
6.1.48. oc create route edge 复制链接链接已复制到粘贴板!
创建使用边缘 TLS 终止的路由
用法示例
6.1.49. oc create route passthrough 复制链接链接已复制到粘贴板!
创建使用 passthrough TLS 终止的路由
用法示例
6.1.50. oc create route reencrypt 复制链接链接已复制到粘贴板!
创建使用重新加密 TLS 终止的路由
用法示例
6.1.51. oc create secret docker-registry 复制链接链接已复制到粘贴板!
创建用于 Docker registry 的 secret
用法示例
If you don't already have a .dockercfg file, you can create a dockercfg secret directly by using:
# If you don't already have a .dockercfg file, you can create a dockercfg secret directly by using:
oc create secret docker-registry my-secret --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
# Create a new secret named my-secret from ~/.docker/config.json
oc create secret docker-registry my-secret --from-file=.dockerconfigjson=path/to/.docker/config.json
6.1.52. oc create secret generic 复制链接链接已复制到粘贴板!
从本地文件、目录或字面值创建 secret
用法示例
6.1.53. oc create secret tls 复制链接链接已复制到粘贴板!
创建 TLS secret
用法示例
Create a new TLS secret named tls-secret with the given key pair
# Create a new TLS secret named tls-secret with the given key pair
oc create secret tls tls-secret --cert=path/to/tls.cert --key=path/to/tls.key
6.1.54. oc create service clusterip 复制链接链接已复制到粘贴板!
创建 ClusterIP 服务
用法示例
Create a new ClusterIP service named my-cs
# Create a new ClusterIP service named my-cs
oc create service clusterip my-cs --tcp=5678:8080
# Create a new ClusterIP service named my-cs (in headless mode)
oc create service clusterip my-cs --clusterip="None"
6.1.55. oc create service externalname 复制链接链接已复制到粘贴板!
创建 ExternalName 服务
用法示例
Create a new ExternalName service named my-ns
# Create a new ExternalName service named my-ns
oc create service externalname my-ns --external-name bar.com
6.1.56. oc create service loadbalancer 复制链接链接已复制到粘贴板!
创建 LoadBalancer 服务
用法示例
Create a new LoadBalancer service named my-lbs
# Create a new LoadBalancer service named my-lbs
oc create service loadbalancer my-lbs --tcp=5678:8080
6.1.57. oc create service nodeport 复制链接链接已复制到粘贴板!
创建 NodePort 服务
用法示例
Create a new NodePort service named my-ns
# Create a new NodePort service named my-ns
oc create service nodeport my-ns --tcp=5678:8080
6.1.58. oc create serviceaccount 复制链接链接已复制到粘贴板!
使用指定名称创建服务帐户
用法示例
Create a new service account named my-service-account
# Create a new service account named my-service-account
oc create serviceaccount my-service-account
6.1.59. oc create token 复制链接链接已复制到粘贴板!
请求服务帐户令牌
用法示例
6.1.60. oc debug 复制链接链接已复制到粘贴板!
启动用于调试的 pod 的新实例
用法示例
6.1.61. oc delete 复制链接链接已复制到粘贴板!
通过文件名、stdin、资源和名称或者资源和标签选择器删除资源
用法示例
6.1.62. oc describe 复制链接链接已复制到粘贴板!
显示特定资源或一组资源的详情
用法示例
6.1.63. oc diff 复制链接链接已复制到粘贴板!
针对 would-be 应用的版本对 live 版本进行 diff 操作
用法示例
Diff resources included in pod.json
# Diff resources included in pod.json
oc diff -f pod.json
# Diff file read from stdin
cat service.yaml | oc diff -f -
6.1.64. oc edit 复制链接链接已复制到粘贴板!
编辑服务器上的资源
用法示例
6.1.65. oc events 复制链接链接已复制到粘贴板!
列出事件
用法示例
6.1.66. oc exec 复制链接链接已复制到粘贴板!
在容器中执行命令
用法示例
6.1.67. oc explain 复制链接链接已复制到粘贴板!
获取资源的文档
用法示例
Get the documentation of the resource and its fields
# Get the documentation of the resource and its fields
oc explain pods
# Get the documentation of a specific field of a resource
oc explain pods.spec.containers
6.1.68. oc expose 复制链接链接已复制到粘贴板!
将复制的应用程序作为服务或路由公开
用法示例
6.1.69. oc extract 复制链接链接已复制到粘贴板!
将 secret 或配置映射提取到磁盘
用法示例
6.1.70. oc get 复制链接链接已复制到粘贴板!
显示一个或多个资源
用法示例
6.1.71. oc image append 复制链接链接已复制到粘贴板!
向镜像添加层并将其推送到 registry
用法示例
6.1.72. oc image extract 复制链接链接已复制到粘贴板!
将文件从镜像复制到文件系统
用法示例
6.1.73. oc image info 复制链接链接已复制到粘贴板!
显示镜像的信息
用法示例
6.1.74. oc image mirror 复制链接链接已复制到粘贴板!
将镜像从一个存储库镜像到另一个存储库
用法示例
6.1.75. oc kustomize 复制链接链接已复制到粘贴板!
从目录或 URL 构建 kustomization 目标
用法示例
6.1.76. oc label 复制链接链接已复制到粘贴板!
更新资源上的标签
用法示例
6.1.77. oc logs 复制链接链接已复制到粘贴板!
显示 pod 中容器的日志
用法示例
6.1.78. oc observe 复制链接链接已复制到粘贴板!
观察资源的变化并对其做出反应(实验性)
用法示例
6.1.79. oc patch 复制链接链接已复制到粘贴板!
更新资源字段
用法示例
6.1.80. oc plugin list 复制链接链接已复制到粘贴板!
列出用户 PATH 中的所有可见插件可执行文件
用法示例
List all available plugins
# List all available plugins
oc plugin list
6.1.81. oc policy add-role-to-user 复制链接链接已复制到粘贴板!
为当前项目的用户或服务帐户添加角色
用法示例
Add the 'view' role to user1 for the current project
# Add the 'view' role to user1 for the current project
oc policy add-role-to-user view user1
# Add the 'edit' role to serviceaccount1 for the current project
oc policy add-role-to-user edit -z serviceaccount1
6.1.82. oc policy scc-review 复制链接链接已复制到粘贴板!
检查哪个服务帐户可以创建 pod
用法示例
6.1.83. oc policy scc-subject-review 复制链接链接已复制到粘贴板!
检查用户或服务帐户是否可以创建 pod
用法示例
6.1.84. oc port-forward 复制链接链接已复制到粘贴板!
将一个或多个本地端口转发到一个 pod
用法示例
6.1.85. oc proxy 复制链接链接已复制到粘贴板!
运行到 Kubernetes API 服务器的代理
用法示例
6.1.86. oc rollback 复制链接链接已复制到粘贴板!
将应用程序的一部分还原回以前的部署
用法示例
6.1.87. oc rollout cancel 复制链接链接已复制到粘贴板!
取消进行中的部署
用法示例
Cancel the in-progress deployment based on 'nginx'
# Cancel the in-progress deployment based on 'nginx'
oc rollout cancel dc/nginx
6.1.88. oc rollout history 复制链接链接已复制到粘贴板!
查看推出(rollout)历史记录
用法示例
View the rollout history of a deployment
# View the rollout history of a deployment
oc rollout history dc/nginx
# View the details of deployment revision 3
oc rollout history dc/nginx --revision=3
6.1.89. oc rollout latest 复制链接链接已复制到粘贴板!
使用来自触发器的最新状态为部署配置启动一个新的 rollout 操作
用法示例
Start a new rollout based on the latest images defined in the image change triggers
# Start a new rollout based on the latest images defined in the image change triggers
oc rollout latest dc/nginx
# Print the rolled out deployment config
oc rollout latest dc/nginx -o json
6.1.90. oc rollout pause 复制链接链接已复制到粘贴板!
将提供的资源标记为暂停
用法示例
Mark the nginx deployment as paused. Any current state of
# Mark the nginx deployment as paused. Any current state of
# the deployment will continue its function, new updates to the deployment will not
# have an effect as long as the deployment is paused
oc rollout pause dc/nginx
6.1.91. oc rollout restart 复制链接链接已复制到粘贴板!
重启资源
用法示例
6.1.92. oc rollout resume 复制链接链接已复制到粘贴板!
恢复暂停的资源
用法示例
Resume an already paused deployment
# Resume an already paused deployment
oc rollout resume dc/nginx
6.1.93. oc rollout retry 复制链接链接已复制到粘贴板!
重试最新失败的 rollout 操作
用法示例
Retry the latest failed deployment based on 'frontend'
# Retry the latest failed deployment based on 'frontend'
# The deployer pod and any hook pods are deleted for the latest failed deployment
oc rollout retry dc/frontend
6.1.94. oc rollout status 复制链接链接已复制到粘贴板!
显示推出部署的状态
用法示例
Watch the status of the latest rollout
# Watch the status of the latest rollout
oc rollout status dc/nginx
6.1.95. oc rollout undo 复制链接链接已复制到粘贴板!
撤消之前的推出部署
用法示例
Roll back to the previous deployment
# Roll back to the previous deployment
oc rollout undo dc/nginx
# Roll back to deployment revision 3. The replication controller for that version must exist
oc rollout undo dc/nginx --to-revision=3
6.1.96. oc rsh 复制链接链接已复制到粘贴板!
在容器中启动 shell 会话
用法示例
6.1.97. oc rsync 复制链接链接已复制到粘贴板!
在本地文件系统和 pod 间复制文件
用法示例
Synchronize a local directory with a pod directory
# Synchronize a local directory with a pod directory
oc rsync ./local/dir/ POD:/remote/dir
# Synchronize a pod directory with a local directory
oc rsync POD:/remote/dir/ ./local/dir
6.1.98. oc run 复制链接链接已复制到粘贴板!
在集群中运行特定镜像
用法示例
6.1.99. oc scale 复制链接链接已复制到粘贴板!
为部署、副本集或复制控制器设置新大小
用法示例
6.1.100. oc secrets link 复制链接链接已复制到粘贴板!
将 secret 链接到服务帐户
用法示例
Add an image pull secret to a service account to automatically use it for pulling pod images
# Add an image pull secret to a service account to automatically use it for pulling pod images
oc secrets link serviceaccount-name pull-secret --for=pull
# Add an image pull secret to a service account to automatically use it for both pulling and pushing build images
oc secrets link builder builder-image-secret --for=pull,mount
6.1.101. oc secrets unlink 复制链接链接已复制到粘贴板!
从服务帐户分离 secret
用法示例
Unlink a secret currently associated with a service account
# Unlink a secret currently associated with a service account
oc secrets unlink serviceaccount-name secret-name another-secret-name ...
6.1.102. oc set data 复制链接链接已复制到粘贴板!
更新配置映射或 secret 中的数据
用法示例
6.1.103. oc set env 复制链接链接已复制到粘贴板!
更新 pod 模板上的环境变量
用法示例
6.1.104. oc set image 复制链接链接已复制到粘贴板!
更新 pod 模板的镜像
用法示例
6.1.105. oc set image-lookup 复制链接链接已复制到粘贴板!
更改部署应用程序时镜像的解析方式
用法示例
6.1.106. oc set probe 复制链接链接已复制到粘贴板!
更新 pod 模板上的探测
用法示例
6.1.107. oc set resources 复制链接链接已复制到粘贴板!
使用 pod 模板更新对象上的资源请求/限制
用法示例
6.1.108. oc set route-backends 复制链接链接已复制到粘贴板!
更新路由的后端
用法示例
6.1.109. oc set selector 复制链接链接已复制到粘贴板!
在资源上设置选择器
用法示例
Set the labels and selector before creating a deployment/service pair.
# Set the labels and selector before creating a deployment/service pair.
oc create service clusterip my-svc --clusterip="None" -o yaml --dry-run | oc set selector --local -f - 'environment=qa' -o yaml | oc create -f -
oc create deployment my-dep -o yaml --dry-run | oc label --local -f - environment=qa -o yaml | oc create -f -
6.1.110. oc set serviceaccount 复制链接链接已复制到粘贴板!
更新资源的服务帐户
用法示例
Set deployment nginx-deployment's service account to serviceaccount1
# Set deployment nginx-deployment's service account to serviceaccount1
oc set serviceaccount deployment nginx-deployment serviceaccount1
# Print the result (in YAML format) of updated nginx deployment with service account from a local file, without hitting the API server
oc set sa -f nginx-deployment.yaml serviceaccount1 --local --dry-run -o yaml
6.1.111. oc set subject 复制链接链接已复制到粘贴板!
更新角色绑定或集群角色绑定中的用户、组或服务帐户
用法示例
6.1.112. oc set volumes 复制链接链接已复制到粘贴板!
更新 pod 模板中的卷
用法示例
6.1.113. oc tag 复制链接链接已复制到粘贴板!
将现有镜像标记到镜像流中
用法示例
6.1.114. oc version 复制链接链接已复制到粘贴板!
输出客户端和服务器版本信息
用法示例
6.1.115. oc wait 复制链接链接已复制到粘贴板!
实验性:等待一个或多个资源上的特定条件
用法示例