Openshift PaaS云平台部署通俗讲解二(实操)

Posted 风水月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Openshift PaaS云平台部署通俗讲解二(实操)相关的知识,希望对你有一定的参考价值。

上篇文章Openshift PaaS云平台部署通俗讲解一(介绍概念) 介绍了在使用Openshift前必知概念,本篇文章,将以图文的方式,讲述如何在Openshift中部署自己的应用。

1.创建项目

2.部署hello应用


2.1 Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deployment
  namespace:myfirstpaasproject   			#project名称
spec:
  selector:
    matchLabels:
      app: hello
  replicas: 2								#分配2个Pod
  template:
    metadata:
      labels:
        app: hello
    spec:	  
	  imagePullSecrets:
	    - name: hello-pull					#自己创建的名为hello-pull的secret,用于镜像下载的身份认证
      containers:
      - name: hello              			#容器名称,自定义
        image: 'base_url/hello.1.0.1'		#镜像路径
		command:							#部署进行时用到的指令
		  - java
		args:
		  - '-Dfile.encoding=utf-8'
		  - '-DJAVA_FONTS=/myfolder'
		  - '-jar'
		  - '-Xms512m'
		  - '-Xmx2048m'
		  - /myfolder/hello.jar
		  - '--spring.profiles.active=dev'
		  - '--log.path=/myfolder/logs'
		  - '--server.port=8080'
		env:
		  - name: TZ
		    value: Asia/Beijing
		  - name: JAVA_OPTS
		    value: >-
			  -Dfile.encoding=UTF-8 -DJAVA_FONTS=/myfolder
			  -Ddefault.client.encoding=UTF-8 -Dspring.profiles.active=dev
			  -Dlog.path=/myfolder/logs
		ports:								#hello程序用到的端口
		  - containerPort: 8080
		    protocol: TCP
		resources:							#container用到的资源,一个CPU、2G内存
		  limits:
		    cpu: '1'
			memory: 2Gi
		  requests:
			cpu: 500m
			memory: 1Gi
		readinessProbe:
		  httpGet:
		    path: /login
			port: 8080
			scheme: HTTP
		volumeMounts:						#将hello-pv存储挂载到/hello目录下(即通过/hello 即可访问该存储)
		  - name: hello-pv
		    mountPath: /hello
	  volumes:								#hello-pv 对应的实际storage的名称是 hello-pvc
	    - name: hello-pv
		  persistentVolumeClaim:
		    claimName: hello-pvc

Deployment用到了PersistentVolumeClaim、ImagePullSecrets

2.1.1 PersistentVolumeClaim创建

2.1.2 ImagePullSecret

2.2 Service

apiVersion: v1
kind: Service
metadata:
  name: hello-service
spec:
  ports:
    - protocol: TCP
	  port: 8080
      targetPort: 8080
  selector:   			#选择器,该service用于 app名为 hello的应用
    app: hello

2.3 Route

apiVersion: v1
kind: Route
metadata:
  name: hello-route
  namespace: myfirstpaasproject
spec:
  host: hello.com
  to:
    kind: Service
	name: hello-service		#自己刚才定义的service name
  ports:
    targetPort: 8080

Route用web网页创建也很方便,如下图

部署成功,回到Project,如下图

3.通过Jenkins流水线部署

登录Jenkins,点击配置配置pipeline后,点击立即构建,即可完成源码的编译到PaaS云上一键部署


在Script填入如下内容后,保存,点击立即构建,即可完成hello程序的部署

def project_name='myfirstpaasproject'
def app_name='hello'
def base_image='openjdk18-openshift:simsunv02'
def tfs_credential='tfs-credential-hello'
def tfs_branch='master'
def tfs_url='http://github.com/xxx'
def user_name=xxxx
def password=xxxx

pipeline{
	agent{
		label 'maven'
	}
	stages{
		#编译程序
		stage('Compile Package'){ 					
			steps{
				git branch:tfs_branch,credentialsId:"${tfs_credential}",url:"${tfs_url}"
				script{
					def pom=readMavenPom file:"./pom.xml"
					def version=pom.version
					echo "version:${version}
				}
				sh "mvn clean compile -s /home/jenkins/.m2/settings_maven_test_public.xml"
			}
		}
		#打包程序
		stage('Build Intall'){
			steps{
				sh "mvn install -DskipTests=true -s /home/jenkins/.m2/settings_maven_test_public.xml"
			}
		}
		#创建程序镜像,并上传至xxxx程序库
		stage('Create Image Builder'){
			steps{
				script{	
					openshift.withCluster(){
						openshift.withProject("${project_name}"){
							if(openshift.selector("bc","${app_name}").exists(){
								openshift.selector("bc","${app_name}").delete()
							}
							openshift.newBuild("--name=${app_name}","--binary=true","--to=xxxx/${service_name}.1.0.{BUILD_ID}"
							,"--to-docker=true","--push-secret=hello-pull","--image-stream=${base_image}")
						}
					}
				}
			}
		}
		#将hello的jar包放入oc-build/myfirstdeployment下,开始构建镜像
		stage('Build Image'){
			steps{
				sh "rm -rf oc-build && mkdir -p oc-build/myfirstdeployment"
				sh "cp ./hello/target/*.jar oc-build/myfirstdeployment/"
				script{
					openshift.withcluster(){
						openshift.withProject("${project_name}"){
							openshift.selector("bc","${app_name}".startBuild("--from-dir=oc-build","--wait=true")
						}
					}
				}
			}
		}
		#部署阶段
		stage("Deploy DEV"){
			steps{
				script{
					openshift.withCluster(){
						openshift.withProject("${project_name}"){
							if(!openshift.selector("bc","service_name").exists()){
								#将hello-template.xml文件中的BUILD_ID修改为构建号
								sh "sed -i 's/BUILD_ID/'${BUILD_ID}'/g' hello-template.yaml"
								#将修改后的yaml文件上传至自定义的镜像仓库
								sh "curl -u ${user_name}:${password} -T hello-template.yam http://xxxx/${app_name}.1.0.{BUILD_ID}"
								#上传模板至myfirstpaasproject项目
								sh "oc process -f hello-template.yaml | oc apply -n ${project_name} -f -"
							}else{
								openshift.selector("bc","${sapp_name}").rollout().latest()
							}
						}
					}
				}
			}
		}
	}
}

以上是关于Openshift PaaS云平台部署通俗讲解二(实操)的主要内容,如果未能解决你的问题,请参考以下文章

Openshift PaaS云平台部署通俗讲解二(实操)

Openshift PaaS云平台部署通俗讲解一(介绍概念)

Openshift PaaS云平台部署通俗讲解一(介绍概念)

Openshift PaaS云平台部署通俗讲解一(介绍概念)

Openshift PaaS云平台部署通俗讲解三(Container启动时运行多条命令)

Openshift PaaS云平台部署通俗讲解三(Container启动时运行多条命令)