一、在服务器上编译go程序
1.1 编译go语言程序
# 安装go yum install go -y mkdir /root/test && cd /root/test # 设置代理 go env -w GOPROXY=https://goproxy.cn,direct # 创建源码文件
[root@master test]# cat main.go package main import ( "net/http" "github.com/gin-gonic/gin" ) func statusOKHander(c *gin.Context){ c.JSON(http.StatusOK,gin.H{"status":"success~welcome to study"}) } func versionHandler(c *gin.Context){ c.JSON(http.StatusOK,gin.H{"version":"v1.1版本"}) } func main() { router := gin.new() router.Use(gin.Recovery()) router.GET("/", statusOKHandler) router.GET("/version", versionHandler) router.Run(":8080") }
# Go mod初始化项目 [root@master test]# go mod init test go: creating new go.mod: module test go: to add module requirements and sums: go mod tidy [root@master test]# go mod tidy ... go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 go: downloading golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 # 构建源码 [root@master test]# CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-demo main.go [root@master test]# ll 总用量 10784 -rw-r--r-- 1 root root 1288 8月 30 15:17 go.mod -rw-r--r-- 1 root root 7701 8月 30 15:17 go.sum -rwxr-xr-x 1 root root 11024637 8月 30 15:27 k8s-demo -rw-r--r-- 1 root root 464 8月 30 15:27 main.go
1.2 编写dockerfile文件构建镜像
[root@master test]# cat dockerfile FROM alpine ADD k8s-demo /data/app/ WORKDIR /data/app/ CMD ["/bin/sh","-c","./k8s-demo"] [root@master test]# docker build -t harbor96.9you.com:6443/tools/k8sdemo:v1 . [root@master test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE harbor96.9you.com:6443/tools/k8sdemo v1 4084cba544d7 23 seconds ago 16.6MB # 将镜像上传到harbor,并导入到k8s工作节点 [root@master test]# docker save -o k8sdemo.tar.gz harbor96.9you.com:6443/tools/k8sdemo:v1 # 导入镜像 [root@node1 ~]# ctr -n=k8s.io images import k8sdemo.tar.gz
1.3 创建deployment的yaml文件
[root@master test]# cat k8s-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-demo
namespace: default
labels:
app: k8s-demo
spec:
selector:
matchLabels:
app: k8s-demo
replicas: 2
template:
metadata:
labels:
app: k8s-demo
spec:
containers:
- name: k8s-demo
image: harbor96.9you.com:6443/tools/k8sdemo:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 100m
memory: 200Mi
requests:
cpu: 50m
memory: 100Mi
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 10
timeoutSeconds: 3
readinessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 10
timeoutSeconds: 2
---
apiVersion: v1
kind: Service
metadata:
name: k8s-demo-svc
namespace: default
labels:
app: k8s-demo
spec:
selector:
app: k8s-demo
ports:
- name: api
port: 8080
protocol: TCP
targetPort: 8080
[root@master test]# kubectl apply -f k8s-demo.yaml deployment.apps/k8s-demo created service/k8s-demo-svc created [root@master test]# kubectl get pods -o wide \NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES k8s-demo-57b577c5c9-6grz5 1/1 Running 0 2m9s 10.244.104.20 node2 <none> <none> k8s-demo-57b577c5c9-mc5hb 1/1 Running 0 2m9s 10.244.166.158 node1 <none> <none> [root@master test]# kubectl get svc -n default NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE k8s-demo-svc ClusterIP 10.105.15.228 <none> 8080/TCP 93s # 将svc的TYPE类型从ClusterIP改为nodePort [root@master test]# kubectl edit svc k8s-demo-svc -n default type: NodePort # 修改这行 service/k8s-demo-svc edited
[root@master test]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE k8s-demo-svc NodePort 10.105.15.228 <none> 8080:30485/TCP 5m19s # 浏览器访问: http://192.168.10.10:30485 返回:{"status":"success~welcome to study"} http://192.168.10.10:30485/version 返回:{"version":"v1.1版本"}