云原生 kubenetes pod管理

128道kubenetes高频面试题汇总(带答案) 18、简述Kubernetes中Pod可能位于的状态?52、简述Kubernetes中flannel的作用?54、简述Kubernetes共享存储的作用?20、简述Kubernetes中Pod的重启策略?21、简述Kubernetes中Pod的健康检查方式?8、简述Kubernetes的优势、适应场景及其特点?34、简述Kubernetes镜像的下载策略?35、简述Kubernetes的负载均衡器?5、简述Kubernetes中什么是Minikube、Kubectl、Kubele t? 阅读详情

一.命令式对象管理

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

Android HDMI-CEC协议解析:从机顶盒到电视的无缝遥控联动 本文深入解析Android HDMI-CEC协议,实现机顶盒与电视间的无缝遥控联动。通过剖析设备角色、消息格式及核心操作码,并结合Android HdmiControlManager API实战,详细讲解如何实现电源同步、按键穿透及音量控制,最终解决多设备遥控繁琐的痛点,提升用户体验。 阅读详情

相关推荐

Avalonia跨平台开发入门:从安装到运行第一个应用(Windows/Linux/macOS)

本文详细介绍了Avalonia跨平台开发的基础入门指南,从安装到运行第一个应用的全过程。涵盖Windows、Linux和macOS三大平台的环境配置,特别推荐使用Visual Studio进行开发,并提供常见问题的解决方案,帮助开发者快速上手Avalonia框架。

weixin_28688385的博客 542

kubenetes: pod has unbound PersistentVolumeClaims

背景 使用helm chart 部署rabbitmq的时候 pod报错 pod has unbound PersistentVolumeClaims 原因分析 查看了 rabbitmq的 charts文件,发现value.yaml 中的storageClassName 取数为空,当storageClassName为空的时候如果没有指定 DefaultStorageClass ,那么是不会分配 pv...

feiger的专栏 2万+

Vivado实战:如何用BUFGMUX_CTRL实现FPGA双时钟热切换(附时序约束模板)

本文详细介绍了如何使用Xilinx FPGA中的BUFGMUX_CTRL原语实现双时钟热切换的工业级解决方案。通过分析BUFGMUX_CTRL的核心机制、PCB布局约束和电源噪声抑制方法,提供Vivado实现全流程和时序约束模板,帮助工程师在工业自动化、车载电子等场景中提升系统可靠性。

weixin_29186249的博客 572

kubenetes从入门到上天系列第二十四篇:Kubernetes Pod的自动扩缩容

弹性伸缩是根据用户的业务需求和策略,自动 “调整” 其 “弹性资源” 的管理服务。通过弹性伸缩功能,用户可设置定时、周期或监控策略,恰到好处地增加或减少 “弹性资源”,并完成实例配置,保证业务平稳健康运行。在实际工作中,我们常常需要做一些扩容缩容操作,如:电商平台在 618 和双十一搞秒杀活动。由于资源紧张、工作负载降低等都需要对服务实例数进行扩缩容操作。在 k8s 中扩缩容分为两种: 阿里的ack可以实现Node方面的扩容,但是得买人家的云服务,加购一个扩缩容服务。底层用的是OpenStac

七叔的博客 900

【入门篇】第五章 第一次与 AI Agent 交互

本文介绍了OpenClaw中最基本的用户与AI Agent交互方式,剖析了Agent和Gateway两个核心组件的作用,通过简单示例展示了从用户请求到结果返回的完整执行流程,说明了最小交互过程对验证系统可用性的重要意义,为后续学习复杂任务执行机制奠定基础。

lanyv的博客 512

我每天如何使用 ChatGPT

我偶尔会用这个功能作为开始。我本人非常喜欢阅读,让 ChatGPT 来概括所有文章,感觉就像身处于但丁描述的地狱的某个层次那样痛苦。然而,有时一些文章实在是太啰嗦、无聊,或者写得让人不愉快(比如一个无趣记者对一个让人讨厌的人进行的漫无焦点的采访)。我需要获取这些文章中的一些信息,以便保持信息的更新和了解。在晚期资本主义的广告经济下,很多文章只在一个段落里提供新信息,其余都是无聊的填充内容,只是为了展示糟糕的广告,这实在是对人们的时间和生命的一种浪费。我有几次利用 ChatGPT 来。

KevinDai007的博客 3783

kubenetes从入门到上天系列第五篇:Kubernetes的Pod入门到实战

