天天看點

openshift 學習筆記-5 template使用

建立微服務 new-app

[email protected]:~$ oc new-project myci
Already on project "myci" on server "https://openshift-cluster.example.com:8443".

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

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

to build a new example application in Ruby.

[email protected]:~$ oc get project
NAME      DISPLAY NAME   STATUS
myci                     Active

[email protected]:~$ oc new-app golang-builder:~http:///liujunbang/mybank.git --name=test-hello
--> Found image 3de3f0e (3 weeks old) in image stream "openshift/golang-builder" under tag "1.01" for "golang-builder:1.01"

    builder golang  
    ------------------ 
    Platform for building golang

    Tags: builder, golang

    * A source build using source code from http:///liujunbang/mybank.git will be created
      * The resulting image will be pushed to image stream "test-hello:latest"
      * Use 'start-build' to trigger a new build
    * This image will be deployed in deployment config "test-hello"
    * The image does not expose any ports - if you want to load balance or send traffic to this component
      you will need to create a service with 'expose dc/test-hello --port=[port]' later

--> Creating resources ...
    imagestream "test-hello" created
    buildconfig "test-hello" created
    deploymentconfig "test-hello" created
--> Success
    Build scheduled, use 'oc logs -f bc/test-hello' to track its progress.
    Run 'oc status' to view your app.
           

應用建立成功。

1、順便驗證一下service 的功能,此時build完成,部署完成,微服務成功啟動,但是沒有建立service。此時進入啟動的為服務容器内部,env檢查環境變量,肯定沒有service的資訊,因為沒有建立。

2、建立service

oc create -f service.yaml

yaml檔案内容如下:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: test-svc
  name: test-svc
  namespace: myci
spec:
  ports:
    - name: test-ci
      port: 
      protocol: TCP
      targetPort: 
  selector:
    app: test-hello
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}
           

3、執行擴充,把pod數量擴充微兩個:

$ oc scale dc test-hello --replicas=
deploymentconfig "test-hello" scaled
           

4、進入容器内部,檢視兩個pod的環境變量:

第一個pod:

openshift 學習筆記-5 template使用

第二個pod:

openshift 學習筆記-5 template使用

我們發現,雖然建立了service,原來的pod裡面仍然沒有注入service的資訊,必須重新開機pod以後,service的環境變量資訊才能注入到pod中。

繼續往下template

導出模闆template

執行模闆導入指令:

oc export bc,dc,svc,is,route -o json –as-template=”test-template” > ~/test-template.yaml

本地生成yaml檔案:

test-template.yaml

導入模闆 create template

1、模闆建立

oc create -f test-template.yaml

2、在web console 中使用模闆建立項目,搜尋test-template模闆

openshift 學習筆記-5 template使用

3、建立

openshift 學習筆記-5 template使用

4、建立完成

openshift 學習筆記-5 template使用

5、檢視進度

openshift 學習筆記-5 template使用

6、完成後,通路測試微服務

繼續閱讀