Haven't written a blog for a long time, the company engaged in a period of 997 is too brutal. 997 End also fast 1 weeks, finally slow down a bit, first two easy article, and then a little work, write a Kubernetes series. This article mainly introduces the kubernetes related commands, for the initial contact Kubernetes reference.
Kubernetes through the kube-apiserver as the entire cluster management portal. Apiserver is the primary management node for the entire cluster, where the user configures and organizes the cluster through Apiserver, while the interactions between the nodes in the cluster and the ETCD store interact with the Apiserver. Apiserver implements a set of Restfull interfaces that allow users to interact directly with Apiserver using APIs. In addition, the official provides a client-side Kubectl packaged with the toolset to interact with the cluster directly from the KUBECTL in a command line manner.
Due to the limited level of Bo Master, this article mainly introduces some bloggers in the daily use of the command, in addition to the recent official release of Kubernetes 1.2 newly added some of the feature, because bloggers have not yet in-depth research, so not too much involved.
1. HelpSimilar to all command-line tool tools, Kubectl can also execute the <kubectl> or <kubectl help> directly | <kubectl--help> can obtain help information for the command. As shown, the Kubectl is used in the following ways:
Usage: kubectl [Flags] Kubectl [Commond]
In addition, all command options can be used to obtain help for specific commands by executing--help.
2. GetThe GET command is used to get one or some resource information for a cluster. Use--help to view more information. Kubectl's help information, examples are quite detailed and easy to understand. We recommend that you get used to the help information. Kubectl can list the details of all resource in the cluster. Resource includes cluster nodes, running Pod,replicationcontroller,service, and so on.
Usage: kubectl Get [(-o|--output=) json|yaml|wide|go-template=...| go-template-file=...| jsonpath=...| Jsonpath-file= ...] (TYPE [NAME |-L Label] | Type/name ...) [Flags] [Flags]
1) For example, to get pod information, you can use the Kubectl get PO directly to get information about all the pods that are currently running, or use "Kubectl get po-o Wide" to get information on which node the pod is running on. Note: In a cluster where multiple namespace can be created, and the specified namespace is not displayed, all actions are for the default namespace. The default and Kube-system pods are listed as shown:
2) Get namespace information
# Kubectl Get namespace
3) Similar can be obtained by using "Kubectl get RC", "Kubectl Get Svc", "Kubectl Get Nodes" and other resource information.4) Get some more specific information that can be obtained by using the option "-O". Such as:(1) Kubectl get PO <podname>-o yaml output pod details in yawl format.
(2) Kubectl get PO <podname>-o JSON output pod details in Jison format.
(3) You can also use the "-o=custom-columns=" definition to get the value of the specified content directly. As in the previous output using JSON and ymal formats, the value of Metadata.labels.app can be obtained using the following command.
Kubectl Get PO Rc-nginx-2-btv4j-o=custom-columns=labels:.metadata.labels.app
The domain name where labels is the column header that is displayed, ". Metadata.labels.app" for the query
(4) Other resources can also be used in a similar manner. 3. DescribeDescribe is similar to get and is also used to get information about resource. The difference is that get gets more detailed information about the resource personality, and describe gets information about the resource cluster. The describe command is similar to get, but describe does not support the-o option, and the content domain is the same as the information format for resource,describe output of the same type.
Note: If you find information that queries a resource, use the GET command to get more detailed information. However, if you want to query the state of a resource, such as if a pod is not in the running state, you should use the describe command when you need to get more detailed status information.
Kubectl describe PO rc-nginx-2-btv4j
4. The Create KUBECTL command is used for creating cluster resource based on files or inputs. If you have defined a Yaml or son file of the corresponding resource, you can create resource defined in the file directly kubectl create-f filename. The corresponding resource can also be created directly using only subcommands [Namespace/secret/configmap/serviceaccount]. From a tracking and maintenance perspective, it is recommended that you define resources in JSON or YAML.
For example, the contents of the replication controller file for the two nginx pods obtained in the previous get are as follows. File name is: Rc-nginx.yaml
ApiVersion:v1kind:ReplicationControllermetadata: name:rc-nginx-2spec: replicas:2 Template: Metadata: Labels: app:nginx-2 Spec: containers: -name:nginx-2 Image: Xingwangc.docker.rg/nginx Ports: -containerport:80
Directly using Create, you can create a Replicationcontroller (RC) based on the Rc-nginx.yaml file, and RC creates two replicas:
Kubectl create-f Rc-nginx.yaml
Once created, using "Kubectl get RC" You can see that a replicationcontroller named Rc-nginx-2 will be created, and the result of "Kubectl get Po" will have two more pods prefixed with "rc-nginx-2-". About kubernetes clusters in resource,pod, Replicationcontroller ... And so on follow-up will be a new blog detailed introduction. 5. ReplaceThe Replace command is used to update and replace an existing resource. As the nginx created in the previous create, when we need to update some properties of resource, if you modify the number of copies, add, modify the label, change the image version, modify the port and so on. You can modify the original Yaml file directly, and then execute the Replace command.
Note: Names cannot be updated more. In addition, if the label is updated, the original tag's pod will be disconnected from the RC after the label is updated, and RC with the new label will create a new pod with the specified number of copies, but the original pod will not be deleted by default. So at this point, if you use Get PO will find the number of pods doubled, further check will find that the original pod has not been the new RC control, here only the command is unknown to talk about this problem, the curiosity can self-experiment.
6. PatchIf a container is already running, you need to modify some of the container properties, do not want to delete the container, or do not make it easy to update by using replace. Kubernetes also provides a way to directly modify a container when the container is running, which is the patch command.
If the label for the pod you created earlier is app=nginx-2, if you need to change its label to App=nginx-3 during the run, the patch command is as follows:
7. Edit edit provides another way to update the resource source, through edit can be flexible in a common resource based on the development of more significant resource. For example, the command to update the previously created pod directly with edit is:
The effect of the above command is equivalent to:
8. Delete Deletes the resource based on the resource name or label.
9. The Apply apply command provides a more rigorous way to update resource than Patch,edit. With apply, users can maintain the resource configuration in the repository using source control. Push the configuration file to server each time there is an update, and then use KUBECTL apply to apply the update to resource. Kubernetes will compare the configuration in the current configuration file with the configured configuration before referencing the update, and update only the parts of the changes without actively changing any parts that are not specified by the user.
The Apply command is used in the same way as replace, where apply does not delete the original resource and then creates a new one. Apply is updated directly on the basis of the original resource. Kubectl Apply also adds a comment to the resource to mark the current apply. Similar to GIT operations.
The logs logs command is used to display the contents of the pod in which the program output to standard output in the container. Similar to Docker's logs command. You can also use the-f option if you want to get the Tail-f method.
Rolling-update Rolling-update is a very important command for a business that has already been deployed and is running, Rolling-update provides a way to update uninterrupted business. Rolling-update each time a new pod, wait for the new pod to completely get up and delete an old pod, and then start a new pod to replace the old pod until all the pods are replaced.
Rolling-update need to make sure that the new version has different name,version and labels, otherwise it will be an error.
If you find a problem during the upgrade process, you can stop the update halfway and roll back to the previous version
Rolling-update also has a number of other options that provide rich functionality, such as-update-period for a specified interval, and can be used to view help information using-H
Scale scales are used by programs to expand or shrink replicas when the load is increased or reduced, such as two copies of Nginx created earlier, which makes it easy to extend or shrink the number of replicas using the scaled command.
Number of extended replicas to 4:
To re-reduce the number of replicas to 2:
Autoscale scale, although it is convenient to expand or reduce the number of replicas, but still need manual intervention, can not be automatically based on the system load on the number of copies of the expansion and contraction. The Autoscale command provides the ability to automatically scale out copies of a POD payload.
The Autoscale command assigns a range of copies to an RC, which automatically expands or shrinks the pod within the specified range, depending on the load of the program running in the pod. As previously created Nginx, you can use the following command to specify the scope of the replica 1~4
Cordon, drain, Uncordon These three commands are the 1.2 newly added commands of the official release, three commands are introduced together, because three commands can be used in conjunction with the maintenance of the nodes. Before 1.2, because there is no corresponding command support, if you want to maintain a node, only stop Kubelet on that node to exit the cluster, the cluster is not scheduled to the new pod on the node. If there is no pod running on that node, there will be no business impact. If a pod is running on that node, when Kubelet is stopped, Master discovers that the node is unreachable and marks the node as Notready state, and the new node is not dispatched to the node. At the same time, a new pod is created on the other node to replace the pod on that node. While this approach ensures the robustness of the cluster, there is still some violence, and if the business has only one copy, and the copy is running on the maintained node, it may cause a brief disruption to the business.
The 1.2 newly added 3 commands ensure that when the nodes are maintained, smooth operations on the maintenance node are migrated to other nodes, ensuring that the business is unaffected. As shown in the process of an entire node maintenance (to facilitate the demo add some operations to view the node information): 1) First view the current cluster all node status, you can see a total of four nodes are in the ready state ; 2) View current Nginx two copies run on D-node1 and K-node2 two nodes respectively, 3) use Cordon command to mark D-node1 as not scheduled, 4) then use Kubectl get nodes to view node status. It is found that D-node1 is still in the ready state, but it is also forbidden to dispatch, which means that the new pod will not be dispatched to D-node1. 4) Check Nginx status, no changes, two copies are still running on D-node1 and K-node2; 5) Execute drain command, smooth the pod running on D-node1 to other nodes, and 6) View Nginx status Discovery again. The copy on the D-node1 has been migrated to the K-node1, which allows the d-node1 to perform some node maintenance operations, such as upgrading the kernel, upgrading Docker, etc. 7) after the node is maintained, use the Uncordon command to unlock the d-node1 and make it re-scheduled ; 8) Check node status and find D-node1 back to ready state.
The Attach attach command, similar to the Docker attach command, allows you to view the output of a process running in daemon in the container directly, with an effect similar to logs-f, exiting view using CTRL-C. If there are multiple containers in a pod, to see the output of a specific container, you need to specify the container to run after the pod name using the-C containers name. The command for the example below looks at the output of the Skydns container in the Kube-dns-v9-rcfuk pod in Kube-system namespace.
The exec EXEC command is also similar to the Docker EXEC command, which executes a shell command in a container that is already running, and if there are multiple containers in a pod container, you need to specify the container with the-C option.
Port-forward forwarding a local port to the container port, the blogger typically uses YAML to orchestrate the container, so this command is not used basically.
Proxy bloggers have only tried to use Nginx as Kubernetes multi-master HA mode agent, have not used this command for Kubernetes API server ran a proxy
Run a Run command similar to Docker, running an image directly.
Label for the resource of the Kubernetes cluster, as mentioned in the previous example, the RC tag to the RC group. You can also label the nodes so that when you orchestrate the container, you can specify Nodeselector for the container to dispatch the container to the machine on the specified lable, such as if the cluster has IO-intensive, compute-intensive machine groupings, different machines can be labeled differently, The containers of different characteristics are then dispatched to different groupings.
In versions prior to 1.2, use Kubectl get nodes to list information for all nodes, including node labels, the node's label information is no longer listed in version 1.2, and if you need to see which tags the node is tagged in, you need to use describe to view the node's information. 21. OtherOthers are like the cluster-info information to see some information about the current cluster, version to view the cluster versions information, and some other commands related to cluster configuration.
Kubernetes's Kubectl common commands