Skip to content

Commit ba185af

Browse files
committed
add readme for kubernetes.
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent af1c2d6 commit ba185af

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Deploy golang app using drone with Kubernetes
2+
3+
See the [chinese blog](https://blog.wu-boy.com/2018/06/drone-kubernetes-with-golang/)
4+
5+
## write simple golang app
6+
7+
see the [main.go](./main.go)
8+
9+
```go
10+
package main
11+
12+
import (
13+
"log"
14+
"net/http"
15+
"os"
16+
"strings"
17+
)
18+
19+
var version = "master"
20+
21+
func showVersion(w http.ResponseWriter, r *http.Request) {
22+
log.Println(version)
23+
w.Write([]byte(version))
24+
}
25+
26+
func sayHello(w http.ResponseWriter, r *http.Request) {
27+
message := r.URL.Path
28+
message = strings.TrimPrefix(message, "/")
29+
message = "Hello, got the message: " + message
30+
log.Println(message)
31+
w.Write([]byte(message))
32+
}
33+
34+
func main() {
35+
// use PORT environment variable, or default to 8080
36+
port := "8080"
37+
if fromEnv := os.Getenv("PORT"); fromEnv != "" {
38+
port = fromEnv
39+
}
40+
http.HandleFunc("/version", showVersion)
41+
http.HandleFunc("/", sayHello)
42+
log.Println("Listen server on " + port + " port")
43+
if err := http.ListenAndServe(":"+port, nil); err != nil {
44+
log.Fatal(err)
45+
}
46+
}
47+
```
48+
49+
write app version using `-ldflags`. see the following example:
50+
51+
```makefile
52+
build:
53+
ifneq ($(DRONE_TAG),)
54+
go build -v -ldflags "-X main.version=$(DRONE_TAG)" -a -o release/linux/amd64/hello
55+
else
56+
go build -v -ldflags "-X main.version=$(DRONE_COMMIT)" -a -o release/linux/amd64/hello
57+
endif
58+
```
59+
60+
how to build binary using drone?
61+
62+
```yaml
63+
pipeline:
64+
build_linux_amd64:
65+
image: golang:1.10
66+
group: build
67+
environment:
68+
- GOOS=linux
69+
- GOARCH=amd64
70+
- CGO_ENABLED=0
71+
commands:
72+
- cd example19-deploy-with-kubernetes && make build
73+
```
74+
75+
## upload to dockerhub
76+
77+
See the drone config:
78+
79+
```yaml
80+
docker_golang:
81+
image: plugins/docker:17.12
82+
secrets: [ docker_username, docker_password ]
83+
repo: appleboy/golang-http
84+
dockerfile: example19-deploy-with-kubernetes/Dockerfile
85+
default_tags: true
86+
when:
87+
event: [ push, tag ]
88+
```
89+
90+
## rolling update using kubernetes
91+
92+
Please preare the following drone secrets
93+
94+
1. KUBERNETES_SERVER
95+
2. KUBERNETES_CERT
96+
3. KUBERNETES_TOKEN
97+
98+
then see the drone config
99+
100+
```yaml
101+
deploy:
102+
image: sh4d1/drone-kubernetes
103+
kubernetes_template: example19-deploy-with-kubernetes/deployment.yml
104+
kubernetes_namespace: default
105+
secrets: [ kubernetes_server, kubernetes_cert, kubernetes_token ]
106+
```
107+
108+
see the `deployment.yaml`
109+
110+
```yaml
111+
apiVersion: extensions/v1beta1
112+
kind: Deployment
113+
metadata:
114+
name: frontend
115+
# these labels can be applied automatically
116+
# from the labels in the pod template if not set
117+
labels:
118+
app: gotraining
119+
tier: frontend
120+
spec:
121+
# this replicas value is default
122+
# modify it according to your case
123+
replicas: 3
124+
# selector can be applied automatically
125+
# from the labels in the pod template if not set
126+
# selector:
127+
# app: guestbook
128+
# tier: frontend
129+
strategy:
130+
type: RollingUpdate
131+
rollingUpdate:
132+
maxSurge: 1
133+
maxUnavailable: 1
134+
minReadySeconds: 5
135+
template:
136+
metadata:
137+
labels:
138+
app: gotraining
139+
tier: frontend
140+
spec:
141+
containers:
142+
- name: go-hello
143+
image: appleboy/golang-http:VERSION
144+
imagePullPolicy: Always
145+
resources:
146+
requests:
147+
cpu: 100m
148+
memory: 100Mi
149+
ports:
150+
- containerPort: 8080
151+
env:
152+
- name: FOR_GODS_SAKE_PLEASE_REDEPLOY
153+
value: 'THIS_STRING_IS_REPLACED_DURING_BUILD'
154+
```

0 commit comments

Comments
 (0)