In the process of developing kubernetes application, we usually deploy to Kubernetes cluster by CI/CD after local development and debugging test. This process is very tedious at first, and very inefficient, because you want to verify your every code modification. We had to submit the code and go through the CI/CD process again. We knew that compiling and packaging the image was time-consuming. Even if we built a kubernetes cluster locally, it would also be inefficient. In practice, debugging becomes challenging without running those services locally. Just a few days ago, I came across Skaffold, a command line tool designed to facilitate the continued development of Kubernetes applications. Skaffold automates the process of building, pushing, and deploying applications to a Kubernetes cluster
introduce
Skaffold is a command line tool designed to facilitate the continued development of Kubernetes applications. You can iterate over the application’s source code locally and then deploy it to a local or remote Kubernetes cluster. Skaffold handles workflows for build, upload, and application deployment. It can be used in automated environments, such as CI/CD pipelinization, to implement the same workflow and as a tool when migrating applications to production – Skaffold official documentation
Skaffold features:
- There are no server-side components, so it doesn’t add to your cluster overhead
- Automatically detects changes in source code and automatically builds/pushes/deploys
- Automatically update imagesTAGDon’t worry about making changes manually
kubernetes
The manifest file - Build/deploy/upload different applications at once, so it’s also perfect for microservices
- Support development and production environments by running the MANIFEST only once, or continuously observing changes
In additionSkaffold
Is a pluggable architecture that allows developers to choose which workflow tool is most appropriate for them
We can see it in the GIF belowSkaffold
The use of
use
The best way to use Skaffold is to install a single-node Kubernetes cluster locally, such as Minikube or The Edge version of Docker for MAC/Windows
The installation
You will need to install the following components to start using Skaffold:
1. skaffold
To download the latest Linux version, run the following command:
$ curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64 && chmod +x skaffold && sudo mv skaffold /usr/local/binCopy the code
To download the latest version of OSX, run:
$ curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-darwin-amd64 && chmod +x skaffold && sudo mv skaffold /usr/local/bin
Copy the code
Of course, if for some reason you can’t access the above link, you can go to Skaffold’s Github Release page to download the installation package.
2. Kubernetes cluster
Minikube, GKE, Docker for Mac (Edge) and Docker for Windows (Edge) have been tested, but any Kubernetes cluster is available. For simplicity, I’m using Docker for Mac (Edge)
3. kubectl
To use Kubernetes, kubectl must be developed in the current context of your target cluster on a local configuration
4. Docker
I don’t think I need to say more about that, do I?
5. Docker image warehouse
If you have a private mirror repository, you need to configure login authentication and so on. Here for convenience, I will directly use Docker Hub, of course, to push the image above, you have to go to Docker Hub in advance to register an account
The development of
We can develop a very simple application locally and iterate through Skaffold. Here we use the official Skaffold example directly, first clone the code:
$ git clone https://github.com/GoogleCloudPlatform/skaffold
Copy the code
Then we navigate to examples/ geing-started:
$CD examples/ Getting - Started $tree.. ├── Finger Exercises - - Finger Exercises - - Finger Exercises - - Finger Exercises - - Finger Exercises - - Finger Exercises - - Finger Exercises skaffold.yaml 0 directories, 5 filesCopy the code
Below this directory is a very simple golang program :(main.go)
package main import ( "fmt" "time" ) func main() { for { fmt.Println("Hello Skaffold!" ) time.Sleep(time.Second * 2) } }Copy the code
Yaml file can be ignored for the moment. This file is used in combination with Google Cloud. We can take a look at the content of skaffold.yaml file. Here I have changed the image name to my own (cnych/skaffold-example) as follows:
apiVersion: skaffold/v1alpha1
kind: Config
build:
artifacts:
- imageName: cnych/skaffold-example
workspace: .
local: {}
deploy:
kubectl:
manifests:
- paths:
- k8s-*
parameters:
IMAGE_NAME: cnych/skaffold-example
Copy the code
Yaml file, where the image name is an IMAGE_NAME parameter, Skaffold will automatically replace the image address for us, so we don’t need to make any changes, as follows:
apiVersion: v1
kind: Pod
metadata:
name: getting-started
spec:
containers:
- name: getting-started
image: IMAGE_NAME
Copy the code
We can then execute the skaffold dev command in the get-started directory:
$ skaffold dev Starting build... Found minikube or Docker for Desktop context, Using local docker daemon. Sending build context to docker daemon 6.144KB Step 1/5: FROM GOLang: 1.9.4-ALPINE3.7 --> FB6E10BF973b Step 2/5: WORKDIR /go/src/github.com/GoogleCloudPlatform/skaffold/examples/getting-started ---> Using cache ---> e6ae5322ee52 Step 3/5 : CMD ["./app"] ---> Using cache ---> bac5f3fd392e Step 4/5 : COPY main.go . ---> Using cache ---> 47fa1e536263 Step 5/5 : RUN go build -o app main.go ---> Using cache ---> f1470fe9f398 Successfully built f1470fe9f398 Successfully tagged a250d03203f9a5df267d8ad63bae8dba:latest Successfully tagged cnych/skaffold-example:f1470fe9f3984775f5dea87b5f720d67b6c2eeaaf2ca5efd1ca3c3ec7c4d4cce Build complete. Starting deploy... Deploying k8s-pod.yaml... Deploy complete. Dependencies may be incomplete. [getting-started getting-started] Hello Skaffold! [getting-started getting-started] Hello Skaffold!Copy the code
Skaffold has done a lot of things for us:
- Build Docker images with native source code
- With its
sha256
Value as the label for the image - Set up the
skaffold.yaml
The mirrored address of the Kubernetes Manifests defined in the document - with
kubectl apply -f
Command to deploy the Kubernetes application
When the deployment is complete, we can see that the POD prints the following information:
[getting-started getting-started] Hello Skaffold!
[getting-started getting-started] Hello Skaffold!
[getting-started getting-started] Hello Skaffold!
Copy the code
Similarly, we can view the currently deployed POD using the Kubectl tool:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
getting-started 1/1 Running 3 1h
Copy the code
We can then print the details of the POD above:
$ kubectl get pod getting-started -o yaml
...
spec:
containers:
- image: cnych/skaffold-example:f1470fe9f3984775f5dea87b5f720d67b6c2eeaaf2ca5efd1ca3c3ec7c4d4cce
imagePullPolicy: IfNotPresent
name: getting-started
...
Copy the code
We can see that the image address of our deployed POD is the same as the image address and tag of our existing Docker:
$ docker images |grep skaffold
cnych/skaffold-example f1470fe9f3984775f5dea87b5f720d67b6c2eeaaf2ca5efd1ca3c3ec7c4d4cce f1470fe9f398 8 minutes ago 271MB
Copy the code
Now let’s change our main.go file:
package main import ( "fmt" "time" ) func main() { for { fmt.Println("Hello blog.qikqiak.com!" ) time.Sleep(time.Second * 2) } }Copy the code
After we save the file, observe the output of the POD:
[getting-started getting-started] Hello Skaffold!
[getting-started getting-started] Hello Skaffold!
[getting-started getting-started] Hello blog.qikqiak.com!
[getting-started getting-started] Hello blog.qikqiak.com!
[getting-started getting-started] Hello blog.qikqiak.com!
[getting-started getting-started] Hello blog.qikqiak.com!
[getting-started getting-started] Hello blog.qikqiak.com!
Copy the code
Does it immediately become the result of our modification? Also, we can use the above style to check whether the image label in POD has been changed.
conclusion
I am here to explain the use of Skaffold, the description may seem a bit slow, but when you use it yourself, you can fully feel the convenience and efficiency of Skaffold to develop Kubernetes applications, greatly improve our productivity. In addition, in the field of Kubernetes development automation tools, there are some other options, such as Azure Draft, Datawire Forge and Weavework Flux, you can use them. Other Microsoft Draft is very well combined with Helm. Tools are always tools, and what makes us more efficient is a good tool, but from an open source point of view, you can trust Google.
The resources
- Github.com/GoogleCloud…
- draft.sh/