1

Cluster information:
Kubernetes version: 1.8
Cloud being used: (put bare-metal if not on a public cloud) AWS EKS
Host OS: debian linux

When I deploy pods,I want to my pod to install and start sysstat automatically

this is my two yaml flies below but it doesn’t work CrashLoopBackoff when I put the command: ["/bin/sh", “-c”]、args: [“apt-get install sysstat”]」 below 「image:」

cat deploy/db/statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: 
    sbdemo-postgres-sfs
spec:
  serviceName: sbdemo-postgres-service
  replicas: 1
  selector:
    matchLabels:
      app: sbdemo-postgres-sfs
  template:
    metadata:
      labels:
        app: sbdemo-postgres-sfs
    spec:
      containers:
       - name: postgres
         image: dayan888/springdemo:postgres9.6
         ports:
          - containerPort: 5432
         **command: ["/bin/bash", "-c"]**
         **args: ["apt-get install sysstat"]**
         volumeMounts:
         - name: pvc-db-volume
           mountPath: /var/lib/postgresql
  volumeClaimTemplates:
  - metadata:
      name: pvc-db-volume
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1G

cat deploy/web/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sbdemo-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sbdemo-nginx
  template:
    metadata:
      labels:
        app: sbdemo-nginx
    spec:
      containers:
      - name: nginx
        image: gobawoo21/springdemo:nginx
         **command: ["/bin/bash", "-c"]**
         **args: ["apt-get install sysstat"]**
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-conf
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
        - name: server-conf
          mountPath: /etc/nginx/conf.d/server.conf
          subPath: server.conf
      volumes:
      - name: nginx-conf
        configMap: 
          name: nginx-conf
          items:
            - key: nginx.conf
              path: nginx.conf
      - name: server-conf
        configMap: 
          name: server-conf
          items:
            - key: server.conf
              path: server.conf

Does anyone know about how to set the repository automatically when deploy pods?

Regards

Bawoo
  • 11
  • 1
  • 3

2 Answers2

7

The best practice is to install packages at image build stage. You can simply add this step to your Dockerfile.

FROM postgres:9.6

RUN apt-get update &&\ 
    apt-get install sysstat -y &&\
    rm -rf /var/lib/apt/lists/*

COPY deploy/db/init_ddl.sh /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/init_ddl.sh

Kube Manifest

   spec:
      containers:
       - name: postgres
         image: harik8/sof:62298191
         imagePullPolicy: Always
         ports:
          - containerPort: 5432
         env:
         - name: POSTGRES_PASSWORD
           value: password
         volumeMounts:
         - name: pvc-db-volume
           mountPath: /var/lib/postgresql

It should run (Please ignore POSTGRES_PASSWORD env variable)

$ kubectl get po
NAME                    READY   STATUS      RESTARTS   AGE
sbdemo-postgres-sfs-0   1/1     Running     0          8m46s

Validation

$ kubectl exec -it sbdemo-postgres-sfs-0 bash
root@sbdemo-postgres-sfs-0:/# iostat
Linux 4.19.107 (sbdemo-postgres-sfs-0)  06/10/2020      _x86_64_        (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          10.38    0.01    6.28    0.24    0.00   83.09

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
vda             115.53      1144.72      1320.48    1837135    2119208
scd0              0.02         0.65         0.00       1048          0
hariK
  • 2,722
  • 13
  • 18
  • Thankyou for your comment . Why I have to need this command? rm -rf /var/lib/apt/lists/* – Bawoo Jun 10 '20 at 10:02
  • 1
    @Bawoo "When you clean up the apt cache by removing /var/lib/apt/lists it reduces the image size". Refer [here](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/). – hariK Jun 10 '20 at 10:05
  • can't you help me what I'm wrong? it dosen't work my nginx pod – Bawoo Jun 10 '20 at 10:24
3

If this is possible, something is wrong. Your container should not be running as root and so even if you fixed this approach, it shouldn’t work. What you need to do is put this in your container build instead (I.e. in the Dockerfile).

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • Thankyou for your comment You mean if I want to use that command and args I have to modify my Dockerfile? Thanks regards – Bawoo Jun 10 '20 at 09:09
  • Yes, you need to add `RUN apt-get install sysstat` to your Dockerfile. Probably you want to add more than just that because there is a subtle art to writing good container images but start there. – coderanger Jun 10 '20 at 10:27