Search code examples
jenkinskubectltekton

kubectl deploy from within kubernetes container


How do you deploy from within Kubernetes container - using CI/CD?

Senario:

  1. I am building within Kubernetes using Kaniko
  2. Now how to run kubectl within Kuberneters. And I do have the right serviceAccount for it. First problem is to have a container ready for executing kubectl.

Note: - /bin/cat

I found this, but it give errors:

apiVersion: v1
kind: Pod
metadata:
  name: kubectl-deploy
spec:
  containers:
  - name: kubectl
    image: bitnami/kubectl:latest
    imagePullPolicy: Always
    command:
    - /bin/cat
    tty: true

Errors:

Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  78s                default-scheduler  Successfully assigned default/kubectl-deploy to master
  Normal   Pulled     76s                kubelet            Successfully pulled image "bitnami/kubectl:latest" in 874.059036ms
  Normal   Pulled     74s                kubelet            Successfully pulled image "bitnami/kubectl:latest" in 860.59161ms
  Normal   Pulled     60s                kubelet            Successfully pulled image "bitnami/kubectl:latest" in 859.31958ms
  Normal   Pulling    33s (x4 over 77s)  kubelet            Pulling image "bitnami/kubectl:latest"
  Normal   Created    32s (x4 over 76s)  kubelet            Created container kubectl
  Normal   Started    32s (x4 over 76s)  kubelet            Started container kubectl
  Normal   Pulled     32s                kubelet            Successfully pulled image "bitnami/kubectl:latest" in 849.398179ms
  Warning  BackOff    7s (x7 over 73s)   kubelet            Back-off restarting failed container

Solution

  • I found this, but it give errors

    When you run a Pod in Kubernetes, by default, it expect it to be a long running service. But in your case, you run a one-off command that terminates immediately. To run one-off commands in Kubernetes, it is easiest to run them as Kubernetes Jobs.

    First problem is to have a container ready for executing kubectl.

    Since you are using Tekton, have a look at the "deploy task" from Tekton Hub, it is configured with an image that includes kubectl.