Kubectl Basic Usage 1:
Ⅰ:Command Outline
1、Basic command
1>create Build resource by filename or standard input 2>expose Expose a resource as a new Service 3>run Run a specified image in a cluster 4>set Set a specified function for an object 5>get Get one or multiple resource 6>explain Document reference 7>edit Edit a resource using the default editor 8>delete Deletes a resource by filename, standard input, resource name, or label selector |
---|
2、Deployment command
1>rollout Rollout of management resource 2>rolling-update Rolling updates to a given replication controller 3>scale Expand or shrink the scale of Pod,Deployment、ReplicaSet、RC or Job 4>autoscale Create an automatic option to expand or shrink and set the number of pods |
---|
3、Cluster management command
1>certificate Modify certificate resource 2>cluster-info display cluster information 3>top display resource(CPU/Memory/Storage) usage。Need Heapster execution 4>cordon The token node is not dispatchable 5>uncordon The token node is dispatchable 6>drain Exclude nodes during maintenance 7>Marked as a taint, no dispatchable task |
---|
4、Troubleshooting and debugging commands
1>describe Display detailed information about a particular resource or group of resources 2>logs Print a docker log in a Pod.If the Pod has only one docker, the docker's name is optional 3>attach Attach to a running docker 4>exec Execute the command to a docker 5>port-forward Forward one or more local ports to a pod 6>proxy Run a proxy to the Kubernetes API server 7>cp Copy files or directories into docker 8>auth Check authorization |
---|
5、Senior command
1>apply Applies a configuration to a resource by filename or standard input 2>patch Use patches to modify and update the fields of the resource 3>replace Replaces a resource with a filename or standard input 4>convert Convert configuration files between different API versions |
---|
6、Setting command
1>label Update the resource lable 2>annotate Update the resource annotate 3>completion Used to implement automatic completion of kubectl tool |
---|
7、Other commands
1>api-versions Print the supported API version 2>config Modify the kubeconfig file (for accessing apis, such as configuring authentication information) 3>help All commands 4>plugin Run a command-line plug-in 5>version Print client and service version information |
---|
Ⅱ:kubectl tool management cluster
1、Build
kubectl run nginx --replicas=3 --labels="app=nginx-example" --image=nginx:1.10 --port=80
2、Check
kubectl get deployment
kubectl get pods --display-labels
kubectl get pods -l app=nginx-example
kubectl get pod -o wide
kubectl describe po/nginx-f95d765f9-5jl9z
kubectl get all
kubectl describe deployment/nginx
kubectl describe rs/nginx-f95d765f9
3、Expose
kubectl expose deployment nginx --port=8080 --type=NodePort --target-port=80 --name=nginx-service
kubectl describe svc nginx-service
1> Forwarding rules are implemented through iptable
iptables-save |grep 8080
4、Troubleshooting
kubectl describe pod nginx-f95d765f9-zcdx6
kubectl logs nginx-f95d765f9-zcdx6
kubectl exec -it nginx-f95d765f9-zcdx6 /bin/bash
5、Update
kubectl set image deploy/nginx nginx=nginx:1.11 --record
kubectl edit deploy/nginx
6、Resource release management
kubectl rollout status deployment/nginx
kubectl rollout history deployment/nginx
kubectl rollout history deployment/nginx --revision=3
7、Rollback (default rollback to previous version, --to-revision can be specified)
kubectl rollout undo deployment/nginx
kubectl rollout undo deployment nginx --to-revision=1
8、Expand and shrink
kubectl scale deployment/nginx --replicas=10
kubectl scale deployment/nginx --replicas=8
kubectl edit deployment/nginx
kubectl delete svc/nginx-service
Kubectl Basic Usage 2:
Ⅲ:Basic operation of kubernests cluster(Take the namespace of onap in linux client as an example)
1、Check the cluster node
kubectl get node
2、Check cluster namespace
kubectl get ns
3、View the pod information and the pod on which the node is located, under the namespace specified (for example, namespace on onap)
kubectl get pod -o wide
kubectl get pod -n onap
4、 Connected to the docker in pod
Check the docker's name , return two dockers' name after execution, -c specifie the docker that needed ti go in.
kubectl -n onap get pod dev-vfc-catalog-68cb7c9878-v4kt2 -o jsonpath={.spec.containers[*].name}
kubectl -n onap exec -it dev-vfc-catalog-68cb7c9878-v4kt2 -c vfc-catalog /bin/bash
5、Copy files (take the catlog example). When the data copy is lost after the pod is restarted or migrated, the multi-copy pod copy operation only exists for the current pod
Copy from local to dockers in pod
kubectl -n onap cp copy_test.sh dev-vfc-catalog-68cb7c9878-v4kt2: -c vfc-catalog
Copy pod's content to local:
kubectl -n onap cp dev-vfc-catalog-68cb7c9878-v4kt2:copy_test.sh -c vfc-catalog /tmp/copy_test.sh
6、Remote command (to see the current path of the container as an example)
kubectl -n onap exec -it dev-vfc-catalog-68cb7c9878-v4kt2 -c vfc-catalog pwd
7、View pod basic information and logs (no -c parameter added for single container pod)
kubectl -n onap describe pod dev-vfc-catalog-68cb7c9878-v4kt2
kubectl -n onap logs dev-vfc-catalog-68cb7c9878-v4kt2 -c vfc-catalog
8、Check the service listener port and manually expose the port, which is commonly used for testing, such as nginx under test namespace
1>Build namespace 4> Check svc(ports that pod exposed , The cluster internally accesses this pod via port 88., external access to the cluster using floatingip+30531) |
---|
9、 Modify the container image and pod strategy (deployment, statefulset), the completion of modification will trigger the rolling update
1>To determine whether the pod is a stateful application (efullset) or a stateful application (deployment) kubectl -n onap describe pod dev-vfc-catalog-68cb7c9878-v4kt2 |grep Controlled 2>Stateless application deployment kubectl -n onap get deploy |grep catalog kubectl -n onap edit deploy dev-vfc-catalog-68cb7c9878-v4kt2 3>Stateful application statefulset kubectl -n onap get statefulset |grep cassandra kubectl -n onap edit statefulset dev-aai-cassandra |
---|
10、Restart pod(After removing the pod, deployment will recreate a same pod and randomly assign it to any node.)
kubectl -n onap delete pod dev-vfc-catalog-68cb7c9878-v4kt2 -c vfc-catalog
11、View the virtual machine where the portal-app resides in order to add host resolution
10.0.0.13 corresponding Floating IP is 172.30.3.36
kubectl -n onap get svc |grep portal-app
pportal-app LoadBalancer 10.43.181.163 10.0.0.13 8989:30215/TCP,8006:30213/TCP,8010:30214/TCP,8443:30225/TCP
12、pod expansion and shrinkage
pod expansion:kubectl scale deployment nginx --replicas 3
pod shrinkage: kubectl scale deployment nginx --replicas 1