Pod是Kubernetes中的最小调度单元,k8s是通过定义一个Pod的资源,然后在Pod里面运行容器,容器需要指定一个镜像,这样就可以用来运行具体的服务。一个Pod封装一个容器(也可以封装多个容器),Pod里的容器共享存储、网络等。也就是说,应该把整个pod看作虚拟机,然后每个容器相当于运行在虚拟机的进程。Pod是需要调度到k8s集群的工作节点来运行的,具体调度到哪个节点,是根据 scheduler 调度器实现的。

七叔的博客 830

直流电动机的建模仿真(1)他励,串励和并励电动机建模仿真

本文根据不同励磁方式的直流电动机的接线原理图,建立了相应的数学模型,然后根据与数学模型对应的等效电路以及模拟机械运动方程的等效电路,用Matlab/simulink中的SpecializedPower system库中的电路元件,建立了他励,并励和串励直流电动机的全等效电路仿真模型,经过与Matlab自带的直流电动机仿真模型比较以及仿真实例验证,所建立的仿真模型时完全正确的。

weixin_45670943的博客 7516

云原生——Kubenetes基础

Kubernetes,简称 k8s(k,8 个字符,s——明白了?)或者 “kube”,是一个开源的Linux 容器自动化运维平台,它消除了容器化应用程序在部署、伸缩时涉及到的许多手动操作。换句话说,你可以将多台主机组合成集群来运行 Linux 容器,而 Kubernetes 可以帮助你简单高效地管理那些集群。构成这些集群的主机还可以跨越公有云私有云以及混合云。Kubernetes 最开始是由 Google 的工程师设计开发的。Google 作为Linux 容器技术的早期贡献者之一,曾公开演讲介绍。

qdz060525的博客 1360

使用libx265对YUV进行h265编码

使用libx265对YUV进行h265编码

Embedded engineer 3078

R以及Rstudio超详细安装教程(包括安装过程中出现的问题)

R安装及Rstudio安装,以及安装中出现的问题

2301_76908257的博客 1万+

【云原生】Kubenetes快速入门

上图意思就是一个master(主)和6个node(工作)节点的k8s集群Master 负责管理集群负责协调和管理集群中的所有的活动,比如调用应用程序啊、维护应用的状态、扩展和更新应用程序等。Worker节点(即图中的Node)是VM或服务器,它们充当k8s集群中的工作计算机。每个Worker节点都有一个Kubelet,它管理该Worker节点并负责与Master节点通信。每个Worker节点还具有处理容器操作的工具,例如Docker。简单了解下这些概念即可,让我们继续!Pod 容器组。

全网粉丝10W+、全栈领域优质创作者、掘金、阿里云等社区博客专家、专注于全栈领域和毕业项目实战 1280

基于SpringBoot 3 + Vue 3的全栈民宿预约管理系统设计与实现

文章将详细阐述系统的需求分析、架构设计、数据库设计、核心功能模块的实现(包括用户认证、房源管理、订单流程、支付集成等),并提供关键代码片段,最后对系统进行总结与展望。.requestMatchers("/api/admin/**").hasRole("ADMIN") // 管理员接口。.requestMatchers("/api/auth/**").permitAll() // 认证接口公开。.requestMatchers("/api/public/**").permitAll() // 公开资源。

2201_76125393的博客 272

云原生 kubenetes 控制器管理

【代码】云原生 kubenetes 控制器管理。

UseLessQQ的博客 50

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Kubenetes,我知道你!

