Physical deployment

1. Overview

This section describes the physical deployment of the machine, and some of the ways in which the physical deployment background daemon is commonly used:nohup, supervisor, systemd

Nohup, systemd does not need installation, supervisor needs to use yum to install, this section uses nohup as a background daemon method, systemd, supervisor data can use a google method, just one configuration file to configure it.

2. Project code

We create a project name apicode (here we show only one service deployed, other services are in the same way), write api description file and use goctl to generate apicode project code

2.1, apicode.api file

  1. syntax = "v1"
  2. info(
  3. title: "this is a deploy demo"
  4. desc: "this is a deploy demo"
  5. author: "Mikael"
  6. )
  7. type (
  8. HelloReq{
  9. msg string `form:"msg"`
  10. }
  11. HelloResp{
  12. msg string `json:"msg"`
  13. }
  14. )
  15. service apicode{
  16. @doc "hello"
  17. @handler hello
  18. get /hello(HelloReq)returns(HelloResp)
  19. }

2.2 Use goctl to generate code

  1. $ cd apicode && goctl api go -api *.api -dir ./
  2. $ go mod tidy

2.3 Project structure

  1. ├── apicode.api
  2. ├── apicode.go
  3. ├── etc
  4. └── apicode.yaml
  5. ├── go.mod
  6. └── internal
  7. ├── config
  8. └── config.go
  9. ├── handler
  10. ├── hellohandler.go
  11. └── routes.go
  12. ├── logic
  13. └── hellologic.go
  14. ├── svc
  15. └── servicecontext.go
  16. └── types
  17. └── types.go

2.4 Add code

Add a point code to hellologic.go to export return to prove that we visited

  1. .....
  2. func (l *HelloLogic) Hello(req *types.HelloReq) (resp *types.HelloResp, err error) {
  3. fmt.Printf("print -> hello %s \n", req.Msg)
  4. return &types.HelloResp{
  5. Msg: "hello->" + req.Msg,
  6. }, nil
  7. }

Start Project

  1. $ go run apicode.go

Browser input http:///127.0.0.1:8888/hello return visit to see your browser output the code we added in the logic

  1. {
  2. "msg": "hello->zhangsan"
  3. }

2.5 Upload code

Create an apicode repository on our previous gitlab deployed, get code push up

2.6 Public key for configuring jenkins server

jenkins needs a gitlab pull code to build, so we want to configure the public key of the jenkins physical machine to gitlab and unencrypt login

3. Jenkins deployment

We have already deployed jenkins with gitlab and then we use jenkins to publish the code as long as you write pipline

Core ideas:

  • Use jenkins to pack the binary level profile of the project into a compression package
  • Sync packets to deploy machines, start with nohup (or erverisor, systemd)

Before writing pipline, because we need to build a go program in jenkins, because our jenkins is installed using a docker (if you are naked installed in jenkins simply need to install the go environment on the servers), then we need a go environment in jenkins containers where there are three options available

  • A go plugin using jenkins
  • Copy the package to the container at the outlet installation and install it manually in the container.
  • The other is to install jenkins without a docker and jenkins directly with nuds. If jenkins is installed only on the current server

There is always one way to fit you, and here I copy the go offline package into the jenkins container, installed to /usr/local/go

Given that most domestic users have problems accessing golang.org, go.dev here, I use the second mode to download the offline package using the docker cp “copied into the container.”

‘Note’ Here also provides you with jenkins how to support gos through plugins and if conditions allow this to be the best, the action steps below:

Tap “System Managers” on the front page —>”Plugin Management”

In an optional plugin, enter ‘go’, see the go plugin installed directly, and reboot after the installation has succeeded.

Once the plugin is installed, we need to configure it.Tap “System Management” on the front page —> “Global Tool Configuration”, pull down to see “Go”

We click on a new Go, installing gos from golang.org by default. You can also choose how to use tar packages, and then we click “app”

‘To do so, press ‘check now’ in the thePlugin Manager, or start jenkins’, reboot jenkins with the addition of this syntax in pipline

https://github.com/jenkinsci/golang-plugin

3.1 Create pipline

Click “New Item” on the left side of the front page, enter “apicode-docker”, select “Waterlines”, then determine

Then click on “General”, select “This project is parameterized”, “Add parameter”, “Choice Parameter”, like the beacon

Then write the following

Save directly.

3.2 Edit pipline

‘Note’ We have a public key to configure before writing pipline to configure jenkins public key to the server running the service because we build using jenkins and then upload the built tar package to the running server using scp.

View jenkins host public key:

  1. $ cat /root/.ssh/id_rsa.pub

Configure to the /root/.ssh/authorized_keys running the service physics.

Swipe down to find Pipeline scripts, fill in 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: '选择需要构建的分支'
  11. }
  12. stages {
  13. stage('服务信息') {
  14. steps {
  15. sh 'echo 分支:$branch'
  16. }
  17. }
  18. stage('拉取代码') {
  19. steps {
  20. checkout([$class: 'GitSCM',
  21. branches: [[name: '$branch']],
  22. doGenerateSubmoduleConfigurations: false,
  23. extensions: [],
  24. submoduleCfg: [],
  25. userRemoteConfigs: [[credentialsId: 'gitlab-cert', url: 'ssh://git@192.168.1.182:2222/root/apicode.git']]])
  26. }
  27. }
  28. stage('Build') {
  29. steps{
  30. sh 'echo Build'
  31. sh '/usr/local/go/bin/go build -o apicode apicode.go'
  32. sh 'mkdir deploy && cp -r ./etc ./apicode deploy'
  33. sh 'tar -zcvf deploy.tar.gz deploy'
  34. }
  35. }
  36. stage('Deploy') {
  37. steps{
  38. // 192.168.1.183 : 部署服务机器ip , 部署之前一定要把jenkins的公钥配置到192.168.1.183上免密登陆(有多种方式)
  39. sh 'scp ./deploy.tar.gz root@192.168.1.183:/root/'
  40. sh 'ssh root@192.168.1.183 tar -xvf /root/deploy.tar.gz'
  41. sh 'ssh root@192.168.1.183 nohup /root/deploy/apicode -f /root/deploy/etc/apicode.yaml >apicode.stdout.log 2>apicode.stderr.log &'
  42. }
  43. }
  44. }
  45. }

4. Build Publication

Click on the home page to find the apicode service to click on it

Tap Build with Parameters, select the corresponding “branch” to “Service” to start building

Build finished, we go to http://192.168.1.183:8889/hello?msg=mikael to see output on page

  1. {
  2. "msg": "hello->mikael"
  3. }

至此,部署完成。Of course you can forward your favorite gateway to this service like nginx, kong…