Prepare for CKA's daily problem - day 4: Master kubectl command to create resource object and analyze from source code

Keywords: Programming Nginx Kubernetes Docker JSON

This activity is held on WeChat official account (my small bowl soup), and there is book delivery activity. You can't take part in the book delivery activity if you take part in the question answering here!

Yesterday's examination questions

Create a deployment with a single command and expose the Service. The deployment and Service names are cka-1120, using nginx image, and the deployment has two pod s

Yesterday's answer

[root@liabio ~]# kubectl run  cka-1120 --replicas 2 --expose=true --port=80 --image=nginx
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
service/cka-1120 created
deployment.apps/cka-1120 created

[root@liabio ~]# kubectl get all | grep cka-1120
pod/cka-1120-554b9c4798-7jcrb   1/1     Running            0          118m
pod/cka-1120-554b9c4798-fpjwj   1/1     Running            0          118m
service/cka-1120        ClusterIP   10.108.140.25    <none>        80/TCP           118m
deployment.apps/cka-1120   2/2     2            2           118m

Yesterday's analysis

The official website provides detailed usage of kubectl, which is located under the reference -- > > kubectl cli -- > > kubectl commands tab. Namely: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run kubectl run creates a deployment or job to manage Pod. The command syntax is as follows:

kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...]

NAME specifies the NAME of deployment and service; --Replica is abbreviated as - r, which specifies the number of instances. The default value is 1; --If expose is true, a service with ClusterIP will be created. The default value is false; --Port indicates the port exposed by the container. If expose is true, the port is also the service port; --Image specifies the image used by the container; --When dry run is true, only the object to be sent will be printed instead of being sent. The default is false.

Create a deployment named cka-1120-01 with environment variables

kubectl run cka-1120-01 --image=nginx --env="DNS_DOMAIN=cluster.local" --env="POD_NAMESPACE=default"

Create a deployment named cka-1120-02 with label

kubectl run cka-1120-02 --image=nginx --labels="app=nginx,env=prod"

There is also a -- restart parameter, which is Always by default. If it is set to OnFailure, the job will be created; if it is set to Never, the ordinary Pod will be created.

[root@liabio ~]# kubectl run cka-1120-03 --image=nginx --restart=OnFailure
kubectl run --generator=job/v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
job.batch/cka-1120-03 created
[root@liabio ~]# 
[root@liabio ~]# kubectl run cka-1120-04 --image=nginx --restart=Never
pod/cka-1120-04 created

Parameter -- schedule specifies the timing rule of cronjob. If this parameter is specified, cronjob will be created

[root@liabio ~]# kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
cronjob.batch/pi created

Currently, it is not supported to directly create resource objects such as Statefulset and Daemonset.

What happened after kubectl run was executed? It's necessary to look at the kubectl source code. The entry function is in $gopath \ SRC \ k8s. IO \ kubernetes \ CMD \ cli check \ check_cli_conventions.go Where cmd.NewKubectlCommand is to build kubectl and its subcommand line parameters. The final code to execute the business logic is under the pkg\kubectl package. Different subcommands: the application, run, and create entries correspond to the following ones: pkg\kubectl\cmd: The most important o.Run(f, cmd, args) will perform a series of checks on the parameters passed in by kubectl run and fill in the default values.

Call o.createGeneratedObject in line 360 to generate resource objects such as deployment, cronjob, job and pod according to different generator s, and send a creation request to apiserver.

If expose is set to true, in line 372, the same call o.createGeneratedObject to generate and create a service. o. In line 649 of the creategeneratedobject method, different resource objects are generated according to different generator implementations. The generator implementation corresponding to the run command is as follows, and the code is located in the DefaultGenerators function in pkg\kubectl\generate\versioned\generator.go.

	case "run":
		generator = map[string]generate.Generator{
			RunV1GeneratorName:                 BasicReplicationController{},
			RunPodV1GeneratorName:              BasicPod{},
			DeploymentV1Beta1GeneratorName:     DeploymentV1Beta1{},
			DeploymentAppsV1Beta1GeneratorName: DeploymentAppsV1Beta1{},
			DeploymentAppsV1GeneratorName:      DeploymentAppsV1{},
			JobV1GeneratorName:                 JobV1{},
			CronJobV2Alpha1GeneratorName:       CronJobV2Alpha1{},
			CronJobV1Beta1GeneratorName:        CronJobV1Beta1{},
		}

o. Line 689 of the creategeneratedobject method sends an http creation request to APIServer for the generated resource object. For the specific code of kubectl run command, interested students can dig deeper, and I will analyze it in more detail in the following source code analysis series.

Today's exam questions

Through the command line, use nginx image to create a pod and manually schedule it to the node named node1211. The name of pod is cka-1121. It's better to attach the answer questions. The command used and the simplest yaml needed to create the pod. If there are limitations in the comments, please list the points of attention. What do you do with manual scheduling? Note: manual scheduling refers to scheduling without Kube scheduler.

Author concise

Author: small bowl soup, a love and serious writing guy, now maintain the original official account: "my little bowl soup", focus on writing golang, docker, kubernetes and other knowledge to enhance hard power articles, and expect your attention. Reprint: Please specify the source (Note: from the official account: my small bowl soup, the author: small bowl soup)

Author concise

Author: small bowl soup, a love and serious writing guy, now maintains the original official account: "my little bowl soup", focuses on writing go, docker, kubernetes, java and other development, operation and maintenance knowledge and other articles to enhance hard power, and expects your attention. Reprint: Please specify the source (Note: from the official account: my small bowl soup, the author: small bowl soup)

Posted by sprint10s on Wed, 11 Mar 2020 02:21:26 -0700