Can we pass the argument for Kubectl create command via terminal - kubernetes

I have deployment yaml file where I am setting up one ARG.
args['--country=usa']
It runs perfectly and pod will be up with this argument. But for different country name, I need to change this yaml for argument and then run
kubectl create -f deploy.yml command again and again
Is there any way to pass this arg through create command
I tried kubectl create -f deploy.yml '--country=usa' but it seems like this is not the correct way to pass it

Unfortunately, there is no simple way to do that.
Here are some related questions.
Using env variable and configmap:
How to pass command line argument to kubectl create command
Using Helm:
Kubernetes Deployment - Pass arguments

Related

Define variable within docker-compose's config yaml file

Question
Is there a way to define a variable within a docker-compose's config yaml file?
My usecase FYI
Using "extends" (docker.com) I'm thinking to use a base yaml and configure it from many child yaml files by passing a variable.
One limitation is that I cannot make the operational commands complicated at all (intended users are not developers) so I want the command to be a single-line, and cannot be too long. So defining variable in the operation command (e.g. docker-compose -f ... up is fine, but adding VAR=foo at the beginning of it is too long and complicated for me, particularly I might want to define multiple variables).
What I've tried but didn't work
So far I thought of using docker-compose's environment variable features (note that I don't care if the variable I want to set is environment variable or something else. I just gave a try as the features are already there).
NG: Using "environment"
version: '3.3'
services:
service_a:
image: ubuntu:focal
environment:
ENV_A: "env_a"
command: >
/bin/bash -c "echo In docker container: $ENV_A"
Then have env_a, the content of ENV_A, printed. But it didn't get printed:
$ docker-compose -f a.yaml up
WARNING: The ENV_A variable is not set. Defaulting to a blank string.
:
service_a_1 | In docker container:
tmp_service_a_1 exited with code 0
NG: Using "env_file"
$ cat a.env
ENV_A=env_a
$ cat b.yaml
version: '3.3'
services:
service_b:
image: ubuntu:focal
env_file: ./a.env
command: >
/bin/bash -c "echo In docker container: $ENV_A"
Result: NG.
$ docker-compose -f b.yaml up
WARNING: The ENV_A variable is not set. Defaulting to a blank string.
:
service_b_1 | In docker container:
Operation environment
$ docker-compose --version
docker-compose version 1.29.2, build 5becea4c
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.6 LTS
:
First of all, extends is not supported on compose 3.x. This is at the top of the link you sent. So you may not want to rely on that.
As for environment variables, only variables set in the .env file are available to compose. Variables set in env_file or environment settings within the compose file will be passed into the container, but will NOT be available for use in compose itself.
You're asking compose to interpolate $ENV_A, but you haven't set it on the command line (like you described at the top, VAR=foo docker...) or in the .env, so it can't find a value.
More in this thread on GitHub.

Kubernetes custom health check

Do you think there is a way to create custom health check in kubernetes?
For example, using http get but if the content contains some string, then it's counted as failure.
You may use exec probe to create whichever logic you want. If your image contains curl, and your applications listens on 8080 port, you may insert something like
livenessProbe:
exec:
command:
- bash
- -c
- exit "$(curl localhost:8080 | grep -c 'BAD_STRING')"
grep will return 0 if no "bad" strings are found, thus check will pass. Anything non-zero will result in probe failure.
You can use whichever script you find necessary, maybe you can put a healthcheck script inside your container and call it in exec section.
Relevant doc:
kubectl explain deployment.spec.template.spec.containers.readinessProbe.exec
KIND: Deployment
VERSION: apps/v1
RESOURCE: exec <Object>
DESCRIPTION:
One and only one of the following should be specified. Exec specifies the
action to take.
ExecAction describes a "run in container" action.
FIELDS:
command <[]string>
Command is the command line to execute inside the container, the working
directory for the command is root ('/') in the container's filesystem. The
command is simply exec'd, it is not run inside a shell, so traditional
shell instructions ('|', etc) won't work. To use a shell, you need to
explicitly call out to that shell. Exit status of 0 is treated as
live/healthy and non-zero is unhealthy.

Executing commands inside helm install

