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

4/15/2019

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
-- dude Funk1
bash
kubernetes
yaml

7 Answers

4/15/2019

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
-- yasin lachini
Source: StackOverflow

4/15/2019

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 
-- Mark
Source: StackOverflow

4/15/2019

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

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

-- switchboard.op
Source: StackOverflow

4/15/2019

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

-- victortv
Source: StackOverflow

4/15/2019

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: {}
-- jaxxstorm
Source: StackOverflow

4/15/2019

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.

-- Prateek Jain
Source: StackOverflow

4/15/2019

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"
-- altagir
Source: StackOverflow