文章目录
Jenkins部署Python+Golang
Python
编译环境(python)
部署环境
一、部署环境
1、设计
user ---> ingress ---> endprints ---> pod
# 因为endpoints上面有ingress,所以在写配置清单时要用无头service
# ingress
URL: www.django.cluster.local.com
# endPrints
service ---> 8080
# Pod
django:2+
## 构建镜像 :python:3.6.13
2.准备代码
1)克隆仓库
- k8s-master1 部署一个基础环境
- 克隆并生成,上传至自己的代码仓库
- 过程中若需要输入密码,则需将ssh公钥添加到gitlab内,否则密码正确也无效!
[root@k8s-master1 ~]# git clone git@gitee.com:chenyang0910/django_test.git
[root@k8s-master1 django_test]# cd django_test/
[root@k8s-master1 django_test]# rm -rf .git/
[root@k8s-master1 django_test]#
git init
git add .git remote add origin git@192.168.12.82:root/django.git
git remote add origin git@192.168.12.82:root/django.git
git add .
git commit -m 'init'
git push -u origin master
# 拷贝代码链接
git@192.168.12.82:root/django.git # ssh
http://192.168.12.82/root/django.git # http
2)编写Dockerfile
- 编写jenkins-python的Dockerfile与启动脚本
1.编写Dockerfile
[root@k8s-master1 django_test]# cat > Doekerfile <<EOf
# 指定基础镜像
FROM python:3.6.13
# 安装django框架
RUN pip3 install django==2.0.7
# 设置需要挂载的目录
VOLUME ['/opt']
# 暴露端口,默认也是8080
EXPOSE 8080
# 设置工作目录
WORKDIR /opt
# 启动命令,监听所有ip的8080端口
CMD ["./start"]
EOF
2.编写启动脚本
[root@k8s-master1 django_test]# cat > start <<EOF
#!/bin/bash
# 安装插件
pip3 install -r requirements.txt
python manage.py runserver 0.0.0.0:8080
EOF
[root@k8s-master1 django_test]# chmod +x start
2)构建镜像
# 构建
[root@k8s-master1 django_test]# docker build -t 18954354671/jenkins-python:py3.6.13-dj2.0.7 .
# 推送
[root@k8s-master1 django_test]# docker push 18954354671/jenkins-python:py3.6.13-dj2.0.7
4)编写配置清单
- 部署python配置清单
cat > django-python.yaml <<EOF
kind: Namespace
apiVersion: v1
metadata:
name: python-mysql
---
kind: Service
apiVersion: v1
metadata:
name: python-mysql-svc
namespace: python-mysql
spec:
ports:
- port: 3306
targetPort: 3306
selector:
app: python-mysql
deploy: mysql
---
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: python-mysql
name: python-mysql-deployment
spec:
selector:
matchLabels:
app: python-mysql
deploy: mysql
template:
metadata:
labels:
app: python-mysql
deploy: mysql
spec:
nodeName: k8s-master1
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
- name: MYSQL_DATABASE
value: "shuibianba"
---
kind: Namespace
apiVersion: v1
metadata:
name: python-django
---
kind: Service
apiVersion: v1
metadata:
name: python-django-svc
namespace: python-django
spec:
ports:
- port: 8080
targetPort: 8080
clusterIP: None
selector:
app: python-django
deploy: django
---
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: python-django
name: python-django-svc
spec:
selector:
matchLabels:
app: python-django
deploy: django
template:
metadata:
labels:
app: python-django
deploy: django
spec:
nodeName: k8s-master1
containers:
- name: django
image: 18954354671/jenkins-python:py3.6.13-dj2.0.7
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
volumeMounts:
- mountPath: /opt
name: python-django-nfs
volumes:
- name: python-django-nfs
nfs:
path: /nfs/v5
server: 172.16.12.11
---
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: python-django-ingress
namespace: python-django
spec:
rules:
- host: www.django.cluster.local.com
http:
paths:
- backend:
serviceName: python-django-svc
servicePort: 8080
path: /
EOF
二、部署环境
1.部署python
[root@k8s-master1 ~]# kubectl django-python.yaml
[root@k8s-master1 ~]# kubectl get ingress -n python-django
NAME CLASS HOSTS ADDRESS PORTS AGE
python-django-ingress <none> www.django.cluster.local.com 80 2m33s
[root@k8s-master1 ~]# kubectl get pod -n python-mysql
NAME READY STATUS RESTARTS AGE
python-mysql-deployment-7f8c9ff55-srgs6 1/1 Running 0 10m
# 查看访问域名
[root@k8s-master1 ~]# kubectl get ingress -n python-django
NAME CLASS HOSTS ADDRESS PORTS AGE
python-django-ingress <none> www.django.cluster.local.com 80 11m
# 查看访问端口:32145
[root@k8s-master1 ~]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.111.205.92 <none> 80:32145/TCP,443:32075/TCP 4d22h
2.访问测试
- 访问到如下界面即为成功
www.django.cluster.local.com:32145
Golang
编译环境:
1、安装go语言
2、优化测试
部署环境:
1、部署
一、编译环境(Jenkins)
1、安装golang语言
# 下载Golang
wget https://golang.google.cn/dl/go1.16.3.linux-amd64.tar.gz
# 安装
[root@jenkins opt]# tar -xf go1.16.3.linux-amd64.tar.gz -C /usr/local/
# 添加环境变量
[root@jenkins ~]# vim /etc/profile
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 ~]# vim /etc/profile
[root@jenkins ~]# source /etc/profile
[root@jenkins ~]# printenv | grep GO
GO_PATH=/opt/go
GOPROXY=https://mirrors.aliyun.com/goproxy/
GO_HOME=/usr/local/go
GO_ROOT=/usr/local/go
2、测试
[root@jenkins go]# mkdir /opt/go
[root@jenkins go]# cd /opt/go
[root@jenkins go]# go mod init jenkins-go
[root@jenkins go]# go get -u github.com/gin-gonic/gin
[root@jenkins go]# cat > main.go <<EOF
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
}
EOF
[root@jenkins go]# go run main.go
[root@jenkins go]# curl http://192.168.12.83:8888/ping
{"message":"pong"}
二、部署环境(web)
1.jenkins部署
- 只需运行构建的二进制文件
2.构建脚本
#!/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 ../main.go
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 {}
/nfs/v9/target/main &>/dev/null &
cat > golang.yaml <<EOF
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: k8s-master1
containers:
- name: golanbg
image: centos:7
command:
- "/bin/sh"
- "-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/v6
server: 172.16.12.11
---
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-ingrees
spec:
rules:
- host: www.golang.cluster.local.com
http:
paths:
- backend:
serviceName: golang-deployment-svc
servicePort: 38080
path: /
EOF
4.访问
http://www.golang.cluster.local.com:38080/user/
user/内容随便跟