繁体   English   中英

minikube:如何将第二个容器添加到 pod?

[英]minikube: how to add second container to the pod?

我正在寻求有关如何将第二个容器添加到现有 pod 的帮助(我的意思是一个带有两个容器的 pod)。 错误是CrashLoopBackOff 我正在使用 minikube。 我请求任何帮助解决这个问题将不胜感激。

wordpress.yaml
--------XXXX------------------
apiVersion: v1
kind: Service
metadata:
  name: my-nginx-svc
  labels:
    app: nginx
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
     - name: wordpress
        image: wordpress
        ports:
        - containerPort: 80
#watch kubectl get pods

Every 2.0s: kubectl get pods                                                                                                                         cent8-minikube: Wed Jun 10 16:47:30 2020

NAME                            READY   STATUS             RESTARTS   AGE
my-nginx-6b474476c4-9p4cn       1/1     Running            0          5h41m
my-nginx-6b474476c4-m2xkd       1/1     Running            0          5h41m
my-nginx-9f44b5996-744n5        1/2     CrashLoopBackOff   9          21m
my-nginx-9f44b5996-vl6g2        1/2     CrashLoopBackOff   9          22m
test-minikube-f4df69575-2sbl5   1/1     Running            0          26h
[root@cent8-minikube ~]# kubectl describe pod my-nginx-9f44b5996-vl6g2
Name:         my-nginx-9f44b5996-vl6g2
Namespace:    default
Priority:     0
Node:         cent8-minikube/192.168.194.128
Start Time:   Wed, 10 Jun 2020 16:25:14 -0700
Labels:       app=nginx
              pod-template-hash=9f44b5996
Annotations:  <none>
Status:       Running
IP:           172.17.0.8
IPs:
  IP:           172.17.0.8
Controlled By:  ReplicaSet/my-nginx-9f44b5996
Containers:
  nginx:
    Container ID:   docker://5e4cfd4e726373916a105a95644a7a286966482e33eaaac986e44514aef86606
    Image:          nginx:1.14.2
    Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 10 Jun 2020 16:25:16 -0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-lqnj8 (ro)
  wordpress:
    Container ID:   docker://5e37277badca1658a10d4d826428f538a45d5e0eaecabd5e196f8b6ab5848ec7
    Image:          wordpress
    Image ID:       docker-pullable://wordpress@sha256:ff8be61894e74b6a005ab54ba73aa7084b6dbd11605f12ac383549763918bf09
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Wed, 10 Jun 2020 16:46:37 -0700
      Finished:     Wed, 10 Jun 2020 16:46:38 -0700
    Ready:          False
    Restart Count:  9
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-lqnj8 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-lqnj8:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-lqnj8
    Optional:    false
QoS Class:       BestEffort
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  23m                   default-scheduler        Successfully assigned default/my-nginx-9f44b5996-vl6g2 to cent8-minikube
  Normal   Pulled     23m                   kubelet, cent8-minikube  Container image "nginx:1.14.2" already present on machine
  Normal   Created    23m                   kubelet, cent8-minikube  Created container nginx
  Normal   Started    23m                   kubelet, cent8-minikube  Started container nginx
  Normal   Pulling    22m (x4 over 23m)     kubelet, cent8-minikube  Pulling image "wordpress"
  Normal   Pulled     22m (x4 over 23m)     kubelet, cent8-minikube  Successfully pulled image "wordpress"
  Normal   Created    22m (x4 over 23m)     kubelet, cent8-minikube  Created container wordpress
  Normal   Started    22m (x4 over 23m)     kubelet, cent8-minikube  Started container wordpress
  Warning  BackOff    3m35s (x95 over 23m)  kubelet, cent8-minikube  Back-off restarting failed container
[root@cent8-minikube ~]#
[root@cent8-minikube ~]# kubectl describe pod my-nginx-9f44b5996-744n5
Name:         my-nginx-9f44b5996-744n5
Namespace:    default
Priority:     0
Node:         cent8-minikube/192.168.194.128
Start Time:   Wed, 10 Jun 2020 16:25:33 -0700
Labels:       app=nginx
              pod-template-hash=9f44b5996
Annotations:  <none>
Status:       Running
IP:           172.17.0.10
IPs:
  IP:           172.17.0.10
