This is the 7th day of my participation in Gwen Challenge

Four Commands Introduction

4.1 kubectl

Kubectl is apiserver client program, this client program is connected to the master node on the Apiserver, to achieve a variety of K8S object add, delete, change, check and other basic operations, in K8S can be managed by the object has a lot of

Basic Commands (beginner): Create creates a resource from a file or standard input expose Gets a copy controller, service, deploy or expose a POD expose it as a new Kubernetes service Run creates and runs a specific image, Create containers managed using Deployment or jobsetSet the object's specific functionality, such as publishing, each time it goessetImage tag basic command (intermediate): Explain documents or resources that can be used to view a list of resources. Write get to display one or more resources. Edit Edits resources on the server. ReplicaSet, Replication Controller, Job AutoScale automatically scales a deployment, ReplicaSet, Or ReplicationController cluster management command: Certificate Resource cluster-info Displays cluster information. Top Displays resource usage (CPU, memory, and storage). Metrics-server Cordon needs to be installed mark nodes as unschedulable uncordon drain Sets nodes to maintenance mode taint Updates blemishes on one or more nodes troubleshooting and debugging commands: Describe Displays detailed information about a particular resource or resource group Logs Prints a container's log in the container Attach attached to the running containerexecIn the container run the port-forward command to forward one or more local ports to pod proxy Run the proxy to Kubernetes API server cp copy files and directories to container, and copy from container, copy files across containers auth Check authorization advanced commands: Diff Real-time version of the DIFF for the version to be applied Apply applies the configuration to the resource using the file name or standard input patch updates the field of the resource using the policy merge the patch replace replaces the resource with the file name or standard inputwaitExperimental phase commands: wait for specific conditions on one or more resources, define a trigger convert convert configuration files kustomize between different API versions build kustomization target setting commands from directories or remote urls: Other commands: api-resources Prints the supported API resources on the server api-versions to annotate the annotated completion command"group/version"Plugin provides a utility that interacts with the plugin version to print client and server version informationCopy the code

4.2 the run

  • Create a controller and run the image

Create a deployment named nginx mirrored as nginx:latest or 1 if the number of copies is not known

kubectl run nginx --image=nginx:latest
Copy the code
  • Specifies the number of pods to run
kubectl run nginx --image=nginx --replicas=5  Start 5 pods
Copy the code
  • Instead of running the container’s default commands, use custom directives
kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>
Copy the code
  • Run a periodic task
kubectl run pi --schedule="0/5 * * *?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
Copy the code
  • Run nginx in test mode with specified controller name and specified port and number of copies

Specifying the dry-run parameter can be used to verify that the written YAML file is abnormal and will not actually be executed

kubectl run nginx-deploy --image=nginx --port=80 --replicas=1 --dry-run=true
Copy the code
  • Check whether the container is running
kubectl get deployment
Copy the code
  • View hosts that are scheduled
kubectl get pod -o wide
Copy the code
  • Direct access via IP addresses is accessible from within the cluster because all the PODS are on the same network
The curl 10.244.2.2Copy the code
  • If you delete the POD you just created, the replica controller will automatically rebuild the POD on the other nodes
kubectl delete pods nginx-deploy-5c9b546997-jsmk6
Copy the code
  • When you look again, you will see that the container has been scheduled to run on another node
kubectl get pod -o wide
Copy the code

4.3 expose

The problem is that the IP address of the POD can change at any time and therefore cannot be used as an entry point for access, so a service is needed to proxy the POD to create a fixed endpoint.

  • Create a service to expose a service

Create a service named nginx on controller nginx-deploy with port 80, back-end container port 80, and TCP protocol

kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
Copy the code
  • As you can see, the newly created service named nginx can now be accessed from within the cluster using the address of the service, or from outside using NodePort mode
kubectl get service
Copy the code
  • Deleting a Task
kubectl delete deployment nginx-deploy
Copy the code

4.4 the cp

  • Copy the host file or directory to pod, ⚠️ the tar binary must already exist in the container, otherwise the copy will fail
kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir
 
[root@master ~]# kubectl cp flannel.tar  nginx-58cd4d4f44-8pwb7:/usr/share/nginx/html 
[root@master ~]# kubectl cp mainfile/  nginx-58cd4d4f44-8pwb7:/usr/share/nginx/html 
[root@master ~]# kubectl exec -it nginx-58cd4d4f44-8pwb7 -- /bin/bash
root@nginx-58cd4d4f44-8pwb7:/# ls -l /usr/share/nginx/html/
total 54108
-rw-r--r-- 1 root root      537 Jul 11  2017 50x.html
-rw-r--r-- 1 root root      355 May 27 06:47 dashboard-adminuser.yaml
-rw------- 1 root root 55390720 May 27 01:49 flannel.tar
-rw-r--r-- 1 root root      612 Jul 11  2017 index.html
drwxr-xr-x 4 root root       51 Aug 17 14:16 mainfile
Copy the code

4.5 the port – forward

  • Port forwarding: SVC addresses or PODS ports are mapped to the host using Kubelet, and all traffic accessing port 8888 of the host is forwarded to 8111SVC
Kubectl port-forward --address 0.0.0.0 service/nginx 8888 8111Copy the code
  • Forward the Pods port to forward the traffic from port 8888 to port 5000 of the POD
kubectl port-forward pod/mypod 8888:5000
Copy the code

4.6 coredns

Service provides a fixed access endpoint to POD, but the change of service itself is unknown to us. Coredns is required to resolve the domain name of Service.

  • Check the coreDNS running status
kubectl get pods -n kube-system -o wide |grep coredns
Copy the code
  • You can see the IP address of the kube-DNS server by viewing the services running in each kube-system namespace
kubectl get service -n kube-system
Copy the code
  • Use kube-DNS to resolve the address of nginx service
Dig - t A nginx. Default. SVC. Cluster. The local @ 10.96.0.10Copy the code
  • Create an access nginx client container and enter interactive mode. The default DNS server of this container is the server where Kube-DNS resides
kubectl run client --image=busybox --replicas=1 -it --restart=Never
Copy the code
/ # cat /etc/resolv.conf Nameserver 10.96.0.10# kube - DNS address
search default.svc.cluster.local svc.cluster.local cluster.local    # Default parse search field
options ndots:5
Copy the code
  • Busybox: busyBox: busyBox: busyBox: busyBox: busyBox: BusyBox: BusyBox
wget -O - -q http://nginx:80/
Copy the code

4.7 Analog POD is deleted

  • Now when we remove the POD from the service backend, the replica controller will automatically create a new POD, and the Service will automatically point to the newly created POD
kubectl delete pods nginx-deploy-5c9b546997-4w24n
Copy the code
  • View the POD automatically created by the replica controller
kubectl get pods
Copy the code
  • The busyBox container requests the nginx domain service, and access is not affected
wget -O - -q http://nginx:80/
Copy the code

4.8 Simulated Service is Deleted

  • The address of the service has changed when we delete the service and create a new service
kubectl delete service nginx
Copy the code
kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
Copy the code
kubectl get service
Copy the code
  • The busybox container requests the nginx domain service, and access remains unaffected
wget -O - -q http://nginx:80/
Copy the code

4.9 labels

After Pod is deleted, SERVIC can still be correctly scheduled to the new Pod, which is guaranteed by the LABELS mechanism of K8S.

There are not only pods that can use tags. In K8S, many objects can use tags, such as Node and Service

  • Looking at the details of the service, you will find the label selector
kubectl describe service nginx
Copy the code
Name: nginx Namespace: default Labels: run=nginx-deploy Annotations: <none> Selector: Run =nginx-deploy # This selector will automatically select the run tag and value nginx-deploy POD Type: ClusterIP IP: 10.101.149.4 Port: <unset> 80/TCP TargetPort: 80/TCP Endpoints: 10.244.2.4:80 None Events: <none>Copy the code
  • When a POD is manually deleted, the label on the copy created by the replica controller does not change, so the label is associated with the service.
