linux12Devops -->08jenkins部署Golang
Posted FikL-09-19
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux12Devops -->08jenkins部署Golang相关的知识,希望对你有一定的参考价值。
文章目录
Jenkins部署Golang
编译环境:
1.安装go语言
2.优化测试
部署环境:
1.部署
第一步:编译环境(Jenkins)
1.安装golang语言
#1.下载golang
[root@jenkins opt]# wget https://golang.google.cn/dl/go1.16.7.linux-amd64.tar.gz
#2.安装
[root@jenkins opt]# tar -xf go1.16.7.linux-amd64.tar.gz -C /usr/local/
#3.添加软连接
[root@jenkins /usr/local]# ln -s /usr/local/go /usr/local/golang
#4.添加环境变量
[root@jenkins opt]# vim /etc/profile.d/go.sh
export GO_HOME=/usr/local/go
export GO_ROOT=/usr/local/go
export GO_PATH=/opt/go
export GOPROXY=https://mirrors.aliyun.com/goproxy/
PATH=$PATH:$GO_HOME:$GO_ROOT:$GO_PATH:$GO_HOME/bin
export PATH
[root@jenkins opt]# source /etc/profile
[root@jenkins opt]# printenv |grep GO
GO_PATH=/opt/go
GOPROXY=https://mirrors.aliyun.com/goproxy/
GO_HOME=/usr/local/go
GO_ROOT=/usr/local/go
2.测试
# 1.创建目录
[root@jenkins opt]# mkdir /opt/go
[root@jenkins go]# go mod init jenkins-go #创建一个go.mod文件
total 4
-rw-r--r-- 1 root root 27 Aug 16 20:40 go.mod
#2下载go
[root@jenkins go]# go get -u github.com/gin-gonic/gin #下载
#3.编写main.go文件
[root@jenkins go]# vim main.go
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run("0.0.0.0:8888") // listen and serve on 0.0.0.0:8080 #因8080端口被占用,修改默认访问端口8080,改为8888
}
#4.运行go
# 方式一:
[root@jenkins go]# go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on 0.0.0.0:8888
#方式二
[root@jenkins /opt/go]# go build main.go
[root@jenkins /opt/go]# ll
total 14556
-rw-r--r-- 1 root root 631 Aug 16 20:43 go.mod
-rw-r--r-- 1 root root 8914 Aug 16 20:46 go.sum
-rwxr-xr-x 1 root root 14883140 Aug 16 20:53 main
-rw-r--r-- 1 root root 235 Aug 16 20:48 main.go
[root@jenkins /opt/go]# ./main
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on 0.0.0.0:8888
第二步:部署环境(web)
1.准备源代码到远程仓库
#1.将源代码文件传到远程仓库
#下载远程仓库到本地
[root@k8s-m-01 go]# git clone git@192.168.15.101:root/jenkins-go.git
#将源代码移动到本地仓库
[root@k8s-m-01 go]# mv gin/ jenkins-go/
[root@k8s-m-01 go]# cd jenkins-go/
[root@k8s-m-01 jenkins-go]# ll
total 4
drwxr-xr-x 3 root root 88 Aug 16 21:07 gin
-rw-r--r-- 1 root root 24 Aug 16 21:09 README.md
[root@k8s-m-01 jenkins-golang]# git add gin/
#提交源代码到远程仓库
[root@k8s-m-01 jenkins-golang]# git commit -m 'init' .
[master d0dfdb5] init
3 files changed, 149 insertions(+)
create mode 100644 gin/README.en.md
create mode 100644 gin/README.md
create mode 100644 gin/main.go
[root@k8s-m-01 jenkins-go]# git push -u origin master
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 2.41 KiB | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To git@192.168.15.101:root/jenkins-go.git
349e7be..e4ce76a master -> master
Branch master set up to track remote branch master from origin.
#推送完成并创建标签
2.Jenkins部署
#!/bin/bash
git checkout $GIT_TAG
export GOPROXY=https://mirrors.aliyun.com/goproxy/
/usr/local/go/bin/go mod init jenkins-go
/usr/local/go/bin/go get -u github.com/gin-gonic/gin
rm -rf target && mkdir target
cd target
/usr/local/go/bin/go build ../gin/main.go
选择标签进行构建
在Jenkins服务器上测试构建
#go 保存的路径
[root@jenkins /usr/local/go/pkg/linux_amd64]# pwd
/usr/local/go/pkg/linux_amd64
[root@jenkins /usr/local/go/pkg/linux_amd64]# ls
archive crypto.a expvar.a html.a log net.a regexp sync time.a
bufio.a database flag.a image log.a os regexp.a sync.a unicode
bytes.a debug fmt.a image.a math os.a runtime syscall.a unicode.a
compress embed.a go index math.a path runtime.a testing vendor
container encoding hash internal mime path.a sort.a testing.a
context.a encoding.a hash.a io mime.a plugin.a strconv.a text
crypto errors.a html io.a net reflect.a string s.a time
构建前部署
# 先main同步到挂载目录
[root@jenkins /var/lib/jenkins/workspace/jenkins-go/target]# scp -r main 192.168.15.111:/nfs/v4
yaml部署
#1.编写配置清单上传远程仓库
[root@k8s-m-01 ~]# vim go.yaml
kind: Namespace
apiVersion: v1
metadata:
name: golang
---
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: golang
name: golang-deployment
spec:
selector:
matchLabels:
app: golang
deploy: main
template:
metadata:
labels:
app: golang
deploy: main
spec:
nodeName: gdx1
containers:
- name: golang
image: centos:7
command:
- "/bin/bash"
- "-c"
- "/opt/target/main"
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
livenessProbe:
exec:
command:
- "/bin/bash"
- "-c"
- "cat /opt/target/main"
periodSeconds: 2
readinessProbe:
tcpSocket:
port: 38080 #访问的端口
initialDelaySeconds: 30
periodSeconds: 2
volumeMounts:
- mountPath: /opt
name: golang-data
volumes:
- name: golang-data
nfs:
path: /nfs/v4 #挂载的目录
server: 192.168.15.111 #本机
---
kind: Service
apiVersion: v1
metadata:
name: golang-deployment-svc
namespace: golang
spec:
ports:
- port: 38080 #容器端口
targetPort: 38080 #外部映射端口
selector:
app: golang
deploy: main
clusterIP: None
---
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
namespace: golang
name: golang-ingress
spec:
rules:
- host: www.golang.cluster.local.com
http:
paths:
- backend:
serviceName: golang-deployment-svc
servicePort: 38080
path: /
构建后部署
#!/bin/bash
chmod +x /nfs/v9/target/main
kubectl get pods -n golang | awk '{print $1}' | grep -v "NAME" | xargs -I {} kubectl delete pod -n golang {}
3.测试访问
#!/bin/bash
chmod +x /nfs/v9/target/main
kubectl get pods -n golang | awk '{print $1}' | grep -v "NAME" | xargs -I {} kubectl delete pod -n golang {}
3.测试访问
以上是关于linux12Devops -->08jenkins部署Golang的主要内容,如果未能解决你的问题,请参考以下文章
linux12Devops -->04Jenkins参数化构建
linux12Devops -->06Jenkins部署tomcat
linux12Devops -->07jenkins部署python
linux12Devops -->08jenkins部署Golang