美文网首页
通过 sidecar 和 busybox 调试项目

通过 sidecar 和 busybox 调试项目

作者: 追风骚年 | 来源:发表于2021-04-18 23:14 被阅读0次
    apiVersion: apps/v1 # for versions before 1.16.0 use extensions/v1beta1
    kind: Deployment
    metadata:
      name: nginx-deployment # deployment的名称
    spec:
      selector:
        matchLabels:
          app: nginx # 选择器必须匹配 template中的metadata.labels
      replicas: 3 # tells deployment to run 3 pods matching the template
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.16.0
            ports:
            - containerPort: 80
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-svc-cip
    spec:
      selector:
        app: nginx
      type: ClusterIP
      ports:
      - port: 80
        protocol: TCP
    

    这边想调试这个项目,进到 nginx 的 pods 中里面想通过 curl 访问 Service 的 ClusterIP,但是由于 nginx 镜像中没有 curl,需要 apt install curl,但是国内的原因无法更新成功,又需要修改镜像源,修改镜像源需要用到 vim ,但是nginx 镜像中还是没有 vim,只能通过 echo 的办法。

    $ echo "">sources.list
    $ echo "deb http://mirrors.163.com/ubuntu/ artful main restricted universe multiverse">>sources.list
    $ echo "deb http://mirrors.163.com/ubuntu/ artful-security main restricted universe multiverse">>sources.list
    $ echo "deb http://mirrors.163.com/ubuntu/ artful-updates main restricted universe multiverse">>sources.list
    $ echo "deb http://mirrors.163.com/ubuntu/ artful-proposed main restricted universe multiverse">>sources.list
    $ echo "deb http://mirrors.163.com/ubuntu/ artful-backports main restricted universe multiverse">>sources.list
    $ echo "deb-src http://mirrors.163.com/ubuntu/ artful main restricted universe multiverse">>sources.list
    $ echo "deb-src http://mirrors.163.com/ubuntu/ artful-security main restricted universe multiverse">>sources.list
    $ echo "deb-src http://mirrors.163.com/ubuntu/ artful-updates main restricted universe multiverse">>sources.list
    $ echo "deb-src http://mirrors.163.com/ubuntu/ artful-proposed main restricted universe multiverse">>sources.list
    $ echo "deb-src http://mirrors.163.com/ubuntu/ artful-backports main restricted universe multiverse">>sources.list
    

    问题是我这边依然 apt update 更新失败了!

    最后我还是想到一个技巧可以试试,应该可以通过 sidecar 的办法访问,所以修改配置文件如下:

    apiVersion: apps/v1 # for versions before 1.16.0 use extensions/v1beta1
    kind: Deployment
    metadata:
      name: nginx-deployment # deployment的名称
    spec:
      selector:
        matchLabels:
          app: nginx # 选择器必须匹配 template中的metadata.labels
      replicas: 3 # tells deployment to run 3 pods matching the template
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.16.0
            ports:
            - containerPort: 80
          - name: busybox
            image: busybox:1.33
            command: ["tail","-f" ,"/dev/null"]
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-svc-cip
    spec:
      selector:
        app: nginx
      type: ClusterIP
      ports:
      - port: 80
        protocol: TCP
    

    由于 pods 下可以存放多个 container ,所以选择工具箱 busybox,并且为了让 busybox 不主动退出,通过 tail -f /dev/null 模拟 damon 模式 。

    有个注意的点 busybox 中没有 bash,只有 sh :

     kubectl exec -it nginx-deployment-76dd48b5f7-2nnv7 -c busybox -- sh
    

    -c 指定 container

    相关文章

      网友评论

          本文标题:通过 sidecar 和 busybox 调试项目

          本文链接:https://www.haomeiwen.com/subject/hsdblltx.html