...
1# Create, Update & Delete Deployment
2
3This example program demonstrates the fundamental operations for managing on
4[Deployment][1] resources, such as `Create`, `List`, `Update` and `Delete`.
5
6You can adopt the source code from this example to write programs that manage
7other types of resources through the Kubernetes API.
8
9## Running this example
10
11Make sure you have a Kubernetes cluster and `kubectl` is configured:
12
13 kubectl get nodes
14
15Compile this example on your workstation:
16
17```
18cd create-update-delete-deployment
19go build -o ./app
20```
21
22Now, run this application on your workstation with your local kubeconfig file:
23
24```
25./app
26# or specify a kubeconfig file with flag
27./app -kubeconfig=$HOME/.kube/config
28```
29
30Running this command will execute the following operations on your cluster:
31
321. **Create Deployment:** This will create a 2 replica Deployment. Verify with
33 `kubectl get pods`.
342. **Update Deployment:** This will update the Deployment resource created in
35 previous step by setting the replica count to 1 and changing the container
36 image to `nginx:1.13`. You are encouraged to inspect the retry loop that
37 handles conflicts. Verify the new replica count and container image with
38 `kubectl describe deployment demo`.
393. **List Deployments:** This will retrieve Deployments in the `default`
40 namespace and print their names and replica counts.
414. **Delete Deployment:** This will delete the Deployment object and its
42 dependent ReplicaSet resource. Verify with `kubectl get deployments`.
43
44Each step is separated by an interactive prompt. You must hit the
45<kbd>Return</kbd> key to proceed to the next step. You can use these prompts as
46a break to take time to run `kubectl` and inspect the result of the operations
47executed.
48
49You should see an output like the following:
50
51```
52Creating deployment...
53Created deployment "demo-deployment".
54-> Press Return key to continue.
55
56Updating deployment...
57Updated deployment...
58-> Press Return key to continue.
59
60Listing deployments in namespace "default":
61 * demo-deployment (1 replicas)
62-> Press Return key to continue.
63
64Deleting deployment...
65Deleted deployment.
66```
67
68## Cleanup
69
70Successfully running this program will clean the created artifacts. If you
71terminate the program without completing, you can clean up the created
72deployment with:
73
74 kubectl delete deploy demo-deployment
75
76## Troubleshooting
77
78If you are getting the following error, make sure Kubernetes version of your
79cluster is v1.6 or above in `kubectl version`:
80
81 panic: the server could not find the requested resource
82
83[1]: https://kubernetes.io/docs/user-guide/deployments/
View as plain text