I'm writing a script that executes multiple helm install commands based on a config file.
In this config file the user needs to be able to specify additional arguments e.g. --set userPw="password".
Some of these arguments require retrieval of variables from somewhere.
In powershell, I can store this in a variable, and use this variable in the helm install --set argument as follows:
$mySecret = az keyvault secret show --name MYSECRET--vault-name MYVAULT| ConvertFrom-Json).value
helm install mydeployment repo/mychart -n my-namespace --set azureSecret=$mySecret
I can't do this dynamically however...
Different deployments ofcourse need different variables.
I want to use the config file rather than variables, since I don't want the users to edit the script that runs these commands.
Is something like this possible for helm install?:
helm install mydeployment repo/mychart -n my-namespace --set azureSecret=(az keyvault secret show --name MYSECRET--vault-name MYVAULT| ConvertFrom-Json).value)
In that case users would be able to put commands like this in the config file.
My syntax could be off, I tried some variations but it doesn't seem to work.
This script is only used by our team to easily deploy some stuff, so security is not an issue.
I think it isn't allowed according to:
https://github.com/helm/helm/issues/5145
Just want to make sure.
Helm in fact can't directly launch subcommands. Helm also disables the Sprig functions that can query the shell environment. The only input to templates is the values object built from the chart's values.yaml file and -f and --set options.
In a Unix-like environment you can use $(az keyvault ...) command-substitution syntax in your helm install command. (I know there are a couple of ways to run the GNU Bash shell on Windows and one of these might work.) The command you show is involved enough that writing a short script to run it would be beneficial, and if you have it in a script, your two-line Powershell script will work as well.
In a continuous-integration environment another approach that can work well is to write out a YAML values file, and then pass that via the helm install -f option. All valid JSON should be valid YAML, so you can set up e.g. a Jenkins pipeline to compute some values, make a Groovy map object, dump it as JSON to a file, and then use that as an input values file.
this is possible using simple bash command like EX. you want to get the password value from abc.txt file from your machine location you can simple do like this
helm install . --name abc --set userPw=userPw,[^"]*' /opt/abc.txt
use your bash command in inside `` to grep/sed to get the values from some file or get the values using command.

How to pass variables to helm install --set parameters

I want to know if it is possible to pass variables to an helm install command's set parameter. Below is an example of what I'm looking to achieve.
appgw_name = "myappgateway"
export appgw_name
helm install applicationgw application-gateway-kubernetes-ingress/ingress-azure --set appgw.name=$appgw_name
I'm executing the above two lines as a shell script and when I try to execute them I get the below error:
Error: execution error at (ingress-azure/templates/configmap.yaml):
Please either provide appgw.applicationGatewayID or appgw.name.
The parameter will be resolved by your shell. If you write these directly from the command line you need to either export the env variable or execute then together in one line.
Try this:
export appgw_name="myappgateway"
helm install applicationgw application-gateway-kubernetes-ingress/ingress-azure --set appgw.name=${appgw_name}
Solved it. Was just a few spacings that had to be altered. The issue was with bash and had nothing to do with helm. So this is how I finally declared the variables export appgw_name="myappgateway" Just removed all the spaces and that's it. It worked like charm.

docker compose environment variable for command

I am having troubles in passing environment variables to my custom image via the compose command option:
My compose file:
---
version: '2'
services:
myservice:
image: mycustomimage_lms
environment:
CONF_HOME: /opt/apps-java/
APP_ENV: dev
UUID: me1
command: -Dconfig.home=${CONF_HOME} -Dcomponent.name=LMS -Denv=${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=${UUID} -jar /opt/apps-java/my.jar
ports:
- "9060"
volumes:
- ./:/opt/apps-java/
- ./:/var/logs/apps-logs/
- ./:/tmp/data
My image is just a custom jre image which has an entrypoint set to a shell script that accepts jvm arguments. My run.sh that is called from enrtypoint
#!/bin/sh
export JAVA_HOME="/usr/java/latest/"
exec $JAVA_HOME/bin/java $#
I need to pass values to command at runtime since I can then use my image for a lot of other jars and just changing parameters to my image.
This is what i get:
$> docker-compose up
WARNING: The CONF_HOME variable is not set. Defaulting to a blank string.
WARNING: The APP_ENV variable is not set. Defaulting to a blank string.
WARNING: The UUID variable is not set. Defaulting to a blank string.
I have also
gone through couple of answers such as :
Docker Compose - Command using Container Environment Variable
and
Docker-compose environment variables
but could not get it working. Any directions please?
The variables are being read by Compose when the file is parsed. But setting environment only provides values to the container, not to the file parsing.
If you're trying to pass those variables into the container, you need to escape them in the command using an extra $
-Dconfig.home=$${CONF_HOME} -Dcomponent.name=LMS -Denv=$${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=$${UUID
If you're just trying to use variables in the Compose file, you need to put those variables into an .env file.
See https://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution for the full documentation
Escaping the $ so the variable is not substituted immediately but later in the container environment is the trick, as the accepted answer does in the the docker-compose.yml.
I just wanted to add that in case you pass a command to the docker-compose call, you need to escape the character in the shell, i.e. with \
docker-compose run --rm myservice "-Dconfig.home=\${CONF_HOME} -Dcomponent.name=LMS -Denv=\${APP_ENV} -Duser.dir=/tmp/ -DLMS_UUID=\${UUID} -jar /opt/apps-java/my.jar"