一个node 节点就是一个物理机或者虚拟机,可以运行多个pod, pod是最小的调度单元,一个pod是一个或多个应用容器的组合(建议一个pod只存放一个容器,可以更好的实现应用程序的结偶和扩展。外部服务访问:开发测试时:通过node:port访问,生产正式访问时: ingress通过域名实现外部访问内容,可以配置路由规则,ingress也可以配置负载均衡和SSL。容器化技术是一种轻量级虚拟化技术,把应用和依赖打包到一个独立、可移植的容器中运行,实现一次构建、到处运行,Docker就是容器化技术的一种实现。

wangsong412的专栏 154

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

kubenetes

获取资源 : kube get svc,pod......

daizikui的博客 97

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

kubenetes kubectl命令记录

1.显示所有pod kubectl get pod2.显示所有rc kubectl get rc3.显示所有service kubectl get service4.删除rc kubectl delete rc rcname5.删除service kubectl delete service servicename6.删除pod kubectl delete pod podname7.查

凤歌 1万+

【Kubenetes】边缘计算KubeEdge架构设计详解

KubeEdge的理论熏陶

Jzin的博客 2749

【云原生】Kubernetes网络知识

随着Docker容器化的兴起,云计算面对的挑战越来越大。例如:网络管理、存储等。一个数据中心基本上都有成千个容器,这么多的容器需要运维人员集中管理。而在云计算的世界中,计算是最基础的,存储是最重要的,网络则是最复杂的Kubernetes网络的实现不是集群内部自己实现,而是依赖于第三方网络插件。本章主要介绍Kubernetes网络相关知识与Kubernetes网络组件中的一个重要成员——Calico。

weixin_73059729的博客 1677

Kubernetes 集群架构和Pod创建流程

计算容器变更主要包括:Pod的sandbox是否变更、短声明周期容器、初始化容器是否完成、业务容器是否已经完成,相应的我们会得到一个几个对应的容器列表:需要被kill掉的容器列表、需要启动的容器列表,注意如果我们的初始化容器未完成,则不会进行将要运行的业务容器加入到需要启动的容器列表,可以看到这个地方是两个阶段。如果容器配置了PostStart钩子,则会在此处进行对应钩子的执行,如果钩子的类型是Exec类则会调用CNI的EXec接口完成在容器内的执行。Pod资源现在还处于Pending状态。

shafsyjwqt的博客 1109

Kubenetes 扩展教程(三)

Kubernetes 的上层使得用更复杂的 Kubernetes 资源开发可伸缩的、可靠的、云原生的应用成为可能。使用边车容器不是强制性的;此外,它创建了一个封闭的环境,因为添加新的存储供应商需要成为 Kubernetes 源代码的一部分。但是,您需要将数据库、ERP 系统和以数据为中心的应用部署到 Kubernetes,因此并不是集群中运行的所有应用都是短暂的。通过遵循本节中的步骤,您已经将 CSI 插件部署到 Kubernetes 集群中,并检查了它是如何集成到 Kubernetes 生态系统中的。

龙哥盟 590

云原生 黑马Kubernetes教程(K8S教程)笔记——第一章 kubernetes介绍——Master集群控制节点、Node工作负载节点、Pod控制单元

kubernetes,是一个全新的基于容器技术的分布式架构领先方案,是谷歌严格保密十几年的秘密武器----Borg系统的一个开源版本,于2014年9月发布第一个版本,2015年7月发布第一个正式版本。

Dontla的博客 2623

kubenetes从入门到上天系列第九篇:Kubernetes的探针

在 Kubernetes 中 Pod 是最小的计算单元,而一个 Pod 又由多个容器组成,相当于每个容器就是一个应用,应用在运行期间,可能因为某些意外情况致使程序挂掉。那么如何监控这些容器状态稳定性,保证服务在运行期间不会发生问题,发生问题后进行重启等机制,就成为了重中之重的事情,考虑到这点 kubernetes 推出了。

七叔的博客 471

Kubenetes Service的类型(ClusterIP/NodePort/LoadBalancer/ExternalName)以及使用场景

在 Kubernetes 中,Service 是用于将网络流量路由到 Pod 的一种抽象。ClusterIP/NodePort/LoadBalancer/ExternalName这些不同类型的 Service 提供了灵活的选项以满足不同的网络需求,具体的选择依赖于服务的访问模式及场景。选择 LoadBalancer 而不是 NodePort 的主要原因是你的服务需要更直接、稳定和用户友好的外部访问,尤其是在生产环境中。如果你需要本地开发、调试则NodePort更适合。

★【World Of Moshow 郑锴】★ 2140

【云原生】一文细数kubernetes常见20道问题

1、K8S是什么?2、容器和主机部署应用的区别是什么?3、K8S架构的组成是什么?4、kubenetes针对pod资源对象的健康监测机制5、如何控制滚动更新过程?6、镜像下载策略是什么?7、image的状态有哪些?8、pod的重启策略是什么?9、K8S中部署应用版本回滚的命令10、标签和标签选择器的作用是什么?11、常用的标签分类有哪些?12、查看标签的方式?13、添加、...

http://www.54tianzhisheng.cn/ 379

云原生应用9:Kubernetes搭建dashboard,UI界面操作k8s资源

上篇都是使用 kubectl 命令行工具操作k8s集群资源,为了提供更丰富的用户体验,Kubernetes 还开发了一个基于 Web Dashboard,用户可以用 Kubernetes Dashboard 部署容器化的应用、监控应用的状态、执行故障排查任务以及管理 Kubernetes 各种资源。 k8s有这些资源: Pod,ReplicaSet,Deployment,StatefulSet,...

zhuyu19911016520 843
上一篇: 云计算k8s集群部署
下一篇: 云原生 kubenetes 控制器管理
UseLessQQ
博客等级 码龄3年 73粉丝 42原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值