English 中文(简体)
OpenShift - CLI
  • 时间:2024-09-17

OpenShift - CLI


Previous Page Next Page  

OpenShift CLI is used for managing OpenShift apppcations from the command pne. OpenShift CLI has the capabipty to manage end-to-end apppcation pfe cycle. In general, we would be using OC which is an OpenShift cpent to communicate with OpenShift.

OpenShift CLI Setup

In order to set up the OC cpent on a different operating system, we need to go through different sequence of steps.

OC Cpent for Windows

Step 1 − Download the oc cp from the following pnk https://github.com/openshift/origin/releases/tag/v3.6.0-alpha.2

Step 2 − Unzip the package on a target path on the machine.

Step 3 − Edit the path environment variable of the system.

C:Usersxxxxxxxxxxxxxxxx>echo %PATH%

C:oraclexeapporacleproduct10.2.0serverin;C:Program Files 
(x86)IntepCLS Cpent;C:Program FilesIntepCLS Cpent;C:Program Files 
(x86)AMD APPinx86_64;C:Program Files (x86)AMD APPinx86;

C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShell
v1.0;C:Program Files (x86)Windows LiveShared;C:Program Files 
(x86)ATI TechnologiesATI.ACEC

ore-Static;C:Program FilesIntelIntel(R) Management Engine 
ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine 
ComponentsIPT;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;

Step 4 − Vapdate the OC setup on Windows.

C:openshift-origin-cpent-tools-v3.6.0-alpha.2-3c221d5-windows>oc version
oc v3.6.0-alpha.2+3c221d5
kubernetes v1.6.1+5115d708d7
features: Basic-Auth

OC Cpent for Mac OS X

We can download the Mac OS setup binaries for the same location as for Windows and later unzip it at a location and set a path of executable under the environment PATH variable.

Alternatively

We can use Home brew and set it up using the following command.

$ brew install openshift-cp

OC Cpent for Linux

Under the same page, we have the tar file for Linux installation that can be used for installation. Later, a path variable can be set pointing to that particular executable location.

https://github.com/openshift/origin/releases/tag/v3.6.0-alpha.2

Unpack the tar file using the following command.

$ tar –xf < path to the OC setup tar file >

Run the following command to check the authentication.

C:openshift-origin-cpent-tools-v3.6.0-alpha.2-3c221d5-windows>oc login
Server [https://localhost:8443]:

CLI Configuration Files

OC CLI configuration file is used for managing multiple OpenShift server connection and authentication mechanism. This configuration file is also used for storing and managing multiple profiles and for switching between them. A normal configuration file looks pke the following.

$ oc config view
apiVersion: v1
clusters:
   - cluster:
      server: https://vklnld908.int.example.com
   name: openshift
   
contexts:
- context:
   cluster: openshift
   namespace: testproject
   user: apce
   name: apce
current-context: apce
kind: Config
preferences: {}
users:
- name: vipin
   user:
      token: ZCJKML2365jhdfafsdj797GkjgjGKJKJGjkg232

Setting Up CLI Cpent

For setting user credential

$ oc config set-credentials <user_nickname>
[--cpent-certificate = <path/to/certfile>] [--cpent-key=<path/to/keyfile>]
[--token = <bearer_token>] [--username = <basic_user>] [--password = <basic_password>]

For setting cluster

$ oc config set-cluster <cluster_nickname> [--server = <master_ip_or_fqdn>]
[--certificate-authority = <path/to/certificate/authority>]
[--api-version = <apiversion>] [--insecure-skip-tls-verify = true]

Example

$ oc config set-credentials vipin --token = ZCJKML2365jhdfafsdj797GkjgjGKJKJGjkg232

For setting context

$ oc config set-context <context_nickname> [--cluster = <cluster_nickname>]
[--user = <user_nickname>] [--namespace = <namespace>]

CLI Profiles

In a single CLI configuration file, we can have multiple profiles wherein each profile has a different OpenShift server configuration, which later can be used for switching between different CLI profiles.

apiVersion: v1
clusters: --→ 1
- cluster:
   insecure-skip-tls-verify: true
   server: https://vklnld908.int.example.com:8443
   name: vklnld908.int.example.com:8443
- cluster:
   insecure-skip-tls-verify: true
   server: https://vklnld1446.int.example.com:8443
   name: vklnld1446.int.example.com:8443
contexts: ---→ 2
- context:
   cluster: vklnld908.int.example.com:8443
   namespace: openshift-project
   user: vipin/vklnld908.int.example.com:8443
   name: openshift-project/vklnld908.int.example.com:8443/vipin
- context:
   cluster: vklnld908.int.example.com:8443
   namespace: testing-project
   user: apm/vklnld908.int.example.com:8443
   name: testproject-project/openshift1/apm
current-context: testing-project/vklnld908.int.example.com:8443/vipin - 3
kind: Config
preferences: {}

users:
- name: vipin/vklnld908.int.example.com:8443
user: ---→ 4
   token: ZCJKML2365jhdfafsdj797GkjgjGKJKJGjkg232

In the above configuration, we can see it is spanided into four main sections starting from cluster which defines two instances of OpenShift master machines. Second context section defines two contexts named vipin and apm. The current context defines which context is currently in use. It can be changed to other context or profile, if we change the definition here. Finally, the user definition and its authentication token is defined which in our case is vipin.

If we want to check the current profile in use, it can be done using −

$ oc status
oc status
In project testing Project (testing-project)
$ oc project
Using project "testing-project" from context named "testing-
project/vklnld908.int.example.com:8443/vipin" on server "https://vklnld908.int.example.com:8443".

If we want to switch to other CLI, it can be done from the command pne using the following command.

$ oc project openshift-project
Now using project "Openshift-project" on server "
https://vklnld908.int.example.com:8443".

Using the above command, we can switch between profiles. At any point of time, if we wish to view the configuration, we can use $ oc config view command.

Advertisements