This is how i am building the image and deployments within shell script which is being called from jenkins?

9/21/2020

In K8s world and in the shell script i have below content

#!/bin/bash
docker build -t imagename:1 .

kubectl delete -f services/nodeport.service.yml
kubectl delete -f deployments/springapsimplehellopod.deployment.yml
kubectl apply -f deployments/springapsimplehellopod.deployment.yml
kubectl apply -f services/nodeport.service.yml
kubectl apply -f services/loadbalancer.service.yml

Am i following correct standards ? What is disadvantages of not using image registry rather built image will be kept in local docker and from the newly created image i am creating deployments and services

-- Nagappa L M
docker
jenkins
kubernetes

1 Answer

9/23/2020

As you are using script, you can just point folder with your YAMLs. It's mentioned in Kubernetes docs

Use kubectl apply -f <directory>. This looks for Kubernetes configuration in all .yaml, .yml, and .json files in <directory> and passes it to apply.

$ kubectl apply -f ./tst
service/nginx created
statefulset.apps/web created
service/nginx unchanged
persistentvolume/pv0003 created

Also you don't need to delete your deployments, as kubectl apply will reconfigure deployment.

$ kubectl apply -f deployment.yaml
deployment.apps/mywebtestapp-deployment created
user@cloudshell:~ (project)$ vi deployment.yaml             # editing container name from nginx to httpd
user@cloudshell:~ (project)$ kubectl apply -f deployment.yaml
deployment.apps/mywebtestapp-deployment configured

Until you will want to change immutable field.

The Service "nginx" is invalid: spec.clusterIP: Invalid value: "": field is immutable

In this situation you would need to delete this resource and create new one.

Regarding best practice using image registry it might be opinion based and depends on your needs. You can check this article or check this thread.

You could also check Helm - The package manager for Kubernetes if you would like for example divide your application into helm charts.

-- PjoterS
Source: StackOverflow