...
1# Create, Update & Delete Deployment with the Dynamic Package
2
3This 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
7The 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
10The `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
12The 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
14Being 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
19Make sure you have a Kubernetes cluster and `kubectl` is configured:
20```
21kubectl get nodes
22```
23
24Compile this example on your workstation:
25
26```
27cd dynamic-create-update-delete-deployment
28go build -o ./app
29```
30
31Now, 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
39Running this command will execute the following operations on your cluster:
40
411. **Create Deployment:** This will create a 2 replica Deployment. Verify with
42 `kubectl get pods`.
432. **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`.
483. **List Deployments:** This will retrieve Deployments in the `default`
49 namespace and print their names and replica counts.
504. **Delete Deployment:** This will delete the Deployment object and its
51 dependent ReplicaSet resource. Verify with `kubectl get deployments`.
52
53Each 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
55a break to take time to run `kubectl` and inspect the result of the operations
56executed.
57
58You should see an output like the following:
59
60```
61Creating deployment...
62Created deployment "demo-deployment".
63-> Press Return key to continue.
64
65Updating deployment...
66Updated deployment...
67-> Press Return key to continue.
68
69Listing deployments in namespace "default":
70 * demo-deployment (1 replicas)
71-> Press Return key to continue.
72
73Deleting deployment...
74Deleted deployment.
75```
76
77## Cleanup
78
79Successfully running this program will clean the created artifacts. If you
80terminate the program without completing, you can clean up the created
81deployment with:
82
83 kubectl delete deploy demo-deployment
84
85## Troubleshooting
86
87If you are getting the following error, make sure Kubernetes version of your
88cluster 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
View as plain text