我以前有下面的配置,我们以前只有一个url。通过这种配置,我们能够在没有任何问题的情况下启动吊舱。
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: {{ .Chart.Name }}
annotations:
reloader.stakater.com/auto: "true"
spec:
selector:
matchLabels:
some-service: {{ .Chart.Name }}
template: #required fields
spec:
hostNetwork: yes
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: HOST_URL
valueFrom:
configMapKeyRef:
name: some-config
key: some-url现在我们有了多个url,因此我们将yaml更改为使用initContainer获取有效的url,然后将其设置为env,如下所示
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: {{ .Chart.Name }}
annotations:
reloader.stakater.com/auto: "true"
spec:
selector:
matchLabels:
some-service: {{ .Chart.Name }}
template: #required fields
spec:
hostNetwork: yes
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command:
- sh
- -c
- env HOST_URL=$(cat /tmp/ip.env)
volumeMounts:
- name: workdir
mountPath: /tmp
initContainers:
- name: wait-for-url
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
volumeMounts:
- mountPath: "/tmp"
name: workdir
env:
- name: multipleUrls
valueFrom:
configMapKeyRef:
name: some-config
key: multipleUrls
command:
- '/bin/bash'
- '-c'
- |
echo "Actual Data: $multipleUrls"
:
:
Logic to find the valid url and store it in the /tmp/ip.env file
:
:
echo "Accessible Url is:$hosturl"
echo $hosturl> /tmp/ip.env
if [ -s /tmp/ip.env ]; then
echo "HOSTURL is: " `cat /tmp/ip.env`
exit 0;
else
sleep 10
exit 1;
fi
volumes:
- name: workdir
emptyDir: {}我们看到initContainer是成功执行的,没有任何错误。但我们看到Pod还没开始。
库贝克尔豆荚
Name: some-name-nckfs
Namespace: default
Priority: 0
Node: 10.22.44.169/10.22.44.169
Start Time: Sat, 29 Oct 2022 15:11:11 +0000
Labels: controller-revision-hash=55ff545b89
pod-template-generation=32
Annotations: <none>
Status: Running
IP: 10.22.44.169
IPs:
IP: 10.22.44.169
Controlled By: DaemonSet/some-something
Init Containers:
wait-for-hosturl:
Container ID: docker://62e3b20b6eeeca4f37e78c02fa8d68b482bde1644fc30a0ed94f788c35d3cd86
Image ID: docker://sha256:805f01431e534713510826a63dab75873d45aa96c6a9614f2c9bfbeab800a774
Port: <none>
Host Port: <none>
Command:
/bin/bash
-c
::
::
State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 29 Oct 2022 15:11:12 +0000
Finished: Sat, 29 Oct 2022 15:11:12 +0000
Ready: True
Restart Count: 0
Environment:
multipleUrls: <set to the key 'multipleUrls' of config map 'some-config'> Optional: false
Mounts:
/tmp from workdir (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-vks68 (ro)
Containers:
some-something:
Container ID: docker://1fd6d60a70a6e2c10339809e4bb3084738aee7500dc0087973801c1c3a5a497a
Image ID: docker://sha256:805f01431e534713510826a63dab75873d45aa96c6a9614f2c9bfbeab800a774
Port: 9091/TCP
Host Port: 9091/TCP
Command:
sh
-c
env HOST_URL=$(cat /tmp/ip.env)
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 29 Oct 2022 15:57:42 +0000
Finished: Sat, 29 Oct 2022 15:57:42 +0000
Ready: False
Restart Count: 14
Environment:
Mounts:
/tmp from workdir (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-vks68 (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
workdir:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
Medium:
SizeLimit: <unset>
default-token-vks68:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-vks68
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/disk-pressure:NoSchedule op=Exists
node.kubernetes.io/memory-pressure:NoSchedule op=Exists
node.kubernetes.io/network-unavailable:NoSchedule op=Exists
node.kubernetes.io/not-ready:NoExecute op=Exists
node.kubernetes.io/pid-pressure:NoSchedule op=Exists
node.kubernetes.io/unreachable:NoExecute op=Exists
node.kubernetes.io/unschedulable:NoSchedule op=Exists
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Created 49m kubelet Created container wait-for-hosturl
Normal Started 49m kubelet Started container wait-for-hosturl
Normal Created 48m (x5 over 49m) kubelet Created container some-something
Normal Started 48m (x5 over 49m) kubelet Started container some-something
Warning BackOff 4m57s (x212 over 49m) kubelet Back-off restarting failed containerkubectl podname日志:
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.43.0.1:443
HOSTNAME=something
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.43.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.43.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.43.0.1
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
PWD=/
HOST_URL=10.22.44.168发布于 2022-10-29 17:01:05
根据您向我们展示的描述,看起来您的主容器运行了一个命令然后退出:
Containers:
some-something:
Command:
sh
-c
env HOST_URL=$(cat /tmp/ip.env)
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 29 Oct 2022 15:57:42 +0000这告诉我们容器确实使用了代码0退出。
而这个命令看上去是错的。它不会改变你的环境。它肯定会马上退出。
命令应该启动一个长时间运行的进程,如果您想摆脱这些CrashLoopBackOff的话。
如果您打算运行期望退出的一次性命令,则应该使用作业而不是部署。
应该看看kubectl logs -p,假设您在某种程度上没有共享确切的配置,并且需要启动一些命令,而不是env HOST_URL。
我应该仔细看看initContainer,才能意识到有一种简单的方法可以完成你想要做的事情。您正在使用hostNetwork,所以您的Pod IP实际上是您的节点IP。
如果您想获得Pod IP作为环境变量,您可以这样做:
[daemonset / deployment / ...]
spec:
template:
containers:
- env:
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.podIP从Pod中删除命令/ args覆盖。然后将使用容器中的默认入口点。那么,也许你就不会有那些crashLoopBackOff了。或者如果是这样的话:检查豆荚日志(kubectl logs -n <ns> -p <pod>)。
https://stackoverflow.com/questions/74246803
复制相似问题