openshift 容器云从入门到崩溃之五《部署应用》

1、配置部署模板

配置好用户权限之后就可以部署应用了oc常用的两种部署方式:

Deploy Image方式

优点:这种方式是最简单的部署方式,你只需要有一个容器镜像就行了或者公开的docker hub 镜像

缺点:但是这种方式的确定是不能随时变更,也不能提前定义其他配置

比较适合部署那些不经常变更的第三方服务

catalog方式(template)

优点:可以基于模板提前定义各种配置包括build、service、deployments等

缺点:一个类型的应用需要独立的模板

比较适合自己开发的应用使用,使用template可以省去上线新应用的时间

oc的catalog里面包括大量的模板常用的一些数据库一些开发环境都有

 

下面主要说一下自定义个模板怎么创建

 

# vim nginx-openresty.yaml

apiVersion: template.openshift.io/v1
kind: Template
metadata:
  annotations:
    description: OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。
    tags: nodejs
  creationTimestamp: 2018-10-10T07:17:57Z
  name: nginx-openresty
  namespace: openshift
  resourceVersion: "71699318"
  selfLink: /apis/template.openshift.io/v1/namespaces/openshift/templates/nginx-openresty
  uid: 9d1b5626-cc5c-11e8-a187-00163e0e008f
objects:
- apiVersion: v1
  kind: Route
  metadata:
    name: ${APP_NAME}
  spec:
    host: ${DOMAIN_NAME}
    to:
      kind: Service
      name: ${APP_NAME}
- apiVersion: v1
  kind: Service
  metadata:
    name: ${APP_NAME}
  spec:
    ports:
    - name: ${APP_NAME}
      port: 8080
      targetPort: 8080
    selector:
      name: ${APP_NAME}
    type: ClusterIP
- apiVersion: v1
  kind: ImageStream
  metadata:
    name: ${APP_NAME}
- apiVersion: v1
  kind: BuildConfig
  metadata:
    labels:
      name: ${APP_NAME}
    name: ${APP_NAME}
  spec:
    output:
      to:
        kind: ImageStreamTag
        name: ${APP_NAME}:latest
    postCommit: {}
    resources: {}
    runPolicy: Serial
    source:
      contextDir: /
      git:
        ref: ${APP_SOURCE_REPOSITORY_REF}
        uri: ${APP_SOURCE_REPOSITORY_URL}
      sourceSecret:
        name: gitlab-user
      type: Git
    strategy:
      sourceStrategy:
        env:
        - name: env
          value: ${ENV}
        - name: Project_Name
          value: ${APP_NAME}
        from:
          kind: ImageStreamTag
          name: ${APP_BUILDER_IMAGE}
          namespace: openshift
      type: Source
    triggers:
    - imageChange: {}
    - type: ImageChang
- apiVersion: v1
  kind: DeploymentConfig
  metadata:
    labels:
      name: ${APP_NAME}
    name: ${APP_NAME}
  spec:
    replicas: 1
    selector:
      name: ${APP_NAME}
    strategy:
      customParams:
        command:
        - /bin/sh
        - -c
        - sleep 5; echo slept for 5; /usr/bin/openshift-deploy
      type: Rolling
    template:
      metadata:
        labels:
          name: ${APP_NAME}
      spec:
        containers:
        - env:
          - name: ETCD_URL
            value: ${ETCD_URL}
          - name: env
            value: ${ENV}
          - name: NODE_ENV
            value: container
          image: ${APP_NAME}:latest
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /status
              port: 80
              scheme: HTTP
            initialDelaySeconds: 20
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 3
          name: ${APP_NAME}
          ports:
          - containerPort: 8080
            protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /status
              port: 80
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 3
          resources:
            limits:
              cpu: ${CPU_LIMIT}
              memory: ${MEMORY_LIMIT}
            requests:
              cpu: 50m
              memory: 64Mi
          volumeMounts:
          - mountPath: /opt/configration
            name: configration
          - mountPath: /etc/localtime
            name: localtime
          - mountPath: /etc/timezone
            name: timezone
        volumes:
        - configMap:
            defaultMode: 420
            name: configration
          name: configration
        - hostPath:
            path: /etc/localtime
          name: localtime
        - hostPath:
            path: /etc/timezone
          name: timezone
    test: false
    triggers:
    - imageChangeParams:
        automatic: true
        containerNames:
        - ${APP_NAME}
        from:
          kind: ImageStreamTag
          name: ${APP_NAME}:latest
      type: ImageChange
    - type: ConfigChange
parameters:
- description: 项目名
  displayName: Name
  name: APP_NAME
  required: true
- description: 对外暴露域名
  displayName: Project domain name
  name: DOMAIN_NAME
  required: true
- description: 请输入Git地址.仅支持HTTP方式.
  displayName: Source Repository URL
  name: APP_SOURCE_REPOSITORY_URL
  required: true
  value: http://git.xxx.cn/ops/nginx-config.git
- description: git仓库的默认分支或者版本号
  displayName: Git Reference
  name: APP_SOURCE_REPOSITORY_REF
  required: true
  value: config-a
- description: git仓库的路径
  displayName: Context Directory
  name: CONTEXT_DIR
  value: /
- description: 设定当前环境,比如test、bts
  displayName: ENV
  name: ENV
  required: true
- description: build 时使用的基础镜像
  displayName: Base builder image of your app
  name: APP_BUILDER_IMAGE
  required: true
  value: openresty:1.11.2.3
- description: 超过此请求额度会被强制重启.单位Mi/Gi.
  displayName: Memory Limits
  name: MEMORY_LIMIT
  required: true
  value: 64Mi
- description: 超过此请求额度会被强制重启.单位m/g.
  displayName: CPU Limits
  name: CPU_LIMIT
  required: true
  value: 50m

 

 下一步导入到oc catalog里面

# oc project openshift
# oc oc create -f nginx-openresty.yaml

在登陆oc的web console 就可以使用nginx-openresty模板部署应用了

 

 

2、deployment策略

deployment顾名思义是最终部署应用的重要环节,包括了变更策略、最终POD的运行配置等关键信息

Deployment Strategy:

这个是部署应用是的变更策略一共有三种策略

Rolling(滚动升级)

spec:
  strategy:
    rollingParams:
      intervalSeconds: 1
      maxSurge: 25%
      maxUnavailable: 25%
      timeoutSeconds: 600
      updatePeriodSeconds: 1
    type: Rolling

Recreate(重新创建)

spec:
  strategy:
    type: Recreate

 Custom(自定义)

 不常用

Deployment Triggers:

这个配置决定了什么情况下Deployment 才会触发

ImageChange 镜像变更

ConfigChange Deployment 配置变更

 
spec:
   triggers:
    - imageChangeParams:
        automatic: true
        containerNames:
          - nginx-template
        from:
          kind: ImageStreamTag
          name: 'nginx-template:latest'
          namespace: test
      type: ImageChange
    - type: ConfigChange

 

  triggers:
    - imageChangeParams:
        automatic: true
        containerNames:
          - nginx-template
        from:
          kind: ImageStreamTag
          name: 'nginx-template:latest'
          namespace: test
      type: ImageChange   #镜像更新
    - type: ConfigChange   #Deployment 配置更新

 

posted @ 2019-02-21 14:45  三木燕  阅读(622)  评论(0编辑  收藏  举报