oc命令

我们已经使用二进制包手工安装了OpenShift,并且使用OpenShift的Web控制台创建了一个项目。 实际上OpenShift还提供了一系列命令行工具,oc命令就是其中的一个,下面我们一起熟悉一下这个命令。

查看OpenShift集群版本信息:

1
2
3
4
5
6
7
8
oc version
oc v3.6.1+008f2d5
kubernetes v1.6.1+5115d708d7
features: Basic-Auth GSSAPI Kerberos SPNEGO

Server https://192.168.61.134:8443
openshift v3.6.1+008f2d5
kubernetes v1.6.1+5115d708d7

使用oc命令前需要先登录:

1
2
3
4
5
6
7
8
9
oc login -u dev https://192.168.61.134:8443
Authentication required for https://192.168.61.134:8443 (openshift)
Username: dev
Password:
Login successful.

You have one project on this server: "prj1"

Using project "prj1".

使用oc命令创建一个新项目prj2:

1
2
3
4
5
6
7
8
oc new-project prj2
Now using project "prj2" on server "https://192.168.61.134:8443".

You can add applications to this project with the 'new-app' command. For example, try:

    oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git

to build a new example application in Ruby.

切换到prj2项目:

1
2
oc project prj2
Already on project "prj2" on server "https://192.168.61.134:8443".

往项目中添加应用:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
oc new-app openshift/hello-openshift
--> Found Docker image 3533a24 (2 days old) from Docker Hub for "openshift/hello-openshift"

    * An image stream will be created as "hello-openshift:latest" that will track this image
    * This image will be deployed in deployment config "hello-openshift"
    * Ports 8080/tcp, 8888/tcp will be load balanced by service "hello-openshift"
      * Other containers can access this service through the hostname "hello-openshift"
    * WARNING: Image "openshift/hello-openshift" runs as the 'root' user which may not be permitted by your cluster administrator

--> Creating resources ...
    imagestream "hello-openshift" created
    deploymentconfig "hello-openshift" created
    service "hello-openshift" created
--> Success
    WARNING: No Docker registry has been configured with the server. Automatic builds and deployments may not function.
    Run 'oc status' to view your app.

可以看到上面的命令输出实际上应该是在Kubernetes上创建了hello-openshift的Deployment和Service。 在添加过程中会下载Docker镜像,如果网络比较慢,可以提前pull好:

1
2
docker pull openshift/origin-pod:v3.6.1
docker pull openshift/origin-deployer:v3.6.1

查看部署的应用:

1
2
3
4
5
6
7
8
oc status
In project prj2 on server https://192.168.61.134:8443

svc/hello-openshift - 172.30.158.204 ports 8080, 8888
  dc/hello-openshift deploys istag/hello-openshift:latest
    deployment #1 deployed 10 minutes ago - 1 pod

View details with 'oc describe <resource>/<name>' or list everything with 'oc get all'.

如果对Kubernetes比较熟悉的话,下面的oc命令应该不用解释了:

1
2
3
4
5
6
7
oc get pod
NAME                      READY     STATUS    RESTARTS   AGE
hello-openshift-1-x6m7b   1/1       Running   0          1m

oc get svc
NAME              CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGE
hello-openshift   172.30.158.204   <none>        8080/TCP,8888/TCP   11m

退出登录:

1
2
oc logout
Logged "dev" out on "https://192.168.61.134:8443"