How to install kubectl in kubernetes container through docker image

后端 未结 4 1779
情书的邮戳
情书的邮戳 2021-02-06 10:36

I want to run \'kubectl\' commands in the container, so i want to install kubectl in the container, through while building the Docker image. Any help is appreciated!

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 11:23

    Weike's solution works fine for me with different kubectl path, any how if some one looking for solution to install the kubectl in the Docker image then here is the Docker file (it also installs python and kubernetes python client api, if we want to access cluster through python client api):

    FROM base_image
    
    WORKDIR /tmp
    
    
    RUN  /usr/bin/curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl \
         && chmod +x ./kubectl  \
         &&  mv ./kubectl /usr/local/bin/kubectl \
         && zypper install -y python2 \
         && zypper install -y python2-pip \
         && pip install kubernetes \
         && zypper install -y git \
         && zypper clean -a \
         && git clone --recursive https://github.com/kubernetes-client/python.git \
         && cd python \
         && python setup.py install
    

    Also here is my deployment file to map kubectl binary and configuration to container to access kubectl in the kubernetes container with in the pod:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: support
      labels:
        app: support
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: support
      template:
        metadata:
          labels:
            app: support
        spec:
          terminationGracePeriodSeconds: 3
          imagePullSecrets:
          - name: mysecret
          containers:
            - name: support
              image: image-name
              command:
                - "/bin/sh"
                - "-c"
                - "sleep infinity"
              volumeMounts:
              - name: kubectl-binary
                mountPath: /usr/bin/kubectl
                readOnly: true
              - name: kubectl-config
                mountPath: /etc/kubernetes/config
                readOnly: true
          volumes:
            - name: kubectl-binary
              hostPath:
                path: /usr/bin/kubectl
            - name: kubectl-config
              hostPath:
                path: /etc/kubernetes/config
    

提交回复
热议问题