OpenShift - 命令列介面 (CLI)



OpenShift 命令列介面 (CLI) 用於從命令列管理 OpenShift 應用。OpenShift CLI 能夠管理端到端的應用程式生命週期。通常,我們會使用 OC(OpenShift 客戶端)與 OpenShift 通訊。

OpenShift CLI 設定

為了在不同的作業系統上設定 OC 客戶端,我們需要執行不同的步驟序列。

Windows 版 OC 客戶端

步驟 1 - 從以下連結下載 oc cli https://github.com/openshift/origin/releases/tag/v3.6.0-alpha.2

步驟 2 - 將壓縮包解壓到機器上的目標路徑。

步驟 3 - 編輯系統的 PATH 環境變數。

C:\Users\xxxxxxxx\xxxxxxxx>echo %PATH%

C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Program Files 
(x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Program Files 
(x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\
v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files 
(x86)\ATI Technologies\ATI.ACE\C

ore-Static;C:\Program Files\Intel\Intel(R) Management Engine 
Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine 
Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;

步驟 4 - 驗證 Windows 上的 OC 設定。

C:\openshift-origin-client-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

Mac OS X 版 OC 客戶端

我們可以從與 Windows 相同的位置下載 Mac OS 設定二進位制檔案,然後將其解壓到某個位置,並將可執行檔案的路徑設定到環境 PATH 變數中。

或者

我們可以使用 Homebrew 並使用以下命令進行設定。

$ brew install openshift-cli

Linux 版 OC 客戶端

在同一頁面上,我們有用於 Linux 安裝的 tar 檔案,可用於安裝。之後,可以設定一個指向該特定可執行檔案位置的路徑變數。

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

使用以下命令解壓 tar 檔案。

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

執行以下命令以檢查身份驗證。

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

CLI 配置檔案

OC CLI 配置檔案用於管理多個 OpenShift 伺服器連線和身份驗證機制。此配置檔案還用於儲存和管理多個配置檔案以及在它們之間切換。普通的配置檔案如下所示。

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

設定 CLI 客戶端

設定使用者憑據

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

設定叢集

$ 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]

示例

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

設定上下文

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

CLI 配置檔案

在一個 CLI 配置檔案中,我們可以有多個配置檔案,每個配置檔案都有不同的 OpenShift 伺服器配置,之後可以用來在不同的 CLI 配置檔案之間切換。

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: alim/vklnld908.int.example.com:8443
   name: testproject-project/openshift1/alim
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

在上面的配置中,我們可以看到它分為四個主要部分,從定義 OpenShift 主機的兩個例項的 cluster 部分開始。第二個 context 部分定義了名為 vipin 和 alim 的兩個上下文。current-context 部分定義了當前正在使用的上下文。如果我們在這裡更改定義,可以將其更改為其他上下文或配置檔案。最後,定義了使用者定義及其身份驗證令牌,在本例中為 vipin。

如果要檢查當前正在使用的配置檔案,可以使用以下命令:

$ 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".

如果要切換到其他 CLI,可以使用以下命令從命令列進行切換。

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

使用上述命令,我們可以在配置檔案之間切換。在任何時候,如果我們希望檢視配置,我們可以使用 `$ oc config view` 命令。

廣告
© . All rights reserved.