Day05-readinessProbe探针,startupProbe探针,Pod生命周期,静态Pod,初始化容器,rc控制器的升级和回滚,rs控制器精讲

Day05-readinessProbe探针,startupProbe探针,Pod生命周期,静态Pod,初始化容器,rc控制器的升级和回滚,rs控制器精讲

  • 0、昨日内容回顾
  • 1、readinessProbe可用性检查探针之exec案例
  • 2、可用性检查之httpGet案例
  • 3、可用性检查之tcpSocket案例
  • 4、readinessProbe和livenessProbe搭配使用案例
  • 5、startupProbe启动探针和其他探针的执行优先案例
  • 6、初始化容器案例:
  • 7、静态Pod(了解即可)
  • 8、Pod的安全上下文securityContext实战
  • 9、Pod的生命周期优雅的终止案例
  • 10、Pod创建流程图解
  • 11、rc的升级和回滚
  • 12、rs的Pod控制器实战案例

0、昨日内容回顾

- harbor认证信息使用secret的资源清单编写;- 标签管理:声明式:修改资源清单。响应式:直接编写命令行参数。-  探针(Probe):LivenessProbe:- exec- httpGet- tcpSocket- 名称空间(namespace):隔离K8S集群资源。- 控制器(rc):用于控制Pod的副本数量。- 服务(services):用于暴露K8S服务。- 对内提供Pod的服务动态发现- 对外提供统一的访问入口,进行Pod的负载均衡四种类型:- ClusterIP- NodePort- ExternalName- LoadBalancer

Pod的资源清单

apiVersion: v1
kind: Pod
metadata:name: namespace:labels:
spec:hostNetwork:imagePullSecrets:- name:restartPolicy:nodeName:volumes:- name:nfs:server:path:- name:emptyDir:{}- name:hostPath:path:- ...containers:- name:image:stdin:comannd:args:ports:env:livenessProbe:exec:httpGet:tcpSocket:...volumeMounts:resources:requests:limits:imagePullPolicy:

1、readinessProbe可用性检查探针之exec案例

(1)编写资源清单

[root@k8s231.oldboyedu.com replicationcontrollers]# cat 02-rc-readinessProbe.yaml 
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-readinessprobelabels:school: oldboyeduclass: linux85apps: rcnamespace: default
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahehobby: k8sauther: jasonyinspec:containers:- name: linux85-execimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinecommand: - /bin/sh- -c- touch /tmp/oldboyedu-linux85-healthy; sleep 5; rm -f /tmp/oldboyedu-linux85-healthy; sleep 600# 可用性检查,周期性检查服务是否可用,从而判断容器是否就绪.readinessProbe:# 使用exec的方式去做健康检查exec:# 自定义检查的命令command:- cat- /tmp/oldboyedu-linux85-healthyfailureThreshold: 3initialDelaySeconds: 15periodSeconds: 1successThreshold: 1timeoutSeconds: 1---apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-readinessprobenamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:selector:hobby: k8sauther: jasonyintype: ClusterIPports:- port: 80targetPort: 80protocol: TCPclusterIP: 10.200.100.200
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(2)创建资源清单

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl apply -f 02-rc-readinessProbe.yaml 

