不同代码发布到k8s集群的细节和注意事项

1. 具体流程

  1. 写代码
  2. dockerfile构建镜像
  3. 把镜像传到harbor镜像仓库
  4. 编写k8s yaml文件,创建资源

2. go代码发布到k8s具体流程

2.1 编写代码

1
2
3
4
5
6
7
8
9
10
11
12
13
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": "hello,this is a test",
})
})
r.Run(":8888")
}

2.2 编译代码

1
2
3
4
5

go mod init test
go env -w GOPROXY=https://goproxy.cn,direct
go get github.com/gin-gonic/gin
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-demo main.go

2.3 编写dockerfile文件

1
2
3
4
5
FROM alpine:latest
ADD k8s-demo /data/app/
WORKDIR /data/app/
CMD ["/bin/sh","-c","./k8s-demo"]
EXPOSE 8888
1
docker build -t repo.792588.xyz:8443/k8s-demo:v1 .

2.4 打包镜像上传k8s集群中

1
docker save -o k8s-demo.tar repo.792588.xyz:8443/k8s-demo:v1

在k8s工作节点中倒入镜像

1
ctr -n k8s.io image import k8s-demo.tar

2.5 编写yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-demo
labels:
app: k8s-demo
spec:
selector:
matchLabels:
app: k8s-demo
replicas: 2
template:
metadata:
labels:
app: k8s-demo
spec:
containers:
- name: go
image: repo.792588.xyz:8443/k8s-demo:v1
imagePullPolicy: Never

2.6 编写svc文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: v1
kind: Service
metadata:
name: go-service
spec:
selector:
app: k8s-demo
type: NodePort
ports:
- name: http
port: 8888
targetPort: 8888
protocol: "TCP"
- name: https
port: 443
targetPort: 443
protocol: "TCP"

3. java代码发布到k8s的具体流程

3.1 拉取代码

1
2
3
4
yum install maven* -y
yum install git -y
git clone https://github.com/luckylucky421/SpringBootDemo.git
cd SpringBootDemo/

3.2 编译代码

1
mvn clean package

3.3 打包镜像

1
2
3
4
5
6
7
8
9
##Dockerfile
FROM repo.792588.xyz:8443/library/ibmjava:sfj

MAINTAINER zy

ADD target/www-0.0.1-SNAPSHOT.jar /app/www.jar
CMD ["java", "-Xmx200m", "-jar", "/app/www.jar"]

EXPOSE 8088
1
docker build -t repo.792588.xyz:8443/library/java:v1 .

3.4 镜像上传到dockerhub上

1
2
docker login repo.792588.xyz:8443
docker push repo.792588.xyz:8443/library/java:v1

3.5 编写deploy的yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: apps/v1
kind: Deployment
metadata:
name: java-web-deploy
labels:
app: java-web-deploy
spec:
replicas: 2
selector:
matchLabels:
app: java-web-deploy
template:
metadata:
name: java-web-deploy
labels:
app: java-web-deploy
spec:
containers:
- name: java-web-deploy
image: repo.792588.xyz:8443/library/java:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8088

3.6 编写svc的yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: Service
metadata:
name: java-web-svc
labels:
app: java-web-deploy
spec:
type: NodePort
selector:
app: java-web-deploy
ports:
- name: http
port: 8088
targetPort: 8088

4. 开源服务部署到k8s中的注意事项

4.1 部署nginx服务

在k8s中,Deployment控制器可以用来管理和部署应用程序。以nginx为例,以下是k8s中基于Deployment控制器部署nginx服务的一般步骤

  1. 创建nginx镜像:首先,你需要创建一个nginx镜像,其中包含nginx的安装和配置
  2. 定义deployment,在k8s中,使用yaml文件来定义deployment,该文件包含了你想要运行的信息,以及如何运行容器的详细信息,例如nginx服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

4.2 k8s部署tomcat服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deployment
labels:
app: tomcat-deployment
spec:
replicas: 2
selector:
matchLabels:
app: tomcat-deployment
template:
metadata:
name: tomcat-deployment
labels:
app: tomcat-deployment
spec:
containers:
- name: tomcat-deployment
image: tomcat:9.0.34
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080