Table of contents
1. Detailed explanation of kubectl command
1. View the detailed information of a resource: kubectl describe pod pod_name -n nameapace
2. Check the basic information of the pod in the specified namespace: kubectl get pods -n namespace
3. Log in to the container across hosts: kubectl exec -it pod_name /bin/bash -n namespace
② Check and find that it is different from the original pod_name,
4.2. Delete pod replica controller deployment:
5. Command line expansion and contraction
① View the original replica set: 1
5.2. Scaling down the replica set
7. Check pod network status details and service exposed ports
9. View the description information of the service
2. Release: kubectl expose command Edit
1. Detailed explanation of kubectl command
1. View the detailed information of a resource: kubectl describe pod pod_name -n nameapace
[root@master ~]# kubectl describe pods -n kube-public
### View the details of pods in the specified namespace
Name: nginx-w1-7464d66457-psk6h
Namespace: kube-public
Priority: 0
Node: node01/192.168.159.11
Start Time: Wed, 02 Nov 2022 01:40:47 +0800
Labels: app=nginx-w1
pod-template-hash=7464d66457
Annotations: <none>
Status: Running
IP: 10.150.1.3
IPs:
IP: 10.150.1.3
Controlled By: ReplicaSet/nginx-w1-7464d66457
Containers:
nginx:
Container ID: docker://4011df060a9ad777793f061675fc1a784f4effe56f5585d91d5ba3670660c620
Image: nginx
Image ID: docker-pullable://nginx@sha256:943c25b4b66b332184d5ba6bb18234273551593016c0e0ae906bab111548239f
Port: <none>
Host Port: <none>
State: Running
Started: Wed, 02 Nov 2022 01:41:06 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-llzgh (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-llzgh:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
Name: nginx-x1-77484d895f-ddhqt
Namespace: kube-public
Priority: 0
Node: node02/192.168.159.13
Start Time: Thu, 03 Nov 2022 16:16:11 +0800
Labels: app=nginx-x1
pod-template-hash=77484d895f
Annotations: <none>
Status: Running
IP: 10.150.2.3
IPs:
IP: 10.150.2.3
Controlled By: ReplicaSet/nginx-x1-77484d895f
Containers:
nginx:
Container ID: docker://ecc07803682f2d7a51037024354dae0d190841443121ca0a8514ab224179cbf5
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:943c25b4b66b332184d5ba6bb18234273551593016c0e0ae906bab111548239f
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 03 Nov 2022 16:16:36 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-47hxr (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-47hxr:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
2. Check the basic information of the pod in the specified namespace: kubectl get pods -n namespace
[root@master ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-w1-7464d66457-psk6h 1/1 Running 0 43h
nginx-x1-77484d895f-ddhqt 1/1 Running 0 5h12m
3. Log in to the container across hosts: kubectl exec -it pod_name /bin/bash -n namespace
root@master ~]# kubectl exec -it nginx-x1-77484d895f-ddhqt /bin/bash -n kube-public
[root@master ~]# kubectl exec -it nginx-x1-77484d895f-ddhqt /bin/bash -n kube-public
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-x1-77484d895f-ddhqt:/#
4. Delete the pod
4.1. Delete the pod directly. But the deployment, the replica controller is still there, and he will create another one by himself.
[root@master ~]# kubectl delete pod nginx- -n kube-public
pod "nginx-w1-7464d66457-psk6h" deleted
①. Delete POD directly
② Check and find that it is different from the original pod_name,
4.2. Delete pod replica controller deployment:
[root@master ~]# kubectl delete deployment nginx-w1 -n kube-public
4.3. If there is a moth during the deletion process, the Pod cannot be deleted and is always in the terminating state, then perform a forced deletion --force --grace-period=0
5. Command line expansion and contraction
5.1. Expansion of replica set
① View the original replica set: 1
② Expand the replica set to 4
5.2. Scaling down the replica set
Experiment based on the above results, set --replicas=2, after specifying the number of replica sets, he will compare the number of existing replica sets with the expected value, so as to decide whether to expand or shrink
6. Delete service
[root@master ~]# kubectl delete svc nginx-server -n default
[root@master ~]# kubectl get svc -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.125.0.1 <none> 443/TCP 2d6h
nginx-server NodePort 10.125.126.34 <none> 80:30537/TCP 45m
nginx1-server NodePort 10.125.103.25 <none> 80:31070/TCP 2m41s
[root@master ~]# kubectl delete svc nginx-server -n default
service "nginx-server" deleted
7. Check pod network status details and service exposed ports
[root@master ~]# kubectl get svc,pods -n default
8. View the associated nodes
[root@master ~]# kubectl get endpoints
[root@master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.159.10:6443 2d6h
nginx1-service 10.150.1.10:80,10.150.1.9:80,10.150.2.9:80 32m
9. View the description information of the service
[root@master ~]# kubectl describe svc nginx
[root@master ~]# kubectl describe svc nginx
Name: nginx1-service
Namespace: default
Labels: app=nginx
Annotations: <none>
Selector: app=nginx
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.125.32.0
IPs: 10.125.32.0
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 31625/TCP
Endpoints: 10.150.1.10:80,10.150.1.9:80,10.150.2.9:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
10. View logs of core components
K8S deployed by kubeadm
kubectl logs -f pod_name -n namespace or journalctl -u kubelet -f
K8S via binary deployment
journalctl -u kubelet -f
2. The life cycle of pod
The pod life cycle refers to the state contained and experienced by the pod in the process from creation to deletion
There are 2 types of pods:
①, autonomous: no controller management is self-service. Just delete it when you delete it
②. Declarative style: Pods managed by the controller. (the type we generally use)
1. Create a Pod, start the nginx instance, expose the container port 80, and set the replica set to 3 (create pod)
namespace is the default
[root@master ~]# kubectl create deployment nginx-ceshi --image=nginx:1.14 --port 80 --replicas=3
deployment.apps/nginx-ceshi created
2. Release: kubectl expose command
3. Check the port
3. The type of service
①, clusterIP: Provide a virtual IP within 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 the port of each Node is the same, through NodeIp:NodePort The program outside the Kubernetes cluster Can access service. Each port can only be one service, and the port range can only be 30000-32767
③. LoadBalancer: Map to the LoadBalancer address provided by the cloud service provider by setting the LoadBalancer. This usage is only used in the scenario of setting up the service on the cloud platform of the public cloud service provider. To access through an external load balancer, it usually requires additional fees to deploy LoadBalancer on the cloud platform.
④, externalName: maps the service name to a DNS domain name, which is equivalent to the CNAME record of the DNS service, and is used to allow the Pod to access resources outside the cluster. It itself does not bind any resources.