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
    1. $ 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 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 server

    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

    2.4、 Add a pipeline

    • 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

    • 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.

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

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

    The content of dockerlogin.sh

    1. 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

    build with parameters

    Guess you wants