This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.此内容没有您所选择的语言版本。
Chapter 4. Developer CLI Operations
4.1. Overview 复制链接链接已复制到粘贴板!
This topic provides information on the developer CLI operations and their syntax. You must setup and login with the CLI before you can perform these operations.
The developer CLI uses the oc
command, and is used for project-level operations. This differs from the administrator CLI, which uses the oc adm
command for more advanced, administrator operations.
4.2. Common Operations 复制链接链接已复制到粘贴板!
The developer CLI allows interaction with the various objects that are managed by OpenShift Container Platform. Many common oc
operations are invoked using the following syntax:
oc <action> <object_type> <object_name>
$ oc <action> <object_type> <object_name>
This specifies:
-
An
<action>
to perform, such asget
ordescribe
. -
The
<object_type>
to perform the action on, such asservice
or the abbreviatedsvc
. -
The
<object_name>
of the specified<object_type>
.
For example, the oc get
operation returns a complete list of services that are currently defined:
oc get svc
$ oc get svc
NAME LABELS SELECTOR IP PORT(S)
docker-registry docker-registry=default docker-registry=default 172.30.78.158 5000/TCP
kubernetes component=apiserver,provider=kubernetes <none> 172.30.0.2 443/TCP
kubernetes-ro component=apiserver,provider=kubernetes <none> 172.30.0.1 80/TCP
The oc describe
operation can then be used to return detailed information about a specific object:
Versions of oc
prior to 3.0.2.0 did not have the ability to negotiate API versions against a server. So if you are using oc
up to 3.0.1.0 with a server that only supports v1 or higher versions of the API, make sure to pass --api-version
in order to point the oc
client to the correct API endpoint. For example: oc get svc --api-version=v1
.
4.3. Object Types 复制链接链接已复制到粘贴板!
The CLI supports the following object types, some of which have abbreviated syntax:
Object Type | Abbreviated Version |
---|---|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
4.4. Basic CLI Operations 复制链接链接已复制到粘贴板!
The following table describes basic oc
operations and their general syntax:
4.4.1. whoami 复制链接链接已复制到粘贴板!
Return information about the current session:
oc whoami [--options]
$ oc whoami [--options]
4.4.2. types 复制链接链接已复制到粘贴板!
Display an introduction to some core OpenShift Container Platform concepts:
oc types
$ oc types
4.4.3. login 复制链接链接已复制到粘贴板!
Log in to the OpenShift Container Platform server:
oc login
$ oc login
4.4.4. logout 复制链接链接已复制到粘贴板!
End the current session:
oc logout
$ oc logout
4.4.5. new-project 复制链接链接已复制到粘贴板!
Create a new project:
oc new-project <project_name>
$ oc new-project <project_name>
4.4.6. new-app 复制链接链接已复制到粘贴板!
Creates a new application based on the source code in the current directory:
oc new-app
$ oc new-app
Creates a new application based on the source code in a remote repository:
oc new-app https://github.com/sclorg/cakephp-ex
$ oc new-app https://github.com/sclorg/cakephp-ex
Creates a new application based on the source code in a private remote repository:
oc new-app https://github.com/youruser/yourprivaterepo --source-secret=yoursecret
$ oc new-app https://github.com/youruser/yourprivaterepo --source-secret=yoursecret
4.4.7. status 复制链接链接已复制到粘贴板!
Show an overview of the current project:
oc status
$ oc status
4.4.8. project 复制链接链接已复制到粘贴板!
Switch to another project. Run without options to display the current project. To view all projects you have access to run oc projects
.
oc project <project_name>
$ oc project <project_name>
4.4.9. explain 复制链接链接已复制到粘贴板!
See the documentation of a resource and its fields:
oc explain <resource_name>
$ oc explain <resource_name>
4.4.10. cluster 复制链接链接已复制到粘贴板!
Start or stop a OpenShift Container Platform cluster:
oc cluster [--options]
$ oc cluster [--options]
4.4.11. completion 复制链接链接已复制到粘贴板!
Output shell completion code for the specified shell:
oc completion [--options]
$ oc completion [--options]
4.4.12. help 复制链接链接已复制到粘贴板!
Get help about any command:
oc <command> --help
$ oc <command> --help
4.4.13. plugin 复制链接链接已复制到粘贴板!
Run a command line plug-in:
oc plugin [--options]
$ oc plugin [--options]
4.4.14. version 复制链接链接已复制到粘贴板!
Display client and server versions:
oc version [--options]
$ oc version [--options]
4.5. Application Modification CLI Operations 复制链接链接已复制到粘贴板!
4.5.1. get 复制链接链接已复制到粘贴板!
Return a list of objects for the specified object type. If the optional <object_name>
is included in the request, then the list of results is filtered by that value.
oc get <object_type> [<object_name>]
$ oc get <object_type> [<object_name>]
You can use the -o
or --output
option to modify the output format.
oc get <object_type> [<object_name>]-o|--output=json|yaml|wide|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...]
$ oc get <object_type> [<object_name>]-o|--output=json|yaml|wide|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...]
The output format can be a JSON or YAML, or an extensible format like custom columns, golang template, and jsonpath.
For example, the following command lists the name of the pods running in a specific project:
oc get pods -n default -o jsonpath='{range .items[*].metadata}{"Pod Name: "}{.name}{"\n"}{end}'
$ oc get pods -n default -o jsonpath='{range .items[*].metadata}{"Pod Name: "}{.name}{"\n"}{end}'
Pod Name: docker-registry-1-wvhrx
Pod Name: registry-console-1-ntq65
Pod Name: router-1-xzw69
4.5.2. describe 复制链接链接已复制到粘贴板!
Returns information about the specific object returned by the query. A specific <object_name>
must be provided. The actual information that is available varies as described in object type.
oc describe <object_type> <object_name>
$ oc describe <object_type> <object_name>
4.5.3. edit 复制链接链接已复制到粘贴板!
Edit the desired object type:
oc edit <object_type>/<object_name>
$ oc edit <object_type>/<object_name>
Edit the desired object type with a specified text editor:
OC_EDITOR="<text_editor>" oc edit <object_type>/<object_name>
$ OC_EDITOR="<text_editor>" oc edit <object_type>/<object_name>
Edit the desired object in a specified format (eg: JSON):
oc edit <object_type>/<object_name> \ --output-version=<object_type_version> \ -o <object_type_format>
$ oc edit <object_type>/<object_name> \
--output-version=<object_type_version> \
-o <object_type_format>
4.5.4. config 复制链接链接已复制到粘贴板!
Change configuration files for the client:
oc config --config=""
$ oc config --config=""
4.5.5. volume 复制链接链接已复制到粘贴板!
Modify a volume:
oc volume <object_type>/<object_name> [--option]
$ oc volume <object_type>/<object_name> [--option]
4.5.6. label 复制链接链接已复制到粘贴板!
Update the labels on a object:
oc label <object_type> <object_name> <label>
$ oc label <object_type> <object_name> <label>
4.5.7. annotate 复制链接链接已复制到粘贴板!
Update the annotations on a resource:
oc annotate [--options]
$ oc annotate [--options]
4.5.8. expose 复制链接链接已复制到粘贴板!
Look up a service and expose it as a route. There is also the ability to expose a deployment configuration, replication controller, service, or pod as a new service on a specified port. If no labels are specified, the new object will re-use the labels from the object it exposes.
If you are exposing a service, the default generator is --generator=route/v1
. For all other cases the default is --generator=service/v2
, which leaves the port unnamed. Generally, there is no need to set a generator with the oc expose
command. A third generator, --generator=service/v1
, is available with the port name default.
oc expose <object_type> <object_name>
$ oc expose <object_type> <object_name>
4.5.9. delete 复制链接链接已复制到粘贴板!
Delete the specified object. An object configuration can also be passed in through STDIN. The oc delete all -l <label>
operation deletes all objects matching the specified <label>
, including the replication controller so that pods are not re-created.
oc delete -f <file_path>
$ oc delete -f <file_path>
oc delete <object_type> <object_name>
$ oc delete <object_type> <object_name>
oc delete <object_type> -l <label>
$ oc delete <object_type> -l <label>
oc delete all -l <label>
$ oc delete all -l <label>
4.5.10. set 复制链接链接已复制到粘贴板!
Modify a specific property of the specified object.
4.5.10.1. set env 复制链接链接已复制到粘贴板!
Sets an environment variable on a deployment configuration or a build configuration:
oc set env dc/mydc VAR1=value1
$ oc set env dc/mydc VAR1=value1
4.5.10.2. set build-secret 复制链接链接已复制到粘贴板!
Sets the name of a secret on a build configuration. The secret may be an image pull or push secret or a source repository secret:
oc set build-secret --source bc/mybc mysecret
$ oc set build-secret --source bc/mybc mysecret
4.6. Build and Deployment CLI Operations 复制链接链接已复制到粘贴板!
One of the fundamental capabilities of OpenShift Container Platform is the ability to build applications into a container from source.
OpenShift Container Platform provides CLI access to inspect and manipulate deployment configurations using standard oc
resource operations, such as get
, create
, and describe
.
4.6.1. start-build 复制链接链接已复制到粘贴板!
Manually start the build process with the specified build configuration file:
oc start-build <buildconfig_name>
$ oc start-build <buildconfig_name>
Manually start the build process by specifying the name of a previous build as a starting point:
oc start-build --from-build=<build_name>
$ oc start-build --from-build=<build_name>
Manually start the build process by specifying either a configuration file or the name of a previous build and retrieve its build logs:
oc start-build --from-build=<build_name> --follow
$ oc start-build --from-build=<build_name> --follow
oc start-build <buildconfig_name> --follow
$ oc start-build <buildconfig_name> --follow
Wait for a build to complete and exit with a non-zero return code if the build fails:
oc start-build --from-build=<build_name> --wait
$ oc start-build --from-build=<build_name> --wait
Set or override environment variables for the current build without changing the build configuration. Alternatively, use -e
.
oc start-build --env <var_name>=<value>
$ oc start-build --env <var_name>=<value>
Set or override the default build log level output during the build:
oc start-build --build-loglevel [0-5]
$ oc start-build --build-loglevel [0-5]
Specify the source code commit identifier the build should use; requires a build based on a Git repository:
oc start-build --commit=<hash>
$ oc start-build --commit=<hash>
Re-run build with name <build_name>
:
oc start-build --from-build=<build_name>
$ oc start-build --from-build=<build_name>
Archive <dir_name>
and build with it as the binary input:
oc start-build --from-dir=<dir_name>
$ oc start-build --from-dir=<dir_name>
Use existing archive as the binary input; unlike --from-file
the archive will be extracted by the builder prior to the build process:
oc start-build --from-archive=<archive_name>
$ oc start-build --from-archive=<archive_name>
Use <file_name>
as the binary input for the build. This file must be the only one in the build source. For example, pom.xml or Dockerfile.
oc start-build --from-file=<file_name>
$ oc start-build --from-file=<file_name>
Download the binary input using HTTP or HTTPS instead of reading it from the file system:
oc start-build --from-file=<file_URL>
$ oc start-build --from-file=<file_URL>
Download an archive and use its contents as the build source:
oc start-build --from-archive=<archive_URL>
$ oc start-build --from-archive=<archive_URL>
The path to a local source code repository to use as the binary input for a build:
oc start-build --from-repo=<path_to_repo>
$ oc start-build --from-repo=<path_to_repo>
Specify a webhook URL for an existing build configuration to trigger:
oc start-build --from-webhook=<webhook_URL>
$ oc start-build --from-webhook=<webhook_URL>
The contents of the post-receive hook to trigger a build:
oc start-build --git-post-receive=<contents>
$ oc start-build --git-post-receive=<contents>
The path to the Git repository for post-receive; defaults to the current directory:
oc start-build --git-repository=<path_to_repo>
$ oc start-build --git-repository=<path_to_repo>
List the webhooks for the specified build configuration or build; accepts all
, generic
, or github
:
oc start-build --list-webhooks
$ oc start-build --list-webhooks
Override the Spec.Strategy.SourceStrategy.Incremental option of a source-strategy build:
oc start-build --incremental
$ oc start-build --incremental
Override the Spec.Strategy.DockerStrategy.NoCache option of a docker-strategy build:
$oc start-build --no-cache
$oc start-build --no-cache
4.6.2. rollback 复制链接链接已复制到粘贴板!
Perform a rollback:
oc rollback <deployment_name>
$ oc rollback <deployment_name>
4.6.3. rollout 复制链接链接已复制到粘贴板!
Manage a Kubernetes deployment or an OpenShift deployment configuration. Start a new rollout, view its status or history, or rollback to a previous revision of your application:
oc rollout [--options]
$ oc rollout [--options]
4.6.4. new-build 复制链接链接已复制到粘贴板!
Create a build configuration based on the source code in the current Git repository (with a public remote) and a container image:
oc new-build .
$ oc new-build .
Create a build configuration based on a remote git repository:
oc new-build https://github.com/sclorg/cakephp-ex
$ oc new-build https://github.com/sclorg/cakephp-ex
Create a build configuration based on a private remote git repository:
oc new-build https://github.com/youruser/yourprivaterepo --source-secret=yoursecret
$ oc new-build https://github.com/youruser/yourprivaterepo --source-secret=yoursecret
4.6.5. cancel-build 复制链接链接已复制到粘贴板!
Stop a build that is in progress:
oc cancel-build <build_name>
$ oc cancel-build <build_name>
Cancel multiple builds at the same time:
oc cancel-build <build1_name> <build2_name> <build3_name>
$ oc cancel-build <build1_name> <build2_name> <build3_name>
Cancel all builds created from the build configuration:
oc cancel-build bc/<buildconfig_name>
$ oc cancel-build bc/<buildconfig_name>
Specify the builds to be canceled:
oc cancel-build bc/<buildconfig_name> --state=<state>
$ oc cancel-build bc/<buildconfig_name> --state=<state>
Example values for state
are new or pending.
4.6.6. image 复制链接链接已复制到粘贴板!
Useful commands for managing images.
oc image [--options]
$ oc image [--options]
4.6.7. import 复制链接链接已复制到粘贴板!
Commands that import applications into OpenShift Container Platform.
oc import [--options]
$ oc import [--options]
4.6.8. import-image 复制链接链接已复制到粘贴板!
Import tag and image information from an external image repository:
oc import-image <image_stream>
$ oc import-image <image_stream>
4.6.9. scale 复制链接链接已复制到粘贴板!
Set the number of desired replicas for a replication controller or a deployment configuration to the number of specified replicas:
oc scale <object_type> <object_name> --replicas=<#_of_replicas>
$ oc scale <object_type> <object_name> --replicas=<#_of_replicas>
4.6.10. tag 复制链接链接已复制到粘贴板!
Take an existing tag or image from an image stream, or a container image "pull spec", and set it as the most recent image for a tag in one or more other image streams:
oc tag <current_image> <image_stream>
$ oc tag <current_image> <image_stream>
4.7. Advanced Commands 复制链接链接已复制到粘贴板!
4.7.1. adm 复制链接链接已复制到粘贴板!
Administrative commands. Tools for managing a cluster:
oc adm [--options]
$ oc adm [--options]
4.7.2. apply 复制链接链接已复制到粘贴板!
Apply a configuration to a resource by file name or stdin:
oc apply [--options]
$ oc apply [--options]
4.7.3. create 复制链接链接已复制到粘贴板!
Parse a configuration file and create one or more OpenShift Container Platform objects based on the file contents. The -f
flag can be passed multiple times with different file or directory paths. When the flag is passed multiple times, oc create
iterates through each one, creating the objects described in all of the indicated files. Any existing resources are ignored.
oc create -f <file_or_dir_path>
$ oc create -f <file_or_dir_path>
4.7.4. replace 复制链接链接已复制到粘贴板!
Attempt to modify an existing object based on the contents of the specified configuration file. The -f
flag can be passed multiple times with different file or directory paths. When the flag is passed multiple times, oc replace
iterates through each one, updating the objects described in all of the indicated files.
oc replace -f <file_or_dir_path>
$ oc replace -f <file_or_dir_path>
4.7.5. process 复制链接链接已复制到粘贴板!
Transform a project template into a project configuration file:
oc process -f <template_file_path>
$ oc process -f <template_file_path>
4.7.6. run 复制链接链接已复制到粘贴板!
Create and run a particular image, possibly replicated. By default, create a deployment configuration to manage the created container(s). You can choose to create a different resource using the --generator
flag:
API Resource | --generator Option |
---|---|
Deployment configuration |
|
Pod |
|
Replication controller |
|
Deployment using |
|
Deployment using |
|
Job |
|
Cron job |
|
You can choose to run in the foreground for an interactive container execution.
4.7.7. patch 复制链接链接已复制到粘贴板!
Updates one or more fields of an object using strategic merge patch:
oc patch <object_type> <object_name> -p <changes>
$ oc patch <object_type> <object_name> -p <changes>
The <changes> is a JSON or YAML expression containing the new fields and the values. For example, to update the spec.unschedulable
field of the node node1
to the value true
, the json expression is:
oc patch node node1 -p '{"spec":{"unschedulable":true}}'
$ oc patch node node1 -p '{"spec":{"unschedulable":true}}'
4.7.8. export 复制链接链接已复制到粘贴板!
Export resources to be used elsewhere:
oc export <object_type> [--options]
$ oc export <object_type> [--options]
See Creating a Template from Existing Objects for more information on exporting existing objects from your project in template form.
4.7.9. extract 复制链接链接已复制到粘贴板!
Extract secrets or config maps to disk:
oc extract [--options]
$ oc extract [--options]
4.7.10. idle 复制链接链接已复制到粘贴板!
Idle scalable resources:
oc idle [--options]
$ oc idle [--options]
4.7.11. observe 复制链接链接已复制到粘贴板!
Observe changes to resources and react to them:
oc observe [--options]
$ oc observe [--options]
4.7.12. auth 复制链接链接已复制到粘贴板!
Inspect authorization:
oc auth [--options]
$ oc auth [--options]
4.7.13. policy 复制链接链接已复制到粘贴板!
Manage authorization policies:
oc policy [--options]
$ oc policy [--options]
4.7.14. convert 复制链接链接已复制到粘贴板!
Convert configuration files between different API versions:
oc convert [--options]
$ oc convert [--options]
4.7.15. secrets 复制链接链接已复制到粘贴板!
Configure secrets:
oc secrets [--options] path/to/ssh_key
$ oc secrets [--options] path/to/ssh_key
4.7.16. autoscale 复制链接链接已复制到粘贴板!
Setup an autoscaler for your application. Requires metrics to be enabled in the cluster. See Enabling Cluster Metrics for cluster administrator instructions, if needed.
oc autoscale dc/<dc_name> [--options]
$ oc autoscale dc/<dc_name> [--options]
4.8. Troubleshooting and Debugging CLI Operations 复制链接链接已复制到粘贴板!
4.8.1. debug 复制链接链接已复制到粘贴板!
Launch a command shell to debug a running application.
oc debug -h
$ oc debug -h
When debugging images and setup problems, you can get an exact copy of a running pod configuration and troubleshoot with a shell. Since a failing pod may not be started and not accessible to rsh
or exec
, running the debug
command creates a carbon copy of that setup.
The default mode is to start a shell inside of the first container of the referenced pod, replication controller, or deployment configuration. The started pod will be a copy of your source pod, with labels stripped, the command changed to /bin/sh
, and readiness and liveness checks disabled. If you just want to run a command, add --
and a command to run. Passing a command will not create a TTY or send STDIN by default. Other flags are supported for altering the container or pod in common ways.
A common problem running containers is a security policy that prohibits you from running as a root user on the cluster. You can use this command to test running a pod as non-root (with --as-user
) or to run a non-root pod as root (with --as-root
).
The debug pod is deleted when the the remote command completes or you interrupt the shell.
4.8.1.1. Usage 复制链接链接已复制到粘贴板!
oc debug RESOURCE/NAME [ENV1=VAL1 ...] [-c CONTAINER] [options] [-- COMMAND]
$ oc debug RESOURCE/NAME [ENV1=VAL1 ...] [-c CONTAINER] [options] [-- COMMAND]
4.8.1.2. Examples 复制链接链接已复制到粘贴板!
To debug a currently running deployment:
oc debug dc/test
$ oc debug dc/test
To test running a deployment as a non-root user:
oc debug dc/test --as-user=1000000
$ oc debug dc/test --as-user=1000000
To debug a specific failing container by running the env
command in the second
container:
oc debug dc/test -c second -- /bin/env
$ oc debug dc/test -c second -- /bin/env
To view the pod that would be created to debug:
oc debug dc/test -o yaml
$ oc debug dc/test -o yaml
4.8.2. logs 复制链接链接已复制到粘贴板!
Retrieve the log output for a specific build, deployment, or pod. This command works for builds, build configurations, deployment configurations, and pods.
oc logs -f <pod>
$ oc logs -f <pod>
4.8.3. exec 复制链接链接已复制到粘贴板!
Execute a command in an already-running container. You can optionally specify a container ID, otherwise it defaults to the first container.
oc exec <pod> [-c <container>] <command>
$ oc exec <pod> [-c <container>] <command>
For security purposes, the oc exec
command does not work when accessing privileged containers except when the command is executed by a cluster-admin
user. Administrators can SSH into a node host, then use the docker exec
command on the desired container.
4.8.4. rsh 复制链接链接已复制到粘贴板!
Open a remote shell session to a container:
oc rsh <pod>
$ oc rsh <pod>
4.8.5. rsync 复制链接链接已复制到粘贴板!
Copy the contents to or from a directory in an already-running pod container. If you do not specify a container, it defaults to the first container in the pod.
To copy contents from a local directory to a directory in a pod:
oc rsync <local_dir> <pod>:<pod_dir> -c <container>
$ oc rsync <local_dir> <pod>:<pod_dir> -c <container>
To copy contents from a directory in a pod to a local directory:
oc rsync <pod>:<pod_dir> <local_dir> -c <container>
$ oc rsync <pod>:<pod_dir> <local_dir> -c <container>
4.8.6. port-forward 复制链接链接已复制到粘贴板!
Forward one or more local ports to a pod:
oc port-forward <pod> <local_port>:<remote_port>
$ oc port-forward <pod> <local_port>:<remote_port>
4.8.7. proxy 复制链接链接已复制到粘贴板!
Run a proxy to the Kubernetes API server:
oc proxy --port=<port> --www=<static_directory>
$ oc proxy --port=<port> --www=<static_directory>
4.8.8. attach 复制链接链接已复制到粘贴板!
Attach to a running container:
oc attach [--options]
$ oc attach [--options]
4.8.9. cp 复制链接链接已复制到粘贴板!
Copy files and directories to and from containers:
oc cp [--options]
$ oc cp [--options]