标签:target remote erp lin def ams mes res capacity
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-mysql-data
namespace: db
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs-mysql
nfs:
path: /data/db
server: 10.61.150.16
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-mysql-data
namespace: db
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: nfs-mysql
2、准备创建服务所需要的dp和svc
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: mysql
namespace: db
spec:
selector:
matchLabels:
app: mysql-db
template:
metadata:
labels:
app: mysql-db
spec:
volumes:
- name: pvc-mysql-data
persistentVolumeClaim:
claimName: pvc-mysql-data
containers:
- name: mysql-db
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: LANG
value: ‘C.UTF-8‘
- name: MYSQL_ROOT_PASSWORD
value: ‘password‘
volumeMounts:
- name: pvc-mysql-data
mountPath: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
namespace: db
labels:
name: mysql-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 31106
selector:
app: mysql-db
mysql -h10.61.150.17 -uroot -P31106 -ppassword
测试是否可以登录mysql
3、使用变量替换yaml文件
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-{pv_name}
namespace: db
spec:
capacity:
storage: {pv_volume}Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs-{pv_name}
nfs:
path: /data/db
server: 10.61.150.16
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-{pv_name}
namespace: db
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: {pv_volume}Gi
storageClassName: nfs-{pv_name}
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: {dp_name}
namespace: db
spec:
selector:
matchLabels:
app: {dp_name}-db
template:
metadata:
labels:
app: {dp_name}-db
spec:
volumes:
- name: pvc-{pv_name}
persistentVolumeClaim:
claimName: pvc-{pv_name}
containers:
- name: {dp_name}-db
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: LANG
value: ‘C.UTF-8‘
- name: MYSQL_ROOT_PASSWORD
value: ‘password‘
volumeMounts:
- name: pvc-{pv_name}
mountPath: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
name: {dp_name}_svc
namespace: db
labels:
name: {dp_name}_svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: {nodeport}
selector:
app: mysql-db
4、上传K8S-yaml到scm
git init
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git remote add origin $git项目地址
git pull --rebase origin master
git add mysql_jenkinsfile.yaml
git commit -m "jenkinsfile commit
git push -u origin master
5、从scm中拉取jenkinsfile
pipeline {
agent any
stages{
stage("Git clone"){
steps {
//拉取front代码
echo "工作目录是:$WORKSPACE"
echo ‘正在拉取代码...‘
dir("projdir"){
git credentialsId: ‘fad39094-6d07-4e3a-a86d-8b011f37b5de‘, url: ‘http://192.168.2.32:8090/root/devops.git‘,branch:‘master‘,changelog:true
}
}
}
}
}
6、使用parameters获取第三步中替换掉的变量
使用jenkins的变量替换掉yaml文件中的name、端口等信息
需要替换的变量
pipeline {
agent any
parameters {
string(name: ‘pv_name‘, defaultValue: ‘test_mysql_pv‘, description: ‘请输入pv的名称 eg:test_mysql_pv‘)
string(name: ‘pv_volume‘, defaultValue: ‘5‘, description: ‘请输入pv的大小 eg:5‘)
string(name: ‘dp_name‘, defaultValue: ‘test_mysql‘, description: ‘请输入dp的名称 eg:test_mysql‘)
string(name: ‘dp_port‘, defaultValue: ‘32260‘, description: ‘请输入dp的端口号 eg:32260‘)
}
stages {
stage(‘get_params‘){
steps {
echo "pv_name: ${params.pv_name}"
echo "pv_volume: ${params.pv_volume}"
echo "dp_name: ${params.dp_name}"
echo "dp_port: ${params.dp_port}"
}
}
stage("Git clone"){
steps {
//拉取front代码
echo "工作目录是:$WORKSPACE"
echo ‘正在拉取代码...‘
dir("projdir"){
git credentialsId: ‘fad39094-6d07-4e3a-a86d-8b011f37b5de‘, url: ‘http://192.168.2.32:8090/root/devops.git‘,branch:‘master‘,changelog:true
}
}
}
stage("update yaml"){
steps {
dir("projdir"){
sh "sed -e ‘s#{pv_name}#${params.pv_name}#g;s#{pv_volume}#${params.pv_volume}#g;s#{dp_name}#${params.dp_name}#g;s#{nodeport}#${params.dp_port}#g‘ mysql_k8s.yaml > mysql_k8s.yaml"
}
}
}
}
}
7、使用可以使用kubectl的angent代理执行部署
stage("update yaml"){
agent {label ‘kubectl-agent‘}
steps {
dir("projdir"){
sh "sed -e ‘s#{pv_name}#${params.pv_name}#g;s#{pv_volume}#${params.pv_volume}#g;s#{dp_name}#${params.dp_name}#g;s#{nodeport}#${params.dp_port}#g‘ mysql_k8s.yaml > mysql_k8s_temp.yaml"
sh "kubectl apply -f mysql_k8s_temp.yaml"
}
}
}
agent {label ‘kubectl-agent‘} 需要提前制作的docker agent代理
网上有其他方案,将K8S_CONFIG中的内容进行base64解密并存为~/.kube/config配置文件,再从pipeline中读取并输出到.kube中,执行kubectl命令部署至k8s。
8、完整的pipeline
pipeline {
agent any
parameters {
string(name: ‘pv_name‘, defaultValue: ‘test_mysql‘, description: ‘请输入pv的名称 eg:test-mysql‘)
string(name: ‘pv_volume‘, defaultValue: ‘5‘, description: ‘请输入pv的大小 eg:5‘)
string(name: ‘dp_name‘, defaultValue: ‘test_mysql‘, description: ‘请输入dp的名称 eg:test-mysql‘)
string(name: ‘dp_port‘, defaultValue: ‘32260‘, description: ‘请输入dp的端口号 eg:32260‘)
}
stages {
stage(‘get_params‘){
steps {
echo "pv_name: ${params.pv_name}"
echo "pv_volume: ${params.pv_volume}"
echo "dp_name: ${params.dp_name}"
echo "dp_port: ${params.dp_port}"
}
}
stage("Git clone"){
steps {
//拉取front代码
echo "工作目录是:$WORKSPACE"
echo ‘正在拉取代码...‘
dir("projdir"){
git credentialsId: ‘fad39094-6d07-4e3a-a86d-8b011f37b5de‘, url: ‘http://192.168.2.32:8090/root/devops.git‘,branch:‘master‘,changelog:true
}
}
}
stage("update yaml"){
agent {label ‘kubectl-agent‘}
steps {
dir("projdir"){
sh "sed -e ‘s#{pv_name}#${params.pv_name}#g;s#{pv_volume}#${params.pv_volume}#g;s#{dp_name}#${params.dp_name}#g;s#{nodeport}#${params.dp_port}#g‘ mysql_k8s.yaml > mysql_k8s_temp.yaml"
sh "kubectl apply -f mysql_k8s_temp.yaml"
}
}
}
}
}
标签:target remote erp lin def ams mes res capacity
原文地址:https://www.cnblogs.com/zhaobowen/p/13526910.html