kubectl get pods --show-labels
Copy the code
NAME                            READY   STATUS    RESTARTS   AGE     LABELS
client                          1/1     Running   0          21m     run=client
nginx-deploy-5c9b546997-kh88w   1/1     Running   0          8m37s   pod-template-hash=5c9b546997,run=nginx-deploy
Copy the code
  • You can also view POD details
kubectl describe deployment nginx-deploy
Copy the code
  • Filter by label. Use -l to specify the label name or to filter its value
kubectl get pods --show-labels -l run=nginx-deploy
Copy the code
  • The label selector performs centralized operations
Relationship: KEY,KEY KEY=VALUE2,KEY=VALUE2# -l run,appVALUE relationship :KEY = VALUE KEY! = VALUE# -l run=nginx-deploy,app! =myappSet relation :KYEin|not in (VALUE1,VALUE2)      # -l "release in (canary,bata,alpha)"  
Copy the code
  • Displays the value of the specified label. Two labels are shown below
kubectl get pods --show-labels -L run,pod-template-hash
Copy the code
  • Tag the specified POD with a release tag for the client POD with a value of canary
kubectl label pods client release=canary
Copy the code
  • To modify the POD label, use –overwrite to modify the original label
kubectl label pods client release=stable --overwrite
Copy the code
  • Deletes the label on the specified Nodes using the label name plus the – symbol
kubectl label nodes node2 disktype-
Copy the code
  • Many resources support inline fields that define the label selectors they use, such as when a service is associated with a POD:
MatchExpressions: matchExpressions are defined based on a given expression using the tag selector: {key:"KEY",operator:"OPERATOR",value:[VAL1,VAL2,...] } Operator: In, NotIn: its value list must have a value Exists, NotExists: its value must be emptyCopy the code
  • Many objects in K8S can be labeled, for example, nodes can be labeled, and then resources can be tilted toward nodes when adding resources
kubectl label nodes node2 disktype=ssd
Copy the code
kubectl get nodes --show-labels
Copy the code

4.10 Dynamic Capacity Expansion

  • To expand a cluster of pods, change the number of copies of the Nginx-Deply container in the Deployment controller to 2
kubectl scale --replicas=5 deployment nginx-deploy
Copy the code

4.11 Rolling Upgrade

  • Replace the image of the nginx-deploy container to ikubernetes/myapp:v2
kubectl set image deployment nginx-deploy nginx-deploy=ikubernetes/myapp:v2
Copy the code
  • Watch the update process until all images running in the five containers are updated
kubectl rollout status deployment nginx-deploy
Copy the code
[root@node1 ~]# kubectl rollout status deployment nginx-deploy
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 4 of 5 updated replicas are available...
deployment "nginx-deploy" successfully rolled out
Copy the code
  • Rollback operation. If no mirror is specified, the mirror of the previous version is used
kubectl rollout undo deployment nginx-deploy
Copy the code

If you want to prevent the update process from being scheduled, then you need to learn readiness detection to achieve this

4.12 External Cluster Access

  • Example Change the network type of service to NodePort
kubectl edit service nginx
Copy the code
type: ClusterIP -> type: NodePort
Copy the code
  • Port 30982 is added to the service
kubectl get service
Copy the code
NAME TYPE cluster-ip external-ip PORT(S) AGE kubernetes ClusterIP 10.96.0.1 < None > 443/TCP 15h nginx NodePort 10.105.27.11 < none > 80:30982 / TCP 42 mCopy the code
  • Access from outside the cluster using any Node IP address + port
http://172.16.100.101:30982/
Copy the code

4.13 Checking Logs

  • View run logs for a container of a POD
kubectl logs pod-demo busybox
Copy the code

4.14 Connecting to a POD Container

kubectl exec -it pod-demo -c myapp -- /bin/sh
Copy the code

other

Send your notes to: github.com/redhatxl/aw… Welcome one button three links.