最近項目有用到Kubernetes做集羣配置,因此學習下相關命令,記錄下以備下次使用...java
kubectl help 顯示具體的用法node
kubectl controls the Kubernetes cluster manager. Find more information at https://github.com/kubernetes/kubernetes. Usage: kubectl [flags] kubectl [command] Available Commands: get Display one or many resources describe Show details of a specific resource or group of resources create Create a resource by filename or stdin replace Replace a resource by filename or stdin. patch Update field(s) of a resource using strategic merge patch. delete Delete resources by filenames, stdin, resources and names, or by resources and label selector. edit Edit a resource on the server apply Apply a configuration to a resource by filename or stdin namespace SUPERSEDED: Set and view the current Kubernetes namespace logs Print the logs for a container in a pod. rolling-update Perform a rolling update of the given ReplicationController. scale Set a new size for a Replication Controller, Job, or Deployment. cordon Mark node as unschedulable drain Drain node in preparation for maintenance uncordon Mark node as schedulable attach Attach to a running container. exec Execute a command in a container. port-forward Forward one or more local ports to a pod. proxy Run a proxy to the Kubernetes API server run Run a particular image on the cluster. expose Take a replication controller, service or pod and expose it as a new Kubernetes Service autoscale Auto-scale a deployment or replication controller rollout rollout manages a deployment label Update the labels on a resource annotate Update the annotations on a resource config config modifies kubeconfig files cluster-info Display cluster info api-versions Print the supported API versions on the server, in the form of "group/version". version Print the client and server version information. explain Documentation of resources. convert Convert config files between different API versions Flags: --alsologtostderr[=false]: log to standard error as well as files --certificate-authority="": Path to a cert. file for the certificate authority. --client-certificate="": Path to a client certificate file for TLS. --client-key="": Path to a client key file for TLS. --cluster="": The name of the kubeconfig cluster to use --context="": The name of the kubeconfig context to use --insecure-skip-tls-verify[=false]: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure. --kubeconfig="": Path to the kubeconfig file to use for CLI requests. --log-backtrace-at=:0: when logging hits line file:N, emit a stack trace --log-dir="": If non-empty, write log files in this directory --log-flush-frequency=5s: Maximum number of seconds between log flushes --logtostderr[=true]: log to standard error instead of files --match-server-version[=false]: Require server version to match client version --namespace="": If present, the namespace scope for this CLI request. --password="": Password for basic authentication to the API server. -s, --server="": The address and port of the Kubernetes API server --stderrthreshold=2: logs at or above this threshold go to stderr --token="": Bearer token for authentication to the API server. --user="": The name of the kubeconfig user to use --username="": Username for basic authentication to the API server. --v=0: log level for V logs --vmodule=: comma-separated list of pattern=N settings for file-filtered logging Use "kubectl [command] --help" for more information about a command.
get命令獲取全部資源的詳細信息nginx
kubectl get 會顯示具體的信息git
kubectl get po -o wide 獲取pod運行在哪一個節點上的信息github
describe相似於get,一樣用於獲取resource的相關信息。不一樣的是,get得到的是更詳細的resource個性的詳細信息,describe得到的是resource集羣相關的信息。describe命令同get相似,可是describe不支持-o選項,對於同一類型resource,describe輸出的信息格式,內容域相同。 web
kubectl describe pod pod名docker
ubectl命令用於根據文件或輸入建立集羣resource。若是已經定義了相應resource的yaml或son文件,直接kubectl create -f filename便可建立文件內定義的resource。也能夠直接只用子命令[namespace/secret/configmap/serviceaccount]等直接建立相應的resource。從追蹤和維護的角度出發,建議使用json或yaml的方式定義資源。
如,前面get中獲取的兩個nginx pod的replication controller文件內容以下。文件名爲:rc-nginx.yaml shell
apiVersion: v1 kind: ReplicationController metadata: name: siweb labels: app: si tier: web spec: template: metadata: labels: app: si tier: web spec: nodeName: 10.18.97.152 containers: - imageSiweb name: siweb-war lifecycle: postStart: exec: command: - "cp" - "/siweb.war" - "/app" volumeMounts: - mountPath: /app name: webapps-volume - image: 192.168.1.1/ismp/tomcat name: siweb volumeMounts: - mountPath: /usr/local/tomcat/webapps name: webapps-volume ports: - containerPort: 8080 hostPort: 8003 volumes: - name: webapps-volume emptyDir: {}
直接使用create則能夠基於rc-nginx.yaml文件建立出ReplicationController(rc),rc會建立兩個副本:json
kubectl create -f rc-nginx.yaml
建立後,使用「kubectl get rc」能夠看到一個名爲rc-nginx-2的ReplicationController將被建立,同時「kubectl get po」的結果中會多出兩個前綴爲「rc-nginx-2-」的podvim
ubectl replace -f rc-nginx.yaml
kubectl delete -f rc-nginx.yaml kubectl delete po rc-nginx-btv4j kubectl delete po -lapp=nginx-2
kubectl attach kube-dns-v9-rcfuk -c skydns —namespace=kube-system