2016年12月19日星期一

OpenShift_016:如何发布定制的 builder image ?

环境:OSE 3.0.1

假设你已经有了一个定制的 go builder image,怎么把它发布到 OpenShift 上,让别人可以使用呢?

1. 把 tar 文件导入为镜像
[student@workstation ~]$ sudo docker load -i customizing-go/go-rhel7.tar.gz
[student@workstation ~]$ sudo docker images | grep go-rhel7
openshift3/go-rhel7   v1.0                4834dc05f775        14 months ago       502.9 MB

2. 把 image 打上 tag,并放入私有仓库
[student@workstation ~]$ sudo docker tag openshift3/go-rhel7:v1.0 workstation.pod0.example.com:5000/openshift3/go-rhel7:v1.0
[student@workstation ~]$ sudo docker push workstation.pod0.example.com:5000/openshift3/go-rhel7:v1.0

3. 创建 image stream
[root@master ~]# oc login -u system:admin
[root@master ~]# oc create -f go-is.json -n openshift
go-is.json 内容如下:
{

    "kind":"ImageStream",
    "apiVersion":"v1",
    "metadata":{
        "name":"go",
        "creationTimestamp":null,
        "annotations":{
            "openshift.io/image.dockerRepositoryCheck":"2015-08-24T15:16:26Z"
        }
    },
    "spec":{
        "dockerImageRepository":"workstation.pod0.example.com:5000/openshift3/go-rhel7",
        "tags":[
            {
                "name":"v1.0"
            }
        ]
    },
    "status":{
        "dockerImageRepository":""
    }

}

[root@master ~]# oc get is -n openshift | grep go-thel7
输出如下:
go                                   workstation.pod0.example.com:5000/openshift3/go-rhel7
 
发现 TAGS 一栏是空的,需要把 image 的 tag 导入到 image stream 中。
[root@master ~]# oc import-image go -n openshift
输出如下:
Waiting for the import to complete, CTRL+C to stop waiting.
The import completed successfully.

Name:            go
Created:        17 seconds ago
Labels:           
Annotations:        openshift.io/image.dockerRepositoryCheck=2016-12-19T03:22:59Z
Docker Pull Spec:    workstation.pod0.example.com:5000/openshift3/go-rhel7

Tag    Spec    Created            PullSpec                            Image
v1.0        Less than a second ago    workstation.pod0.example.com:5000/openshift3/go-rhel7:v1.0    4834dc05f7753ee9df37b6e7b78ca6b12b0e88cb0b12e36f23e560211ce35cc5


[root@master ~]# oc get is -n openshift | grep go-thel7
输出如下:
go                                   workstation.pod0.example.com:5000/openshift3/go-rhel7  v1.0
这次 TAGS 栏有了版本号。

4. 为新的 builder image 创建新的 template,这样让用户使用起来更方便
[root@master ~]# oc create -f go-template.json -n openshift
go-template.json 内容如下:

