'Automating Kubernetes deployment using script

I have 'deployment.yaml' & 'service.yaml' files to deploy application in kubernetes (minikube).

To do a manual deployment I can use kubectl apply -f <file.yaml> to create resources but I am looking for a way to automate deployment using scripts. So I can trigger a script and all the needed yaml can be triggered and pods created accordingly.

Any suggestion or guidance will be helpful.



Solution 1:[1]

It sounds to me like you need some simple bash script where you put all commands you need

#!/bin/bash

NAMESPACE='mebbns'
DEPL_DIR='~/mebb_depl'
SVC_DIR='~/svc_depl'

kubectl create namespace $NAMESPACE 
kubectl apply -n $NAMESPACE -f "$DEPL_DIR/deployment.yaml"
kubectl apply -n $NAMESPACE -f "$SVC_DIR/service.yaml"
kubectl get all -n $NAMESPACE

For more complex tasks you can check examples like setup-aks-vnet-acr.sh

kubectl Cheat Sheet should help you with basic commands you may require.

Solution 2:[2]

You can use a script to call kubectl for simple scenarious. For more complex setups have a look at Argo cd https://argoproj.github.io/

Solution 3:[3]

There are many ways to automate your task. Any scripting language would be alright; such as Bash, Ansible, etc. If you are looking for sophisticated deployment tools, you can have a look at the common Kubernetes CI/CD tools

Kubernetes automation doesn't end with the CI/CD. When you have multiple applications with each of them has specific environment variables, security credentials, specific network and security requirements etc., then you will also need tools for organizing Kubernetes configuration, such as Helm and Kustomize.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vit
Solution 2 Thomas
Solution 3 Akif