Controlled By:  ReplicaSet/my-nginx-9f44b5996
Containers:
  nginx:
    Container ID:   docker://9e3d6f0073e51eb475c2f2677fa413509f49a07c955d04b09417811d37ba8433
    Image:          nginx:1.14.2
    Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 10 Jun 2020 16:25:35 -0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-lqnj8 (ro)
  wordpress:
    Container ID:   docker://2ca8f22ab14b88973dca8d4d486f82a5b0d9bc7b84960882cff0a81afd744bf4
    Image:          wordpress
    Image ID:       docker-pullable://wordpress@sha256:ff8be61894e74b6a005ab54ba73aa7084b6dbd11605f12ac383549763918bf09
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Wed, 10 Jun 2020 16:46:41 -0700
      Finished:     Wed, 10 Jun 2020 16:46:42 -0700
    Ready:          False
    Restart Count:  9
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-lqnj8 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-lqnj8:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-lqnj8
    Optional:    false
QoS Class:       BestEffort
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  26m                  default-scheduler        Successfully assigned default/my-nginx-9f44b5996-744n5 to cent8-minikube
  Normal   Pulled     26m                  kubelet, cent8-minikube  Container image "nginx:1.14.2" already present on machine
  Normal   Created    26m                  kubelet, cent8-minikube  Created container nginx
  Normal   Started    26m                  kubelet, cent8-minikube  Started container nginx
  Normal   Pulling    25m (x4 over 26m)    kubelet, cent8-minikube  Pulling image "wordpress"
  Normal   Pulled     25m (x4 over 26m)    kubelet, cent8-minikube  Successfully pulled image "wordpress"
  Normal   Created    25m (x4 over 26m)    kubelet, cent8-minikube  Created container wordpress
  Normal   Started    25m (x4 over 26m)    kubelet, cent8-minikube  Started container wordpress
  Warning  BackOff    67s (x116 over 26m)  kubelet, cent8-minikube  Back-off restarting failed container
[root@cent8-minikube ~]#
``` [root@cent8-minikube ~]# kubectl logs my-nginx-9f44b5996-vl6g2 -c wordpress WordPress not found in /var/www/html - copying now... Complete! WordPress has been successfully copied to /var/www/html AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.8. Set the 'ServerName' directive globally to suppress this message (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down AH00015: Unable to open logs 
  1. 如何向 pod 添加第二个容器?
  2. 在哪里检查崩溃容器的日志或如何调试?
  3. 你能指导我,如何解决这个错误?

查看 pod 中容器的日志

kubectl logs my-pod -c my-container 

检查 pod 中先前容器实例的日志

kubectl logs my-pod -c my-container --previous 

在您的情况下,这将转化为

 kubectl logs my-nginx-9f44b5996-vl6g2 -c nginx
 kubectl logs my-nginx-9f44b5996-vl6g2 -c wordpress

Wordpress 在同一个 pod 中与 nginx 一起运行可能不是一个好主意。 多容器 pod上查看本指南

查看本指南以在 kubernetes 上运行 wordpress

您的错误的重要部分是:

Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down

如果您在单个 pod 中运行多个容器,它们将共享一个网络命名空间。 Service 转发到 pod 的 80 端口的地方,它可以到达任何容器。 在这个例子中,这意味着你不能让同一个 pod 中的两个容器监听同一个端口。

更好的做法是将这两个组件拆分为两个单独的部署,每个部署都有一个匹配的服务。 去做这个:

  1. 制作现有部署的两个副本。 一方面,删除 Wordpress 的所有提及。 另一方面,删除 Nginx 容器,否则将nginx全局替换为wordpress
  2. 制作现有服务的两份副本。 将第二个更改为type: ClusterIP ,并将nginx全局替换为wordpress
  3. 在您的 Nginx 代理配置中,您proxy_pass到 Wordpress 容器,将其后端更改为http://my-wordpress-svc/

为此使用 5 个单独的 YAML 文件(两个部署、两个服务、一个 ConfigMap)是非常常规的,您可以在一个目录上运行kubectl apply -f以一次性安装它们。

虽然我同意在单独的 Pod 中运行单独的应用程序,但有时将它们放在一起更容易。 在当前情况下,这可以通过以下方式实现:

Wordpress 是硬配置,因此将 Nginx 移动到单独的端口更可行(如果不是完全直接的话)

 apiVersion: v1 kind: ConfigMap metadata: name: nginx data: default.conf.template: | server { listen ${NGINX_PORT}; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } --- apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx env: - name: NGINX_PORT value: "81" volumeMounts: - mountPath: /etc/nginx/templates/ name: config ports: - containerPort: 81 - name: wordpress image: wordpress ports: - containerPort: 80 volumes: - name: config configMap: name: nginx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM