Make your own docker image and publish it to k8s

This article mainly describes how to generate docker image from your program and publish it to k8s environment. Before that, you need to understand the basic docker usage, basic k8s concepts and commands.

  • Step1 Coding
    To facilitate the use of the previous code, put the KV value into the ETC cluster. The code is as follows:

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "log"
        "time"
    
        "crypto/tls"
        "crypto/x509"
    
        "go.etcd.io/etcd/clientv3"
        "golang.org/x/net/context"
    )
    
    var (
        dialTimeout    = 5 * time.Second
        requestTimeout = 4 * time.Second
        endpoints      = []string{"https://172.17.84.204:2379", "https://172.17.84.205:2379", "https://172.17.84.206:2379"}
    )
    
    func main() {
    
        log.Println("----Start running-----------")
        var etcdCert = "../ca/etcd-client.pem"
        var etcdCertKey = "../ca/etcd-client-key.pem"
        var etcdCa = "../ca/ca.pem"
    
        cert, err := tls.LoadX509KeyPair(etcdCert, etcdCertKey)
        if err != nil {
            log.Fatal("LoadX509KeyPair failed")
            return
        }
    
        caData, err := ioutil.ReadFile(etcdCa)
        if err != nil {
            log.Fatal("ReadFile(etcdCa) failed")
            return
        }
    
        pool := x509.NewCertPool()
        pool.AppendCertsFromPEM(caData)
    
        _tlsConfig := &tls.Config{
            Certificates: []tls.Certificate{cert},
            RootCAs:      pool,
        }
    
        cfg := clientv3.Config{
            Endpoints: endpoints,
            TLS:       _tlsConfig,
        }
    
        cli, err := clientv3.New(cfg)
    
        if err != nil {
            log.Fatal(err)
        }
    
        defer cli.Close()
    
        done := make(chan bool)
    
        key1 := "testkey1"
    
        go func() {
            log.Println("----Start putting value-----------")
            for cnt := 0; cnt < 65535; cnt++ {
                log.Println(cnt)
                value := fmt.Sprintf("%s%d", "value", cnt)
                _, err = cli.Put(context.Background(), key1, value)
                if err != nil {
                    log.Println("Put failed. ", err)
                } else {
                    log.Printf("Put {%s:%s} succeed\n", key1, value)
                }
            }
            done <- true
        }()
    
        <-done
    
        log.Println("Done!")
    }
    

    Compile code to generate executable files under Linux

    GOOS=linux GOARCH=amd64 go build
  • Step2 Writes Dockerfile

    FROM alpine:latest
    RUN mkdir /app
    RUN mkdir /ca
    WORKDIR /app
    ADD ./putvalue /app/putvalue
    CMD ["./putvalue"]
  • Step3 Generates Docker Image
    For convenience, the generated image is sent directly to docker hub. Please register by yourself. https://hub.docker.com

    (1) Execute docker login landing

    (2) Executing commands in the Dockerfile directory to generate docker images

    docker build -t etcdpvimg .

    If the build succeeds, the following information will be displayed

    (3) tag and push to docker hub
    Execute commands to tag the generated image

    docker tag etcdpvimg your_account_xxx/etcdpvimg:latest

    push mirror to docker hub

    docker push your_account_xxx/etcdpvimg:latest
  • Step4 Writes the K8s Deployment deployment file
    Because access to ETC requires the use of certificates, use hosPath to mount the file directory on the host machine on Pod
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: etcd-put-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: etcd-putvalue
  template:
    metadata:
      labels:
        app: etcd-putvalue
    spec:
      volumes:
        - name: cavol
          hostPath:
            path: "/ca"
      containers:
        - name: putvalue-test
          image: your_account_xxx/etcdpvimg:latest
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: /root/app/ca
              name: cavol

  • Step5 release test

    Execute the apply command of K8s

    kubectl apply -f etcd-test-deployment.yml

Keywords: Go Docker Linux

Added by geekette on Fri, 11 Oct 2019 01:02:44 +0300