k8s.io/client-go@v0.31.1/examples/dynamic-create-update-delete-deployment/README.md (about) 1 # Create, Update & Delete Deployment with the Dynamic Package 2 3 This example program demonstrates the fundamental operations for managing on 4 [Deployment][1] resources, such as `Create`, `List`, `Update` and `Delete` using client-go's `dynamic` package. 5 6 ## Typed Vs. Dynamic 7 The code in this directory is based on a similar [example that uses Kubernetes typed client sets][2]. The typed client sets make it simple to communicate with the API server using pre-generated local API objects to achieve an RPC-like programming experience. Typed clients uses program compilations to enforce data safety and some validation. However, when using typed clients, programs are forced to be tightly coupled with the version and the types used. 8 9 10 The `dynamic` package on the other hand, uses a simple type, `unstructured.Unstructured`, to represent all object values from the API server. Type `Unstructured` uses a collection of nested `map[string]interface{}` values to create an internal structure that closely resemble the REST payload from the server. 11 12 The dynamic package defers all data bindings until runtime. This means programs that use the dynamic client will not get any of the benefits of type validation until the program is running. This may be a problem for certain types of applications that require strong data type check and validation. 13 14 Being loosely coupled, however, means that programs that uses the `dynamic` package do not require recompilation when the client API changes. The client program has more flexibility in handling updates to the API surface without knowing ahead of time what those changes are. 15 16 17 ## Running this example 18 19 Make sure you have a Kubernetes cluster and `kubectl` is configured: 20 ``` 21 kubectl get nodes 22 ``` 23 24 Compile this example on your workstation: 25 26 ``` 27 cd dynamic-create-update-delete-deployment 28 go build -o ./app 29 ``` 30 31 Now, run this application on your workstation with your local kubeconfig file: 32 33 ``` 34 ./app 35 # or specify a kubeconfig file with flag 36 ./app -kubeconfig=$HOME/.kube/config 37 ``` 38 39 Running this command will execute the following operations on your cluster: 40 41 1. **Create Deployment:** This will create a 2 replica Deployment. Verify with 42 `kubectl get pods`. 43 2. **Update Deployment:** This will update the Deployment resource created in 44 previous step by setting the replica count to 1 and changing the container 45 image to `nginx:1.13`. You are encouraged to inspect the retry loop that 46 handles conflicts. Verify the new replica count and container image with 47 `kubectl describe deployment demo`. 48 3. **List Deployments:** This will retrieve Deployments in the `default` 49 namespace and print their names and replica counts. 50 4. **Delete Deployment:** This will delete the Deployment object and its 51 dependent ReplicaSet resource. Verify with `kubectl get deployments`. 52 53 Each step is separated by an interactive prompt. You must hit the 54 <kbd>Return</kbd> key to proceed to the next step. You can use these prompts as 55 a break to take time to run `kubectl` and inspect the result of the operations 56 executed. 57 58 You should see an output like the following: 59 60 ``` 61 Creating deployment... 62 Created deployment "demo-deployment". 63 -> Press Return key to continue. 64 65 Updating deployment... 66 Updated deployment... 67 -> Press Return key to continue. 68 69 Listing deployments in namespace "default": 70 * demo-deployment (1 replicas) 71 -> Press Return key to continue. 72 73 Deleting deployment... 74 Deleted deployment. 75 ``` 76 77 ## Cleanup 78 79 Successfully running this program will clean the created artifacts. If you 80 terminate the program without completing, you can clean up the created 81 deployment with: 82 83 kubectl delete deploy demo-deployment 84 85 ## Troubleshooting 86 87 If you are getting the following error, make sure Kubernetes version of your 88 cluster is v1.13 or higher in `kubectl version`: 89 90 panic: the server could not find the requested resource 91 92 [1]: https://kubernetes.io/docs/user-guide/deployments/ 93 [2]: ../create-update-delete-deployment