|
1 | 1 | # Kaniko example |
2 | 2 |
|
3 | | -In this example is shown, how kaniko can be used instead of docker to build and push an docker image. |
| 3 | +This example shows how kaniko can be used instead of docker to build and push an docker image directly inside the cluster. |
| 4 | + |
| 5 | +# Step 0: Prerequisites |
| 6 | + |
| 7 | +In order for this example to work you need access to a docker registry, where you can push images to (e.g. hub.docker.com, gcr.io etc.). There are two options how you can push images to registries with devspace. |
| 8 | + |
| 9 | +## Option 1: Use docker credentials store |
| 10 | +If you have docker installed, devspace can take the required auth information directly out of the docker credentials store and will create the needed secret for you in the target cluster automatically. Make sure you are logged in the registry with `docker login`. |
| 11 | + |
| 12 | +## Option 2: Provide auth information yourself |
| 13 | +As a second option you can provide your credentials directly in the config.yaml. See example below: |
| 14 | + |
| 15 | +```yaml |
| 16 | +images: |
| 17 | + default: |
| 18 | + build: |
| 19 | + kaniko: |
| 20 | + cache: true |
| 21 | + namespace: "" |
| 22 | + # Don't prefix image name with registry url |
| 23 | + name: name/devspace |
| 24 | + registry: myRegistry |
| 25 | +registries: |
| 26 | + myRegistry: |
| 27 | + # Registry url here |
| 28 | + url: gcr.io |
| 29 | + auth: |
| 30 | + username: my-user |
| 31 | + password: my-password |
| 32 | +``` |
| 33 | +
|
| 34 | +devspace will then automatically create a secret for you which kaniko can use to push to that registry. |
| 35 | +
|
| 36 | +## Optional: Use self hosted cluster (minikube, GKS etc.) instead of devspace-cloud |
| 37 | +
|
| 38 | +If you want to use your own cluster instead of the devspace-cloud as deployment target, make sure `kubectl` is configured correctly to access the target cluster. Then just erase the `cluster` section in the `.devspace/config.yaml` and devspace will use your current kubectl context as deployment target. |
| 39 | + |
| 40 | +# Step 1: Start the devspace |
| 41 | + |
| 42 | +To deploy the application to the target cluster simply run `devspace up`. The output of the command should look similar to this: |
| 43 | + |
| 44 | +``` |
| 45 | +[INFO] Building image 'fabian1991/devspace' with engine 'kaniko' |
| 46 | +[DONE] √ Authentication successful (hub.docker.com) |
| 47 | +[DONE] √ Kaniko build pod started |
| 48 | +[DONE] √ Uploaded files to container |
| 49 | +[INFO] build >>> WARN[0001] Error while retrieving image from cache: getting image from path: open /cache/sha256:df8db4a3a7dee9782e0f1bdcc9d676bc2de0dc1d2dc2952d9b9b3718445b1455: no such file or directory |
| 50 | +[INFO] build >>> INFO[0001] Downloading base image golang:1.11 |
| 51 | +[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:27:56.335503 34 metadata.go:142] while reading 'google-dockercfg' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg |
| 52 | +[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:27:56.337941 34 metadata.go:159] while reading 'google-dockercfg-url' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg-url |
| 53 | +[INFO] build >>> INFO[0002] Executing 0 build triggers |
| 54 | +[INFO] build >>> INFO[0002] Extracting layer 0 |
| 55 | +[INFO] build >>> INFO[0017] Extracting layer 1 |
| 56 | +[INFO] build >>> INFO[0020] Extracting layer 2 |
| 57 | +[INFO] build >>> INFO[0021] Extracting layer 3 |
| 58 | +[INFO] build >>> INFO[0040] Extracting layer 4 |
| 59 | +[INFO] build >>> INFO[0060] Extracting layer 5 |
| 60 | +[INFO] build >>> INFO[0103] Extracting layer 6 |
| 61 | +[INFO] build >>> INFO[0104] Taking snapshot of full filesystem... |
| 62 | +[INFO] build >>> INFO[0503] RUN mkdir -p "$GOPATH/src/app" && ln -s $GOPATH/src/app /app |
| 63 | +[INFO] build >>> INFO[0503] Checking for cached layer index.docker.io/fabian1991/devspace/cache:f141885fc82d849f3eba2d72bf74ce9842b3f9874ef5b003dd9b846726ee46b4... |
| 64 | +[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:36:18.645323 34 metadata.go:142] while reading 'google-dockercfg' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg |
| 65 | +[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:36:18.648540 34 metadata.go:159] while reading 'google-dockercfg-url' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg-url |
| 66 | +[INFO] build >>> INFO[0505] No cached layer found, executing command... |
| 67 | +[INFO] build >>> INFO[0505] cmd: /bin/sh |
| 68 | +[INFO] build >>> INFO[0505] args: [-c mkdir -p "$GOPATH/src/app" && ln -s $GOPATH/src/app /app] |
| 69 | +[INFO] build >>> INFO[0506] Using files from context: [/src] |
| 70 | +[INFO] build >>> INFO[0506] ADD . $GOPATH/src/app |
| 71 | +[INFO] build >>> INFO[0506] RUN cd $GOPATH/src/app && go get ./... && go build . && cd /app |
| 72 | +[INFO] build >>> INFO[0506] Checking for cached layer index.docker.io/fabian1991/devspace/cache:c58d7863dc5744a3c34de75247c0fa5f6d0a0bcaeb46981ff1c190470bc277a4... |
| 73 | +[INFO] build >>> INFO[0507] No cached layer found, executing command... |
| 74 | +[INFO] build >>> INFO[0507] cmd: /bin/sh |
| 75 | +[INFO] build >>> INFO[0507] args: [-c cd $GOPATH/src/app && go get ./... && go build . && cd /app] |
| 76 | +[INFO] build >>> INFO[0533] WORKDIR /app |
| 77 | +[INFO] build >>> INFO[0533] cmd: workdir |
| 78 | +[INFO] build >>> INFO[0533] Changed working directory to /app |
| 79 | +[INFO] build >>> INFO[0533] CMD ["$GOPATH/src/app/app"] |
| 80 | +[INFO] build >>> INFO[0533] Taking snapshot of full filesystem... |
| 81 | +[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:42:58.250833 34 metadata.go:142] while reading 'google-dockercfg' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg |
| 82 | +[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:42:58.255121 34 metadata.go:159] while reading 'google-dockercfg-url' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg-url |
| 83 | +[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:bc9ab73e5b14b9fbd3687a4d8c1f1360533d6ee9ffc3f5ecc6630794b40257b7 |
| 84 | +[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:997731689cfbc58c8e74f2a20079338ce66965a40b21f27169b3d5a45ab61cbd |
| 85 | +[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:1bc310ac474b880a5e4aeec02e6423d1304d137f1a8990074cb3ac6386a0b654 |
| 86 | +[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:e5c3f8c317dc30af45021092a3d76f16ba7aa1ee5f18fec742c84d4960818580 |
| 87 | +[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:193a6306c92af328dbd41bbbd3200a2c90802624cccfe5725223324428110d7f |
| 88 | +[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:202760eb4a0043cd84cd9971c47052617855ff653abec8ae479e89d369afd500 |
| 89 | +[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:a587a86c9dcb9df6584180042becf21e36ecd8b460a761711227b4b06889a005 |
| 90 | +[INFO] build >>> 2018/10/18 09:43:00 pushed blob sha256:3299c5b4c55d202d9faab27d32f34cda6622d1f3d2d40c9ff30d16949aed41dc |
| 91 | +[INFO] build >>> 2018/10/18 09:43:03 pushed blob sha256:c425aa94dbf6aa35a519a459df4536d905cc4be08ecb803ad2721a917c27c4d2 |
| 92 | +[INFO] build >>> 2018/10/18 09:43:03 index.docker.io/fabian1991/devspace:82i7ApF: digest: sha256:16639a35fd55e1b982e50b6ae1a4039d224c820e5fb4387ba9819d816e922cc1 size: 1578 |
| 93 | +[DONE] √ Done building image |
| 94 | +[INFO] Image pushed to registry (hub.docker.com) |
| 95 | +[DONE] √ Done building and pushing image 'fabian1991/devspace' |
| 96 | +[INFO] Deploying devspace-default with helm |
| 97 | +[DONE] √ Deployed helm chart (Release revision: 2) |
| 98 | +[DONE] √ Successfully deployed devspace-default |
| 99 | +[DONE] √ Sync started on /go-workspace/src/github.com/covexo/devspace/examples/kaniko <-> /app (Pod: e388779b2b49465855bb0322057a9fff/devspace-default-864d677f99-t5488) |
| 100 | +root@devspace-default-864d677f99-t5488:/go/src/app# |
| 101 | +``` |
| 102 | + |
| 103 | +The command created a new kubernetes namespace for you in the devspace-cloud and built your Dockerfile with a kaniko build pod and pushed it to the target docker registry. Afterwards, it deployed the chart in the `chart` folder to that namespace. It also created a new kubectl context for you. If you want to access kubernetes resources via kubectl in the devspace-cloud you can simply change your kubectl context via `devspace up --switch-context`. Now you can check the running pods via `kubectl get po`. |
| 104 | + |
| 105 | +Furthermore a bi-directional sync was started between the local folder `/go-workspace/src/github.com/covexo/devspace/examples/kaniko` and `/app` within the docker container. Whenever you change a file in either of those two folders the change will be synchronized. In addition the container port 8080 was forwarded to your local port 8080. |
| 106 | + |
| 107 | +# Step 2: Start developing |
| 108 | + |
| 109 | +You can start the server now with `go run main.go` in the open terminal. Now navigate in your browser to `localhost:8080` and you should see the output 'Hello World!'. |
| 110 | + |
| 111 | +Change something in `main.go` and re-run `go run main.go`. Now just refresh your browser and you should see the changes immediately. |
0 commit comments