Difference between revisions of "IT-SDK-Openshift-cmd"
Jump to navigation
Jump to search
(Created page with "<p> Source: https://github.com/openshift/openshift-docs/blob/master/cli_reference/cli_by_example_content.adoc == An introduction to the concepts and types in OpenShift == /...") |
Samerhijazi (talk | contribs) |
||
| Line 1: | Line 1: | ||
| − | + | * src: https://github.com/openshift/openshift-docs/blob/master/cli_reference/cli_by_example_content.adoc | |
| + | |||
== An introduction to the concepts and types in OpenShift == | == An introduction to the concepts and types in OpenShift == | ||
| − | |||
// View all projects you have access to | // View all projects you have access to | ||
$ oc projects | $ oc projects | ||
| Line 13: | Line 13: | ||
// Show the images tagged into an image stream | // Show the images tagged into an image stream | ||
$ oc describe is ruby-centos7 | $ oc describe is ruby-centos7 | ||
| − | |||
| − | |||
== Log in to an OpenShift server == | == Log in to an OpenShift server == | ||
| − | |||
// Log in interactively | // Log in interactively | ||
$ oc login | $ oc login | ||
| Line 25: | Line 22: | ||
// Log in to the given server with the given credentials (will not prompt interactively) | // Log in to the given server with the given credentials (will not prompt interactively) | ||
$ oc login localhost:8443 --username=myuser --password=mypass | $ oc login localhost:8443 --username=myuser --password=mypass | ||
| − | |||
| − | |||
== Request a new project == | == Request a new project == | ||
| − | |||
// Create a new project with minimal information | // Create a new project with minimal information | ||
$ oc new-project web-team-dev | $ oc new-project web-team-dev | ||
| Line 34: | Line 28: | ||
// Create a new project with a display name and description | // Create a new project with a display name and description | ||
$ oc new-project web-team-dev --display-name="Web Team Development" --description="Development project for the web team." | $ oc new-project web-team-dev --display-name="Web Team Development" --description="Development project for the web team." | ||
| − | |||
== Create a new application == | == Create a new application == | ||
| − | |||
// Create an application based on the source code in the current git repository (with a public remote) and a container image | // Create an application based on the source code in the current git repository (with a public remote) and a container image | ||
$ oc new-app . --docker-image=repo/langimage | $ oc new-app . --docker-image=repo/langimage | ||
| Line 60: | Line 52: | ||
// Create an application based on a template file, explicitly setting a parameter value | // Create an application based on a template file, explicitly setting a parameter value | ||
$ oc new-app --file=./example/myapp/template.json --param=MYSQL_USER=admin | $ oc new-app --file=./example/myapp/template.json --param=MYSQL_USER=admin | ||
| − | |||
== Show an overview of the current project== | == Show an overview of the current project== | ||
// Show an overview of the current project | // Show an overview of the current project | ||
$ oc status | $ oc status | ||
| − | |||
== Switch to another project == | == Switch to another project == | ||
// Switch to 'myapp' project | // Switch to 'myapp' project | ||
| Line 71: | Line 61: | ||
// Display the project currently in use | // Display the project currently in use | ||
$ oc project | $ oc project | ||
| − | |||
== Starts a new build == | == Starts a new build == | ||
// Starts build from build config matching the name "golang" | // Starts build from build config matching the name "golang" | ||
| Line 82: | Line 71: | ||
// completes or fails | // completes or fails | ||
$ oc start-build golang --follow | $ oc start-build golang --follow | ||
| − | |||
== Show container logs from the build container == | == Show container logs from the build container == | ||
// Stream logs from the latest build for build config "golang" to stdout | // Stream logs from the latest build for build config "golang" to stdout | ||
| Line 90: | Line 78: | ||
// Stream logs from build "golang-1" to stdout | // Stream logs from build "golang-1" to stdout | ||
$ oc logs -f build/golang-1 | $ oc logs -f build/golang-1 | ||
| − | |||
| − | |||
== View, start, cancel, or retry rollouts== | == View, start, cancel, or retry rollouts== | ||
// Display information for the 'database' DeploymentConfig | // Display information for the 'database' DeploymentConfig | ||
| Line 104: | Line 90: | ||
// View the history of the 'database' DeploymentConfig | // View the history of the 'database' DeploymentConfig | ||
$ oc rollout history dc/database | $ oc rollout history dc/database | ||
| − | |||
== Show logs from the deployment process== | == Show logs from the deployment process== | ||
| − | |||
| − | |||
// Stream logs from the latest deployment for deployment config "frontend" to stdout | // Stream logs from the latest deployment for deployment config "frontend" to stdout | ||
// Note that if the deployment process is successful, logs from the application will | // Note that if the deployment process is successful, logs from the application will | ||
| Line 116: | Line 99: | ||
// be returned. | // be returned. | ||
$ oc logs --version=1 dc/database | $ oc logs --version=1 dc/database | ||
| − | |||
== Revert part of an application back to a previous deployment== | == Revert part of an application back to a previous deployment== | ||
| − | |||
| − | |||
// Perform a rollback | // Perform a rollback | ||
$ oc rollout undo dc/foo | $ oc rollout undo dc/foo | ||
| Line 128: | Line 108: | ||
// Perform a rollback to a specific revision | // Perform a rollback to a specific revision | ||
$ oc rollout undo dc/foo --to-revision=2 | $ oc rollout undo dc/foo --to-revision=2 | ||
| − | |||
== Create a new build configuration== | == Create a new build configuration== | ||
| − | |||
| − | |||
// Create a build config based on the source code in the current git repository (with a public remote) and a container image | // Create a build config based on the source code in the current git repository (with a public remote) and a container image | ||
$ oc new-build . --docker-image=repo/langimage | $ oc new-build . --docker-image=repo/langimage | ||
| Line 140: | Line 117: | ||
// Create a build config from a remote repository using its beta2 branch | // Create a build config from a remote repository using its beta2 branch | ||
$ oc new-build https://github.com/openshift/ruby-hello-world#beta2 | $ oc new-build https://github.com/openshift/ruby-hello-world#beta2 | ||
| − | |||
== Cancel a pending or running build== | == Cancel a pending or running build== | ||
| − | |||
| − | |||
// Cancel the build with the given name | // Cancel the build with the given name | ||
$ oc cancel-build 1da32cvq | $ oc cancel-build 1da32cvq | ||
| Line 152: | Line 126: | ||
// Cancel the named build and create a new one with the same parameters | // Cancel the named build and create a new one with the same parameters | ||
$ oc cancel-build 1da32cvq --restart | $ oc cancel-build 1da32cvq --restart | ||
| − | |||
== Imports images from a container image registry== | == Imports images from a container image registry== | ||
| − | |||
| − | |||
$ oc import-image mystream | $ oc import-image mystream | ||
== Change the number of pods in a deployment== | == Change the number of pods in a deployment== | ||
| − | |||
| − | |||
// Scale replication controller named 'foo' to 3. | // Scale replication controller named 'foo' to 3. | ||
$ oc scale --replicas=3 replicationcontrollers foo | $ oc scale --replicas=3 replicationcontrollers foo | ||
| Line 168: | Line 137: | ||
== Tag existing images into image streams== | == Tag existing images into image streams== | ||
| − | |||
| − | |||
// Tag the current image for the image stream 'openshift/ruby' and tag '2.0' into the image stream 'yourproject/ruby with tag 'tip'. | // Tag the current image for the image stream 'openshift/ruby' and tag '2.0' into the image stream 'yourproject/ruby with tag 'tip'. | ||
$ oc tag openshift/ruby:2.0 yourproject/ruby:tip | $ oc tag openshift/ruby:2.0 yourproject/ruby:tip | ||
| Line 183: | Line 150: | ||
== Display one or many resources== | == Display one or many resources== | ||
| − | |||
| − | |||
// List all pods in ps output format. | // List all pods in ps output format. | ||
$ oc get pods | $ oc get pods | ||
| Line 198: | Line 163: | ||
== Show details of a specific resource== | == Show details of a specific resource== | ||
| − | |||
| − | |||
// Provide details about the ruby-20-centos7 image repository | // Provide details about the ruby-20-centos7 image repository | ||
$ oc describe imageRepository ruby-20-centos7 | $ oc describe imageRepository ruby-20-centos7 | ||
| Line 207: | Line 170: | ||
== Edit a resource on the server== | == Edit a resource on the server== | ||
| − | |||
| − | |||
// Edit the service named 'docker-registry': | // Edit the service named 'docker-registry': | ||
$ oc edit svc/docker-registry | $ oc edit svc/docker-registry | ||
| Line 222: | Line 183: | ||
== Update the environment on a resource with a pod template== | == Update the environment on a resource with a pod template== | ||
| − | |||
| − | |||
// Update deployment 'registry' with a new environment variable | // Update deployment 'registry' with a new environment variable | ||
$ oc set env dc/registry STORAGE_DIR=/local | $ oc set env dc/registry STORAGE_DIR=/local | ||
| Line 250: | Line 209: | ||
== Update volume on a resource with a pod template== | == Update volume on a resource with a pod template== | ||
| − | |||
| − | |||
// Add new volume of type 'emptyDir' for deployment config 'registry' and mount under /opt inside the containers | // Add new volume of type 'emptyDir' for deployment config 'registry' and mount under /opt inside the containers | ||
// The volume name is auto generated | // The volume name is auto generated | ||
| Line 290: | Line 247: | ||
== Update the labels on a resource== | == Update the labels on a resource== | ||
| − | |||
| − | |||
// Update pod 'foo' with the label 'unhealthy' and the value 'true'. | // Update pod 'foo' with the label 'unhealthy' and the value 'true'. | ||
$ oc label pods foo unhealthy=true | $ oc label pods foo unhealthy=true | ||
| Line 309: | Line 264: | ||
== Expose a replicated application as a service or route== | == Expose a replicated application as a service or route== | ||
| − | |||
| − | |||
// Create a route based on service ruby-hello-world. The new route will re-use the ruby-hello-world labels | // Create a route based on service ruby-hello-world. The new route will re-use the ruby-hello-world labels | ||
$ oc expose svc/ruby-hello-world | $ oc expose svc/ruby-hello-world | ||
| Line 326: | Line 279: | ||
== Autoscale an application== | == Autoscale an application== | ||
| − | |||
| − | |||
// Auto scale a deployment config "foo", with the number of pods between 2 to 10, target CPU utilization at a default value that server applies | // Auto scale a deployment config "foo", with the number of pods between 2 to 10, target CPU utilization at a default value that server applies | ||
$ oc autoscale dc/foo --min=2 --max=10 | $ oc autoscale dc/foo --min=2 --max=10 | ||
| Line 336: | Line 287: | ||
ifdef::openshift-enterprise,openshift-origin,openshift-dedicated[] | ifdef::openshift-enterprise,openshift-origin,openshift-dedicated[] | ||
== Securely expose containers via a route== | == Securely expose containers via a route== | ||
| − | |||
| − | |||
// Create a secure edge-terminated route using the specified | // Create a secure edge-terminated route using the specified | ||
// certificates/keys and hostname. If the certificates/keys | // certificates/keys and hostname. If the certificates/keys | ||
| Line 355: | Line 304: | ||
$ oc create route reencrypt --service=backend --dest-ca-cert=ca.crt | $ oc create route reencrypt --service=backend --dest-ca-cert=ca.crt | ||
endif::[] | endif::[] | ||
| − | |||
ifdef::openshift-online[] | ifdef::openshift-online[] | ||
== Securely expose containers via a route== | == Securely expose containers via a route== | ||
| − | |||
| − | |||
// Create a secure edge-terminated route. | // Create a secure edge-terminated route. | ||
$ oc create route edge --service=frontend | $ oc create route edge --service=frontend | ||
| Line 367: | Line 313: | ||
oc create route passthrough --service=frontend | oc create route passthrough --service=frontend | ||
endif::[] | endif::[] | ||
| − | |||
== Delete a resource by filename, stdin, resource and ID, or by resources and label selector.== | == Delete a resource by filename, stdin, resource and ID, or by resources and label selector.== | ||
| − | |||
| − | |||
// Delete a pod using the type and ID specified in pod.json. | // Delete a pod using the type and ID specified in pod.json. | ||
$ oc delete -f pod.json | $ oc delete -f pod.json | ||
| Line 388: | Line 331: | ||
== Print the logs for a container in a pod.== | == Print the logs for a container in a pod.== | ||
| − | |||
| − | |||
// Returns snapshot of ruby-container logs from pod backend. | // Returns snapshot of ruby-container logs from pod backend. | ||
$ oc logs backend -c ruby-container | $ oc logs backend -c ruby-container | ||
| Line 398: | Line 339: | ||
== Execute a command in a container.== | == Execute a command in a container.== | ||
| − | |||
| − | |||
// Get output from running 'date' in ruby-container from pod 123456-7890 | // Get output from running 'date' in ruby-container from pod 123456-7890 | ||
$ oc exec -p 123456-7890 -c ruby-container date | $ oc exec -p 123456-7890 -c ruby-container date | ||
| Line 407: | Line 346: | ||
== Open a remote shell session to a container. It will default to the first container if none is specified.== | == Open a remote shell session to a container. It will default to the first container if none is specified.== | ||
| − | |||
| − | |||
// Open a shell session on the first container in pod 123456-7890 | // Open a shell session on the first container in pod 123456-7890 | ||
$ oc rsh 123456-7890 | $ oc rsh 123456-7890 | ||
| Line 416: | Line 353: | ||
== Forward one or more local ports to a pod.== | == Forward one or more local ports to a pod.== | ||
| − | |||
| − | |||
// Listens on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod | // Listens on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod | ||
$ oc port-forward -p mypod 5000 6000 | $ oc port-forward -p mypod 5000 6000 | ||
| Line 431: | Line 366: | ||
== Run a proxy to the Kubernetes API server== | == Run a proxy to the Kubernetes API server== | ||
| − | |||
| − | |||
// Run a proxy to Kubernetes apiserver on port 8011, serving static content from ./local/www/ | // Run a proxy to Kubernetes apiserver on port 8011, serving static content from ./local/www/ | ||
$ oc proxy --port=8011 --www=./local/www/ | $ oc proxy --port=8011 --www=./local/www/ | ||
| Line 441: | Line 374: | ||
== Create a resource by filename or stdin== | == Create a resource by filename or stdin== | ||
| − | |||
| − | |||
// Create a pod using the data in pod.json. | // Create a pod using the data in pod.json. | ||
$ oc create -f pod.json | $ oc create -f pod.json | ||
| Line 450: | Line 381: | ||
== Update a resource by filename or stdin.== | == Update a resource by filename or stdin.== | ||
| − | |||
| − | |||
// Update a pod using the data in pod.json. | // Update a pod using the data in pod.json. | ||
$ oc replace -f pod.json | $ oc replace -f pod.json | ||
| Line 459: | Line 388: | ||
== Process a template into list of resources== | == Process a template into list of resources== | ||
| − | |||
| − | |||
// Convert template.json file into resource list | // Convert template.json file into resource list | ||
$ oc process -f template.json | $ oc process -f template.json | ||
| Line 475: | Line 402: | ||
== Export resources so they can be used elsewhere==[NOTE] | == Export resources so they can be used elsewhere==[NOTE] | ||
`oc export` is deprecated. See link:https://docs.openshift.com/container-platform/3.11/release_notes/ocp_3_11_release_notes.html#ocp-311-notable-technical-changes[Notable Technical Changes] for more details. | `oc export` is deprecated. See link:https://docs.openshift.com/container-platform/3.11/release_notes/ocp_3_11_release_notes.html#ocp-311-notable-technical-changes[Notable Technical Changes] for more details. | ||
| − | |||
// export the services and deployment configurations labeled name=test | // export the services and deployment configurations labeled name=test | ||
| Line 496: | Line 422: | ||
== End the current server session== | == End the current server session== | ||
| − | |||
| − | |||
// Logout | // Logout | ||
$ oc logout | $ oc logout | ||
== Display merged kubeconfig settings or a specified kubeconfig file.== | == Display merged kubeconfig settings or a specified kubeconfig file.== | ||
| − | |||
| − | |||
// Show Merged kubeconfig settings. | // Show Merged kubeconfig settings. | ||
$ oc config view | $ oc config view | ||
| Line 511: | Line 433: | ||
== Set a cluster entry in kubeconfig== | == Set a cluster entry in kubeconfig== | ||
| − | |||
| − | |||
// Set only the server field on the e2e cluster entry without touching other values. | // Set only the server field on the e2e cluster entry without touching other values. | ||
$ oc config set-cluster e2e --server=https://1.2.3.4 | $ oc config set-cluster e2e --server=https://1.2.3.4 | ||
| Line 523: | Line 443: | ||
== Set a user entry in kubeconfig== | == Set a user entry in kubeconfig== | ||
| − | |||
| − | |||
// Set only the "client-key" field on the "cluster-admin" | // Set only the "client-key" field on the "cluster-admin" | ||
// entry, without touching other values: | // entry, without touching other values: | ||
| Line 536: | Line 454: | ||
== Set a context entry in kubeconfig== | == Set a context entry in 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 | $ oc config set-context gce --user=cluster-admin | ||
== Change configuration files for the client== | == Change configuration files for the client== | ||
| − | |||
| − | |||
// Change the config context to use | // Change the config context to use | ||
oc config use-context my-context | oc config use-context my-context | ||
Revision as of 00:48, 15 June 2019
- src: https://github.com/openshift/openshift-docs/blob/master/cli_reference/cli_by_example_content.adoc
Contents
- 1 An introduction to the concepts and types in OpenShift
- 2 Log in to an OpenShift server
- 3 Request a new project
- 4 Create a new application
- 5 Show an overview of the current project
- 6 Switch to another project
- 7 Starts a new build
- 8 Show container logs from the build container
- 9 View, start, cancel, or retry rollouts
- 10 Show logs from the deployment process
- 11 Revert part of an application back to a previous deployment
- 12 Create a new build configuration
- 13 Cancel a pending or running build
- 14 Imports images from a container image registry
- 15 Change the number of pods in a deployment
- 16 Tag existing images into image streams
- 17 Display one or many resources
- 18 Show details of a specific resource
- 19 Edit a resource on the server
- 20 Update the environment on a resource with a pod template
- 21 Update volume on a resource with a pod template
- 22 Update the labels on a resource
- 23 Expose a replicated application as a service or route
- 24 Autoscale an application
- 25 Securely expose containers via a route
- 26 Securely expose containers via a route
- 27 Delete a resource by filename, stdin, resource and ID, or by resources and label selector.
- 28 Print the logs for a container in a pod.
- 29 Execute a command in a container.
- 30 Open a remote shell session to a container. It will default to the first container if none is specified.
- 31 Forward one or more local ports to a pod.
- 32 Run a proxy to the Kubernetes API server
- 33 Create a resource by filename or stdin
- 34 Update a resource by filename or stdin.
- 35 Process a template into list of resources
- 36 Create a new secret based on a key file or on files within a directory
- 37 End the current server session
- 38 Display merged kubeconfig settings or a specified kubeconfig file.
- 39 Set a cluster entry in kubeconfig
- 40 Set a user entry in kubeconfig
- 41 Set a context entry in kubeconfig
- 42 Change configuration files for the client
An introduction to the concepts and types in OpenShift
// View all projects you have access to $ oc projects
// See a list of all services in the current project $ oc get svc
// Describe a deployment configuration in detail $ oc describe dc mydeploymentconfig
// Show the images tagged into an image stream $ oc describe is ruby-centos7
Log in to an OpenShift server
// Log in interactively $ oc login
// Log in to the given server with the given certificate authority file $ oc login localhost:8443 --certificate-authority=/path/to/cert.crt
// Log in to the given server with the given credentials (will not prompt interactively) $ oc login localhost:8443 --username=myuser --password=mypass
Request a new project
// Create a new project with minimal information $ oc new-project web-team-dev
// Create a new project with a display name and description $ oc new-project web-team-dev --display-name="Web Team Development" --description="Development project for the web team."
Create a new application
// Create an application based on the source code in the current git repository (with a public remote) and a container image $ oc new-app . --docker-image=repo/langimage
// Create a Ruby application based on the provided [image]~[source code] combination $ oc new-app openshift/ruby-20-centos7~https://github.com/openshift/ruby-hello-world.git
// Use the public Docker Hub MySQL image to create an app. Generated artifacts will be labeled with db=mysql $ oc new-app mysql -l db=mysql
// Use a MySQL image in a private registry to create an app and override application artifacts' names $ oc new-app --docker-image=myregistry.com/mycompany/mysql --name=private
// Create an application from a remote repository using its beta4 branch $ oc new-app https://github.com/openshift/ruby-hello-world#beta4
// Create an application based on a stored template, explicitly setting a parameter value $ oc new-app --template=ruby-helloworld-sample --param=MYSQL_USER=admin
// Create an application from a remote repository and specify a context directory $ oc new-app https://github.com/youruser/yourgitrepo --context-dir=src/build
// Create an application based on a template file, explicitly setting a parameter value $ oc new-app --file=./example/myapp/template.json --param=MYSQL_USER=admin
Show an overview of the current project
// Show an overview of the current project $ oc status
Switch to another project
// Switch to 'myapp' project $ oc project myapp
// Display the project currently in use $ oc project
Starts a new build
// Starts build from build config matching the name "golang" $ oc start-build golang
// Starts build from build matching the name "golang-1" $ oc start-build --from-build=golang-1
// Starts build from build config matching the name "golang" and watches the logs until the build // completes or fails $ oc start-build golang --follow
Show container logs from the build container
// Stream logs from the latest build for build config "golang" to stdout $ oc logs -f bc/golang // Stream logs from the first build for build config "golang" to stdout $ oc logs -f --version=1 bc/golang // Stream logs from build "golang-1" to stdout $ oc logs -f build/golang-1
View, start, cancel, or retry rollouts
// Display information for the 'database' DeploymentConfig $ oc describe dc/database
// Start a new deployment based on the 'database' DeploymentConfig $ oc rollout latest dc/database
// View the status of the latest rollout $ oc rollout status dc/database
// View the history of the 'database' DeploymentConfig $ oc rollout history dc/database
Show logs from the deployment process
// Stream logs from the latest deployment for deployment config "frontend" to stdout // Note that if the deployment process is successful, logs from the application will // be returned. $ oc logs -f dc/frontend // Return the logs from the first deployment for deployment config "database" to stdout // Note that if the deployment process was successful, logs from the application will // be returned. $ oc logs --version=1 dc/database
Revert part of an application back to a previous deployment
// Perform a rollback $ oc rollout undo dc/foo
// See what the rollback will look like, but don't perform the rollback $ oc rollout undo dc/foo --dry-run
// Perform a rollback to a specific revision $ oc rollout undo dc/foo --to-revision=2
Create a new build configuration
// Create a build config based on the source code in the current git repository (with a public remote) and a container image $ oc new-build . --docker-image=repo/langimage
// Create a NodeJS build config based on the provided [image]~[source code] combination $ oc new-build openshift/nodejs-010-centos7~https://bitbucket.com/user/nodejs-app
// Create a build config from a remote repository using its beta2 branch $ oc new-build https://github.com/openshift/ruby-hello-world#beta2
Cancel a pending or running build
// Cancel the build with the given name $ oc cancel-build 1da32cvq
// Cancel the named build and print the build logs $ oc cancel-build 1da32cvq --dump-logs
// Cancel the named build and create a new one with the same parameters $ oc cancel-build 1da32cvq --restart
Imports images from a container image registry
$ oc import-image mystream
Change the number of pods in a deployment
// Scale replication controller named 'foo' to 3. $ oc scale --replicas=3 replicationcontrollers foo
// If the replication controller named foo's current size is 2, scale foo to 3. $ oc scale --current-replicas=2 --replicas=3 replicationcontrollers foo
Tag existing images into image streams
// Tag the current image for the image stream 'openshift/ruby' and tag '2.0' into the image stream 'yourproject/ruby with tag 'tip'. $ oc tag openshift/ruby:2.0 yourproject/ruby:tip
// Tag a specific image. $ oc tag openshift/ruby@sha256:6b646fa6bf5e5e4c7fa41056c27910e679c03ebe7f93e361e6515a9da7e258cc yourproject/ruby:tip
// Tag an external container image. $ oc tag --source=docker openshift/origin:latest yourproject/ruby:tip
// Remove the specified spec tag from an image stream. $ openshift cli tag openshift/origin:latest -d
Display one or many resources
// List all pods in ps output format. $ oc get pods
// List a single replication controller with specified ID in ps output format. $ oc get replicationController 1234-56-7890-234234-456456
// List a single pod in JSON output format. $ oc get -o json pod 1234-56-7890-234234-456456
// Return only the status value of the specified pod. $ oc get -o template pod 1234-56-7890-234234-456456 --template=Template:.currentState.status
Show details of a specific resource
// Provide details about the ruby-20-centos7 image repository $ oc describe imageRepository ruby-20-centos7
// Provide details about the ruby-sample-build build configuration $ oc describe bc ruby-sample-build
Edit a resource on the server
// Edit the service named 'docker-registry': $ oc edit svc/docker-registry
// Edit the DeploymentConfig named 'my-deployment': $ oc edit dc/my-deployment
// Use an alternative editor $ OC_EDITOR="nano" oc edit dc/my-deployment
// Edit the service 'docker-registry' in JSON using the v1beta3 API format: $ oc edit svc/docker-registry --output-version=v1beta3 -o json
Update the environment on a resource with a pod template
// Update deployment 'registry' with a new environment variable $ oc set env dc/registry STORAGE_DIR=/local
// List the environment variables defined on a deployment 'registry' $ oc set env dc/registry --list
// List the environment variables defined on all pods $ oc set env pods --all --list
// Output a YAML object with updated environment for deployment 'registry' // Does not alter the object on the server $ oc set env dc/registry STORAGE_DIR=/local -o yaml
// Update all replication controllers in the project to have ENV=prod $ oc set env replicationControllers --all ENV=prod
// Remove the environment variable ENV from all deployment configs $ oc set env deploymentConfigs --all ENV-
// Remove the environment variable ENV from a pod definition on disk and update the pod on the server $ oc set env -f pod.json ENV-
// Set some of the local shell environment into a deployment on the server $ env | grep RAILS_ | oc set env -e - dc/registry
Update volume on a resource with a pod template
// Add new volume of type 'emptyDir' for deployment config 'registry' and mount under /opt inside the containers // The volume name is auto generated $ oc set volume dc/registry --add --mount-path=/opt
// Add new volume 'v1' with secret 'magic' for pod 'p1' $ oc set volume pod/p1 --add --name=v1 -m /etc --type=secret --secret-name=magic
// Add new volume to pod 'p1' based on gitRepo (or other volume sources not supported by --type) $ oc set volume pod/p1 --add -m /repo --source=<json-string>
// Add emptyDir volume 'v1' to a pod definition on disk and update the pod on the server $ oc set volume -f pod.json --add --name=v1
// Create a new persistent volume and overwrite existing volume 'v1' for replication controller 'r1' $ oc set volume rc/r1 --add --name=v1 -t persistentVolumeClaim --claim-name=pvc1 --overwrite
// Change pod 'p1' mount point to /data for volume v1 $ oc set volume pod p1 --add --name=v1 -m /data --overwrite
// Remove all volumes for pod 'p1' $ oc set volume pod/p1 --remove --confirm
// Remove volume 'v1' from deployment config 'registry' $ oc set volume dc/registry --remove --name=v1
// Unmount volume v1 from container c1 on pod p1 and remove the volume v1 if it is not referenced by any containers on pod p1 $ oc set volume pod/p1 --remove --name=v1 --containers=c1
// List volumes defined on replication controller 'r1' $ oc set volume rc r1 --list
// List volumes defined on all pods $ oc set volume pods --all --list
// Output json object with volume info for pod 'p1' but don't alter the object on server $ oc set volume pod/p1 --add --name=v1 --mount=/opt -o json
Update the labels on a resource
// Update pod 'foo' with the label 'unhealthy' and the value 'true'. $ oc label pods foo unhealthy=true
// Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value. $ oc label --overwrite pods foo status=unhealthy
// Update all pods in the namespace $ oc label pods --all status=unhealthy
// Update pod 'foo' only if the resource is unchanged from version 1. $ oc label pods foo status=unhealthy --resource-version=1
// Update pod 'foo' by removing a label named 'bar' if it exists. // Does not require the --overwrite flag. $ oc label pods foo bar-
Expose a replicated application as a service or route
// Create a route based on service ruby-hello-world. The new route will re-use the ruby-hello-world labels $ oc expose svc/ruby-hello-world
// Create a route and specify your own label and route name $ oc expose svc/ruby-hello-world -l name=myroute --name=fromdowntown
ifdef::openshift-enterprise,openshift-origin,openshift-dedicated[]
// Create a route and specify a hostname $ oc expose svc/ruby-hello-world --hostname=www.example.com
endif::[]
// Expose a deployment configuration as a service and use the specified port $ oc expose dc/ruby-hello-world --port=8080
Autoscale an application
// Auto scale a deployment config "foo", with the number of pods between 2 to 10, target CPU utilization at a default value that server applies $ oc autoscale dc/foo --min=2 --max=10
// Auto scale a replication controller "foo", with the number of pods between 1 to 5, target CPU utilization at 80% $ oc autoscale rc/foo --max=5 --cpu-percent=80
ifdef::openshift-enterprise,openshift-origin,openshift-dedicated[]
Securely expose containers via a route
// Create a secure edge-terminated route using the specified
// certificates/keys and hostname. If the certificates/keys
// are not specified, those from the router will be re-used.
$ oc create route edge --service=frontend \
--cert=${MASTER_CONFIG_DIR}/ca.crt \
--key=${MASTER_CONFIG_DIR}/ca.key \
--ca-cert=${MASTER_CONFIG_DIR}/ca.crt \
--hostname=www.example.com
// Create a secure passthrough route. $ oc create route passthrough --service=registry
// Create a secure reencrypt-terminated route in // a similar fashion to edge. The only additional // requirement is to specify a destination CA certificate. $ oc create route reencrypt --service=backend --dest-ca-cert=ca.crt
endif::[]
ifdef::openshift-online[]
Securely expose containers via a route
// Create a secure edge-terminated route. $ oc create route edge --service=frontend
// Create a secure passthrough route. oc create route passthrough --service=frontend
endif::[]
Delete a resource by filename, stdin, resource and ID, or by resources and label selector.
// Delete a pod using the type and ID specified in pod.json. $ oc delete -f pod.json
// Delete a pod based on the type and ID in the JSON passed into stdin. $ cat pod.json | oc delete -f -
// Delete pods and services with label name=myLabel. $ oc delete pods,services -l name=myLabel
// Delete a pod with ID 1234-56-7890-234234-456456. $ oc delete pod 1234-56-7890-234234-456456
// Delete all pods $ oc delete pods --all
Print the logs for a container in a pod.
// Returns snapshot of ruby-container logs from pod backend. $ oc logs backend -c ruby-container
// Starts streaming of ruby-container logs from pod backend. // Both NAME and TYPE/NAME syntax are supported for pods. $ oc logs -f pod/backend -c ruby-container
Execute a command in a container.
// Get output from running 'date' in ruby-container from pod 123456-7890 $ oc exec -p 123456-7890 -c ruby-container date
// Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-780 and sends stdout/stderr from 'bash' back to the client $ oc exec -p 123456-7890 -c ruby-container -i -t -- bash -il
Open a remote shell session to a container. It will default to the first container if none is specified.
// Open a shell session on the first container in pod 123456-7890 $ oc rsh 123456-7890
// Run the command 'cat /etc/resolv.conf' inside pod 123456-7890 $ oc rsh 123456-7890 cat /etc/resolv.conf
Forward one or more local ports to a pod.
// Listens on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod $ oc port-forward -p mypod 5000 6000
// Listens on port 8888 locally, forwarding to 5000 in the pod $ oc port-forward -p mypod 8888:5000
// Listens on a random port locally, forwarding to 5000 in the pod $ oc port-forward -p mypod :5000
// Listens on a random port locally, forwarding to 5000 in the pod $ oc port-forward -p mypod 0:5000
Run a proxy to the Kubernetes API server
// Run a proxy to Kubernetes apiserver on port 8011, serving static content from ./local/www/ $ oc proxy --port=8011 --www=./local/www/
// Run a proxy to Kubernetes apiserver, changing the api prefix to k8s-api // This makes e.g. the pods api available at localhost:8011/k8s-api/v1beta3/pods/ $ oc proxy --api-prefix=k8s-api
Create a resource by filename or stdin
// Create a pod using the data in pod.json. $ oc create -f pod.json
// Create a pod based on the JSON passed into stdin. $ cat pod.json | oc create -f -
Update a resource by filename or stdin.
// Update a pod using the data in pod.json. $ oc replace -f pod.json
// Update a pod based on the JSON passed into stdin. $ cat pod.json | oc replace -f -
Process a template into list of resources
// Convert template.json file into resource list $ oc process -f template.json
// Process template while passing a user-defined label $ oc process -f template.json -l name=mytemplate
// Convert stored template into resource list $ oc process foo
// Convert template.json into resource list $ cat template.json | oc process -f -
== Export resources so they can be used elsewhere==[NOTE] `oc export` is deprecated. See link:https://docs.openshift.com/container-platform/3.11/release_notes/ocp_3_11_release_notes.html#ocp-311-notable-technical-changes[Notable Technical Changes] for more details.
// export the services and deployment configurations labeled name=test oc get -o yaml --export svc,dc -l name=test
// export to JSON oc get -o json --export service
Create a new secret based on a key file or on files within a directory
[ons="nowrap"]
// Create a new secret named my-secret with a key named ssh-privatekey $ oc create secret generic my-secret --from-file=ssh-privatekey=<path/to/ssh/private/key>
// Create a new secret named my-secret with keys named ssh-privatekey and ssh-publickey instead of the names of the keys on disk $ oc create secret generic my-secret --from-file=ssh-privatekey=<path/to/ssh/private/key> --from-file=ssh-publickey=<path/to/ssh/public/key>
// Create a new secret named my-secret with keys for each file in the folder "bar" $ oc create secret generic my-secret --from-file=<path/to/bar>
End the current server session
// Logout $ oc logout
Display merged kubeconfig settings or a specified kubeconfig file.
// Show Merged kubeconfig settings. $ oc config view
// Get the password for the e2e user $ oc config view -o template --template='Template:Range .usersTemplate:If eq .name "e2e"Template:Index .user.passwordTemplate:EndTemplate:End'
Set a cluster entry in kubeconfig
// Set only the server field on the e2e cluster entry without touching other values. $ oc config set-cluster e2e --server=https://1.2.3.4
// Embed certificate authority data for the e2e cluster entry $ oc config set-cluster e2e --certificate-authority=~/.kube/e2e/kubernetes.ca.crt
// Disable cert checking for the dev cluster entry $ oc config set-cluster e2e --insecure-skip-tls-verify=true
Set a user entry in kubeconfig
// Set only the "client-key" field on the "cluster-admin" // entry, without touching other values: $ oc config set-credentials cluster-admin --client-key=~/.kube/admin.key
// Set basic auth for the "cluster-admin" entry $ oc config set-credentials cluster-admin --username=admin --password=uXFGweU9l35qcif
// Embed client certificate data in the "cluster-admin" entry $ oc config set-credentials cluster-admin --client-certificate=~/.kube/admin.crt --embed-certs=true
Set a context entry in kubeconfig
// Set the user field on the gce context entry without touching other values $ oc config set-context gce --user=cluster-admin
Change configuration files for the client
// Change the config context to use oc config use-context my-context
// Set the value of a config preference oc config set preferences.some true