k8s 重启策略、健康检查、环境变量、初始化容器
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
FIELD: restartPolicy
DESCRIPTION:
Restart policy for all containers within the pod. One of Always, OnFailure,
Never. Default to Always. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy
//修改为Never
[root@master ~]# cat test.yml
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
- name: test
image: busybox
imagePullPolicy: IfNotPresent
command: [“bin/sh”,“-c”,“sleep 45”]
restartPolicy: Never #默认的话不用修改,改为never后停止容器不会重启
[root@master ~]# kubectl apply -f test.yml
pod/web created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web 2/2 Running 0 12s
[root@master ~]# kubectl get pod -o wide -w #-w实时监控,在node2上面用docker命令关上其中一个,发现不会重启
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web 2/2 Running 0 23s 10.244.1.78 node1
web 1/2 NotReady 0 46s 10.244.1.78 node1
//修改为Always
[root@master ~]# cat test.yml
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
- name: test
image: busybox
imagePullPolicy: IfNotPresent
command: [“bin/sh”,“-c”,“sleep 45”]
restartPolicy: Always
//删除原来的test.yml ,重启启动一个新的pod
[root@master ~]# kubectl delete -f test.yml
pod “web” deleted
[root@master ~]# kubectl apply -f test.yml
pod/web created
//启动好后,在node1上停止test
[root@master ~]# kubectl get pods -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web 2/2 Running 0 6s 10.244.1.79 node1
web 1/2 NotReady 0 46s 10.244.1.79 node1
web 2/2 Running 1 47s 10.244.1.79 node1
web 1/2 NotReady 1 92s 10.244.1.79 node1
web 1/2 CrashLoopBackOff 1 102s 10.244.1.79 node1
web 2/2 Running 2 103s 10.244.1.79 node1
等待一定的时间后重启,而后自动起一台新的
//OnFailure
[root@master ~]# cat test.yml
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
- name: test
image: busybox
imagePullPolicy: IfNotPresent
command: [“bin/sh”,“-c”,“sleep 45”]
restartPolicy: OnFailure
//删除原来的test.yml ,重启启动一个新的pod
[root@master ~]# kubectl delete -f test.yml
pod “web” deleted
[root@master ~]# kubectl apply -f test.yml
pod/web created
//启动好后,在node1上停止test
[root@node1 ~]# docker ps | grep test
6ed4e11715d8 ffe9d497c324 “bin/sh -c ‘sleep 45’” 3 seconds ago Up 3 seconds k8s_test_web_default_cc265ebd-4dae-49e7-8235-d1ba1102b4af_0
[root@node1 ~]# docker kill 6ed4e11715d8
6ed4e11715d8
//又起来了
[root@node1 ~]# docker ps | grep test
bf042b813e7c ffe9d497c324 “bin/sh -c ‘sleep 45’” 14 seconds ago Up 13 seconds k8s_test_web_default_cc265ebd-4dae-49e7-8235-d1ba1102b4af_1
[root@master ~]# kubectl get pods -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web 2/2 Running 0 1s 10.244.1.80 node1
web 1/2 Error 0 34s 10.244.1.80 node1
//异常退出(手动杀掉)
web 2/2 Running 1 35s 10.244.1.80 node1
//正常退出(60秒)
web 1/2 NotReady 1 80s 10.244.1.80 node1
web 1/2 NotReady 1 2m26s 10.244.1.80 node1
-
livenessProbe(存活检查)︰如果检查失败,将杀死容器,根据Pod的restartPolicy来操作
-
readinessProbe(就绪检查)︰如果检查失败,Kubernetes会把Pod从service endpoints中剔除
支持的检查方式:
-
httpGet:发送HTTP请求,返回200-400范围状态码为成功
-
exec: 执行hell命令返回状态码是0为成功
-
tcpSocket:发起TCP Socket建立成功
与重启策略相结合使用
重启策略+健康检查(应用自修复)
//端口探测
[root@master ~]# cat test.yml
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
hostPort: 80
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 20 #启动容器后多少秒健康检查
periodSeconds: 10 #以后间隔多少秒检查一次
readinessProbe:
httpGet:
port: 80
initialDelaySeconds: 20
periodSeconds: 10
[root@master ~]# kubectl apply -f test.yml
pod/web created
//查看pod,发现在进行初始化
[root@master ~]# kubectl get pod
NAME READY(就绪状态) STATUS(存活状态) RESTARTS AGE
web 0/1 Running 0 18s
//等待一定时间后会进入运行
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web 1/1 Running 0 34s
如果失败init容器默认会在State中显示CrashLoopBackOff (重启/异常)
在Reason会显示Error
State 代表状态
Reason 原因
Terminated 终止
Completed 完成
变量值几种定义方式:
-
自定义变量值
-
变量值从Pod属性获取
-
变量值从Secrt,ConfigMap
[root@master ~]# kubectl explain pod.spec.containers.env.valueFrom
KIND: Pod
VERSION: v1
RESOURCE: valueFrom
DESCRIPTION:
Source for the environment variable’s value. Cannot be used if value is not
empty.
EnvVarSource represents a source for the value of an EnvVar.
FIELDS:
configMapKeyRef
Selects a key of a ConfigMap.
fieldRef
Selects a field of the pod: supports metadata.name, metadata.namespace,
metadata.labels['<KEY>']
, metadata.annotations['<KEY>']
, spec.nodeName,
spec.serviceAccountName, status.hostIP, status.podIP, status.podIPs.
resourceFieldRef
Selects a resource of the container: only resources limits and requests
(limits.cpu, limits.memory, limits.ephemeral-storage, requests.cpu,
requests.memory and requests.ephemeral-storage) are currently supported.
secretKeyRef
Selects a key of a secret in the pod’s namespace
第一种
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: bi
image: busybox
imagePullPolicy: IfNotPresent
command: [“bin/sh”,“-c”,“sleep 45”]
env:
- name: HN
value: tom
[root@master ~]# kubectl apply -f test.yml
pod/test created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test 1/1 Running 0 21s
[root@master ~]# kubectl exec -it test – /bin/sh
/ # echo $HN
tom
第二种
[root@master ~]# cat test.yml
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: bi
image: busybox
imagePullPolicy: IfNotPresent
command: [“bin/sh”,“-c”,“sleep 45”]
env:
- name: HN
valueFrom:
fieldRef:
fieldPath: metadata.name
[root@master ~]# kubectl delete -f test.yml
pod “test” deleted
[root@master ~]# kubectl apply -f test.yml
pod/test created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test 1/1 Running 0 21s
[root@master ~]# kubectl exec -it test – /bin/sh
/ # echo $HN
test
第三种
[root@master ~]# cat test.yml
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: bi
image: busybox
imagePullPolicy: IfNotPresent
command: [“bin/sh”,“-c”,“sleep 45”]
env:
- name: HN
valueFrom:
fieldRef:
fieldPath: spec.nodeName
[root@master ~]# kubectl delete -f test.yml
pod “test” deleted
[root@master ~]# kubectl apply -f test.yml
pod/test created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test 1/1 Running 0 17s
[root@master ~]# kubectl exec -it test – /bin/sh
/ # echo $HN
node1
第四种
[root@master ~]# cat test.yml
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: bi
image: busybox
imagePullPolicy: IfNotPresent
command: [“bin/sh”,“-c”,“sleep 45”]
env:
- name: HN
valueFrom:
fieldRef:
fieldPath: status.podIP
[root@master ~]# kubectl delete -f test.yml
pod “test” deleted
[root@master ~]# kubectl apply -f test.yml
pod/test created
[root@master ~]# kubectl exec -it test – /bin/sh
/ # echo $HN
10.244.1.84
/ # exit
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test 1/1 Running 1 50s 10.244.1.84 node1
初始化容器
-
Init Container:用于初始化工作,执行完就结束(一次性任务)
-
支持大部分应用容器配置,但不支持健康检查
-
优先应用容器执行
应用场景:
-
环境检查:例如确保应用容器依赖的服务启动后再启动应用容器
-
初始化配置:例如给应用容器准备配置文件
示例
这里部署一个web网站,网站程序没有打到镜像中,而是希望从代码仓库中动态拉取放到应用容器中
在这里插入代码片[root@master ~]# cat test.yml
apiVersion: v1
kind: Pod
metadata:
name: web
namespace: default
spec:
initContainers:
- name: download
image: busybox
imagePullPolicy: IfNotPresent
volumeMounts:
- name: data
mountPath: /tmp
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
hostPort: 80
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumes:
- name: data
hostPath:
path: /var/www/html
//不管在哪个节点我都创建
[root@node1 ~]# mkdir /var/www/html/ -p
[root@node1 ~]# cd /var/www/html/
[root@node1 html]# echo “1314444” > index.html
[root@node1 html]# cat index.html
1314444
[root@node2 ~]# mkdir /var/www/html/ -p
[root@node2 ~]# cd /var/www/html/
[root@node2 html]# echo “hello world” > index.html
[root@node2 html]# cat index.html
hello world
[root@master ~]# kubectl apply -f test.yml
pod/web created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web 1/1 Running 0 8s
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web 1/1 Running 0 45s 10.244.1.85 node1
[root@master ~]# curl 10.244.1.85
1314444
//详细信息
[root@master ~]# kubectl describe pod web
Name: web
Namespace: default
Priority: 0
Node: node1/192.168.129.135
Start Time: Wed, 22 Dec 2021 22:27:06 +0800
Labels:
Annotations:
Status: Running
IP: 10.244.1.86
IPs:
IP: 10.244.1.86
Init Containers:
download:
Container ID: docker://ec747a2654371d818661cc8e1da08d465d15e8d8d13c3a8d6a47f1ca21106d94
Image: busybox
Image ID: docker-pullable://busybox@sha256:b5cfd4befc119a590ca1a81d6bb0fa1fb19f1fbebd0397f25fae164abe1e8a6a
Port:
Host Port:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Wed, 22 Dec 2021 22:27:07 +0800
Finished: Wed, 22 Dec 2021 22:27:07 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/tmp from data (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-ck7n4 (ro)
Containers:
nginx:
Container ID: docker://6dd618f9591a8d27623d1394d07def8498e25741af293ff5ef101db3516206e1
Image: nginx
Image ID: docker-pullable://nginx@sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Port: 80/TCP
Host Port: 80/TCP
State: Running
Started: Wed, 22 Dec 2021 22:27:08 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/usr/share/nginx/html from data (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-ck7n4 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
data:
Type: HostPath (bare host directory volume)
Path: /var/www/html
HostPathType:
default-token-ck7n4:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-ck7n4
Optional: false
QoS Class: BestEffort
Node-Selectors:
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
Normal Scheduled 6m35s default-scheduler Successfully assigned default/web to node1
Normal Pulled 6m34s kubelet Container image “busybox” already present on machine
Normal Created 6m34s kubelet Created container download
Normal Started 6m34s kubelet Started container download
Normal Pulled 6m33s kubelet Container image “nginx” already present on machine
Normal Created 6m33s kubelet Created container nginx
Normal Started 6m33s kubelet Started container nginx
总结:Pod中会有这几种类型的容器
- Infrastructure Container:基础容器
维护整个Pod网络空间
- lnitContainers:初始化容器
先于业务容器开始执行
- Containers:业务容器
并行启动
[root@master httpd]# vim Dockerfile
FROM busybox
RUN mkdir /data && \
echo “test page on jjyy” > /data/index.html
ENTRYPOINT [“/bin/httpd”,“-f”,“-h”,“/data”]
[root@master ~]# docker build -t 1314444/httpd:v0.1 httpd
[root@master ~]# vim httpd/Dockerfile
FROM busybox
RUN mkdir /data && \
echo “test page on 666” > /data/index.html
ENTRYPOINT [“/bin/httpd”,“-f”,“-h”,“/data”]
[root@master ~]# docker build -t 1314444/httpd:v0.2 httpd
[root@master ~]# cat manifest/web.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: web1
template:
metadata:
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
dir /data && \
echo “test page on 666” > /data/index.html
ENTRYPOINT [“/bin/httpd”,“-f”,“-h”,“/data”]
[root@master ~]# docker build -t 1314444/httpd:v0.2 httpd
[root@master ~]# cat manifest/web.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: web1
template:
metadata:
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-x4hI1BFM-1715790961270)]
[外链图片转存中…(img-vRfkGNg5-1715790961271)]
[外链图片转存中…(img-PdbFk8Uq-1715790961271)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
更多推荐
所有评论(0)