截取pod ip地址最后一列

资源清单:

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: init-demo
  namespace: test
  labels:
    app: init
spec:
  replicas: 2
  selector:
    matchLabels:
      app: init
  template:
    metadata:
      labels:
        app: init
    spec:
      initContainers:
        - name: init
          image: busybox
          command: ["/bin/sh","-c", "hostname -i | cut -d '.' -f4 > /tmp/machine_id.txt"]
          volumeMounts:
            - name: initdir
              mountPath: "/tmp"
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
          volumeMounts:
            - name: initdir
              mountPath: /project
      volumes:
        - name: initdir
          emptyDir: {}

验证:

# ctl get po -n test -o wide
NAME                         READY   STATUS    RESTARTS   AGE     IP           NODE                       NOMINATED NODE   READINESS GATES
init-demo-785647c8-6xlgv     1/1     Running   0          5m54s   10.0.3.126   cn-shenzhen.172.16.0.168   <none>           <none>
init-demo-785647c8-7q995     1/1     Running   0          5m54s   10.0.0.21    cn-shenzhen.172.16.0.89    <none>           <none>


# ctl exec -it init-demo-785647c8-6xlgv -n test -- /bin/sh
# cd /project
# ls
machine_id.txt
# cat machine_id.txt
126

# ctl exec -it init-demo-785647c8-7q995 -n test -- /bin/sh
# cd /project
# ls
machine_id.txt
# cat machine_id.txt
21

👌!

posted @ 2021-01-25 17:38  紫色飞猪  阅读(141)  评论(0编辑  收藏  举报