我想在使用 kubectl 映像的 yaml 檔案命令列中宣告任何“kubectl”命令,即等待另一個 pod 進入就緒狀態。
如果我在命令中運行:
kubectl wait pod/mypod --for=condition=ready --timeout=120s
我得到了一個真實的資訊:
pod/mypod 條件滿足
一、如何運行命令提示符,方便使用?
即使用kubectl version,因此輸出是 kube 的版本,用于使用影像:kubectl:
kubectl run test -it --rm --image=bitnami/kubectl get pods --restart=Never --command --
/bin/kubectl version
(我想運行一次,并在結束時自動洗掉 pod。命令相同:kubectl wait pod/mypod --for=condition=ready --timeout=120s或任何命令使用 kubectl 映像)。
以上不起作用。
另外 - 我應該如何將上述內容轉換為 kubernetes yaml 檔案(一次運行 - 完成后,pod 將被自動洗掉)?
當我等待時,以下內容不起作用,即 mypod 完成。
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
selector:
matchLabels:
app: myapp
replicas: 3
template:
metadata:
labels:
app: myapp
spec:
initContainers:
- name: wait-for-pod
image: bitnami/kubectl
args:
- wait
- pod/mypod
- --for=condition=ready
- --timeout=120s
containers:
- name: myapp
image: myapp
有狀態:Init:ContainerCannotRun。
當我運行: 時kubectl describe pod <mypod>,我收到訊息:
OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "wait": executable file not found in $PATH: unknown
謝謝。
uj5u.com熱心網友回復:
你的kubectl run命令是錯誤的。--image=bitnami/kubectl get pods部分不正確。您只需要指定影像,而不是命令。
正確的作業命令將是
kubectl run test -it --rm --image=bitnami/kubectl --restart=Never -- version
當談到部署清單時,您幾乎就在那里。只需將command串列添加到清單中,它應該可以作業。
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
selector:
matchLabels:
app: myapp
replicas: 3
template:
metadata:
labels:
app: myapp
spec:
initContainers:
- name: wait-for-pod
image: bitnami/kubectl
command:
- kubectl
args:
- wait
- --for=condition=Ready
- pod/mypod
- --timeout=120s
containers:
- name: myapp
image: myapp
現在,您需要記住,system:serviceaccount:default:default附加到每個 pod 的服務帳戶沒有足夠的權限來列出集群中的 pod。除非您為默認服務帳戶提供適當的權限,否則上述所有方法均無效
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
namespace: default
name: service-reader
rules:
- apiGroups: [""]
resources: ["services"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: service-reader-pod
subjects:
- kind: ServiceAccount
name: default
namespace: default
roleRef:
kind: ClusterRole
name: service-reader
apiGroup: rbac.authorization.k8s.io
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/446657.html
