github.com/GoogleContainerTools/skaffold/v2@v2.13.2/codelab/02_kpt-deploy/tutorial.md (about) 1 # Deploy Your Resource via `kpt` 2 3 <walkthrough-disable-features toc></walkthrough-disable-features> 4 5 ## Introduction 6 7 8 #### What is kpt? 9 10 Kpt is [an OSS tool](https://github.com/GoogleContainerTools/kpt) for Kubernetes packaging, which uses a standard format to bundle, publish, customize, update, and apply configuration manifests. 11 12 #### What you'll learn in this codelab 13 14 * The difference between `kpt` pruning and `kubectl` pruning 15 * How to enable kpt to deploy your configurations in their live state. 16 * How to use kpt with kustomize. 17 18 This codelab is the second session. Check out the [01_kpt-validate](https://github.com/GoogleContainerTools/skaffold/tree/main/codelab/01_kpt-validate) about how to use kpt to validate your configuration before deploying to the cluster. 19 20 ## Prerequisites 21 22 If you are new to skaffold, you can check out [the skaffold tutorials](https://skaffold.dev/docs/tutorials/) to get a basic idea. 23 Or just follow this codelab, we will explain what happens in each step. 24 25 #### Install `skaffold` 26 27 * Check if `skaffold` is installed and the installed version is >= v1.17.0 28 ```bash 29 skaffold version 30 ``` 31 32 * If you haven't installed `skaffold` previously, run 33 ```bash 34 curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-linux-amd64 && \ 35 sudo install skaffold /usr/local/bin/ | bash 36 ``` 37 * To upgrade skaffold to a newer version, run 38 ```bash 39 sudo rm -f /usr/local/bin/skaffold && curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-linux-amd64 && \ 40 sudo install skaffold /usr/local/bin/ | bash 41 ``` 42 43 #### Install `kpt` 44 45 * Check if `kpt` is installed and the installed version is >= 0.34.0 46 ```bash 47 kpt version 48 ``` 49 * If you haven't installed `kpt` previously, run 50 ```bash 51 sudo apt-get install google-cloud-sdk-kpt 52 ``` 53 54 #### Install `kustomize` 55 56 * Check if `kustomize` is installed and the installed version is >= v3.4.3 57 ```bash 58 kustomize version 59 ``` 60 * If you haven't installed `kustomize` previously, run 61 ```bash 62 curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash && sudo mv kustomize /usr/local/go/bin 63 ``` 64 65 ## Getting started 66 67 Time to complete: **About 3 minutes** 68 69 70 * Run this command to start your local minikube cluster: 71 72 ```bash 73 minikube start 74 ``` 75 76 * Once your cluster is set up, the following message is displayed: 77 78 ```terminal 79 Done! kubectl is now configured to use "minikube" 80 ``` 81 82 * Now let's use `kpt pkg` to download the application example. 83 84 ```bash 85 kpt pkg get https://github.com/GoogleContainerTools/skaffold.git/codelab/02_kpt-deploy/resources/sample-app guestbook-cl2 && cd guestbook-cl2 86 ``` 87 88 <walkthrough-pin-section-icon></walkthrough-pin-section-icon> 89 **Tips** 90 > If you haven't used `skaffold dev`, you can go through this [codelab](https://github.com/GoogleContainerTools/skaffold/tree/main/codelab/01_kpt-validate). 91 92 ## Deploy 93 Time to complete: **About 2 minutes** 94 95 `Skaffold` supports using `kpt` to deploy your configurations to the cluster. 96 97 This deployment is equivalent to [`kpt live`](https://googlecontainertools.github.io/kpt/reference/live/) which reconciles the resources in their live states. 98 99 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. 100 101 #### "prune" 102 103 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. 104 105 ```terminal 106 kubectl apply --prune -f DIR 107 ``` 108 109 <walkthrough-pin-section-icon></walkthrough-pin-section-icon> 110 **Extended Reading** 111 > This [KEP](https://github.com/kubernetes/enhancements/pull/810) gives a full context 112 > about the problems the `kubectl` "prune" may cause. 113 114 Next, you will compare the pruning result between `kpt` and `kubectl` to see why `kpt` is more reliable and accurate. 115 116 ## `kubectl` pruning 117 Time to complete: **About 5 minutes** 118 119 * 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. 120 121 ```yaml 122 deploy: 123 kubectl: 124 flags: 125 apply: 126 - "--prune=true" 127 - "--all=true" 128 - "--namespace=default" 129 manifests: 130 - config/frontend/*.yaml 131 ``` 132 133 * Run `skaffold dev` to deploy the resources. 134 135 ```bash 136 skaffold dev 137 ``` 138 139 * 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*. 140 141 ```text 142 cd guestbook-cl2 143 mv config/frontend/deployment.yaml . 144 ``` 145 146 * Check if the Deployment resource is pruned. You can run the following command in the terminal opened from the previous step. 147 148 ```text 149 kubectl get deployment 150 ``` 151 152 You should expect to see the following information, meaning the *Deployment* resource is not pruned. 153 154 ```terminal 155 NAME READY UP-TO-DATE AVAILABLE AGE 156 frontend 1/1 1 1 6m17s 157 ``` 158 <walkthrough-notification-menu-icon></walkthrough-notification-menu-icon> 159 **Note** 160 > Do not exit the `skaffold dev` in the first cloud 161 > shell tab, otherwise the Deployment resource is cleaned up due to the exist. 162 163 164 ## `kpt` pruning 165 166 #### How `kpt` prunes the resource 167 168 kpt uses a [three-way merge strategy](https://pwittrock-kubectl.firebaseapp.com/pages/app_management/field_merge_semantics.html) 169 to compare the resources' previous state, current state and current desired state. This allows kpt to make changes more wisely. 170 171 #### Let's try it out. 172 173 * First, you can exit `skaffold dev` with *Ctrl+ C* 174 175 * You should add back the deployment.yaml from the previous step. 176 177 ```bash 178 mv deployment.yaml config/frontend/ 179 ``` 180 181 * 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. 182 183 ```yaml 184 deploy: 185 kpt: 186 dir: config 187 ``` 188 189 **Tips** 190 > `kpt` enables pruning by default. 191 192 * Run `skaffold dev` to deploy the resources. 193 194 ```bash 195 skaffold dev 196 ``` 197 198 * 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>. 199 200 *You can **either** manually delete the line **or** run the following command in 201 the new terminal* 202 203 ```text 204 cd guestbook-cl2 205 sed -i '2d' ./config/kustomization.yaml 206 ``` 207 208 `kpt` is compatible with kustomize by default. Thus, removing the file reference from the kustomization.yaml resource will exclude deployment.yaml from the deployment.* 209 210 * Check resource on the cluster side. You can run the following command in the terminal opened from the previous step. 211 212 ```text 213 kubectl get deployment 214 ``` 215 You should expect to see the following message. 216 ```terminal 217 No resources found in default namespace. 218 ``` 219 220 ## Conclusion 221 222 <walkthrough-conclusion-trophy></walkthrough-conclusion-trophy> 223 224 Congratulations, you know how to use kpt in skaffold! You can explore other kpt features 225 from the skaffold.yaml[ reference doc](https://skaffold.dev/docs/references/yaml/#deploy-kpt). 226 227 You can also try out other kpt features like `kpt pkg` and `kpt cfg` from 228 [the user guide](https://googlecontainertools.github.io/kpt/reference/). They will be supported 229 in the skaffold soon. Stay tuned!