Primary focus of Developer is to write code. Build, Test and Deploy of the application are better left to be managed by tools. skaffold can help in automating some of mundane tasks that comes with using kubernetes.
Skaffold facilitates fast local development with kubernetes as Developers go through iterative code and test cycle. As you code and save the file, skaffold detects changes initiates a pipeline to build, tag and deploy application to your local or remote kubernetes cluster.
Install
Virtual Box: https://www.virtualbox.org/wiki/Downloads
Minikube: https://kubernetes.io/docs/tasks/tools/install-minikube/
Skaffold: https://skaffold.dev/docs/install/
Start minikube
$ minikube start --driver=virtualbox
Lets take a simple hello world example written in python and walk through the work flow.
app.py
from flask import Flask
app = Flask('hello')
@app.route('/')
def hello():
return "Hello World!\n"
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 8080)
Dockerfile to create container image.
FROM python:3.7-slim
RUN pip install flask
WORKDIR /app
COPY app.py /app/app.py
ENTRYPOINT ["python"]
CMD ["/app/app.py"]
kubernetes yaml to deploy pod for testing. pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: hello-app
spec:
containers:
- name: hello-app
image: hello-app
In skaffold configuration yaml file we specify steps to build container image and use pod.yaml to deploy hello-app on minikube.
skaffold.yaml
apiVersion: skaffold/v2beta1
kind: Config
metadata:
name: hello-app
build:
artifacts:
- image: hello-app
deploy:
kubectl:
manifests:
- pod.yaml
skaffold dev
command will watch an application’s source files, and when it detects changes, will rebuild your images, push any new images, and redeploy the application to your local cluster.
$ skaffold dev
Listing files to watch...
- hello-app
Generating tags...
- hello-app -> hello-app:302875c-dirty
Checking cache...
- hello-app: Not found. Building
Found [minikube] context, using local docker daemon.
Building [hello-app]...
...
Successfully built cae7e96d9aa6
Successfully tagged hello-app:302875c-dirty
...
Starting deploy...
- pod/hello-app created
skaffold runs the build and deploy steps.
Check the pod deployed.
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-app 1/1 Running 0 36s
Use port-forwarding to access the pod.
$ kubectl port-forward pod/hello-app 8080
$ curl localhost:8080
Hello World!
Make any change, save file. skaffold dev automatically runs build and deploy.
$ curl localhost:8080
Hello New World!
skaffold can help increasing Developer productivity by automating build, tag and deploy of application to local kubernetes cluster.
Photo by Christopher Gower on Unsplash