前言
总结常规报错及相应解决方案,涉及K8S、HELM、Shell等。
1. Helm关于模板command和args
容器环境流程的部署:使用Helm或K8S原生的apply部署环境的方式。部署时,将调用指定的shell脚本。 在shell脚本执行时,获取yml变量参数有两种方式:第一,yml中将k8s参数为环境变量env注入形式docker,第二,k8s参数作为脚本的参数传入。
- 第一种方法:env形式传入,脚本直接执行${a}并且可以重复使用。
env: # 容器运行前应设置的环境变量列表 - name: a # 环境变量名称 value: b # 环境变量值
执行容器export可查看设置的环境变量参数。
- 第二种方法:k8s参数作为脚本的参数传入,即yml中指定args如果参数较多,可以使用参数shell的getopts但这些参数不能在容器中使用。 关于command和args使用场景:
- 如果command和args如果没有指定,则使用Dockerfile的配置。
- 如果command没有指定,但是指定了args,那么Dockerfile中配置的ENTRYPOINT执行并将执行命令行动args添加填写的参数ENTRYPOINT中。
- 如果command指定了,但args没写,所以Dockerfile默认配置会被忽略,输入会被执行command(当然没有任何参数。command中可自带参数)。
- 如果command和args都指定了,所以Dockerfile忽略和执行配置command并追加上args参数。
截取deployment.yaml例如:(args2中函数可用于解析)
env: - name: a value: b args: - -a key=value #添加key=value默认配置文件 - -a key1=value1 - -c temp/config1.properties #指定新的配置文件,如果不存在,则创建 - -a key=value #添加key=value至config1.properties - -a key1=value1
2.shell中使用getopts脚本参数分析
#!/bin/bash declare SCRIPT=$0 declare SCRIPT_PATH=$(dirname $SCRIPT) declare VERSION=1.0 declare DEFAULT_CONFIG_FILE="/home/config.properties" declare CONFIG_FILE=$DEFAULT_CONFIG_FILE function help_info(){
echo "The parameter info of $0 -h help -v version of $0 -a add config properties to specified config file -c create another new config file and specify -r get cofiguration according to key " } function add_config(){
local parameter=$1
local config_file=$CONFIG_FILE
}
function create_config_file(){
local config_file=$1
if [ -f "$config_file" ];then
echo "" > $SCRIPT_PATH/$config_file
if [ $? -nq 0 ];then
echo "create failed"
exit 1
fi
fi
CONFIG_FILE=$SCRIPT_PATH/$config_file
}
function get_config(){
}
# 参数说明,:开头,hv表示可以不传参数,a: c: r:表示必须传参
while getopts :hva:c:r: opt;
do
case $opt
h|help ) help_info
exit 0
;;
v|version ) echo "$0:the version is ${VERSION}"
exit 0
;;
a ) add_config $(trim $OPTARG)
;;
c ) create_config_file $(trim $OPTARG)
;;
r ) get_config $(trim $OPTARG)
;;
* ) echo "wrong parameters"
;;
esac
done
3.imgaeSecret
记录一次docker镜像拉取失败的错误:K8S报错,Pod状态为ImagePullErr,describe Pod后的错误信息“you may not docker login”(大致是这个)。重复执行docker login后,重弹Pod依然报该错误。 解决: 修改yml文件,如下添加了imageSecret后解决。
spec:
imagePullSecrets:
- name: default-secret
containers:
- name: test
image: ***
imagePullPolicy: IfNotPresent
参考资料:https://blog.csdn.net/weixin_39865204/article/details/111857811