codelab/02_kpt-deploy/tutorial.md
kpt<walkthrough-disable-features toc></walkthrough-disable-features>
Kpt is an OSS tool for Kubernetes packaging, which uses a standard format to bundle, publish, customize, update, and apply configuration manifests.
kpt pruning and kubectl pruningThis codelab is the second session. Check out the 01_kpt-validate about how to use kpt to validate your configuration before deploying to the cluster.
If you are new to skaffold, you can check out the skaffold tutorials to get a basic idea. Or just follow this codelab, we will explain what happens in each step.
skaffoldCheck if skaffold is installed and the installed version is >= v1.17.0
skaffold version
If you haven't installed skaffold previously, run
curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-linux-amd64 && \
sudo install skaffold /usr/local/bin/ | bash
To upgrade skaffold to a newer version, run
sudo rm -f /usr/local/bin/skaffold && curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-linux-amd64 && \
sudo install skaffold /usr/local/bin/ | bash
kptkpt is installed and the installed version is >= 0.34.0
kpt version
kpt previously, run
sudo apt-get install google-cloud-sdk-kpt
kustomizekustomize is installed and the installed version is >= v3.4.3
kustomize version
kustomize previously, run
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash && sudo mv kustomize /usr/local/go/bin
Time to complete: About 3 minutes
Run this command to start your local minikube cluster:
minikube start
Once your cluster is set up, the following message is displayed:
Done! kubectl is now configured to use "minikube"
Now let's use kpt pkg to download the application example.
kpt pkg get https://github.com/GoogleContainerTools/skaffold.git/codelab/02_kpt-deploy/resources/sample-app guestbook-cl2 && cd guestbook-cl2
<walkthrough-pin-section-icon></walkthrough-pin-section-icon> Tips
If you haven't used
skaffold dev, you can go through this codelab.
Time to complete: About 2 minutes
Skaffold supports using kpt to deploy your configurations to the cluster.
This deployment is equivalent to kpt live which reconciles the resources in their live states.
Compared to other deployment tools, one key advantage of the kpt deployment is the "pruning" feature. "Prune" means removing the resources from the cluster if the resources do not appear in the applied configuration.
You may have used the following command to remove resources that are not shown up in the DIR. However, the prune operation cannot always remove the desired resource.
kubectl apply --prune -f DIR
<walkthrough-pin-section-icon></walkthrough-pin-section-icon> Extended Reading
This KEP gives a full context about the problems the
kubectl"prune" may cause.
Next, you will compare the pruning result between kpt and kubectl to see why kpt is more reliable and accurate.
kubectl pruningTime to complete: About 5 minutes
To enable the kubectl pruning, you can configure the <walkthrough-editor-open-file filePath="guestbook-cl2/skaffold.yaml">skaffold.yaml</walkthrough-editor-open-file>, and replace the following code in the <walkthrough-editor-select-line filePath="guestbook-cl2/skaffold.yaml" startLine="8" endLine="10" startCharacterOffset="0" endCharacterOffset="100">deploy</walkthrough-editor-select-line> section.
deploy:
kubectl:
flags:
apply:
- "--prune=true"
- "--all=true"
- "--namespace=default"
manifests:
- config/frontend/*.yaml
Run skaffold dev to deploy the resources.
skaffold dev
Now, let's open a new <walkthrough-editor-spotlight spotlightId="menu-terminal-new-terminal">terminal</walkthrough-editor-spotlight> and run the following command in the terminal. This will move the <walkthrough-editor-open-file filePath="guestbook-cl2/config/frontend/deployment.yaml">deployment.yaml</walkthrough-editor-open-file> out of the <walkthrough-editor-select-line filePath="guestbook-cl2/skaffold.yaml" startLine="16" endLine="16" startCharacterOffset="5" endCharacterOffset="28">config/frontend</walkthrough-editor-select-line>, meaning the Deployment resource should be pruned.
cd guestbook-cl2
mv config/frontend/deployment.yaml .
Check if the Deployment resource is pruned. You can run the following command in the terminal opened from the previous step.
kubectl get deployment
You should expect to see the following information, meaning the Deployment resource is not pruned.
NAME READY UP-TO-DATE AVAILABLE AGE
frontend 1/1 1 1 6m17s
<walkthrough-notification-menu-icon></walkthrough-notification-menu-icon> Note
Do not exit the
skaffold devin the first cloud shell tab, otherwise the Deployment resource is cleaned up due to the exist.
kpt pruningkpt prunes the resourcekpt uses a three-way merge strategy to compare the resources' previous state, current state and current desired state. This allows kpt to make changes more wisely.
First, you can exit skaffold dev with Ctrl+ C
You should add back the deployment.yaml from the previous step.
mv deployment.yaml config/frontend/
In <walkthrough-editor-open-file filePath="guestbook-cl2/skaffold.yaml">skaffold.yaml</walkthrough-editor-open-file>, replace the <walkthrough-editor-select-line filePath="guestbook-cl2/skaffold.yaml" startLine="8" endLine="16" startCharacterOffset="0" endCharacterOffset="28">deploy</walkthrough-editor-select-line> with the following content.
deploy:
kpt:
dir: config
Tips
kptenables pruning by default.
Run skaffold dev to deploy the resources.
skaffold dev
Open a new <walkthrough-editor-spotlight spotlightId="menu-terminal-new-terminal">terminal</walkthrough-editor-spotlight>, remove the <walkthrough-editor-select-line filePath="guestbook-cl2/config/kustomization.yaml" startLine="1" endLine="1" startCharacterOffset="0" endCharacterOffset="26">deployment.yaml</walkthrough-editor-select-line> from the <walkthrough-editor-open-file filePath="guestbook-cl2/config/kustomization.yaml">kustomization.yaml</walkthrough-editor-open-file>.
You can either manually delete the line or run the following command in the new terminal
cd guestbook-cl2
sed -i '2d' ./config/kustomization.yaml
kpt is compatible with kustomize by default. Thus, removing the file reference from the kustomization.yaml resource will exclude deployment.yaml from the deployment.*
Check resource on the cluster side. You can run the following command in the terminal opened from the previous step.
kubectl get deployment
You should expect to see the following message.
No resources found in default namespace.
<walkthrough-conclusion-trophy></walkthrough-conclusion-trophy>
Congratulations, you know how to use kpt in skaffold! You can explore other kpt features from the skaffold.yaml reference doc.
You can also try out other kpt features like kpt pkg and kpt cfg from
the user guide. They will be supported
in the skaffold soon. Stay tuned!