Manjunath v
Manjunath v

Reputation: 41

Create one liner (Imperative way) command in kubernetes

Create one liner (Imperative way) command in kubernetes

kubectl run test --image=ubuntu:latest --limits="cpu=200m,memory=512Mi" --requests="cpu=200m,memory=512Mi" --privileged=false

And also I need to set securityContext in one liner, is it possible? basically I need to run container as securityContext/runAsUser not as root account.

Yes declarative works, but I'm looking for an imperative way.

Upvotes: 3

Views: 3041

Answers (1)

Dawid Kruk
Dawid Kruk

Reputation: 9905

Posting this answer as a community wiki to highlight the fact that the solution was posted in the comments (a link to another answer):

Hi, check this answer: stackoverflow.com/a/37621761/5747959 you can solve this with --overrides – CLNRMN 2 days ago

Feel free to edit/expand.


Citing $ kubectl run --help:

  --overrides='': An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.

Following on --overrides example that have additionals field included and to be more specific to this particular question (securityContext wise):

kubectl run -it ubuntu --rm --overrides='
{
  "apiVersion": "v1",
  "spec": {
    "securityContext": {
      "runAsNonRoot": true,
      "runAsUser": 1000,
      "runAsGroup": 1000,
      "fsGroup": 1000
    },
    "containers": [
      {
        "name": "ubuntu",
        "image": "ubuntu",
        "stdin": true,
        "stdinOnce": true,
        "tty": true,
        "securityContext": {
          "allowPrivilegeEscalation": false
        }
      }
    ]
  }
}
' --image=ubuntu --restart=Never -- bash

By above override you will use a securityContext to constrain your workload.

Side notes!

  • The example above is specific to running a Pod that you will exec into (bash)
  • The --overrides will override the other specified parameters outside of it (for example: image)

Additional resources:

Upvotes: 6

Related Questions