title: How to add an username when create a k8s pod
body: <p>I have a yaml file for creating k8s pod with just one container. Is it possible to pre-add an username and its password from yaml file during k8s pod creation?</p>
  
  <p>I searched many sites and found the env variable. However, I could not make the pod as my wish. The pod's status is always showing Crashoff after pod creation.</p>
  
  <p>Is it possible to pre-add an username and its password from yaml file during k8s pod creation?</p>
  
  <p>Following are my yaml file:</p>
  
  <pre><code>---
  apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    name: centos610-sp-v1
  spec:
    replicas: 1
    template:
      metadata:
        labels:
          app: centos610-sp-v1
      spec:
        containers:
        - name: centos610-pod-v1
          image: centos-done:6.10
          env:
          - name: SSH_USER
            value: "user1"
          - name: SSH_SUDO
            value: "ALL=(ALL) NOPASSWD:ALL"
          - name: PASSWORD
            value: "password"
          command: ["/usr/sbin/useradd"]
          args: ["$(SSH_USER)"]
          ports:
          - containerPort: 22
          resources:
            limits:
              cpu: "500m"
              memory: "1G"
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: centos610-sp-v1
    labels:
      app: centos610-sp-v1
  spec:
    selector:
      app: centos610-sp-v1
    ports:
    - port: 22
      protocol: TCP
      nodePort: 31022
    type: NodePort
  ---
  </code></pre>
  
  <p>Should I use specific command as </p>
  
  <pre><code>env:
  - name: MESSAGE
    value: "hello world"
  command: ["/bin/echo"]
  args: ["$(MESSAGE)"]
  </code></pre>
  
  <p>or </p>
  
  <pre><code>command: ["/bin/sh"]
  args: ["-c", "while true; do echo hello; sleep 10;done"]
  </code></pre>
  
  <h2><strong>pod's status after get</strong></h2>
  
  <pre><code>root@zero:~/k8s-temp# kubectl get pod
  NAME                               READY     STATUS             RESTARTS   AGE
  centos610-sp-v1-6689c494b8-nb9kv   0/1       CrashLoopBackOff   5          3m
  </code></pre>
  
  <h2><strong>pod's status after describe</strong></h2>
  
  <pre><code>root@zero:~/k8s-temp# kubectl describe pod centos610-sp-v1-6689c494b8-nb9kv 
  Name:           centos610-sp-v1-6689c494b8-nb9kv
  Namespace:      default
  Node:           zero/10.111.33.15
  Start Time:     Sat, 16 Mar 2019 01:16:59 +0800
  Labels:         app=centos610-sp-v1
                  pod-template-hash=2245705064
  Annotations:    &lt;none&gt;
  Status:         Running
  IP:             10.233.127.104
  Controlled By:  ReplicaSet/centos610-sp-v1-6689c494b8
  Containers:
    centos610-pod-v1:
      Container ID:  docker://5fa076c5d245dd532ef7ce724b94033d93642dc31965ab3fbde61dd59bf7d314
      Image:         centos-done:6.10
      Image ID:      docker://sha256:26362e9cefe4e140933bf947e3beab29da905ea5d65f27fc54513849a06d5dd5
      Port:          22/TCP
      Host Port:     0/TCP
      Command:
        /usr/sbin/useradd
      Args:
        $(SSH_USER)
      State:          Terminated
        Reason:       Completed
        Exit Code:    0
        Started:      Sat, 16 Mar 2019 01:17:17 +0800
        Finished:     Sat, 16 Mar 2019 01:17:17 +0800
      Last State:     Terminated
        Reason:       Completed
        Exit Code:    0
        Started:      Sat, 16 Mar 2019 01:17:01 +0800
        Finished:     Sat, 16 Mar 2019 01:17:01 +0800
      Ready:          False
      Restart Count:  2
      Limits:
        cpu:     500m
        memory:  1G
      Requests:
        cpu:     500m
        memory:  1G
      Environment:
        SSH_USER:  user1
        SSH_SUDO:  ALL=(ALL) NOPASSWD:ALL
        PASSWORD:  password
      Mounts:
        /var/run/secrets/kubernetes.io/serviceaccount from default-token-qbd8x (ro)
  Conditions:
    Type           Status
    Initialized    True 
    Ready          False 
    PodScheduled   True 
  Volumes:
    default-token-qbd8x:
      Type:        Secret (a volume populated by a Secret)
      SecretName:  default-token-qbd8x
      Optional:    false
  QoS Class:       Guaranteed
  Node-Selectors:  &lt;none&gt;
  Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                   node.kubernetes.io/unreachable:NoExecute for 300s
  Events:
    Type     Reason                 Age               From               Message
    ----     ------                 ----              ----               -------
    Normal   Scheduled              22s               default-scheduler  Successfully assigned centos610-sp-v1-6689c494b8-nb9kv to zero
    Normal   SuccessfulMountVolume  22s               kubelet, zero      MountVolume.SetUp succeeded for volume "default-token-qbd8x"
    Normal   Pulled                 5s (x3 over 21s)  kubelet, zero      Container image "centos-done:6.10" already present on machine
    Normal   Created                5s (x3 over 21s)  kubelet, zero      Created container
    Normal   Started                4s (x3 over 21s)  kubelet, zero      Started container
    Warning  BackOff                4s (x3 over 19s)  kubelet, zero      Back-off restarting failed container
  </code></pre>
  
  <p><strong>2019/03/18 UPDATE</strong></p>
  
  <p>Although pre-add username and password from pod's yaml is not suggested but I just want to clarify how to use command &amp; args from yaml file. Finally, I use following yaml file to create a username "user1" and its password "1234" successfully. Thank you all of you guys' great answer to make me more familiar with k8s about configMap, RBAC, container's behavior.</p>
  
  <p>Actually, this link gave me a reference on how to use command &amp; args</p>
  
  <p><a href="https://stackoverflow.com/questions/33887194/how-to-set-multiple-commands-in-one-yaml-file-with-kubernetes">How to set multiple commands in one yaml file with Kubernetes?</a></p>
  
  <p>Here are my final yaml file content:</p>
  
  <pre><code>---
  apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    name: centos610-sp-v1
  spec:
    replicas: 1
    template:
      metadata:
        labels:
          app: centos610-sp-v1
      spec:
        containers:
        - name: centos610-pod-v1
          image: centos-done:6.10
          env:
          - name: SSH_USER
            value: "user1"
          - name: SSH_SUDO
            value: "ALL=(ALL) NOPASSWD:ALL"
          - name: PASSWORD
            value: "password"
          command: ["/bin/bash", "-c"]
          args: ["useradd $(SSH_USER); service sshd restart; echo $(SSH_USER):1234 | chpasswd; tail -f /dev/null"]
          ports:
          - containerPort: 22
          resources:
            limits:
              cpu: "500m"
              memory: "1G"
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: centos610-sp-v1
    labels:
      app: centos610-sp-v1
  spec:
    selector:
      app: centos610-sp-v1
    ports:
    - port: 22
      protocol: TCP
      nodePort: 31022
    type: NodePort
  ---
  </code></pre>