Service Deployment

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

This section uses jenkins to demonstrate a simple service deployment to k8s.

Prepare

  • k8s cluster installation
  • gitlab environment installation
  • jenkins environment installation
  • redis&mysql&nginx&etcd installation
  • goctl install

[!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.

Service deployment

1、Relevant preparations for gitlab code warehouse

1.1、Add SSH Key

Enter gitlab, click on the user center, find Settings, find the SSH Keys tab on the left ssh key

  • 1、View the public key on the machine where jenkins is located
  1. $ cat ~/.ssh/id_rsa.pub
  • 2、If not, you need to generate it, if it exists, please skip to step 3
  1. $ ssh-keygen -t rsa -b 2048 -C "email@example.com"

“email@example.com” 可以替换为自己的邮箱

After completing the generation, repeat the first step

  • 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、jenkins

2.1、Add credentials

  • View the private key of the machine where Jenkins is located, which corresponds to the previous gitlab public key
  1. $ cat id_rsa
  • Enter jenkins, click on Manage Jenkins-> Manage Credentials credentials

  • Go to the Global Credentials page, add credentials, Username is an identifier, add pipeline later, you know that this identifier represents the credentials of gitlab, and Private Key` is the private key obtained above jenkins-add-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] The 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. jenkins-git

jenkins-configure

2.4、 Add a pipeline

The pipeline is used to build the project, pull code from gitlab->generate Dockerfile->deploy to k8s are all done in this step, here is the demo environment, in order to ensure the smooth deployment process, Need to install jenkins on the machine where one of the nodes of the k8s cluster is located, I installed it on the master here.

  • Get the credential id Go to the credential page and find the credential id whose Username is gitlab jenkins-credentials-id

  • Go to the jenkins homepage, click on New Item, the name is user jenkins-add-item

  • View project git address gitlab-git-url

  • Add the service type Choice Parameter, check This project is parameterized in General, Click Add parameter and select Choice 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. jenkins-choice-parameter

  • Configure user, on the user configuration page, swipe down to find Pipeline script, fill in the script content

  1. pipeline {
  2. agent any
  3. parameters {
  4. gitParameter name: 'branch',
  5. type: 'PT_BRANCH',
  6. branchFilter: 'origin/(.*)',
  7. defaultValue: 'master',
  8. selectedValue: 'DEFAULT',
  9. sortMode: 'ASCENDING_SMART',
  10. description: 'Select the branch'
  11. }
  12. stages {
  13. stage('service info') {
  14. steps {
  15. sh 'echo branch: $branch'
  16. sh 'echo build service type:${JOB_NAME}-$type'
  17. }
  18. }
  19. stage('check out') {
  20. steps {
  21. checkout([$class: 'GitSCM',
  22. branches: [[name: '$branch']],
  23. doGenerateSubmoduleConfigurations: false,
  24. extensions: [],
  25. submoduleCfg: [],
  26. userRemoteConfigs: [[credentialsId: '${credentialsId}', url: '${gitUrl}']]])
  27. }
  28. }
  29. stage('get commit_id') {
  30. steps {
  31. echo 'get commit_id'
  32. git credentialsId: '${credentialsId}', url: '${gitUrl}'
  33. script {
  34. env.commit_id = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
  35. }
  36. }
  37. }
  38. stage('goctl version detection') {
  39. steps{
  40. sh '/usr/local/bin/goctl -v'
  41. }
  42. }
  43. stage('Dockerfile Build') {
  44. steps{
  45. sh '/usr/local/bin/goctl docker -go service/${JOB_NAME}/${type}/${JOB_NAME}.go'
  46. script{
  47. env.image = sh(returnStdout: true, script: 'echo ${JOB_NAME}-${type}:${commit_id}').trim()
  48. }
  49. sh 'echo image:${image}'
  50. sh 'docker build -t ${image} .'
  51. }
  52. }
  53. stage('Upload to the mirror warehouse') {
  54. steps{
  55. sh '/root/dockerlogin.sh'
  56. sh 'docker tag ${image} ${dockerServer}/${image}'
  57. sh 'docker push ${dockerServer}/${image}'
  58. }
  59. }
  60. stage('Deploy to k8s') {
  61. steps{
  62. script{
  63. env.deployYaml = sh(returnStdout: true, script: 'echo ${JOB_NAME}-${type}-deploy.yaml').trim()
  64. env.port=sh(returnStdout: true, script: '/root/port.sh ${JOB_NAME}-${type}').trim()
  65. }
  66. sh 'echo ${port}'
  67. sh 'rm -f ${deployYaml}'
  68. 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}'
  69. sh '/usr/bin/kubectl apply -f ${deployYaml}'
  70. }
  71. }
  72. stage('Clean') {
  73. steps{
  74. sh 'docker rmi -f ${image}'
  75. sh 'docker rmi -f ${dockerServer}/${image}'
  76. cleanWs notFailBuild: true
  77. }
  78. }
  79. }
  80. }

[!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. user-pipepine-script

port.sh

  1. case $1 in
  2. "user-api") echo 1000
  3. ;;
  4. "user-rpc") echo 1001
  5. ;;
  6. "course-api") echo 1002
  7. ;;
  8. "course-rpc") echo 1003
  9. ;;
  10. "selection-api") echo 1004
  11. esac

The content of dockerlogin.sh

  1. #!/bin/bash
  2. 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

View pipeline

build with parameters build with parameters

View k8s service

k8s01

Guess you wants