{
    "kind": "Template",
    "apiVersion": "v1",
    "metadata": {
        "name": "go-sti",
        "creationTimestamp": null
    },
    "objects": [
        {
            "apiVersion": "v1",
            "kind": "Service",
            "metadata": {
                "annotations": {
                    "description": "The web server's http port."
                },
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            },
            "spec": {
                "ports": [
                    {
                        "port": 8080,
                        "targetPort": 8080
                    }
                ],
                "selector": {
                    "deploymentConfig": "${APPLICATION_NAME}"
                }
            }
        },
        {
            "apiVersion": "v1",
            "id": "${APPLICATION_NAME}-http-route",
            "kind": "Route",
            "metadata": {
                "annotations": {
                    "description": "Route for application's http service."
                },
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}-http-route"
            },
            "spec": {
                "host": "${APPLICATION_HOSTNAME}",
                "to": {
                    "name": "${APPLICATION_NAME}"
                }
            }
        },
        {
            "apiVersion": "v1",
            "kind": "ImageStream",
            "metadata": {
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            }
        },
        {
            "apiVersion": "v1",
            "kind": "BuildConfig",
            "metadata": {
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            },
            "spec": {
                "env": [
                    {
                        "name": "GO_MAIN",
                        "value": "${GO_MAIN}"
                    },
                    {
                        "name": "DOUGLAS",
                        "value": "${DOUGLAS}"
                    }
                ],
                "output": {
                    "to": {
                        "name": "${APPLICATION_NAME}"
                    }
                },
                "source": {
                    "contextDir": "${GIT_CONTEXT_DIR}",
                    "git": {
                        "ref": "${GIT_REF}",
                        "uri": "${GIT_URI}"
                    },
                    "type": "Git"
                },
                "strategy": {
                    "sourceStrategy": {
                        "from": {
                            "kind": "ImageStreamTag",
                            "name": "go:v1.0",
                            "namespace": "openshift"
                        }
                    },
                    "type": "Source"
                },
                "triggers": [
                    {
                        "github": {
                            "secret": "${GITHUB_TRIGGER_SECRET}"
                        },
                        "type": "github"
                    },
                    {
                        "generic": {
                            "secret": "${GENERIC_TRIGGER_SECRET}"
                        },
                        "type": "generic"
                    },
                    {
                        "imageChange": {},
                        "type": "imageChange"
                    }
                ]
            }
        },
        {
            "apiVersion": "v1",
            "kind": "DeploymentConfig",
            "metadata": {
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            },
            "spec": {
                "replicas": 1,
                "selector": {
                    "deploymentConfig": "${APPLICATION_NAME}"
                },
                "strategy": {
                    "type": "Recreate"
                },
                "template": {
                    "metadata": {
                        "labels": {
                            "application": "${APPLICATION_NAME}",
                            "deploymentConfig": "${APPLICATION_NAME}"
                        },
                        "name": "${APPLICATION_NAME}"
                    },
                    "spec": {
                        "containers": [
                            {
                                "env": [],
                                "image": "${APPLICATION_NAME}",
                                "imagePullPolicy": "Always",
                                "name": "${APPLICATION_NAME}",
                                "ports": [
                                    {
                                        "containerPort": 8080
                                    }
                                ]
                            }
                        ]
                    }
                },
                "triggers": [
                    {
                        "imageChangeParams": {
                            "automatic": true,
                            "containerNames": [
                                "${APPLICATION_NAME}"
                            ],
                            "from": {
                                "kind": "ImageStream",
                                "name": "${APPLICATION_NAME}"
                            }
                        },
                        "type": "ImageChange"
                    }
                ]
            }
        }
    ],
    "parameters": [
        {
            "name": "APPLICATION_NAME",
            "description": "Application NAME"
        },
        {
            "name": "GIT_URI",
            "description": "GIT URI"
        },
        {
            "name": "GIT_REF",
            "description": "Git branch"
        },
        {
            "name": "GO_MAIN",
            "description": "Main GO package"
        },
        {
            "name": "GIT_CONTEXT_DIR",
            "description": "Path within Git project to build; empty for root project directory."
        },
        {
            "name": "GITHUB_TRIGGER_SECRET",
            "description": "Github trigger secret",
            "generate": "expression",
            "from": "[a-zA-Z0-9]{8}"
        },
        {
            "name": "GENERIC_TRIGGER_SECRET",
            "description": "Generic build trigger secret",
            "generate": "expression",
            "from": "[a-zA-Z0-9]{8}"
        },
        {
            "name": "APPLICATION_HOSTNAME",
            "description": "Application hostname"
        }
    ],
    "labels": {
        "template": "go-sti"
    }
}

5. 创建一个 project 使用该 template
[student@workstation ~]$ oc login -u student -p redhat

[student@workstation ~]$ oc new-project go

在控制台,点击 show all templates,找到 go-sti,并点击,输入如下参数:

点击 Create,剩下的就是等待 build 和 deploy 啦。

没有评论: