创建K8S窗格时如何添加用户名

时间:2019-03-15 15:18:52

标签: kubernetes yaml

我有一个Yaml文件,可以用一个容器创建k8s pod。在创建K8s Pod期间是否可以从yaml文件中预添加用户名及其密码?

我搜索了许多站点并找到了env变量。但是,我无法按照自己的意愿制作豆荚。创建广告连播后,广告连播的状态始终显示为“崩溃”。

是否可以在创建k8s pod时从yaml文件中预添加用户名和密码?

以下是我的Yaml文件:

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

我应该将特定命令用作

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

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

获得后的豆荚状态

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

描述后的豆荚状态

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

尽管不建议从pod的yaml中预先添加用户名和密码,但我只是想阐明如何使用yaml文件中的命令和参数。最后,我使用以下yaml文件成功创建用户名“ user1”及其密码“ 1234”。谢谢大家的出色回答,使我对k8s更加熟悉configMap,RBAC和容器的行为。

实际上,此链接为我提供了有关如何使用命令和参数的参考

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

这是我最后的Yaml文件内容:

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

3 个答案:

答案 0 :(得分:1)

将用户名和密码保留在configMap或秘密objec中。将这些值作为环境变量加载到容器中

按照参考 https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

答案 1 :(得分:0)

如果您仍然想添加用户,无论您可以使用A kubernetes本机方式来实现同一目的,那么 请改为在Docker映像(Dockerfile,然后构建它)中设置用户。

希望这会有所帮助。

答案 2 :(得分:0)

2019/03/18更新

尽管不建议从pod的yaml中预先添加用户名和密码,但我只是想阐明如何使用yaml文件中的命令和参数。最后,我使用以下yaml文件成功创建用户名“ user1”及其密码“ 1234”。谢谢大家的出色回答,使我对k8s更加熟悉configMap,RBAC和容器的行为。

实际上,此链接为我提供了有关如何使用命令和参数的参考

如何使用Kubernetes在一个Yaml文件中设置多个命令?

这是我最后的Yaml文件内容:

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