一.命令式对象管理
1.命名空间管理
查看命名空间
[root@k8s-master ~]# kubectl get namespaces

创建命名空间
[root@k8s-master ~]# kubectl create namespace uselessqq
namespace/uselessqq created

删除命名空间
[root@k8s-master ~]# kubectl delete namespaces uselessqq
namespace "uselessqq" deleted
2.pod管理
查看pod的运行情况
[root@k8s-master ~]# kubectl get pods -o wide
创建pod
[root@k8s-master ~]# kubectl run lee --image nginx:1.23
pod/lee created

删除pod【可在最后加上--force实现快速删除】
[root@k8s-master ~]# kubectl delete pods lee
pod "lee" deleted from default namespace
当pod出现问题时

查看pod运行详情
[root@k8s-master ~]# kubectl describe pods error

二.kubectl 命令实操
1.上传实验镜像
[root@k8s-master ~]# docker load -i myapp.tar.gz
Loaded image: timinglee/myapp:v1
Loaded image: timinglee/myapp:v2
[root@k8s-master ~]# docker tag timinglee/myapp:v1 reg.timinglee.org/library/myapp:v1
[root@k8s-master ~]# docker push reg.timinglee.org/library/myapp:v1

2.生成实验所需yml文件
[root@k8s-master ~]# vim replica.yml

3.kubectl命令使用方法
create 建立
[root@k8s-master ~]# kubectl create deployment webcluster --replicas 2 --image myapp:v1
deployment.apps/webcluster created

edit 编辑
[root@k8s-master ~]# kubectl create deployment webcluster --image myapp:v1
deployment.apps/webcluster created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-kr88n 1/1 Running 0 10s
[root@k8s-master ~]# kubectl edit deployments.apps webcluster


patch 补丁
[root@k8s-master ~]# kubectl patch deployments.apps webcluster -p '{"spec":{"replicas":1}}'

expose 暴露端口
[root@k8s-master ~]# kubectl expose deployment webcluster --port 80 --target-port 80



logs 查看日志
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
webcluster-77c87d9946-9pzjq 1/1 Running 0 7m5s 10.244.1.3 k8s-node1 <none> <none>
[root@k8s-master ~]# kubectl logs pods/webcluster-77c87d9946-9pzjq

attach 缠绕
[root@k8s-master ~]# kubectl run -it testpod --image busybox:latest
All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt.
If you don't see a command prompt, try pressing enter.
/ #
/ #
/ # Session ended, resume using 'kubectl attach testpod -c testpod -i -t' command when the pod is running #此处可用exit退出
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 30s
webcluster-77c87d9946-9pzjq 1/1 Running 0 11m
[root@k8s-master ~]# kubectl attach pods/testpod -it
All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt.
If you don't see a command prompt, try pressing enter.
/ #
/ #
/ #
exec
[root@k8s-master ~]# kubectl run testpod --image nginx:1.23
pod/testpod created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 4s
root@k8s-master ~]# kubectl exec -it pods/testpod -c testpod -- /bin/bash
root@testpod:/#
cp 复制
pod内复制到pod外
[root@k8s-master ~]# kubectl cp testpod:/usr/share/nginx/html/index.html /mnt/test
tar: Removing leading `/' from member names
[root@k8s-master ~]# kubectl cp testpod:/usr/share/nginx/html /mnt/
tar: Removing leading `/' from member names
![]()
pod外复制到pod内
[root@k8s-master ~]# echo timinglee > /mnt/index.html
[root@k8s-master ~]# kubectl cp /mnt/index.html testpod:/usr/share/nginx/html/index.html
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testpod 1/1 Running 0 2m 10.244.2.10 k8s-node2 <none> <none>

rollout
[root@k8s-master ~]# kubectl create deployment webcluster --image myapp:v1 --replicas 2 --dry-run=client -o yaml > webcluster.yml
[root@k8s-master ~]# vim webcluster.yml

