Book source: "CKA/CKAD Test Guide: From Docker to Kubernetes Complete Raiders"
Organize the reading notes while studying, and share them with everyone. If the copyright is violated, it will be deleted. Thank you for your support!
Attach a summary post: Kubernetes certification exam self-study series | Summary_COCOgsta's Blog-CSDN Blog
Step 1: Check how many pods there are.
[root@vms10 ~]# kubectl get pods
No resources found in default namespace.
[root@vms10 ~]#
复制代码
There are currently no pods. The pods in the current namespace are listed here. If you want to list the pods with the specified name, you need to use -n to specify the namespace.
Step 2: To list the namespaces in kube-system, use the following command.
[root@vms10 ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-675d8749dd-sq6mn 1/1 Running 0 20m
calico-node-nvj6x 1/1 Running 0 20m
[root@vms10 ~]#
复制代码
Step 3: If you want to list pods in all namespaces, you need to add the --all-namespaces option or -A, for example:
[root@vms10 ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
... 输出 ...
kube-system calico-kube-controllers-675d8749dd-sq6mn 1/1 Running 0 22m
kube-system calico-node-nvj6x 1/1 Running 0 22m
... 输出 ...
[root@vms10 ~]#
复制代码
5.1.1 Create pods
The syntax of the command line is as follows.
kubectl run 名字 --image=镜像
复制代码
Here you can also specify the label of the pod, the syntax is as follows.
kubectl run 名字 --image=镜像 --1abels=标签=值
复制代码
If you want multiple labels, use multiple --labels options.
You can also specify the variables used in the pod, the syntax is as follows.
kubectl run 名字 --image=镜像 --env="变量名=值"
复制代码
If you want to specify multiple variables, use multiple --env options.
You can also specify the port used by the container in the pod, using the option port.
kubectl run 名字 --image=镜像 --port=端口号
复制代码
You can also specify a mirror download policy.
kubectl run名字 --image=镜像 --image-pull-policy=镜像下载策略
复制代码
Step 1: Create a pod named pod1 below, and use nginx for the image.
[root@vms10 ~]# kubectl run pod1 --image=nginx
pod/pod1 created
[root@vms10 ~]#
复制代码
Step 2: View pods.
[root@vms10 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 38s
[root@vms10 ~]#
复制代码
Step 3: To see which node this pod is running on, you need to add -o wide.
[root@vms10 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE ...
pod1 1/1 Running 0 59s 10.244.14.18 vms12.rhce.cc ...
[root@vms10 ~]#
复制代码
As you can see from here, the pod is running on vms12.rhce.cc, and the IP of the pod is 10.244.14.18.
5.1.2 Delete pods
This section describes how to delete a pod. The syntax for deleting a pod is as follows.
kubectl delete pod 名字 --force
复制代码
Here --force is optional, and its effect is to speed up the deletion of pods.
Step 1: Delete pod1.
[root@vms10 ~]# kubectl delete pod pod1
pod "pod1" deleted
[root@vms10 ~]#
复制代码
Step 2: View existing pods.
[root@vms10 ~]# kubectl get pods
No resources found in default namespace.
[root@vms10 ~]#
复制代码
5.1.3 Generate yaml file to create pod
It is more recommended to use the yaml method to create pods, because in this way various attributes can be specified in the yaml file. The command to generate the yaml file is as follows.
kubectl run 名字 --image=镜像 --dry-run=client -o yaml > pod.yaml
复制代码
Here --dry-run=client means to simulate the creation of pods, but not actually create them. -o yaml means to output in the format of yaml files, and then redirect the results to pod.yaml.
Step 1: The files involved in this chapter are placed separately in a pod directory, create a directory pod and cd into it.
[root@vms10 ~]# mkdir pod ; cd pod
[root@vms10 pod]#
复制代码
Step 2: Create the yaml file pod1.yaml of pod1.
[root@vms10 pod]# kubectl run pod1 --image=nginx --dry-run=client -o yaml > pod1.yaml
[root@vms10 pod]#
复制代码
Step 3: Modify the content of pod1.yaml and add imagePullPolicy: IfNotPresent.
[root@vms10 pod]# cat pod1.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod1 # 这里pod标签设置为run=pod1
name: pod1 # pod名为pod1
spec:
containers:
- image: nginx # pod所使用的镜像
imagePullPolicy: IfNotPresent
name: pod1 # 这个是容器名
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@vms10 pod]#
复制代码
The syntax for creating a pod through this yaml file is as follows.
kubectl apply -f yaml 文件
复制代码
If you want to specify a namespace, use kubectl apply -f yaml -n namespace.
Step 4: Create a pod.
[root@vms10 pod]# kubectl apply -f pod1.yaml
pod/pod1 created
[root@vms10 pod]#
复制代码
Step 5: View pods.
[root@vms10 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 2s
[root@vms10 pod]#
复制代码
Exercise: Generate a pod yaml file
Step 1: Request to execute echo aa in this pod, and then sleep for 1000 seconds.
[root@vms10 pod]# kubectl run pod2 --imgage=nginx --image-pull-policy=IfNotPresent --dry-run=client -o yaml -- sh -c "echo aa ; sleep 1000" > pod2.yaml
[root@vms10 pod]#
复制代码
The content is as follows.
[root@vms10 pod]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod2
name: pod2
spec:
containers:
- args:
- sh
- -c
- echo aa ; sleep 1000
image: nginx
imagePullPolicy: IfNotPresent
name: pod2
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@vms10 pod]#
复制代码
In this way, when pod2 is running, the process running in the container is not the process specified by CMD in mirror nginx.
containers:
- command:
- sh
- -c
- echo aa ; sleep 1000
image: nginx
复制代码
The command here is written in multiple lines, or it can be written in one line, and written in the following format in the format of a json file.
containers:
- command: ["sh", "-c", "echo aa ; sleep 1000"]
image: nginx
复制代码
Note: During the exam, you can use this command to quickly generate a yaml file and then modify it.
Step 2: Create pod 2.
[root@vms10 pod]# kubectl apply -f pod2.yaml
pod/pod2 created
[root@vms10 pod]#
复制代码
Step 3: View existing pods.
[root@vms10 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 6m8s
pod2 1/1 Running 0 3s
[root@vms10 pod]#
复制代码
Step 4: Delete pod 2.
[root@vms10 pod]# kubectl delete -f pod2.yaml
pod "pod2" deleted
[root@vms10 pod]#
复制代码
Step 5: Modify the content of pod2.yaml, as shown in Figure 5-4.
Step 6: Create this pod.
[root@vms10 pod]# kubectl apply -f pod2.yaml
pod/pod2 created
[root@vms10 pod]#
复制代码
Step 7: View pods.
[root@vms10 pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 9m8s
pod2 2/2 Running 0 3s
[root@vms10 pod]#
复制代码
Here pod2 shows 2/2, indicating that there are 2 containers in the pod, and both containers are running normally.
In kubernetes, all resources, such as nodes, pods, and deployments and services that will be discussed later, have labels.
Step 8: View pod and label information.
[root@vms10 pod]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod1 1/1 Running 0 10m18s run=pod1
pod2 2/2 Running 0 7s run=pod2
[root@vms10 pod]#
复制代码
Step 9: Use -l (the initial letter of the label) to specify the label, which is used to list pods with a specific label, such as viewing the pod with the label run=pod1.
[root@vms10 pod]# kubectl get pods -l run=pod1
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 11m49s
[root@vms10 pod]#