K8S simulation project
Kubectl is a command-line tool for managing k8s clusters , which is passed to the apiserver in the generated json format for creation, viewing, and management operations.
//Help information
[root@localhost bin]# kubectl --help
kubectl controls the Kubernetes cluster manager.Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose Use a replication controller, service, deployment or pod
and expose it as a new Kubernetes Service
run Run a specified image set in the cluster
Set a set for objects specified featuresBasic Commands (Intermediate):
explain View documentation for a resource
get Display one or more resources
edit Edit a resource on the server
delete Delete resources by filenames, stdin, resources and names, or by resources and
label selectorDeploy Commands:
rollout Manage the rollout of a resource
scale
Set a new number of copies for a Deployment, ReplicaSet, Replication Controller or Job
autoscale Automatically adjust
the number of copies of a Deployment, ReplicaSet, or ReplicationControllerCluster Management Commands:
certificate modify certificate resource.
cluster-info display cluster information
top Display Resource (CPU/Memory/Storage) usage.
cordon mark node as unschedulable
uncordon mark node as schedulable
drain Drain node in preparation for maintenance
taint update one or more taints on nodeTroubleshooting and Debugging Commands:
describe Display detailed resources of a specified resource or group
logs Output container logs in pod
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 runs a proxy to the Kubernetes API server
cp copies files and directories to and from containers
.
auth Inspect authorization
1. Project life cycle
Create –> Publish –> Update –> Rollback –> Delete
2. Create kubectl run command
- Create and run one or more container images
- Create a deployment or job to manage containers
- kubectl run --help View usage help
Start the nginx instance, expose the container port 80, and set the number of copies to 3
kubectl run nginx-deployment --image=nginx:1.14 --port=80 --replicas=3
Use run to report an error
For versions after k8sv1.18.0, this command is deprecated after --replicas, and it is recommended to use deployment to create pods
- When you want to create multiple instances, you can use: kubectl create deployment pg102 --image=pg:12
--port=5432 --replicas=3 to create; - View pod: kubectl get pod, used to view all instances created using the command
- View deploy: kubectl get deploy, used to view the number of instances created;
- It is suggested that the version higher than 1.17 will directly use create deployment to create a pod manager to create a pod;
kubectl create deployment nginx --image=nginx:1.14 --port=80 --replicas=3
3. Issue the kubectl expose command
Expose the resource as a new Service
Create a Service for the nginx of the Deployment, and forward it to port 80 of the container through port 80 of the Service. The name of the Service is nginx-service, and the type is NodePort
kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type
1. The role of service
① Kubernetes needs Service, on the one hand, because the IP of Pod is not fixed (Pod may be rebuilt), on the other hand, because there is always a need for load balancing among a group of Pod instances; ② Service realizes one-to-
one Group Pods to access
③ For container applications, Kubernetes provides a VIP (virtual IP)-based bridge to access the Service, and then the Service redirects to the corresponding Pod
2. The type of service
① ClusterIP: Provide a virtual IP inside the cluster for Pod access (Service default type)
② NodePort: Open a port on each Node for external access, Kubernetes will open a port on each Node and each Node The ports are the same, through NodeIP:NodePort
③ LoadBalancer: access through an external load balancer, usually deploying LoadBalancer on the cloud platform requires additional fees.
3. Check Pod network status details and Service exposed ports
kubectl get pods,svc -o wide
4. View the nodes associated with the backend
kubectl get endpoints View the nodes associated with the backend
5. View the detailed description information of the service
kubectl describe svc nginx View details
6. Access the internal IP to view
curl 10.99.231.111
kubectl describe svc nginx | grep NodePort
curl 192.168.80.70:31405
3.7 View access log
kubectl logs []
4. Update kubectl set
- Change some information of an existing application resource.
kubectl set --help View usage help
4.1 Get the modified template
kubectl set image --help获取
4.2 View the current version number of nginx
curl -I 192.168.109.11:31979
4.3 Update nginx version to 1.14.2
kubectl set image deployment/nginx nginx=nginx:1.14.2
4.4 Monitor pod status
In the dynamic monitoring pod state, since the rolling update method is used, a new pod will be generated first, and then an old pod will be deleted, and so on.
kubectl get pods -w
Note: The update rules can be viewed through the "RollingUpdateStrategy" of "kubetl describe deployment nginx". The default configuration is "25% max unavailable, 25% max surge", that is, the rolling update is performed according to the ratio of 25%.
4.5 Check the IP change of the pod
kubectl get pod -o wide
5. Roll back kubectl rollout
- Rollback management of resources
kubectl rollout --help View usage help
5.1 View historical versions
kubectl rollout history deployment/nginx
5.2 Perform a rollback to the previous version
kubectl rollout undo deployment/nginx
kubectl get pods -o wide
View the current version of nginx
5.3 Perform a rollback to a specified version
View historical version
Go back to revison2, which is version 1.15
kubectl rollout undo deployment/nginx --to-revision=2
View pod ip changes
View current nginx version
6. Delete kubectl delete
6.1 Delete the replica controller
kubectl delete deployment/nginx
6.2 delete service
kubectl delete svc/nginx-service