[root@k8s-master ~]# kubectl apply -f webcluster.yml
deployment.apps/webcluster created
[root@k8s-master ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
webcluster 2/2 2 2 5s
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-phhmr 1/1 Running 0 9s
webcluster-77c87d9946-wdxxs 1/1 Running 0 9s
[root@k8s-master ~]# kubectl rollout status deployment webcluster
deployment "webcluster" successfully rolled out
[root@k8s-master ~]# kubectl rollout restart deployment webcluster
deployment.apps/webcluster restarted
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-7cd889657-87wnv 1/1 Running 0 4s
webcluster-7cd889657-l5nfr 1/1 Running 0 3s
[root@k8s-master ~]# kubectl rollout restart deployment webcluster
deployment.apps/webcluster restarted
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-57d49fd8c5-tvnln 0/1 ContainerCreating 0 1s
webcluster-6865758996-xk2bz 1/1 Running 0 21s
webcluster-6865758996-zbnkd 1/1 Running 0 22s
scale
[root@k8s-master ~]# kubectl apply -f webcluster.yml
deployment.apps/webcluster created
[root@k8s-master ~]# kubectl scale deployment webcluster --replicas 4
deployment.apps/webcluster scaled
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-7fz6h 1/1 Running 0 16s
webcluster-77c87d9946-9295h 1/1 Running 0 11s
webcluster-77c87d9946-pxkvh 1/1 Running 0 11s
webcluster-77c87d9946-wfpgw 1/1 Running 0 16s
[root@k8s-master ~]# kubectl scale deployment webcluster --replicas 2
deployment.apps/webcluster scaled
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-7fz6h 1/1 Running 0 26s
webcluster-77c87d9946-wfpgw 1/1 Running 0 26s
label 标签
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
webcluster-77c87d9946-7fz6h 1/1 Running 0 86s app=webcluster,pod-template-hash=77c87d9946
webcluster-77c87d9946-wfpgw 1/1 Running 0 86s app=webcluster,pod-template-hash=77c87d9946
[root@k8s-master ~]# kubectl get deployments.apps webcluster --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
webcluster 2/2 2 2 101s app=webcluster
删除标签后会自动创建新的pod补上,再将标签补上后,新建立的pod会被删除
[root@k8s-master ~]# kubectl label pods webcluster-77c87d9946-7fz6h app-
pod/webcluster-77c87d9946-7fz6h unlabeled
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
webcluster-77c87d9946-7fz6h 1/1 Running 0 3m6s pod-template-hash=77c87d9946
webcluster-77c87d9946-g2sl2 1/1 Running 0 10s app=webcluster,pod-template-hash=77c87d9946
webcluster-77c87d9946-wfpgw 1/1 Running 0 3m6s app=webcluster,pod-template-hash=77c87d9946
[root@k8s-master ~]# kubectl label pods webcluster-77c87d9946-7fz6h app=webcluster
pod/webcluster-77c87d9946-7fz6h labeled
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
webcluster-77c87d9946-7fz6h 1/1 Running 0 3m23s app=webcluster,pod-template-hash=77c87d9946
webcluster-77c87d9946-wfpgw 1/1 Running 0 3m23s app=webcluster,pod-template-hash=77c87d9946
三.利用控制器实现版本更替
1.建立控制器
[root@k8s-master ~]# vim webcluster.yml

[root@k8s-master ~]# kubectl apply -f webcluster.yml
deployment.apps/webcluster created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-qgvzp 1/1 Running 0 9s
webcluster-77c87d9946-zstjp 1/1 Running 0 9s
[root@k8s-master ~]# kubectl rollout history deployment webcluster
deployment.apps/webcluster
REVISION CHANGE-CAUSE
1 <none>
[root@k8s-master ~]# kubectl expose deployment webcluster --port 80 --target-port 80 --type NodePort
service/webcluster exposed
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 90m
webcluster NodePort 10.107.198.218 <none> 80:31072/TCP 6s

2.更新业务版本
[root@k8s-master ~]# kubectl set image deployments webcluster myapp=myapp:v2
deployment.apps/webcluster image updated
[root@k8s-master ~]# kubectl annotate deployment webcluster kubernetes.io/change-cause="myappv2" --overwrite
deployment.apps/webcluster annotated
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-6c8b4bb9d7-gwv9g 1/1 Running 0 20s
webcluster-6c8b4bb9d7-xxtkr 1/1 Running 0 19s
[root@k8s-master ~]# kubectl rollout history deployment webcluster
deployment.apps/webcluster
REVISION CHANGE-CAUSE
1 <none>
2 myappv2

3.版本回退
[root@k8s-master ~]# kubectl rollout history deployment webcluster
deployment.apps/webcluster
REVISION CHANGE-CAUSE
1 <none>
2 myappv2
[root@k8s-master ~]# kubectl rollout undo deployment webcluster --to-revision 1
deployment.apps/webcluster rolled back

四.利用yaml文件声明资源
1.在pod中运行多容器
[root@k8s-master ~]# kubectl run testpod --image myapp:v1 --dry-run=client -o yaml > testpod.yaml
[root@k8s-master ~]# vim testpod.yaml

[root@k8s-master ~]# kubectl apply -f testpod.yaml
pod/testpod created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 2/2 Running 0 6s
[root@k8s-master ~]# kubectl exec -it pods/testpod -c busybox -- /bin/sh
/bin/sh: shopt: not found
[ root@testpod:/ ]$ curl 127.0.0.1
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
2.在pod运行主机中暴漏端口

[root@k8s-master ~]# kubectl apply -f testpod.yaml
pod/testpod created
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testpod 1/1 Running 0 2m52s 10.244.2.23 k8s-node2 <none> <none>
[root@k8s-master ~]# curl node2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
3.在pod中指定变量

[root@k8s-master ~]# kubectl apply -f mysql.yml
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql 2/2 Running 0 3m21s 10.244.1.14 k8s-node1 <none> <none>

4.选择运行节点
[root@k8s-master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready control-plane 131m v1.35.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node1 Ready <none> 130m v1.35.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux
k8s-node2 Ready <none> 129m v1.35.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
[root@k8s-master ~]# vim mysql.yml
-------------------------------------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: mysql
name: mysql
spec:
nodeSelector:
kubernetes.io/hostname: k8s-node2
containers:
- image: mysql:8.0
name: mysql8
env:
- name: MYSQL_ROOT_PASSWORD
value: lee
- image: phpmyadmin:latest
name: mysqladmin
env:
- name: PMA_ARBITRARY
value: "1"
ports:
- name: phpadminport
containerPort: 80
hostPort: 80
protocol: TCP


5.共享宿主机网络
[root@k8s-master pod]# vim testpod.yaml
-----------------------------------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
-----------------------------------------------------------------------------------------
[root@k8s-master ~]# kubectl apply -f testpod.yaml
pod/testpod created
[root@k8s-master ~]# kubectl exec -it pods/testpod -c busybox -- /bin/sh
/ # ifconfig
cni0 Link encap:Ethernet HWaddr 5E:9F:2B:A2:8C:4C
inet addr:10.244.2.1 Bcast:10.244.2.255 Mask:255.255.255.0
inet6 addr: fe80::5c9f:2bff:fea2:8c4c/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:490 errors:0 dropped:0 overruns:0 frame:0
TX packets:460 errors:0 dropped:1 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:641883 (626.8 KiB) TX bytes:87414 (85.3 KiB)
docker0 Link encap:Ethernet HWaddr 22:24:8B:84:DF:DE
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:8 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
eth0 Link encap:Ethernet HWaddr 00:0C:29:1F:99:4F
inet addr:172.25.254.20 Bcast:172.25.254.255 Mask:255.255.255.0
inet6 addr: fe80::3e0b:c3cb:ef36:19c8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1267270 errors:0 dropped:0 overruns:0 frame:0
TX packets:53333 errors:0 dropped:6 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1877993789 (1.7 GiB) TX bytes:5545362 (5.2 MiB)
flannel.1 Link encap:Ethernet HWaddr 96:9A:2A:44:9F:B3
inet addr:10.244.2.0 Bcast:0.0.0.0 Mask:255.255.255.255
inet6 addr: fe80::949a:2aff:fe44:9fb3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
RX packets:17 errors:0 dropped:0 overruns:0 frame:0
TX packets:14 errors:0 dropped:40 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1525 (1.4 KiB) TX bytes:1303 (1.2 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:1709 errors:0 dropped:0 overruns:0 frame:0
TX packets:1709 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:158882 (155.1 KiB) TX bytes:158882 (155.1 KiB)
6.资源优先级
BestEffort 无资源限制,低优先级
[root@k8s-master pod]# vim testpod.yaml
-----------------------------------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000

Burstable 有资源限制【期望值和限制值不同】,中优先级
[root@k8s-master pod]# vim testpod.yaml
-----------------------------------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
resources:
limits:
cpu: 700m
memory: 200M
requests:
cpu: 500m
memory: 100M
-----------------------------------------------------------------------------------------
[root@k8s-master pod]# kubectl apply -f testpod.yaml

Guaranteed 有资源限制【期望值和限制值相同】,高优先级
[root@k8s-master pod]# vim testpod.yaml
-----------------------------------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
resources:
limits:
cpu: 500m
memory: 100M
requests:
cpu: 500m
memory: 100M
[root@k8s-master pod]# vim testpod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
resources:
limits:
cpu: 500m
memory: 100M
requests:
cpu: 500m
memory: 100M
-----------------------------------------------------------------------------------------
[root@k8s-master pod]# kubectl apply -f testpod.yaml
![]()
7.容器重启规则
Always 无论如何都会重新运行pod
[root@k8s-master ~]# vim testpod.yaml
-------------------------------------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
restartPolicy: Always
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 60
-------------------------------------------------------------------------------------------
[root@k8s-master ~]# kubectl apply -f testpod.yaml
pod/testpod created
[root@k8s-master ~]# kubectl get pods -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testpod 1/1 Running 0 3s 172.25.254.20 k8s-node2 <none> <none>
####################################################################
[root@k8s-node2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a207144cd4e8 40680ace50cf "/bin/sh -c 'sleep 6…" 28 seconds ago Up 27 seconds k8s_busybox_testpod_default_8f1066c4-84dd-455d-9d26-43c2645864c0_0
[root@k8s-node2 ~]# docker rm a207144cd4e8
a207144cd4e8

OnFailure 非正常管关闭会重启pod
[root@k8s-master ~]# vim testpod.yaml
-------------------------------------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
restartPolicy: OnFailure
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 30
-------------------------------------------------------------------------------------------
[root@k8s-master ~]kubectl apply -f testpod.yaml
[root@k8s-node2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5adbb00cd2b5 40680ace50cf "/bin/sh -c 'sleep 3…" 16 seconds ago Up 15 seconds k8s_busybox_testpod_default_b0edd3a4-acf3-47fd-9e9f-e252e6f05816_0
[root@k8s-node2 ~]# docker rm -f 5adbb00cd2b5
5adbb00cd2b5

Never pod关闭后不重启
[root@k8s-master ~]# vim testpod.yaml
-------------------------------------------------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
restartPolicy: Never
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 30
-------------------------------------------------------------------------------------------
[root@k8s-master ~]# kubectl apply -f testpod.yaml
[root@k8s-master ~]# kubectl get pods -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testpod 1/1 Running 0 3s 172.25.254.20 k8s-node2 <none> <none>

五.pod的生命周期
1.init容器
[root@k8s-master pod]# kubectl run webserver --image myapp:v1 --dry-run=client -o yaml > init-example.yml
[root@k8s-master pod]# vim init-example.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
initContainers:
- name: busybox
image: busybox:latest
command:
- /bin/sh
- -c
- "until test -e /testfile;do echo wating for myservice; sleep 2;done"
containers:
- image: myapp:v1
name: webserver
restartPolicy: Always
[root@k8s-master pod]# watch -n 1 kubectl get pods -o wide
[root@k8s-master pod]# kubectl apply -f init-example.yml
pod/webserver created

[root@k8s-master pod]# kubectl exec -it pods/webserver -c busybox -- /bin/sh
/ #
/ # touch /testfile
/ # command terminated with exit code 137

2.Livness存活探针
没有存活探针时
[root@k8s-master pod]# kubectl run testpod --image myapp:v1 --dry-run=client -o yaml > livness-example.yaml
[root@k8s-master pod]# vim livness-example.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
containers:
- image: myapp:v1
name: webserver
command: ["/bin/sh", "-c"]
args:
- |
nginx -g "daemon off;"
sleep 10000
restartPolicy: Always
[root@k8s-master pod]# kubectl apply -f livness-example.yaml
监控程序
[root@k8s-master pod]# watch -n 1 kubectl get pods -o wide

测试操作:
[root@k8s-master pod]# kubectl exec -it pods/webserver -c webserver -- /bin/sh
# nginx -s stop


有存活探针时
[root@k8s-master pod]# kubectl delete -f livness-example.yaml --force
[root@k8s-master pod]# vim livness-example.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
containers:
- image: myapp:v1
name: testpod
command: ["/bin/sh", "-c"]
args:
- |
nginx -g "daemon off;"
sleep 10000
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 3
periodSeconds: 1
timeoutSeconds: 1
restartPolicy: Always
[root@k8s-master pod]# kubectl apply -f livness-example.yaml
[root@k8s-master pod]# kubectl exec -it pods/webserver -- /bin/sh
/ # nginx -s stop

3.readness 就绪探针
[root@k8s-master pod]# vim readness-example.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
containers:
- image: myapp:v1
name: webserver
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
labels:
run: webserver
name: webserver
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: webserver
[root@k8s-master pod]# kubectl apply -f readness-example.yml
pod/webserver created
service/webserver created
[root@k8s-master pod]# kubectl apply -f readness-example.yml
pod/webserver unchanged
service/webserver unchanged
[root@k8s-master pod]# kubectl describe svc webserver
Name: webserver
Namespace: default
Labels: run=webserver
Annotations: <none>
Selector: run=webserver
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.106.199.44
IPs: 10.106.199.44
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.2.29:80
Session Affinity: None
Internal Traffic Policy: Cluster
Events: <none>
删除默认发布文件
[root@k8s-master pod]# kubectl exec -it pods/webserver -c webserver -- /bin/sh
/ # cd /usr/share/nginx/
/usr/share/nginx # ls
html
/usr/share/nginx # cd html/
/usr/share/nginx/html # ls
50x.html index.html
/usr/share/nginx/html # rm -fr index.html
/usr/share/nginx/html # ls
50x.html
/usr/share/nginx/html # exit
验证Endpoints是否被下架
[root@k8s-master pod]# kubectl describe svc webserver
Name: webserver
Namespace: default
Labels: run=webserver
Annotations: <none>
Selector: run=webserver
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.106.199.44
IPs: 10.106.199.44
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.2.29:80
Session Affinity: None
Internal Traffic Policy: Cluster
Events: <none>
有就绪探针情况
[root@k8s-master pod]# vim readness-example.yml
[root@k8s-master pod]# kubectl apply -f readness-example.yml
pod/webserver created
service/webserver created
[root@k8s-master pod]# kubectl describe svc webserver
Name: webserver
Namespace: default
Labels: run=webserver
Annotations: <none>
Selector: run=webserver
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.105.176.71
IPs: 10.105.176.71
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.2.30:80
Session Affinity: None
Internal Traffic Policy: Cluster
Events: <none>
[root@k8s-master pod]# kubectl exec -it pods/webserver -c webserver -- /bin/sh
/ # rm -fr /usr/share/nginx/html/index.html

/ # echo timinglee > /usr/share/nginx/html/index.html

542



被折叠的 条评论
为什么被折叠?



