34

I've been using "kubectl run" with assorted flags to run Jobs interactively, but have recently outgrown what I can do with those flags, and have graduated to using YAML config files to describe my jobs.

However, I can't find an equivalent to the "-i" and "--tty" flags, to attach to the Job I'm creating.

Is there an equivalent YAML spec for:

kubectl run myjob \
            -i \
            --tty \
            --image=grc.io/myproj/myimg:mytag \
            --restart=Never \
            --rm \
            -- \
            my_command

Or is this maybe not the right approach?

garethw
  • 483
  • 1
  • 4
  • 6

2 Answers2

50

I think you are mentioning these fields. https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/core/types.go#L2081-L2088

You can define stdin and tty in yaml file.

apiVersion: v1 
kind: Pod 
metadata: 
  name: test 
spec: 
  containers: 
    - name: test 
      image: test 
      stdin: true 
      tty: true 
Chan Tzish
  • 1,108
  • 9
  • 7
Lantao Liu
  • 601
  • 4
  • 4
  • can you give an example yaml file showing how to do this? I find that it isn't clear from the type definitions. – Robert Bailey Jun 03 '16 at 20:42
  • 1
    IIUC, just add "stdin: true" and "tty: true" to your container spec. For example: ``` apiVersion: v1 kind: Pod metadata: name: test spec: containers: - name: test image: test stdin: true tty: true ``` Oops, I can't write things with multiple lines in comment. Hope you could understand, :P – Lantao Liu Jun 06 '16 at 07:15
  • Thanks for the pointer, Lantao. However, in my case I'm looking at doing this specifically for a Job. If I put the `tty` and `stdin` fields in the `template` spec for the Pods _within_ the Job, they appear to have no effect. I can actually sort of understand that by the usage model. – garethw Jun 07 '16 at 02:13
  • After further experimentation, `tty` and `stdin` don't have the effect I'd hoped even in plain old Pod specs, as shown in Lantao's example (at least on k8s 1.2.4 as currently deployed on GKE). – garethw Jun 07 '16 at 02:36
  • After creating the pods, you can run [`kubectl attach POD -c CONTAINER`](https://github.com/kubernetes/kubernetes/blob/1d96b4c55b6b4e6bc9bda5d12a7b99834056bdf4/docs/user-guide/kubectl/kubectl_attach.md) to attach to the container. – Yu-Ju Hong Jun 07 '16 at 23:19
  • `tty` and `stdin` are just equivalent configuration to `-i` and `--tty`. However, to attach a pod, you need to do what @Yu-JuHong mentioned. :) – Lantao Liu Jun 09 '16 at 00:56
  • It's the attach that matters here. When you do it in `kubectl run` we automatically attach – Tim Hockin Aug 31 '16 at 22:50
  • how can I run command once with kubectl-run by specifying yaml config file? – holms Sep 18 '16 at 17:35
  • 2
    To attach to the pod, I had to also provide `-i` and `-t` to the kubectl attach command as well, so the full command was `kubectl attach POD [ -c CONTAINER ] -i -t` - Note: -c CONTAINER is optional if you only have one container - it'll just default to that. – XP84 Oct 14 '18 at 06:23
1

To complement @Lantao's answer with a kubectl run one liner using the --overrides flag in json format:

kubectl run nodejs --image=node:lts-alpine --rm -i --quiet --overrides='{
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata": {
        "name": "nodejs"
    },
    "spec": {
        "volumes": [
            {
                "name": "host-volume",
                "hostPath": {
                    "path": "/home/Sources/df-sdc/web/themes/custom/"
                }
            }
        ],
        "containers": [
            {
                "name": "nodejs",
                "image": "busybox",
                "stdin": true,
                "tty": true,
                "workingDir": "/app",
                "volumeMounts": [
                    {
                        "name": "host-volume",
                        "mountPath": "/app"
                    }
                ],
                "terminationMessagePolicy": "FallbackToLogsOnError",
                "imagePullPolicy": "Always"
            }
        ],
        "restartPolicy": "Always",
        "securityContext": {
            "runAsUser": 1000,
            "runAsGroup": 1000
        }
    }
}
'
David
  • 49
  • 1
  • 4