(3)查看Pod状态

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get po
NAME                                            READY   STATUS        RESTARTS        AGE
oldboyedu-linux85-web-rc-readinessprobe-9bjr6   0/1     Running       0               2s
oldboyedu-linux85-web-rc-readinessprobe-fqltq   0/1     Running       0               2s
oldboyedu-linux85-web-rc-readinessprobe-p488g   0/1     Running       0               2s
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(4)查看svc的状态

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get svc oldboyedu-linux85-web-readinessprobe 
NAME                                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
oldboyedu-linux85-web-readinessprobe   ClusterIP   10.200.100.200   <none>        80/TCP    28s
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl describe svc oldboyedu-linux85-web-readinessprobe 
Name:              oldboyedu-linux85-web-readinessprobe
Namespace:         default
Labels:            apps=oldboyedu-svcclass=linux85
Annotations:       <none>
Selector:          auther=jasonyin,hobby=k8s
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.200.100.200
IPs:               10.200.100.200
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         
Session Affinity:  None
Events:            <none>
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(5)查看ep资源清单

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get ep oldboyedu-linux85-web-readinessprobe 
NAME                                   ENDPOINTS   AGE
oldboyedu-linux85-web-readinessprobe               64s
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl describe ep oldboyedu-linux85-web-readinessprobe 
Name:         oldboyedu-linux85-web-readinessprobe
Namespace:    default
Labels:       apps=oldboyedu-svcclass=linux85
Annotations:  <none>
Subsets:Addresses:          <none>NotReadyAddresses:  10.100.1.80,10.100.2.61,10.100.2.62Ports:Name     Port  Protocol----     ----  --------<unset>  80    TCPEvents:  <none>
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(6)将任意2个Pod调整为就绪状态

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get po
NAME                                            READY   STATUS    RESTARTS   AGE
oldboyedu-linux85-web-rc-readinessprobe-9bjr6   0/1     Running   0          100s
oldboyedu-linux85-web-rc-readinessprobe-fqltq   0/1     Running   0          100s
oldboyedu-linux85-web-rc-readinessprobe-p488g   0/1     Running   0          100s
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl exec  oldboyedu-linux85-web-rc-readinessprobe-9bjr6  -- touch /tmp/oldboyedu-linux85-healthy
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl exec  oldboyedu-linux85-web-rc-readinessprobe-fqltq -- touch /tmp/oldboyedu-linux85-healthy
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get po
NAME                                            READY   STATUS    RESTARTS   AGE
oldboyedu-linux85-web-rc-readinessprobe-9bjr6   1/1     Running   0          2m4s
oldboyedu-linux85-web-rc-readinessprobe-fqltq   1/1     Running   0          2m4s
oldboyedu-linux85-web-rc-readinessprobe-p488g   0/1     Running   0          2m4s
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(7)查看ep,svc资源状态

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl describe svc oldboyedu-linux85-web-readinessprobe 
Name:              oldboyedu-linux85-web-readinessprobe
Namespace:         default
Labels:            apps=oldboyedu-svcclass=linux85
Annotations:       <none>
Selector:          auther=jasonyin,hobby=k8s
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.200.100.200
IPs:               10.200.100.200
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.100.2.61:80,10.100.2.62:80
Session Affinity:  None
Events:            <none>
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl describe ep oldboyedu-linux85-web-readinessprobe 
Name:         oldboyedu-linux85-web-readinessprobe
Namespace:    default
Labels:       apps=oldboyedu-svcclass=linux85
Annotations:  endpoints.kubernetes.io/last-change-trigger-time: 2023-04-18T03:00:16Z
Subsets:Addresses:          10.100.2.61,10.100.2.62NotReadyAddresses:  10.100.1.80Ports:Name     Port  Protocol----     ----  --------<unset>  80    TCPEvents:  <none>
[root@k8s231.oldboyedu.com replicationcontrollers]# 

2、可用性检查之httpGet案例

(1)编写资源清单

[root@k8s231.oldboyedu.com replicationcontrollers]# cat 03-rc-readinessProbe-httpGet.yaml 
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-readinessprobe-httpgetlabels:school: oldboyeduclass: linux85apps: rcnamespace: default
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahehobby: k8sauther: jasonyinspec:containers:- name: linux85-execimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpine# 可用性检查,周期性检查服务是否可用,从而判断容器是否就绪.readinessProbe:# 使用httpGet的方式去做健康检查httpGet:# 指定访问的端口号port: 80# 检测指定的访问路径path: /index.htmlfailureThreshold: 3initialDelaySeconds: 15periodSeconds: 1successThreshold: 1timeoutSeconds: 1---apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-readinessprobe-httpgetnamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:selector:hobby: k8sauther: jasonyintype: ClusterIPports:- port: 80targetPort: 80protocol: TCPclusterIP: 10.200.100.220
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(2)创建资源清单

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl apply -f 03-rc-readinessProbe-httpGet.yaml 
replicationcontroller/oldboyedu-linux85-web-rc-readinessprobe-httpget created
service/oldboyedu-linux85-web-readinessprobe-httpget created
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(3)查看就绪状态,大概是在15s之后才是就绪状态

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get pods
NAME                                                    READY   STATUS    RESTARTS   AGE
oldboyedu-linux85-web-rc-readinessprobe-httpget-gtrz2   1/1     Running   0          25s
oldboyedu-linux85-web-rc-readinessprobe-httpget-h2nkn   1/1     Running   0          25s
oldboyedu-linux85-web-rc-readinessprobe-httpget-pxqkz   1/1     Running   0          25s
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl describe svc oldboyedu-linux85-web-readinessprobe-httpget 
Name:              oldboyedu-linux85-web-readinessprobe-httpget
Namespace:         default
Labels:            apps=oldboyedu-svcclass=linux85
Annotations:       <none>
Selector:          auther=jasonyin,hobby=k8s
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.200.100.220
IPs:               10.200.100.220
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.100.1.83:80,10.100.2.64:80,10.100.2.65:80
Session Affinity:  None
Events:            <none>
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl describe ep oldboyedu-linux85-web-readinessprobe-httpget 
Name:         oldboyedu-linux85-web-readinessprobe-httpget
Namespace:    default
Labels:       apps=oldboyedu-svcclass=linux85
Annotations:  endpoints.kubernetes.io/last-change-trigger-time: 2023-04-18T03:31:46Z
Subsets:Addresses:          10.100.1.83,10.100.2.64,10.100.2.65NotReadyAddresses:  <none>Ports:Name     Port  Protocol----     ----  --------<unset>  80    TCPEvents:  <none>
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(4)修改其为不就绪状态

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get pods
NAME                                                    READY   STATUS    RESTARTS   AGE
oldboyedu-linux85-web-rc-readinessprobe-httpget-gtrz2   1/1     Running   0          2m3s
oldboyedu-linux85-web-rc-readinessprobe-httpget-h2nkn   1/1     Running   0          2m3s
oldboyedu-linux85-web-rc-readinessprobe-httpget-pxqkz   1/1     Running   0          2m3s
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl exec oldboyedu-linux85-web-rc-readinessprobe-httpget-gtrz2 -- rm -f /usr/share/nginx/html/index.html
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get pods
NAME                                                    READY   STATUS    RESTARTS   AGE
oldboyedu-linux85-web-rc-readinessprobe-httpget-gtrz2   0/1     Running   0          2m29s
oldboyedu-linux85-web-rc-readinessprobe-httpget-h2nkn   1/1     Running   0          2m29s
oldboyedu-linux85-web-rc-readinessprobe-httpget-pxqkz   1/1     Running   0          2m29s
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(5)再次查看svc和ep状态

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl describe svc oldboyedu-linux85-web-readinessprobe-httpget 
Name:              oldboyedu-linux85-web-readinessprobe-httpget
Namespace:         default
Labels:            apps=oldboyedu-svcclass=linux85
Annotations:       <none>
Selector:          auther=jasonyin,hobby=k8s
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.200.100.220
IPs:               10.200.100.220
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.100.2.64:80,10.100.2.65:80
Session Affinity:  None
Events:            <none>
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl describe ep oldboyedu-linux85-web-readinessprobe-httpget 
Name:         oldboyedu-linux85-web-readinessprobe-httpget
Namespace:    default
Labels:       apps=oldboyedu-svcclass=linux85
Annotations:  endpoints.kubernetes.io/last-change-trigger-time: 2023-04-18T03:33:54Z
Subsets:Addresses:          10.100.2.64,10.100.2.65NotReadyAddresses:  10.100.1.83Ports:Name     Port  Protocol----     ----  --------<unset>  80    TCPEvents:  <none>
[root@k8s231.oldboyedu.com replicationcontrollers]# 

