k8s 的容器command用法舉例


問題描述以及解決:

問題一 CrashLoopBackOff

如容器運行報如下錯誤:

 

 

且在describe和kubelet日志中沒有明確記錄原因,基本都是因為command命令不合法導致

如需要運行多條命令,使用;不要使用&&

如下示例:

          command: ["/bin/sh"]

          args: ["-c","/usr/local/bin/redis_start;while true;do echo hello;sleep 1;done"]

 

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

k8s的command對應如上docker命令的[COMMAND] [ARG...]

1. 但在k8里這樣報錯,top必須得有個參數,錯誤寫法:

apiVersion: v1    
kind: Pod    
metadata:    
  name: centos  
  labels:    
    app: centos    
spec:    
  containers:    
  - name: mycentos  
    image: centos  
    imagePullPolicy: IfNotPresent  
    command: ["top",]  

報錯是:env找不到...

給top加上參數:

apiVersion: v1    
kind: Pod    
metadata:    
  name: centos  
  labels:    
    app: centos    
spec:    
  containers:    
  - name: mycentos  
    image: centos  
    imagePullPolicy: IfNotPresent  
    command: ["top","-b"]  

也可以這樣寫:

apiVersion: v1    
kind: Pod    
metadata:    
  name: centos  
  labels:    
    app: centos    
spec:    
  containers:    
  - name: mycentos  
    image: centos  
    imagePullPolicy: IfNotPresent  
    command: ["top"]  
    args: ["-b"]  

 

 

使用shell命令的寫法:

apiVersion: v1    
kind: Pod    
metadata:    
  name: centos  
  labels:    
    app: centos    
spec:    
  containers:    
  - name: mycentos  
    image: centos  
    imagePullPolicy: IfNotPresent  
    command: ["/bin/sh"]  
    args: ["-c","while true;do echo hello;sleep 1;done"]  

或者這樣:

apiVersion: v1      
kind: Pod      
metadata:      
  name: centos    
  labels:      
    app: centos      
spec:      
  containers:      
  - name: mycentos    
    image: centos    
    imagePullPolicy: IfNotPresent    
    command: ["/bin/sh","-c","while true;do echo hello;sleep 1;done"]  

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM