############################DOCKER IMAGE###################################################### docker login -u $dockeruser -p "$dockerpassword" DOCKERIMAGE_ID=$(docker images -q "$DOCKERIMAGE_NAME:$DOCKERIMAGE_TAG_VERSION") if [ -z "$DOCKERIMAGE_ID" ]; then echo "Docker image '$DOCKERIMAGE_NAME' not found. Let us create it" else echo "There is a docker image with id $DOCKERIMAGE_ID, let us remove it first" docker rmi -f $DOCKERIMAGE_ID fi docker build -t $DOCKERIMAGE_NAME:$DOCKERIMAGE_TAG_VERSION . DOCKERIMAGE_ID=$(docker images -q "$DOCKERIMAGE_NAME:$DOCKERIMAGE_TAG_VERSION") docker tag $DOCKERIMAGE_ID $DOCKERHUBURL/$DOCKERIMAGE_NAME:$DOCKERIMAGE_TAG_VERSION docker push $DOCKERHUBURL/$DOCKERIMAGE_NAME:$DOCKERIMAGE_TAG_VERSION ############################NginxIngress and Cert-Manager DEPLOYMENT#################### export KUBECONFIG=/root/.kube/config CERTPODS=$(kubectl get pods -n cert-manager -l app=cert-manager -o custom-columns=NAME:.metadata.name --no-headers) if [ -z "$CERTPODS" ]; then echo "There is no Cert-Manager deployment. Let us create it" kubectl apply -f cert-manager.yaml kubectl wait -n cert-manager deployment/cert-manager --for=condition=Available --timeout=120s else echo "There is already Cert Manager deployment so let us go to next step" fi NGINXPODS=$(kubectl get pods -n ingress-nginx -l app.kubernetes.io/component=controller -o custom-columns=NAME:.metadata.name --no-headers) if [ -z "$NGINXPODS" ]; then echo "There is no Nginx Ingress Controller deployment. Let us create it" kubectl apply -f nginx-ingress-controller.yaml kubectl wait -n ingress-nginx deployment/ingress-nginx-controller --for=condition=Available --timeout=120s kubectl wait -n ingress-nginx pod -l app.kubernetes.io/component=controller --for=condition=Ready --timeout=120s else echo "There is already Nginx Ingress Controller deployment so let us go to next step" fi ############################Python App DEPLOYMENT####################################### export KUBECONFIG=/root/.kube/config export IMAGE_NAME=$DOCKERHUBURL/$DOCKERIMAGE_NAME:$DOCKERIMAGE_TAG_VERSION CERT=$(kubectl get certificate -n $NAMESPACE $CERT_NAME -o custom-columns=NAME:.metadata.name --no-headers) if [ -z "$CERT" ]; then echo "There is no $CERT_NAME certificate. Let us create it" envsubst < cert-request.yaml | kubectl apply -f - echo "Let us wait for certrequest condition is ready" kubectl wait certificate -n $NAMESPACE $CERT_NAME --for=condition=Ready=True --timeout=120s kubectl get certificaterequest kubectl get certificate else echo "There is already certificate with name $CERT_NAME let us go to deployment step" fi PODS=$(kubectl get pods -n $NAMESPACE -l app=$DEPLOYMENT_NAME -o custom-columns=NAME:.metadata.name --no-headers) if [ -z "$PODS" ]; then echo "There is no $DEPLOYMENT_NAME deployment. Let us create it" echo "IMAGE NAME : $IMAGE_NAME" envsubst < onpremisek8s.yaml | kubectl apply -f - kubectl get pods else echo "There is already pods with name $PODS let us update deployment" NEW_REPLICA=$((DEPLOYMENTREPLICA + 1)) kubectl scale deployment/$DEPLOYMENT_NAME --replicas=$NEW_REPLICA kubectl get pods kubectl wait deployment/$DEPLOYMENT_NAME --for=condition=Available --timeout=120s echo Deleting pods: $PODS kubectl delete pod $PODS -n $NAMESPACE kubectl scale deployment/$DEPLOYMENT_NAME --replicas=$DEPLOYMENTREPLICA kubectl get pods fi ############################COnfigure NGINX For Hostname####################################### export KUBECONFIG=/root/.kube/config CONFIGFILE="/etc/nginx/nginx.conf" export NGINXSVCIP=$(kubectl -n ingress-nginx get svc ingress-nginx-controller -o jsonpath='{.spec.clusterIP}') if [ -z "$CONFIGFILE" ]; then echo "Config File not found. Let us create it" else echo "There is already a config file, let us remove it first" rm $CONFIGFILE fi envsubst < nginxconfigfile.conf > $CONFIGFILE service nginx restart echo "let us look at config file" cat $CONFIGFILE