3、可用性检查之tcpSocket案例

(1)编写资源清单

[root@k8s231.oldboyedu.com replicationcontrollers]# cat 04-rc-readinessProbe-tcpSocket.yaml 
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-readinessprobe-tcpsocketlabels:school: oldboyeduclass: linux85apps: rcnamespace: default
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahehobby: k8sauther: jasonyinspec:containers:- name: linux85-execimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinecommand:- /bin/sh- -c- sleep 25; nginx -g "daemon off;"# 可用性检查,周期性检查服务是否可用,从而判断容器是否就绪.readinessProbe:# 使用tcpSocket的方式去做健康检查tcpSocket:port: 80failureThreshold: 3initialDelaySeconds: 15periodSeconds: 1successThreshold: 1timeoutSeconds: 1---
apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-readinessprobe-httpgetnamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:selector:hobby: k8sauther: jasonyintype: ClusterIPports:- port: 80targetPort: 80protocol: TCPclusterIP: 10.200.100.220
[root@k8s231.oldboyedu.com replicationcontrollers]# 

(2)创建并查看pod状态

[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl apply -f 04-rc-readinessProbe-tcpSocket.yaml 
[root@k8s231.oldboyedu.com replicationcontrollers]# kubectl get pods 

4、readinessProbe和livenessProbe搭配使用案例

[root@k8s231.oldboyedu.com replicationcontrollers]# cat 05-rc-readinessProbe-livenessProbe.yaml 
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-readinessprobe-tcpsocketlabels:school: oldboyeduclass: linux85apps: rcnamespace: default
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahehobby: k8sauther: jasonyinspec:containers:- livenessProbe:httpGet:port: 80path: /index.htmlfailureThreshold: 3initialDelaySeconds: 15periodSeconds: 1successThreshold: 1timeoutSeconds: 1name: linux85-execimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinereadinessProbe:httpGet:port: 80path: /oldboyedu.htmlfailureThreshold: 3initialDelaySeconds: 15periodSeconds: 3successThreshold: 1timeoutSeconds: 1---apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-readinessprobe-httpgetnamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:selector:hobby: k8sauther: jasonyintype: ClusterIPports:- port: 80targetPort: 80protocol: TCPclusterIP: 10.200.100.220[root@k8s231 replicationcontrollers]# kubectl get po
NAME                                                      READY   STATUS    RESTARTS   AGE
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-f4tgz   0/1     Running   0          3s
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pgr5m   0/1     Running   0          3s
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pkzfc   0/1     Running   0          3s
[root@k8s231 replicationcontrollers]# kubectl exec oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-f4tgz -- rm -f /usr/share/nginx/html/index.html
[root@k8s231 replicationcontrollers]# kubectl get po
NAME                                                      READY   STATUS    RESTARTS     AGE
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-f4tgz   0/1     Running   1 (9s ago)   73s
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pgr5m   0/1     Running   0            73s
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pkzfc   0/1     Running   0            73s
[root@k8s231 replicationcontrollers]# vim /tmp/oldboyedu.html
<h1>style=`color: green;`>www.oldboyedu.com</h1>
[root@k8s231 replicationcontrollers]# kubectl cp /tmp/oldboyedu.html oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pgr5m:/usr/share/nginx/html
[root@k8s231 replicationcontrollers]# kubectl get po
NAME                                                      READY   STATUS    RESTARTS        AGE
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-f4tgz   0/1     Running   1 (4m28s ago)   5m32s
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pgr5m   1/1     Running   0               5m32s
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pkzfc   0/1     Running   0               5m32s
[root@k8s231 replicationcontrollers]# kubectl cp /tmp/oldboyedu.html oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pgr5m:/usr/share/nginx/html
[root@k8s231 replicationcontrollers]# kubectl cp /tmp/oldboyedu.html oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pkzfc:/usr/share/nginx/html
[root@k8s231 replicationcontrollers]# kubectl get po
NAME                                                      READY   STATUS    RESTARTS       AGE
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-f4tgz   0/1     Running   1 (5m3s ago)   6m7s
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pgr5m   1/1     Running   0              6m7s
oldboyedu-linux85-web-rc-readinessprobe-tcpsocket-pkzfc   1/1     Running   0              6m7s

5、startupProbe启动探针和其他探针的执行优先案例

[root@k8s231.oldboyedu.com replicationcontrollers]# cat 06-rc-readinessProbe-livenessProbe-startupProbe.yaml 
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-probelabels:school: oldboyeduclass: linux85apps: rcnamespace: default
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahehobby: k8sauther: jasonyinspec:containers:- name: linux85-execimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpine# 判断服务是否健康,若检查不通过,将Pod直接重启。livenessProbe:httpGet:port: 80path: /huozhe.htmlfailureThreshold: 3initialDelaySeconds: 15periodSeconds: 1successThreshold: 1timeoutSeconds: 1# 判断服务是否就绪,若检查不通过,将Pod标记为未就绪状态。readinessProbe:httpGet:port: 80path: /oldboyedu.htmlfailureThreshold: 3initialDelaySeconds: 15periodSeconds: 3successThreshold: 1timeoutSeconds: 1# 启动时做检查,若检查不通过,直接杀死容器。# startupProbe探针通过后才回去执行readinessProbe和livenessProbe哟~startupProbe:httpGet:port: 80path: /start.htmlfailureThreshold: 3initialDelaySeconds: 35periodSeconds: 3successThreshold: 1timeoutSeconds: 1---apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-readinessprobe-httpgetnamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:selector:hobby: k8sauther: jasonyintype: ClusterIPports:- port: 80targetPort: 80protocol: TCPclusterIP: 10.200.100.220
[root@k8s231.oldboyedu.com replicationcontrollers]# 

6、初始化容器案例:

[root@k8s231.oldboyedu.com pods]# cat 17-initContainers.yaml 
kind: Pod
apiVersion: v1
metadata:labels:school: oldboyeduclass: linux85name: oldboyedu-linux85-initcontainers-001
spec:volumes:- name: dataemptyDir: {}# 定义初始化容器,初始化容器要在业务容器运行之前运行。# 在Pod创建时,初始化容器仅初始化一次,当容器重启时并不会触发初始化容器。# 如果每次重启容器时需要调用特定的脚本可以参考使用容器的"postStart"字段定义哟!initContainers:- name: init-data-001image: harbor.oldboyedu.com/web/nginx:1.20.1-alpine# command: ['/bin/bash','-c',"for i in `seq 1 5`;do echo '<h1>'$i page access time at $(date +%F_%T) '</h1>' >> /data/index.html;sleep 3;done"]command: - '/bin/sh'- '-c'- "for i in `seq 1 5`;do echo '<h1>'$i page access time at $(date +%F_%T) '</h1>' >> /data/index.html;sleep 3;done"volumeMounts:- mountPath: "/data"name: data- name: init-data-002image: harbor.oldboyedu.com/web/nginx:1.20.1-alpine# command: ['/bin/sh','-c',"/bin/chmod 644 /data/* -R"]command: - '/bin/sh'- '-c'- "/bin/chmod 604 /data/* -R"volumeMounts:- mountPath: "/data"name: datacontainers:- name: mywebimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinevolumeMounts:- mountPath: "/usr/share/nginx/html"name: data[root@k8s231.oldboyedu.com pods]# 

7、静态Pod(了解即可)

vim  /var/lib/kubelet/config.yaml 
...
staticPodPath: /etc/kubernetes/manifests

温馨提示:
(1)静态Pod是由kubelet启动时通过"staticPodPath"配置参数指定路径

(2)静态Pod创建的Pod名称会自动加上kubelet节点的主机名,比如"-k8s151.oldboyedu.com",会忽略"nodeName"字段哟;

(3)静态Pod的创建并不依赖API-Server,而是直接基于kubelet所在节点来启动Pod;

(4)静态Pod的删除只需要将其从staticPodPath指定的路径移除即可;

(5)静态Pod路径仅对Pod资源类型有效,其他类型资源将不被创建哟

(6)咱们的kubeadm部署方式就是基于静态Pod部署的哟;

8、Pod的安全上下文securityContext实战

Pod的安全上下文securityContext

kubectl explain po.spec.containers.securityContext
kubectl explain po.spec.securityContext	

参考案例:

(1)编写dockerfile

[root@k8s231.oldboyedu.com securityContext]# ll
total 8
-rwxr-xr-x 1 root root 235 Apr 18 15:27 build.sh
-rw-r--r-- 1 root root 497 Apr 18 15:25 Dockerfile
[root@k8s231.oldboyedu.com securityContext]# 
[root@k8s231.oldboyedu.com securityContext]# cat Dockerfile 
FROM centos:7LABEL school=oldboyedu \class=linux85# RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
#         -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
#         -i.bak \
#         /etc/yum.repos.d/CentOS-*.repoRUN curl  -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repoRUN yum -y install iptables-services net-tools && rm -rf /var/cache/yumRUN useradd -u 666 oldboyeduCMD ["tail","-f","/etc/hosts"]
[root@k8s231.oldboyedu.com securityContext]# 
[root@k8s231.oldboyedu.com securityContext]# cat build.sh 
#!/bin/bashdocker image build -t harbor.oldboyedu.com/tools/centos7-iptabls:v0.1 .
docker login -u admin -p 1 harbor.oldboyedu.com
docker image push harbor.oldboyedu.com/tools/centos7-iptabls:v0.1
docker logout harbor.oldboyedu.com
[root@k8s231.oldboyedu.com securityContext]# 

(2)部署pod测试

[root@k8s231.oldboyedu.com pods]# cat 18-pod-securityContext.yaml 
apiVersion: v1
kind: Pod
metadata:name: oldboyedu-linux85-securitycontext-004
spec:containers:- name: c1image: harbor.oldboyedu.com/tools/centos7-iptabls:v0.1# args:# - tail# - -f# - /etc/hosts# 配置Pod的安全相关属性securityContext:# 配置容器为特权容器,若配置了特权容器,可能对capabilities测试有影响哟!#privileged: true# 自定义LINUX内核特性# 推荐阅读:#   https://man7.org/linux/man-pages/man7/capabilities.7.html#   https://docs.docker.com/compose/compose-file/compose-file-v3/#cap_add-cap_dropcapabilities:# 添加所有的Linux内核功能add:- ALL# 移除指定Linux内核特性drop:# 代表禁用网络管理的配置,# - NET_ADMIN# 代表禁用UID和GID,表示你无法使用chown命令哟# 比如执行"useradd oldboyedu"时会创建"/home/oldboyedu"目录,并执行chown修改目录权限为"oldboyedu"用户,此时你会发现可以创建用户成功,但无法修改"/home/oldboyedu"目录的属主和属组。- CHOWN# # 代表禁用chroot命令- SYS_CHROOT# 如果容器的进程以root身份运行,则禁止容器启动!# runAsNonRoot: true# 指定运行程序的用户UID,注意,该用户的UID必须存在!# runAsUser: 666
[root@k8s231.oldboyedu.com pods]# 

9、Pod的生命周期优雅的终止案例

[root@k8s231.oldboyedu.com pods]# cat 19-pods-lifecycle-postStart-preStop.yaml 
apiVersion: v1
kind: Pod
metadata:name: oldboyedu-linux85-lifecycle-001
spec:nodeName: k8s232.oldboyedu.comvolumes:- name: datahostPath:path: /oldboyedu-linux85# 在pod优雅终止时,定义延迟发送kill信号的时间,此时间可用于pod处理完未处理的请求等状况。# 默认单位是秒,若不设置默认值为30s。terminationGracePeriodSeconds: 60containers:- name: mywebimage: harbor.oldboyedu.com/tools/centos7-iptabls:v0.1stdin: truevolumeMounts:- name: datamountPath: /data# 定义Pod的生命周期。lifecycle:# Pod启动之后做的事情postStart:exec:command: - "/bin/bash"- "-c"- "echo \"postStart at $(date +%F_%T)\" >> /data/postStart.log"# Pod停止之前做的事情preStop:exec:command: - "/bin/bash"- "-c"- "echo \"preStop at $(date +%F_%T)\" >> /data/preStop.log"
[root@k8s231.oldboyedu.com pods]# 

10、Pod创建流程图解

image-20240625170156849

image-20240625170848648

11、rc的升级和回滚

docker镜像准备:

[root@k8s231.oldboyedu.com web]# ll
total 16
-rw-r--r-- 1 root root 168 Apr 18 17:07 apps-v1
-rw-r--r-- 1 root root 168 Apr 18 17:08 apps-v2
-rw-r--r-- 1 root root 168 Apr 18 17:08 apps-v3
-rwxr-xr-x 1 root root 437 Apr 18 17:11 build.sh
drwxr-xr-x 2 root root  36 Apr 18 17:06 code
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# cat apps-v1 
FROM harbor.oldboyedu.com/web/nginx:1.20.1-alpineLABEL school=oldboyedu \class=linux85 \auther=JasonYinCOPY code/v1 /usr/share/nginx/html/index.html
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# cat apps-v2 
FROM harbor.oldboyedu.com/web/nginx:1.20.1-alpineLABEL school=oldboyedu \class=linux85 \auther=JasonYinCOPY code/v2 /usr/share/nginx/html/index.html
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# cat apps-v3 
FROM harbor.oldboyedu.com/web/nginx:1.20.1-alpineLABEL school=oldboyedu \class=linux85 \auther=JasonYinCOPY code/v3 /usr/share/nginx/html/index.html
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# cat build.sh 
#!/bin/bashdocker build -f apps-v1 -t harbor.oldboyedu.com/update/apps:v1 .
docker build -f apps-v2 -t harbor.oldboyedu.com/update/apps:v2 .
docker build -f apps-v3 -t harbor.oldboyedu.com/update/apps:v3 .docker login -u admin -p 1 harbor.oldboyedu.com
docker push harbor.oldboyedu.com/update/apps:v1
docker push harbor.oldboyedu.com/update/apps:v2
docker push harbor.oldboyedu.com/update/apps:v3
docker logout  harbor.oldboyedu.com
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# cat code/v1 
<h1 style='color: green;'>www.oldboyedu.com  v0.1</h1>
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# cat code/v2 
<h1 style='color: green;'>www.oldboyedu.com  v0.2</h1>
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# 
[root@k8s231.oldboyedu.com web]# cat code/v3 
<h1 style='color: green;'>www.oldboyedu.com  v0.3</h1>
[root@k8s231.oldboyedu.com web]# 

rc的升级和回滚:

[root@k8s231.oldboyedu.com update]# cat 01-apps-old.yaml 
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-old
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahespec:containers:- name: appsimage: harbor.oldboyedu.com/update/apps:v1#image: harbor.oldboyedu.com/update/apps:v2---apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-rc
spec:selector:classroom: jiaoshi05address: oldboyedu-shaheports:- port: 80targetPort: 80[root@k8s231.oldboyedu.com update]# for i in `seq 100`;do curl 10.200.239.166;sleep 0.5;done1.23版本不能使用“rolling-update”,如果想要升级或回滚可以按照以下方式进行:- 修改rc资源的清单并使用apply使之生效;- 删除Pod资源,rc控制器会自动拉起Pod,此时新拉起的Pod会根据上一步修改的镜像来启动- 综上所述,给用户看出来的效果就是像是更新;

12、rs的Pod控制器实战案例

[root@k8s231.oldboyedu.com replicasets]# cat 02-rs-matchExpressions-nginx.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:name: oldboyedu-linux85-rs
spec:# 指定创建Pod的副本数量,默认值为1.replicas: 5# 定义标签选择器,rs资源基于标签选择器关联对应的Pod哟~selector:# 基于表达式匹配matchExpressions:- key: apps# values:# - haha# - xixi# - hehe# - oldboyedu-web# 当operator的值为In或者NotIn时,values的值不能为空。#   - In:#      key的值必须在values定义的数组内。#   - NotIn:#      key的值必须不在values定义的数组内。# operator: In# operator: NotIn# 当operator的值为Exists或者DoesNotExist时,values的值必须为空.#    - Exists:#       只要存在key即可。#    - DoesNotExist:#       只要不存在指定的key即可。# operator: Existsoperator: DoesNotExist# 定义Pod资源创建的模板template:metadata:labels:# apps: oldboyedu-webschool: oldboyeduclass: linux85spec:containers:- name: webimage: harbor.oldboyedu.com/update/apps:v1
[root@k8s231.oldboyedu.com replicasets]# 

测试Pod

[root@k8s231.oldboyedu.com replicasets]# cat /tmp/rs.yaml 
apiVersion: v1
kind: Pod
metadata:labels:apps: xixiname: oldboyedu-linux85-rs-001
spec:containers:- image: harbor.oldboyedu.com/update/apps:v1name: web---
apiVersion: v1
kind: Pod
metadata:labels:apps: hahaname: oldboyedu-linux85-rs-002
spec:containers:- image: harbor.oldboyedu.com/update/apps:v1name: web---
apiVersion: v1
kind: Pod
metadata:labels:apps: hehename: oldboyedu-linux85-rs-003
spec:containers:- image: harbor.oldboyedu.com/update/apps:v1name: web---
apiVersion: v1
kind: Pod
metadata:labels:apps: piapianame: oldboyedu-linux85-rs-004
spec:containers:- image: harbor.oldboyedu.com/update/apps:v1name: web---
[root@k8s231.oldboyedu.com replicasets]# 

今日内容回顾:
livenessProbe: 检查失败时重启容器。
readinessProbe: 检查失败时标记为未就绪状态。
startupProbe: 检查失败将会杀死容器,再次期间不进行readinessProbe和livenessProbe的检查。
Pod的优雅终止
lifecycle:
postStart:
preStop

初始化容器initContianers
Pod创建的流程图解
replicasets: 副本控制器,相比于rc资源功能更加强大。
pod的安全上下文。
静态Pod。

今日作业:
(1)完成课堂的所有练习并整理思维导图;
(2)将昨日作业使用rs资源改写;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/48930.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[数据集][目标检测]躺坐站识别检测数据集VOC+YOLO格式9488张3类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;9488 标注数量(xml文件个数)&#xff1a;9488 标注数量(txt文件个数)&#xff1a;9488 标注…

C语言 | Leetcode C语言题解之第242题有效的字母异位词

题目&#xff1a; 题解&#xff1a; bool isAnagram(char* s, char* t) {int len_s strlen(s), len_t strlen(t);if (len_s ! len_t) {return false;}int table[26];memset(table, 0, sizeof(table));for (int i 0; i < len_s; i) {table[s[i] - a];}for (int i 0; i &…

EMQX 跨域集群:增强可扩展性,打破地域限制

跨域集群的概念 提到 EMQX&#xff0c;人们通常首先会想到它的可扩展性。尽管 EMQX 能随着硬件数量的增加几乎实现线性扩展&#xff0c;但在单个计算实例上的扩展能力终究有限&#xff1a;资源总会耗尽&#xff0c;升级成本也会急剧上升。这时&#xff0c;分布式部署就显得尤为…

JavaScript(11)——对象

对象 声明&#xff1a; let 对象名 { 属性名&#xff1a;属性值, 方法名&#xff1a;函数 } let 对象名 new Object() 对象的操作 先创建一个对象 let op {name:jvav,id:4,num:1001} 查 对象名.属性 console.log(op.name) 对象名[属性名] 改 对象名.属性 新值 op.name …

Pytorch学习笔记day4——训练mnist数据集和初步研读

该来的还是来了hhhhhhhhhh&#xff0c;基本上机器学习的初学者都躲不开这个例子。开源&#xff0c;数据质量高&#xff0c;数据尺寸整齐&#xff0c;问题简单&#xff0c;实在太适合初学者食用了。 今天把代码跑通&#xff0c;趁着周末好好的琢磨一下里面的各种细节。 代码实…

Spring MVC的高级功能——拦截器(三)拦截器的执行流程

一、单个拦截器的执行流程 如果在项目中只定义了一个拦截器&#xff0c;单个拦截器的执行流程如图所示。 二、单个拦截器的执行流程分析 从单个拦截器的执行流程图中可以看出&#xff0c;程序收到请求后&#xff0c;首先会执行拦截器中的preHandle()方法&#xff0c;如果preHa…

bug诞生记——动态库加载错乱导致程序执行异常

大纲 背景问题发生问题猜测和分析过程是不是编译了本工程中的其他代码是不是有缓存是不是编译了非本工程的文件是不是调用了其他可执行文件查看CMakefiles分析源码检查正在运行程序的动态库 解决方案 这个案例发生在我研究ROS 2的测试Demo时发生的。 整体现象是&#xff1a;修改…

聊一聊前端动画的种类,以及动画的触发方式有哪些?

引言 动画在前端开发中扮演着重要的角色。它不仅可以提升用户体验&#xff0c;还可以使界面更加生动和有趣。在这篇文章中&#xff0c;我们将深入探讨前端动画的各种实现方式&#xff0c;包括 CSS 动画、JavaScript 动画、SVG 动画等。我们还将讨论一些触发动画的方式和动画在…

【MQTT(2)】开发一个客户端,ubuntu版本

基本流程如下&#xff0c;先生成Mosquitto的库&#xff0c;然后qt调用库进行开发界面。 文章目录 0 生成库1 有界面的QT版本2 无界面版本 0 生成库 下载源码&#xff1a;https://github.com/eclipse/mosquitto.git 编译ubuntu 版本很简单&#xff0c;安装官方说明直接make&am…

rk3568 OpenHarmony4.1 Launcher定制开发—桌面壁纸替换

Launcher 作为系统人机交互的首要入口&#xff0c;提供应用图标的显示、点击启动、卸载应用&#xff0c;并提供桌面布局设置以及最近任务管理等功能。本文将介绍如何使用Deveco Studio进行单独launcher定制开发、然后编译并下载到开发板&#xff0c;以通过Launcher修改桌面背景…

记录|如何打包C#项目

参考文章&#xff1a; c#窗体应用程序怎么打包 经过检验确实有效 Step1. 生成发布文件 在Visual Studio的菜单中&#xff0c;找到“生成”->“发布” 第一次会有个向导&#xff0c;基本上一路next下来既可以 最后&#xff0c;点击完成即可以 Step2. 获得publish文件 自…

【JavaEE】AQS原理

本文将介绍AQS的简单原理。 首先有个整体认识&#xff0c;全称是 AbstractQueuedSynchronizer&#xff0c;是阻塞式锁和相关的同步器工具的框架。常用的ReentrantLock、Semaphore、CountDownLatch等都有实现它。 本文参考&#xff1a; 深入理解AbstractQueuedSynchronizer只需…

[C++]TinyWebServer

TinyWebServer 文章目录 TinyWebServer1 主体框架2 Buffer2.1 向Buffer写入数据2.2 从Buffer读取数据2.3 动态扩容2.4 从socket中读取数据2.5 具体实现 3 日志系统3.1 生产者-消费者模型3.2 数据一致3.3 代码 4 定时器4.1 调整堆中元素操作4.2 堆的操作4.2.1 增4.2.2 删4.2.3 改…

微信小程序-应用,页面和组件生命周期总结

情景1&#xff1a;小程序冷启动时候的顺序 情景2: 使用navigator&#xff08;保留并打开另一个页面&#xff09;和redirect&#xff08;关闭并打开另一个页面&#xff09;的执行顺序 情景3&#xff1a;切后台和切前台

Linux——组管理和权限管理

目录 组管理 Linux 组基本介绍 文件/目录所有者 组的创建 查看&修改文件/目录所在组 改变用户所在组 权限管理 基本介绍 rwx 文件/目录权限详解 chmod 修改文件或目录权限 chown 修改文件所有者 组管理 Linux 组基本介绍 关于第二张图中问题&#xff0c;答案…

【Qt】Qt的坐标转换(mapToGlobal)

1、QPoint QWidget::mapToGlobal(const QPoint &pos) const 将小部件坐标转换为全局坐标。mapToGlobal(QPoint(0,0))可以得到小部件左上角像素的全局坐标。2、QPoint QWidget::mapToParent(const QPoint &pos) const 将小部件坐标转换为父部件坐标。如果小部件没有父部…

Jmeter之count函数

counter函数 1、功能解释 count函数--计数器&#xff0c;每调用这个函数一次&#xff0c;它就会自动加1。它有两个参数&#xff0c;第一个参数是布尔型的&#xff0c;只能设置成 “TRUE”或者“FALSE”&#xff0c;如果是TRUE&#xff0c;那么每个用户有自己的计数器&#xf…

常用的网络爬虫工具推荐

在推荐常用的网络爬虫工具时&#xff0c;我们可以根据工具的易用性、功能强大性、用户口碑以及是否支持多种操作系统等多个维度进行考量。以下是一些常用的网络爬虫工具推荐&#xff1a; 1. 八爪鱼 简介&#xff1a;八爪鱼是一款免费且功能强大的网站爬虫&#xff0c;能够满足…

vxe-table——实现切换页码时排序状态的回显问题(ant-design+elementUi中table排序不同时回显的bug)——js技能提升

之前写的后台管理系统&#xff0c;都是用的antdelement&#xff0c;table组件中的【排序】问题是有一定的缺陷的。 想要实现的效果&#xff1a; antv——table组件一次只支持一个参数的排序 如下图&#xff1a; 就算是可以自行将排序字段拼接到列表接口的入参中&#xff0c…

环信+亚马逊云科技服务:助力出海AI社交应用扬帆起航

随着大模型技术的飞速发展&#xff0c;AI智能体的社交体验得到了显著提升&#xff0c;AI社交类应用在全球范围内持续火热。尤其是年轻一代对新技术和新体验的热情&#xff0c;使得AI社交产品在海外市场迅速崛起。作为领先的即时通讯解决方案提供商&#xff0c;环信与亚马逊云科…