dude Funk1
dude Funk1

Reputation: 21

How to create a Kubernetes YAML deployment object using bash only (no editor)?

Practicing with Kubernetes.

Is it possible to create a YAML deployment object and its configuration through Bash only?

I have tried this:

kubectl create -f deployment.yaml

to create a yaml so i could edit later. However it just displays

error: the path "deployment.yaml" does not exist

Upvotes: 2

Views: 2300

Answers (8)

Melitta Singarayan
Melitta Singarayan

Reputation: 1

  1. use command vim deployment.yaml to input contents of the file
  2. kubectl create -f deployment.yaml

This works when creating via terminal

Upvotes: 0

jaxxstorm
jaxxstorm

Reputation: 13241

All the answers so far advocate actually deploying to the cluster, then retrieving the running deployment.

Using the --dry-run you can get the YAML format of the object without actually deploying anything. For example:

kubectl create deployment nginx --image=nginx --dry-run -o yaml

Will output the deployment YAML to stdout:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

Upvotes: 3

Prateek Jain
Prateek Jain

Reputation: 3045

You can run following command to generate nginx deployment, even you dont have to create deployment for this.

kubectl create deployment mynginx --image=nginx -o yaml --dry-run > /tmp/mydeployment.yaml

cat /tmp/mydeployment.yaml

Now, you can edit this yaml file to add more details. But this gives you a basic structure to start with.

Upvotes: 1

yasin lachini
yasin lachini

Reputation: 5976

you can use this template to create deployment without bash cat <

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
EOF

Upvotes: 0

switchboard.op
switchboard.op

Reputation: 2268

You can run kubectl create deployment my-deployment --image=my-image

Then if you want the manifest: kubectl get deployment my-deployment --output=yaml

Upvotes: 0

Mark
Mark

Reputation: 4455

If I understand your question correctly, it sounds like you are trying to retrieve the current deployments as YAML so you can play around with them.

Here's the command I believe you need for this:

kubectl get  deployments -o yaml 

Upvotes: 0

victortv
victortv

Reputation: 8892

I see two simple ways to do that:

  1. Using echo. Example:
echo "
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
" > deployment.yaml

  1. Creating a resource using kubectl and then outputing it to yaml:

    kubectl create deployment nginx --image=nginx --replicas=1

    kubectl get deployment nginx --export=true -o yaml

Upvotes: 2

altagir
altagir

Reputation: 640

As stated in the error, it cannot find the location of your file

You should specify the path where your file is, if you run it through a script

If ran through command line only, the file specified is not in the directory you are executing the command

#!/bin/bash
DEPLOYMENT_LOCATION="~/deployments"
kubectl create -f "$DEPLOYMENT_LOCATION/deployment.yaml"

or if relative to your script

#!/bin/bash
SCRIPT_DIR="`dirname \"$0\"`"
kubectl create -f "$SCRIPT_DIR/deployments/deployment.yaml"

Upvotes: 1

Related Questions