风起于青萍之末 2019-08-09
前几天在使用k8s中的CronJob时发现了一个很奇怪的问题, 按照官方文档的demo跑起来是没有任何问题的,但是当我想要设置每天一个固定时间点例如12点20执行一个job的时候,到了时间之后无论如何都不会执行。
一开始怀疑是cron表达式写的有问题,但是经过多次试验倒是没问题,然后就想到了时区的问题。
首先我先尝试使用PodPreset来修改所有容器的时区(在pod 创建时,用户可以使用 podpreset 对象将特定信息注入 pod 中,这些信息可以包括 secret、 卷、 卷挂载和环境变量)
默认k8s是没有开启PodPreset的,开启PodPreset需要修改master节点上的/etc/kubernetes/manifests/kube-apiserver.yaml文件
<figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">首先在<span class="keyword">CMD</span><span class="bash">末尾添加一个--runtime-config=settings.k8s.io/v1alpha1=<span class="literal">true</span></span></span><br><span class="line"><span class="bash">在--<span class="built_in">enable</span>-admission-plugins的后面增加一个PodPreset</span></span><br><span class="line"><span class="bash">重启k8s systemctl restart kubelet</span></span><br></pre></td></tr></table></figure>创建一个yml,为所有的pod挂载一个时区的环境变量
<figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">apiVersion</span>: settings.k8s.io/v1alpha1</span><br><span class="line"><span class="attribute">kind</span>: PodPreset</span><br><span class="line"><span class="attribute">metadata</span>:</span><br><span class="line"> <span class="attribute">name</span>: tz-env</span><br><span class="line"> <span class="attribute">namespace</span>: unicode-pay</span><br><span class="line"><span class="attribute">spec</span>:</span><br><span class="line"> <span class="attribute">selector</span>:</span><br><span class="line"> <span class="attribute">matchLabels</span>:</span><br><span class="line"> <span class="attribute">env</span>:</span><br><span class="line"> - <span class="attribute">name</span>: TZ</span><br><span class="line"> <span class="attribute">value</span>: Asia/Shanghai</span><br></pre></td></tr></table></figure>注意:
此时再次尝试CronJob之后发现还是不执行,然后进入该命名空间下的其他pod之后查看时间发现也没问题。
此时想到了虽然把pod的时区更改了,但是k8s调度器可不是按照pod的时间进行调度的,原来此时需要修改的是k8s的时区
再次来到/etc/kubernetes/manifests目录,使用如下方式修改此目录下4个k8s的组件的时间
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: v1</span><br><span class="line">kind: Pod</span><br><span class="line">metadata:</span><br><span class="line"> name: time</span><br><span class="line">spec:</span><br><span class="line"> containers:</span><br><span class="line"> volumeMounts:</span><br><span class="line"> - name: config</span><br><span class="line"> mountPath: /etc/localtime</span><br><span class="line"> readOnly: <span class="literal">true</span></span><br><span class="line"> volumes:</span><br><span class="line"> - name: config</span><br><span class="line"> hostPath:</span><br><span class="line"> path: /etc/localtime</span><br></pre></td></tr></table></figure>重启k8s,搞定
systemctl restart kubelet
###host字段指定授权使用该证书的etcd节点IP或子网列表,需要将etcd集群的3个节点都添加其中。cp etcd-v3.3.13-linux-amd64/etcd* /opt/k8s/bin/