Service Deployment
This section uses jenkins to demonstrate a simple service deployment to k8s.
- k8s cluster installation
- gitlab environment installation
- jenkins environment installation
- redis&mysql&nginx&etcd installation
[!TIP] Ensure that goctl is installed on each node of k8s
Please google for the installation of the above environment, and I will not introduce it here.
1.1、Add SSH Key
Enter gitlab, click on the user center, find , find the SSH Keys
tab on the left
- 1、View the public key on the machine where jenkins is located
- 2、If not, you need to generate it, if it exists, please skip to step 3
$ ssh-keygen -t rsa -b 2048 -C "email@example.com"
- 3、Add the public key to gitlab
1.2、Upload the code to the gitlab warehouse
Create a new project go-zero-demo
and upload the code. Details are not described here.
2.1、Add credentials
- View the private key of the machine where Jenkins is located, which corresponds to the previous gitlab public key
Enter jenkins, click on
Manage Jenkins
->Manage Credentials
2.2、 Add global variables
Enter Manage Jenkins
->Configure System
, slide to the entry of Global Properties
, add docker private warehouse related information, as shown in the figure is docker username
, docker user password
, docker private warehouse address
[!TIP]
docker_user
your docker username
docker_pass
your docker user password
docker_server
your docker serverThe private warehouse I use here, if you don’t use the private warehouse provided by the cloud vendor, you can build a private warehouse yourself. I won’t go into details here, and you can google it yourself.
2.3、Configure git
Go to Manage Jenkins
->Global Tool Configureation
, find the Git entry, fill in the path of the git executable file of the machine where jenkins is located, if not, you need to download the Git plugin in the jenkins plugin management.
2.4、 Add a pipeline
Get the credential id Go to the credential page and find the credential id whose Username is
gitlab
Go to the jenkins homepage, click on
New Item
, the name isuser
View project git address
Add the service type Choice Parameter, check
This project is parameterized in General
, ClickAdd parameter
and selectChoice Parameter
, add the selected value constant (api, rpc) and the variable (type) of the received value according to the figure, which will be used in the Pipeline script later.Configure
user
, on theuser
configuration page, swipe down to findPipeline script
, fill in the script content
pipeline {
agent any
parameters {
gitParameter name: 'branch',
type: 'PT_BRANCH',
branchFilter: 'origin/(.*)',
defaultValue: 'master',
selectedValue: 'DEFAULT',
sortMode: 'ASCENDING_SMART',
description: 'Select the branch'
}
steps {
sh 'echo branch: $branch'
sh 'echo build service type:${JOB_NAME}-$type'
}
}
stage('check out') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: '$branch']],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '${credentialsId}', url: '${gitUrl}']]])
}
}
stage('get commit_id') {
steps {
echo 'get commit_id'
git credentialsId: '${credentialsId}', url: '${gitUrl}'
script {
env.commit_id = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
}
}
stage('goctl version detection') {
steps{
sh '/usr/local/bin/goctl -v'
}
}
stage('Dockerfile Build') {
steps{
sh '/usr/local/bin/goctl docker -go service/${JOB_NAME}/${type}/${JOB_NAME}.go'
env.image = sh(returnStdout: true, script: 'echo ${JOB_NAME}-${type}:${commit_id}').trim()
}
sh 'echo image:${image}'
sh 'docker build -t ${image} .'
}
stage('Upload to the mirror warehouse') {
steps{
sh '/root/dockerlogin.sh'
sh 'docker tag ${image} ${dockerServer}/${image}'
sh 'docker push ${dockerServer}/${image}'
}
}
stage('Deploy to k8s') {
steps{
script{
env.deployYaml = sh(returnStdout: true, script: 'echo ${JOB_NAME}-${type}-deploy.yaml').trim()
env.port=sh(returnStdout: true, script: '/root/port.sh ${JOB_NAME}-${type}').trim()
}
sh 'echo ${port}'
sh 'rm -f ${deployYaml}'
sh '/usr/local/bin/goctl kube deploy -secret dockersecret -replicas 2 -nodePort 3${port} -requestCpu 200 -requestMem 50 -limitCpu 300 -limitMem 100 -name ${JOB_NAME}-${type} -namespace hey-go-zero -image ${dockerServer}/${image} -o ${deployYaml} -port ${port}'
sh '/usr/bin/kubectl apply -f ${deployYaml}'
}
}
stage('Clean') {
steps{
sh 'docker rmi -f ${image}'
sh 'docker rmi -f ${dockerServer}/${image}'
cleanWs notFailBuild: true
}
}
}
}
[!TIP] ${credentialsId} should be replaced with your specific credential value, that is, a string of strings in the [Add Credentials] module, ${gitUrl} needs to be replaced with the git warehouse address of your code, other variables in the form of ${xxx} are not required Modify it and keep it as it is.
The content of dockerlogin.sh
docker login --username=$docker-user --password=$docker-pass $docker-server
- $docker-user: docker login username
- $docker-pass: docker login user password
- $docker-server: docker private address