k8s部署 pm2项目
当我们在Dockerfile中采用pm2 start的启动命令时,一直无法正常启动,最后改成pm2-runtime启动方式。3、编写jenkins的pipeline文件。1、编写pm2项目Dockerfile文件。2、配置k8s pods yaml文件。查询pods运行情况。
·
1、编写pm2项目Dockerfile文件
遇到的一个问题:
当我们在Dockerfile中采用pm2 start的启动命令时,一直无法正常启动,最后改成pm2-runtime启动方式
具体文件如下
# Set the base image
FROM node:16.20.2
WORKDIR /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-ws
k8s.kuboard.cn/workload: dex-transaction-ws
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: dex-transaction-ws
name: dex-transaction-ws
namespace: test
resourceVersion: '46539186'
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: dex-transaction-ws
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
annotations:
kubectl.kubernetes.io/restartedAt: '2023-12-22T01:18:40+08:00'
creationTimestamp: null
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: dex-transaction-ws
spec:
containers:
- command:
- pm2-runtime
- app.js
- '--name'
- dex-transaction-ws
env:
- name: TZ
value: Asia/Shanghai
image: 'harbor.abc.com/test/dex-transaction-ws:sit'
imagePullPolicy: Always
name: dex-transaction-ws
ports:
- containerPort: 8888
name: msag7
protocol: TCP
- containerPort: 9998
name: mghkd
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /root/.pm2/logs/
name: volume-6mey5
subPath: dex-transaction-ws
workingDir: /home/dex-transaction-ws
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: acr-secret
- name: harbor-secret
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- name: volume-6mey5
nfs:
path: /data/nfs
server: 172.16.10.20
---
apiVersion: v1
kind: Service
metadata:
annotations: {}
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: dex-transaction-ws
name: dex-transaction-ws
namespace: test
resourceVersion: '45909276'
spec:
clusterIP: 10.68.45.215
clusterIPs:
- 10.68.45.215
internalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: ddnfeb
port: 8888
protocol: TCP
targetPort: 8888
- name: 4f7pmc
port: 9998
protocol: TCP
targetPort: 9998
selector:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: dex-transaction-ws
sessionAffinity: None
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations: {}
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: dex-transaction-ws
name: dex-transaction-ws
namespace: test
resourceVersion: '46539488'
spec:
ingressClassName: abc-ingress
rules:
- host: dex-test.abc.com
http:
paths:
- backend:
service:
name: dex-transaction-ws
port:
number: 8888
path: /
pathType: Prefix
- host: dexws-test.abc.com
http:
paths:
- backend:
service:
name: dex-transaction-ws
port:
number: 9998
path: /
pathType: Prefix
tls:
- hosts:
- dex-test.abc.com
- dexws-test.abc.com
secretName: 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 any
parameters {
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"
"""
}
}
}
}
}
更多推荐
已为社区贡献26条内容
所有评论(0)