kuberneteskubernetesgolang
golang
对于 golang 的安装和配置,我们这里就不详细说明了,因为这也不是我们的重点,我相信这一步你是能够自己独立完成的。
golang
新建项目文件夹goappk8s,然后在该目录下面新建一个src目录
$ mkdir goappk8s && cd goappk8s
$ mkdir src
GOPATHsetup-gopath.sh
#!/bin/bash
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export GOPATH="$DIR"
GOPATH
$ source setup-gopath.sh
$ echo $GOPATH
/Users/ych/devs/workspace/yidianzhishi/goappk8s
GOPATH
$ mkdir -p github.com/cnych/goappk8s && cd github.com/cnych/goappk8s
$ touch main.go
golang
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "PONG")
})
router.Run(":8080")
}
github.com/gin-gonic/ginGOPATHgovendorgithub.com/cnych/goappk8s
$ govendor init
$ govendor fetch github.com/gin-gonic/gin
注意上面的包需要拉取一些墙外的包。
GOPATH
$ go install github.com/cnych/goappk8s && ./bin/goappk8s
[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 :8080
golangPONG
Docker
golangDockerfile
FROM golang AS build-env
ADD . /go/src/app
WORKDIR /go/src/app
RUN go get -u -v github.com/kardianos/govendor
RUN govendor sync
RUN GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-server
FROM alpine
RUN apk add -U tzdata
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=build-env /go/src/app/app-server /usr/local/bin/app-server
EXPOSE 8080
CMD [ "app-server" ]
Docker
$ docker build -t cnych/goappk8s:v1.0.0 .
.......(省略了)
Successfully built 00751f94d8a9
Successfully tagged cnych/goappk8s:v1.0.0
$ docker push cnych/goappk8s:v1.0.0
cnych/goappk8s:v1.0.0dockerhub
kubernetes
Kubernetes
kubernetesDockerfilekubernetes
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: goapp-deploy
namespace: kube-apps
labels:
k8s-app: goappk8s
spec:
replicas: 2
revisionHistoryLimit: 10
minReadySeconds: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
k8s-app: goappk8s
spec:
containers:
- image: cnych/goappk8s:v1.0.0
imagePullPolicy: Always
name: goappk8s
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 50m
memory: 50Mi
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 10
timeoutSeconds: 3
readinessProbe:
httpGet:
path: /ping
port: 8080
initialDelaySeconds: 10
timeoutSeconds: 2
---
apiVersion: v1
kind: Service
metadata:
name: goapp-svc
namespace: kube-apps
labels:
k8s-app: goappk8s
spec:
ports:
- name: api
port: 8080
protocol: TCP
targetPort: 8080
selector:
k8s-app: goappk8s
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: goapp-ingress
namespace: kube-apps
spec:
rules:
- host: goappk8s.local
http:
paths:
- path: /
backend:
serviceName: goapp-svc
servicePort: api
k8s.yamlDeploymentServiceIngressYAMLreplicas: 2PODstrategyRollingUpdateresourcesPODlivenessProbereadinessProbekubectl
$ kubectl apply -f k8s.yaml
deployment "goapp-deploy" created
service "goapp-svc" created
ingress "goapp-ingress" created
我们可以看到上面定义的三种资源都创建成功了。然后查看资源状态:
$ kubectl get deployments -n kube-apps |grep goapp
goapp-deploy 2 2 2 2 57s
$ kubectl get svc -n kube-apps |grep goapp
goapp-svc ClusterIP 10.254.109.69 <none> 8080/TCP 1m
$ kubectl get ingress -n kube-apps |grep goapp
goapp-ingress goappk8s.local 80 1m
$ kubectl get pods -n kube-apps |grep goapp
goapp-deploy-84bb6979c-59qkl 1/1 Running 0 2m
goapp-deploy-84bb6979c-mgg2r 1/1 Running 0 2m
kubernetesPOD/etc/hostsIngress
你的k8s集群节点IP goappk8s.local
然后我们在浏览器中访问链接http://goappk8s.local/ping,可以看到页面上已经打印出来PONG。
最后整理下代码,将我们的代码提交到github去吧。
CI/CDkubernetes
k8s技术圈