How to add an username when create a k8s pod

3/15/2019

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?

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.

Is it possible to pre-add an username and its password from yaml file during k8s pod creation?

Following are my yaml file:

---
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
---

Should I use specific command as

env:
- name: MESSAGE
  value: "hello world"
command: ["/bin/echo"]
args: ["$(MESSAGE)"]

or

command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]

pod's status after get

root@zero:~/k8s-temp# kubectl get pod
NAME                               READY     STATUS             RESTARTS   AGE
centos610-sp-v1-6689c494b8-nb9kv   0/1       CrashLoopBackOff   5          3m

pod's status after describe

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:    <none>
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:  <none>
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

2019/03/18 UPDATE

Although pre-add username and password from pod's yaml is not suggested but I just want to clarify how to use command & 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.

Actually, this link gave me a reference on how to use command & args

How to set multiple commands in one yaml file with Kubernetes?

Here are my final yaml file content:

---
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
---
-- Spark1231
kubernetes
yaml

3 Answers

3/15/2019

If you want to add the user anyway , regardless of the fact that you can achive the same thing using A kubernetes native way , then Please setup your user in the Docker image ( Dockerfile and then build it) instead.

Hope this helps.

-- Ijaz Ahmad Khan
Source: StackOverflow

3/18/2019

2019/03/18 UPDATE

Although pre-add username and password from pod's yaml is not suggested but I just want to clarify how to use command & 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.

Actually, this link gave me a reference on how to use command & args

How to set multiple commands in one yaml file with Kubernetes?

Here are my final yaml file content:

---
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
---
-- Spark1231
Source: StackOverflow

3/15/2019

Keep username and password in a configMap or in a secret objec. Load those values into container as environment variables

Follow the reference https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

-- P Ekambaram
Source: StackOverflow