Skip to content

Instantly share code, notes, and snippets.

@timosalm
Last active April 24, 2020 07:24
Show Gist options
  • Save timosalm/c38cf4463e490a14e507ed8ecc296821 to your computer and use it in GitHub Desktop.
Save timosalm/c38cf4463e490a14e507ed8ecc296821 to your computer and use it in GitHub Desktop.
Introduction to kpack a Kubernetes Native Container Build Service

Prerequisites

(Optional) Install local Kubernetes cluster

  1. Install kind with go 1.11+ or see https://github.com/kubernetes-sigs/kind for alternatives
GO111MODULE="on" go get sigs.k8s.io/kind@v0.7.0
  1. Create cluster
kind create cluster
  1. Check current context
kubectl config current-context

Alternatives

  1. Download kpack release from Github, see https://github.com/pivotal/kpack/releases for the most recent release
kubectl apply -f https://github.com/pivotal/kpack/releases/download/v0.0.8/release-0.0.8.yaml
  1. Ensure that the kpack controller has a status of "Running"
kubectl get pods --namespace kpack --watch
  1. Check the logs to confirm the kpack controller started without error
kubectl -n kpack logs deployment/kpack-controller -f
  1. Create a cluster-builder.yaml file based on the 2-cluster-builder.yaml file below for a ClusterBuilder resource and apply
kubectl apply -f cluster-builder.yaml
  1. Ensure that kpack has processed the builder
kubectl get clusterbuilder default-builder -o yaml

Using kpack

  1. Create a secret.yaml file with your push credentials for the docker registry based on the 3-secret.yaml file below and apply
kubectl apply -f secret.yaml
  1. Create a service-account.yaml file that references the created registry secret based on the 4-service-account.yaml file below and apply
kubectl apply -f service-account.yaml
  1. Fork the sample repository https://github.com/buildpack/sample-java-app to test the automatic rebuild functionality

  2. Create a kpack image configuration file (image.yaml) with your forked repository based on the 5-image.yaml file below and apply

kubectl apply -f image.yaml
  1. Download the kpack log untility (https://github.com/pivotal/kpack/blob/master/docs/logs.md) and tail logs from all builds for an image:
./logs -image java-sample-image
  1. Push any update to the forked sample app repository. kpack should recognize the update and automatically rebuild your image.

You can see by viewing the logs(step #5) or by running

kubectl get builds
  1. Run the latest image of the app locally with docker
docker run -p 8080:8080 index.docker.io/<docker-registry-username>/kpack-sample-java-app
  1. Open localhost:8080 in your favorite browser

Destroy the cluster

kind delete cluster
apiVersion: build.pivotal.io/v1alpha1
kind: ClusterBuilder
metadata:
name: default-builder
spec:
image: gcr.io/paketo-buildpacks/builder:full-cf
updatePolicy: polling
apiVersion: v1
kind: Secret
metadata:
name: registry-credentials
annotations:
build.pivotal.io/docker: https://index.docker.io/v1/
type: kubernetes.io/basic-auth
stringData:
username: <docker-registry-username>
password: <docker-registry-password>
apiVersion: v1
kind: ServiceAccount
metadata:
name: service-account
secrets:
- name: registry-credentials
apiVersion: build.pivotal.io/v1alpha1
kind: Image
metadata:
name: java-sample-image
spec:
tag: <docker-registry-username>/kpack-sample-java-app
serviceAccount: service-account
builder:
name: default-builder
kind: ClusterBuilder
source:
git:
url: https://github.com/<github-username>/sample-java-app
revision: master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment