k8s中運行busybox


簡介

參考百度百科

BusyBox 是一個集成了三百多個最常用Linux命令和工具的軟件。

BusyBox 包含了一些簡單的工具,例如ls、cat和echo等等,還包含了一些更大、更復雜的工具,例grep、find、mount以及telnet。

有些人將 BusyBox 稱為 Linux 工具里的瑞士軍刀。

簡單的說BusyBox就好像是個大工具箱,它集成壓縮了 Linux 的許多工具和命令,也包含了 Linux 系統的自帶的shell。

 

 

前台運行BusyBox

1.28:docker pull registry.cn-chengdu.aliyuncs.com/qzcsbj/busybox:1.28

1.34

方式一:kubectl run

不常用,因為如果要寫很多參數不方便

kubectl run -h

  # Start the nginx pod using a different command and custom arguments
  kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>

 

每3600秒RESTART 

kubectl run busybox --image=busybox:1.34 --command -- sleep 3600

如果設置的是60秒

 

方式二:從標准輸入創建

cat << EOF的意思是以EOF輸入字符為標准輸入結束,就是當你輸入cat << EOF的時候,你可以隨意輸入字符,但是當輸入EOF的時候就結束了。

cat<<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: default
spec:
  containers:
  - name: busybox
    image: busybox:1.34
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
EOF

 

或者:

# 從標准輸入創建多個 YAML 對象
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000000"
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep-less
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000"
EOF

 

方式三:yaml資源文件

command會覆蓋容器默認要執行的命令,也就是Dokcerfile里面的CMD

可能找不到終端,所以需要指定sh(也可以寫為/bin/sh)或者bash

-c,command,后面跟上要執行的命令

apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: default
  labels:
    app: busybox
spec:
  containers:
  - name: busybox
    image: busybox:1.34
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 3600"
    imagePullPolicy: IfNotPresent

 

或者

command: ["sh","-c","sleep 3600"]

 

其它參考

https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#init-containers-in-use

command: ['sh', '-c', 'echo The app is running! && sleep 3600']

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]

  

 


免責聲明!

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



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