|NO.Z.00227|——————————|CloudNative|——|KuberNetes&细粒度权限控制.V11|——|RBAC.v03|临时容器在线Debug|

一、使用临时容器在线Debug:
### --- 制作busybox为临时容器,为demo-nginx添加此临时容器
~~~     # 查看demo-nginx是否有一些维护工具
~~~     注:常用的维护命令是没有的

[root@k8s-master01 ~]# kubectl exec -ti demo-nginx-6bdcdcc585-5wrx9 -- bash
root@demo-nginx-6bdcdcc585-5wrx9:/# ps aux 
bash: ps: command not found
root@demo-nginx-6bdcdcc585-5wrx9:/# netstat
bash: betstat: command not found
root@demo-nginx-6bdcdcc585-5wrx9:/# top
bash: top: command not found
### --- 查看busybox是否有这些维护工具
~~~     注:就可以使用busybox作为一个临时容器,注入到damo-nginx容器中,
~~~     这样就可以查看demo-nginx里面的进程或者是链接数这一类的东西。

[root@k8s-master01 ~]# kubectl exec -ti busybox -- sh
/ # ps aux
    1 root      0:00 sleep 3600
/ # netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
/ # top
Mem: 1917076K used, 98844K free, 103004K shrd, 16K buff, 760908K cached
CPU:  0.0% usr  5.5% sys  0.0% nic 94.4% idle  0.0% io  0.0% irq  0.0% sirq
二、将busybox作为临时容器注入到demo-nginx-6bdcdcc585-5wrx9这个pod中
### --- 将busybox注入到demo-nginx中

[root@k8s-master01 ~]# kubectl get po
NAME                          READY   STATUS    RESTARTS   AGE
busybox                       1/1     Running   34         2d15h
demo-nginx-6bdcdcc585-5wrx9   2/2     Running   0          2d1h
### --- 编写ec.json的yaml文件

[root@k8s-master01 ~]# vim ec.json 
{
    "apiVersion": "v1",
    "kind": "EphemeralContainers",
    "metadata": {
            "name": "demo-nginx-6bdcdcc585-5wrx9"   // 注入到容器的容器名称
    },
    "ephemeralContainers": [{
        "command": [
            "sh"                                    // 命令是sh
        ],
        "image": "busybox",                         // 镜像是busybox
        "imagePullPolicy": "IfNotPresent",
        "name": "debugger",                         // 这个容器的名称是debugger
        "stdin": true,
        "tty": true,
        "terminationMessagePolicy": "File"
    }]
}
### --- 注入到demo-nginx容器中
~~~     需要注意的是:当前这个容器的namespace是谁,再加上当前容器的名称
~~~     注:看到这个文件之后说明已经注入成功了

[root@k8s-master01 ~]# kubectl replace --raw /api/v1/namespaces/default/pods/demo-nginx-6bdcdcc585-5wrx9/ephemeralcontainers  -f ec.json
{"kind":"EphemeralContainers","apiVersion":"v1","metadata":{"name":"demo-nginx-6bdcdcc585-5wrx9","namespace":"default","uid":"30ddf35a-6d93-4f3b-b6ab-095206f96a14","resourceVersion":"1672423","creationTimestamp":"2021-05-02T04:35:51Z"},"ephemeralContainers":[{"name":"debugger","image":"busybox","command":["sh"],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","imagePullPolicy":"IfNotPresent","stdin":true,"tty":true}]}
### --- 查看这个pod
~~~     注:这个pod是不会发生任何变化的,也不会造成重启

[root@k8s-master01 ~]# kubectl get po
NAME                          READY   STATUS    RESTARTS   AGE
busybox                       1/1     Running   34         2d15h
demo-nginx-6bdcdcc585-5wrx9   2/2     Running   0          2d1h
### --- 通过describe查看这个pod
~~~     注:可以看到这个地方已经注入进来

[root@k8s-master01 ~]# kubectl describe po demo-nginx-6bdcdcc585-5wrx9 
Ephemeral Containers:
  debugger:
    Container ID:  docker://da4fd2ae548c64de9c049b2549675c9a9eea6ac1d4e76035f53c16e37163676e
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:be4684e4004560b2cd1f12148b7120b0ea69c385bcc9b12a637537a2c60f97fb
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
    State:          Running
      Started:      Tue, 04 May 2021 14:06:51 +0800
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:         <none>
~~~     # 注:可以看到busybox和demo-nginx都在master02节点,所以不需要拉取镜像 

[root@k8s-master01 ~]# kubectl get po -owide
NAME                          READY   STATUS    RESTARTS   AGE     IP               NODE           NOMINATED NODE   READINESS GATES
busybox                       1/1     Running   34         2d15h   172.25.92.90     k8s-master02   <none>           <none>
demo-nginx-6bdcdcc585-5wrx9   2/2     Running   0          2d1h    172.25.92.94     k8s-master02   <none>           <none>

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示