Run terminal commands within deployment file

7/20/2020

I wanted to run an application called rstudio package manager. It has some terminal commands to be run after the deployment. Is there any way to add those commands within the deployment file? I tried this way and I am getting crashloopback error. I am entirely new to writing the deployment file. Am I doing anything wrong or messing up with syntax?

apiVersion: apps/v1 
kind: Deployment
metadata:
  name: rspm-deployment
spec:
  selector:
    matchLabels:
      app: rspm
  replicas: 1 
  template:
    metadata:
      labels:
        app: rspm
    spec:
      nodeSelector:
        kubernetes.io/hostname: nckaoepekin.local
      containers:
      - env:
        - name: RSPM_LICENSE
          value: my_license_key
        image: rstudio/rstudio-package-manager:latest
        name: rstudio-package-manager
        command: ["/bin/sh"]
        args: ["-c", "alias rspm='/opt/rstudio-pm/bin/rspm'; rspm create repo --name=prod-cran --description='Access CRAN packages'; rspm subscribe --repo=prod-cran --source=cran; rspm sync"]
        ports:
        - containerPort: 4242
        resources: {}
        securityContext:
          privileged: true
      restartPolicy: Always
status: {}

---
kind: Service
apiVersion: v1
metadata:
  name: rstudio-pm
spec:
  selector:
    app: rspm
  ports:
  - protocol: TCP
    port: 4242
    targetPort: 4242
    nodePort: 31010
  type: NodePort

I want to add these terminal commands (Specified above also)

alias rspm='/opt/rstudio-pm/bin/rspm'
rspm create repo --name=prod-cran --description='Access CRAN packages'
rspm subscribe --repo=prod-cran --source=cran
rspm sync

When I remove command and args specified, the deployment file is working. But when I add the command and args, I am getting crashloopback error. Any leads would be appreciated. Thanks

-- Siddharth
deployment
docker
kubernetes
linux

3 Answers

7/20/2020

Try using postStart container lifecycle event as show here

Edit

  1. Create & store file in container image
cat > postStart.sh <<
alias rspm='/opt/rstudio-pm/bin/rspm'
rspm create repo --name=prod-cran --description='Access CRAN packages'
rspm subscribe --repo=prod-cran --source=cran
rspm sync
EOF
  1. Try running postStart hook like below
    lifecycle:
      postStart:
        exec:
          command:
            - /bin/bash
            - postStart.sh
-- initanmol
Source: StackOverflow

7/20/2020

You can use poststart hook here

lifecycle:
  postStart:
    exec:
      command:
        - "sh"
        - "-c"
        - |
          alias rspm='/opt/rstudio-pm/bin/rspm' &&
          rspm create repo --name=prod-cran --description='Access CRAN packages'&&
          rspm subscribe --repo=prod-cran --source=cran &&
          rspm sync
-- Tarun Khosla
Source: StackOverflow

7/20/2020

I added sleep after all my bash commands since those files were taking time to execute.

            exec:
              command:
                - "sh"
                - "-c"
                - |
                  alias rspm='/opt/rstudio-pm/bin/rspm' &&
                  sleep 5 &&
                  rspm create repo --name=prod-cran --description='Access CRAN packages' &&
                  sleep 5 &&
                  rspm subscribe --repo=prod-cran --source=cran &&
                  sleep 5 &&
                  rspm sync

And it got worked!

-- Siddharth
Source: StackOverflow