Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From source code to Kubernetes, a Continuous De...

From source code to Kubernetes, a Continuous Deployment tale

This talk will cover the basics of creating good Docker images for your Go projects using multi-stage builds. Once we have that image ready we will need to run it somewhere, I think that a Kubernetes cluster could be a nice place to start. Of course, we don’t want to repeat those steps by hand everytime we want to do a new deployment so we will probably need to learn how to use a CI/CD tool to automate all the steps. And finally, the icing on the cake would be make this automagically run everytime we push code to our repository.

Alexandre González

August 03, 2018
Tweet

More Decks by Alexandre González

Other Decks in Technology

Transcript

  1. main.go func mainHandler(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, message) }

    func main() { r := mux.NewRouter() r.HandleFunc("/", mainHandler)
  2. main.go func mainHandler(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, message) }

    func main() { r := mux.NewRouter() r.HandleFunc("/", mainHandler) port := ":8081" if v, ok := os.LookupEnv("PORT"); ok { port = ":" + v }
  3. main.go func main() { r := mux.NewRouter() r.HandleFunc("/", mainHandler) port

    := ":8081" if v, ok := os.LookupEnv("PORT"); ok { port = ":" + v } log.Println("Listening on " + port) log.Fatal(http.ListenAndServe(port, r)) }
  4. Dockerfile FROM golang:1.10-alpine3.7 as builder WORKDIR /go/src/github.com/gophercon RUN apk --no-cache

    add git RUN go get -u github.com/golang/dep/cmd/dep COPY Gopkg.lock Gopkg.toml ./ RUN dep ensure -vendor-only
  5. Dockerfile FROM golang:1.10-alpine3.7 as builder ... COPY . ./ RUN

    go build FROM alpine:3.7 COPY --from=builder /go/src/github.com/gophercon ./
  6. Dockerfile FROM golang:1.10-alpine3.7 as builder ... COPY . ./ RUN

    go build FROM alpine:3.7 COPY --from=builder /go/src/github.com/gophercon ./ CMD ["./gophercon"]
  7. pod.yaml apiVersion: v1 kind: Pod metadata: name: just-a-pod spec: containers:

    - name: gophercon image: agonzalezro/gophercon:latest
  8. pod.yaml apiVersion: v1 kind: Pod metadata: name: just-a-pod spec: containers:

    - name: gophercon image: agonzalezro/gophercon:latest
  9. deployment.yaml ... template: metadata: labels: app: gophercon spec: containers: -

    name: gophercon # Don't use latest at home! image: agonzalezro/gophercon:latest
  10. service.yaml ... spec: type: LoadBalancer ports: - name: gophercon port:

    80 targetPort: 8081 protocol: TCP selector: app: gophercon
  11. 1. Only deploy if tested 2. Vulnerability scanner 3. Repo

    should ping CI, CI should deploy to k8s
  12. $ helm install -f hack/values.yaml stable/jenkins NAME: zooming-wildebeest LAST DEPLOYED:

    Sat Jun 9 22:27:22 2018 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/Secret