k8s部署 pm2项目
1、编写pm2项目Dockerfile文件
遇到的一个问题:
当我们在Dockerfile中采用pm2 start的启动命令时,一直无法正常启动,最后改成pm2-runtime启动方式
具体文件如下
# Set the base image
FROM node:16.20.2WORKDIR /home/dex-transaction-ws# Copy files
COPY ./ /home/dex-transaction-ws/# Set ENV variables
ENV INAME=${SVC_NAME}# Install dependencies and compile
RUN npm install pm2 -g
#RUN yarn install --frozen-lockfile
RUN yarn# Expose port 8888 - note that docs port is 8080
EXPOSE 8888# Set the default command to run when starting the container
CMD pm2-runtime app.js --name dex-transaction-ws
2、配置k8s pods yaml文件
---
apiVersion: apps/v1
kind: Deployment
metadata:annotations:k8s.kuboard.cn/displayName: dex-transaction-wsk8s.kuboard.cn/workload: dex-transaction-wslabels:k8s.kuboard.cn/layer: webk8s.kuboard.cn/name: dex-transaction-wsname: dex-transaction-wsnamespace: testresourceVersion: '46539186'
spec:progressDeadlineSeconds: 600replicas: 1revisionHistoryLimit: 10selector:matchLabels:k8s.kuboard.cn/layer: webk8s.kuboard.cn/name: dex-transaction-wsstrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:annotations:kubectl.kubernetes.io/restartedAt: '2023-12-22T01:18:40+08:00'creationTimestamp: nulllabels:k8s.kuboard.cn/layer: webk8s.kuboard.cn/name: dex-transaction-wsspec:containers:- command:- pm2-runtime- app.js- '--name'- dex-transaction-wsenv:- name: TZvalue: Asia/Shanghaiimage: 'harbor.abc.com/test/dex-transaction-ws:sit'imagePullPolicy: Alwaysname: dex-transaction-wsports:- containerPort: 8888name: msag7protocol: TCP- containerPort: 9998name: mghkdprotocol: TCPresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /root/.pm2/logs/name: volume-6mey5subPath: dex-transaction-wsworkingDir: /home/dex-transaction-wsdnsPolicy: ClusterFirstimagePullSecrets:- name: acr-secret- name: harbor-secretrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 30volumes:- name: volume-6mey5nfs:path: /data/nfsserver: 172.16.10.20---
apiVersion: v1
kind: Service
metadata:annotations: {}labels:k8s.kuboard.cn/layer: webk8s.kuboard.cn/name: dex-transaction-wsname: dex-transaction-wsnamespace: testresourceVersion: '45909276'
spec:clusterIP: 10.68.45.215clusterIPs:- 10.68.45.215internalTrafficPolicy: ClusteripFamilies:- IPv4ipFamilyPolicy: SingleStackports:- name: ddnfebport: 8888protocol: TCPtargetPort: 8888- name: 4f7pmcport: 9998protocol: TCPtargetPort: 9998selector:k8s.kuboard.cn/layer: webk8s.kuboard.cn/name: dex-transaction-wssessionAffinity: Nonetype: ClusterIP---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:annotations: {}labels:k8s.kuboard.cn/layer: webk8s.kuboard.cn/name: dex-transaction-wsname: dex-transaction-wsnamespace: testresourceVersion: '46539488'
spec:ingressClassName: abc-ingressrules:- host: dex-test.abc.comhttp:paths:- backend:service:name: dex-transaction-wsport:number: 8888path: /pathType: Prefix- host: dexws-test.abc.comhttp:paths:- backend:service:name: dex-transaction-wsport:number: 9998path: /pathType: Prefixtls:- hosts:- dex-test.abc.com- dexws-test.abc.comsecretName: abc.com-ssl
查询pods运行情况
~# kubectl get pods -n biking |grep dex-transaction-ws
dex-transaction-ws-5498654dff-hrpw9 1/1 Running 1 (37h ago) 3d13h
~# kubectl get deployment -n biking |grep dex-transaction-ws
dex-transaction-ws 1/1 1 1 4d14h
~# kubectl get ingress -n biking |grep dex-transaction-ws
dex-transaction-ws biking-ingress dex-test.cuiwjrpcvi.com,dexws-test.cuiwjrpcvi.com 172.16.10.202,172.16.10.203 80, 443 4d14h
3、编写jenkins的pipeline文件
// 参数构建
pipeline {agent anyparameters {gitParameter(name: 'BRANCH_TAG', type: 'PT_BRANCH_TAG', branchFilter: 'origin/(.*)', defaultValue: 'main', selectedValue: 'DEFAULT', sortMode: 'DESCENDING_SMART', description: '请选择需要部署的代码:')choice(name: 'mode', choices: ['deploy','rollback'], description: '请选择发布或者回滚?') choice(name: 'ENVMENT', choices: ['sit','exp','gray','prod'], description: '环境参数') string(name: 'iname', defaultValue: 'dex-transaction-ws', description: '服务名称')}environment {dest_path = "/var/jenkins_home/workspace/${JOB_NAME}"job_path = "/data/docker-compose/jenkins/jenkins_home/workspace/${JOB_NAME}"mod_path = "/data/docker-compose/jenkins/jenkins_home/mod"}stages {stage('clean'){steps {cleanWs(cleanWhenAborted: true, cleanWhenFailure: true, cleanWhenNotBuilt: true, cleanWhenSuccess: true, cleanWhenUnstable: true, cleanupMatrixParent: true, disableDeferredWipeout: true,deleteDirs: true)} } stage('从 gitlab 中拉取代码') {when {environment name: 'mode',value: 'deploy'}steps {deleteDir()checkout([$class: 'GitSCM', branches: [[name: "${params.BRANCH_TAG}"]],gitTool: 'Default', userRemoteConfigs: [[url: 'https://gitlab.abc.net/bk/spot/dex-transaction-ws.git', credentialsId: 'gitlab-deploy',]]])}}stage('Node Install And Build docker image'){steps{script{sh """docker build --build-arg SVC_NAME=${iname} -t harbor.abc.com/test/${iname}:$ENVMENT ."""}}}stage('Push image to hub'){steps{script{withCredentials([usernamePassword(credentialsId: 'harbor-secret-dev',, passwordVariable: 'password', usernameVariable: 'username')]) {sh 'docker login -u ${username} -p ${password} harbor.abc.com'}sh 'docker push harbor.abc.com/test/${iname}:$ENVMENT'}}}stage('deploy Server'){steps{script{sh """curl -X PUT \-H "content-type: application/json" \-H "Cookie: KuboardUsername=admin; KuboardAccessKey=ccpyiaxei7i8.8kjh69sh8amx8sp45x6i3t2tzwscdgeb" \-d '{"kind":"deployments","namespace":"test","name":"${iname}","images":{"harbor.cuiwjrpcvi.com/bktest/${iname}":"harbor.cuiwjrpcvi.com/bktest/${iname}:${ENVMENT}"}}' \"http://X.X.X.X:1234/kuboard-api/cluster/Test/kind/CICDApi/admin/resource/restartWorkload" curl -X PUT \-H "Content-Type: application/yaml" \-H "Cookie: KuboardUsername=admin; KuboardAccessKey=ccpyiaxei7i8.8kjh69sh8amx8sp45x6i3t2tzwscdgeb" \-d '{"kind":"deployments","namespace":"test","name":"${iname}"}' \"http://X.X.X.X:1234/kuboard-api/cluster/Test/kind/CICDApi/admin/resource/restartWorkload""""}}}}
}