简体   繁体   中英

minikube: how to add second container to the pod?

I am looking for help on how to add a second container to the existing pod (i mean one pod with two containers). The error is CrashLoopBackOff . I am using minikube. I request any assistance in resolving the issue will be appreciated.

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. How to add a second container to the pod?
  2. where to check the logs of the crash container or How to debug?
  3. could you guide me, how to fix this error?

To check logs of a container in a pod

kubectl logs my-pod -c my-container 

To check logs of previous instance of a container in a pod

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

In your case this will translate to

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

Wordpress is probably not a good idea to run with nginx in the same pod. Check this guide on multicontainer pod

Check this guide to run wordpress on kubernetes

The important part of your error is this:

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

If you run multiple containers in a single pod, they share a network namespace. Where the Service forwards to port 80 of the pod, it could reach any container. In this example, that means you can't have two containers in the same pod listening to the same port.

A better practice would be to split these two components into two separate Deployments, each with a matching Service. To do this:

  1. Make two copies of your existing Deployment. In one, delete all mentions of Wordpress. In the other, delete the Nginx container and otherwise globally replace nginx with wordpress .
  2. Make two copies of your existing Service. Change the second one to be type: ClusterIP , and globally replace nginx with wordpress .
  3. In your Nginx proxy configuration, where you proxy_pass to the Wordpress container, change its backend to http://my-wordpress-svc/ .

It would be very routine to wind up with 5 separate YAML files for this (two Deployments, two Services, one ConfigMap) and you can run kubectl apply -f on a directory to install them all in one shot.

While I agree that running separate applications in separate Pods, sometimes it's easier to keep them together. In the current scenario this could have been achieved as follows:

Wordpress is hard configure, so moving Nginx to a separate port is more feasible (if not totally straight forward)

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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