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 created
Edit 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 deploy with 3 replicas. The Pods should use the nginx image and the name nginx. The Deployment uses the label tier=backend. The Pods should use the label app=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 CronJob named current-date that runs every minute and executes the shell command echo "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.
$ kubectl create -f deploy.yaml
deployment.apps/deploy created
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy 3 3 3 1 4s
$ kubectl run current-date --schedule="* * * * *" --restart=OnFailure --image=nginx -- /bin/sh -c 'echo "Current date: $(date)"'
kubectl run --generator=cronjob/v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
cronjob.batch/hello created
$ kubectl get cronjobs --watch
NAME COMPLETIONS DURATION AGE
current-date-1557522540 1/1 3s 103s
current-date-1557522600 1/1 4s 43s
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
current-date-1557522540-dp8l9 0/1 Completed 0 1m controller-uid=3aaabf96-7369-11e9-96c6-025000000001,job-name=current-date-1557523140,run=current-date
$ kubectl logs current-date-1557522540-dp8l9
Current date: Fri May 10 21:09:12 UTC 2019