1.开启devops
点击定制资源定义,在搜索栏中输入 clusterconfiguration
,点击搜索结果查看其详细页面。
在该 YAML 文件中,搜索 devops
,将 enabled
的 false
改为 true
。完成后,点击右下角的确定,保存配置
在 kubectl 中执行以下命令检查安装过程:
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f
踩坑:
ok: [localhost] => {
"msg": [
"1. check the storage configuration and storage server",
"2. make sure the DNS address in /etc/resolv.conf is available",
"3. execute 'kubectl logs -n kubesphere-system -l job-name=minio-make-bucket-job' to watch logs",
"4. execute 'helm -n kubesphere-system uninstall ks-minio && kubectl -n kubesphere-system delete job minio-make-bucket-job'",
"5. Restart the installer pod in kubesphere-system namespace"
]
}
原因:有2个默认存储,即local和nfs,将local更改为不是默认
2. 创建项目和devops工程
先创建项目,然后创建devops工程
3.创建凭证
这里我们要创建3个凭证,分别是:
sonarqube-token:sonarqube的token
gitee:gitee的用户名密码
aliyun-docker:阿里云容器镜像服务的用户名密码k8s-kubeconfig:默认内容为当前用户的 kubeconfig 配置。 选择此类型创建默认有值
4.Jenkinsfile
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('拉取项目') {
agent none
steps {
container('maven') {
git(credentialsId: 'gitee', branch: 'master', url: 'https://gitee.com/jjiangyong/devops-maven-sample.git', changelog: true, poll: false)
}
}
}
stage('项目编译') {
agent none
steps {
container('maven') {
sh 'mvn -Dmaven.test.skip=true clean package'
}
}
}
stage('镜像构建') {
agent none
steps {
container('maven') {
sh 'docker build -f Dockerfile-online -t $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:v$MODULE_VERSION .'
}
}
}
stage('镜像推送') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-docker' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker push $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:v$MODULE_VERSION'
}
}
}
}
stage('部署到DEV环境') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigFile(credentialsId: "$KUBECONFIG_CREDENTIAL_ID", variable: 'KUBECONFIG')]) {
script {
sh "echo \'${DEPLOY_YAML}\' > deploy.yaml"
sh "cat deploy.yaml"
sh "envsubst < deploy.yaml | kubectl apply -f -"
}
}
}
}
}
}
environment {
#k8s的rbac权限内容
KUBECONFIG_CREDENTIAL_ID = 'k8s-kubeconfig'
#仓库地址
REGISTRY = 'registry.cn-shenzhen.aliyuncs.com'
#aliyun或者harbor仓库的名称空间
HARBOR_NAMESPACE = 'jiangyongliangqiuxia'
#k8s中的项目
NAMESPACE = 'kubesphere-sample-dev'
#项目名,通常是代码项目
APP_NAME = 'devops-sample'
DEPLOY_YAML = '''
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ${APP_NAME}
name: ${APP_NAME}
namespace: ${NAMESPACE}
spec:
replicas: 1
selector:
matchLabels:
app: ${APP_NAME}
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
template:
metadata:
labels:
app: ${APP_NAME}
spec:
imagePullSecrets:
#需要这个秘密来下载镜像
- name: aliyun-hub
containers:
- name: ${APP_NAME}
imagePullPolicy: Always
image: $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:v$MODULE_VERSION
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: \'1\'
memory: 2Gi
requests:
cpu: 25m
memory: 850Mi
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
name: ${APP_NAME}
namespace: ${NAMESPACE}
labels:
app: ${APP_NAME}
spec:
ports:
- port: 8080
targetPort: 8080
selector:
app: ${APP_NAME}
type: ClusterIP
sessionAffinity: None
'''
}
parameters {
string(name: 'MODULE_VERSION', defaultValue: 'v0.0Beta', description: '模块版本')
}
}