5. Pod Design (20%)
1. Defining and Querying Labels and Annotations
Create three different Pods with the names
frontend,backendanddatabasethat use the imagenginx.Declare labels for those Pods as follows:
frontend: env=prod, team=shiny
backend: env=prod, team=legacy, app=v1.2.4
database: env=prod, team=storageDeclare annotations for those Pods as follows:
frontend: contact=John Doe, commit=2d3mg3
backend: contact=Mary HarrisRender the list of all Pods and their labels.
Use label selectors on the command line to query for all production Pods that belong to the teams shiny and legacy.
Remove the label env from the backend Pod and rerun the selection.
Render the surrounding 3 lines of YAML of all Pods that have annotations.
You can assign labels upon Pod creation with the --labels option.
$ kubectl run frontend --image=nginx --restart=Never --labels=env=prod,team=shiny
pod/frontend created
$ kubectl run backend --image=nginx --restart=Never --labels=env=prod,team=legacy,app=v1.2.4
pod/backend created
$ kubectl run database --image=nginx --restart=Never --labels=env=prod,team=storage
pod/database createdEdit the existing Pods with the edit command and add the annotations as follows:
Render all Pods and their Pods including their assigned labels.
You can combine the selector rules into one expression.
You can add and remove labels with the label command. The selection now doesn’t match for the backend Pod anymore.
The grep command can help with rendering any YAML code around the identified search term.
2. Performing Rolling Updates for a Deployment
Create a Deployment named
deploywith3 replicas.The Pods should use thenginximage and the namenginx. The Deployment uses the labeltier=backend. The Pods should use the labelapp=v1.List the Deployment and ensure that the correct number of replicas is running.
Update the image to nginx:latest.
Verify that the change has been rolled out to all replicas.
Scale the Deployment to 5 replicas.
Have a look at the Deployment rollout history.
Revert the Deployment to revision 1.
Ensure that the Pods use the image nginx.
Generate the YAML for a Deployment plus Pod for further editing.
Edit the labels. The selector should match the labels of the Pods. Change the replicas from 1 to 3.
Create the deployment by pointing it to the YAML file.
Set the new image and check the revision history.
Now scale the Deployment to 5 replicas.
Roll back to revision 1. You will see the new revision. Inspecting the revision should show the image nginx.
3. Creating a Scheduled Container Operation
Create a
CronJobnamedcurrent-datethat runsevery minuteand executes the shell commandecho "Current date: $(date)".Watch the jobs as they are being scheduled.
Identify one of the Pods that ran the CronJob and render the logs.
Determine the number of successful executions the CronJob will keep in its history. Delete the Job.
答案: The run command is deprecated but it provides a good shortcut for creating a CronJob with a single command. 注意:此命令kubectl v1.8并不会显示创建cronjob成功,仅是pod
正确命令:
Watch the Jobs as they are executed.
Identify one of the Pods (the label indicates the Job name) and render its logs.
The value of the attribute successfulJobsHistoryLimit defines how many executions are kept in the history.
Finally, delete the CronJob.
最后更新于