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.yamlto create a yaml so i could edit later. However it just displays
error: the path "deployment.yaml" does not existyou 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
EOFIf 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 You can run kubectl create deployment my-deployment --image=my-image
Then if you want the manifest: kubectl get deployment my-deployment --output=yaml
I see two simple ways to do that:
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
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
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 yamlWill 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: {}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.yamlNow, you can edit this yaml file to add more details. But this gives you a basic structure to start with.
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"