Tutoriales

Cómo usar Podman en Kubernetes

Podman en Kubernetes/OpenShift

En la primera parte, la atención se centra en el Podman en la escena Podman.Vimos algunos rooteados y no rooteados diferentes Podman combinación.también discutimos --privileged bandera.

Pero, ¿qué pasa con Podman y Kubernetes? También hay muchas opciones para vincular los dos servicios.

Para la segunda parte de esta serie, utilicé un clúster de Kubernetes que se ejecutaba con CRI-O como tiempo de ejecución.

[ Free cheat sheet: Kubernetes glossary ]

Rootful Podman con conjunto de banderas privilegiadas

Aquí ejecutamos un contenedor privilegiado con el usuario root para que Podman se ejecute como root dentro del contenedor.

Aquí está el archivo YAML: rootful-priv.yaml:

Publicaciones relacionadas
apiVersion: v1
kind: Pod
metadata:
 name: podman-priv
spec:
 containers:
   - name: priv
     image: quay.io/podman/stable
     args:
       - sleep
       - "1000000"
     securityContext:
       privileged: true
➜ kubectl exec -it podman-priv -- sh
sh-5.0# id
uid=0(root) gid=0(root) groups=0(root)
sh-5.0# podman run ubi8 echo hello
Resolved "ubi8" as an alias (/etc/containers/registries.conf.d/shortnames.conf)
Trying to pull registry.access.redhat.com/ubi8:latest...
Getting image source signatures
Copying blob fdb393d8227c done
Copying blob 6b536614e8f8 done
Copying config 4199acc83c done
Writing manifest to image destination
Storing signatures
hello

También podemos crear imágenes con éxito dentro de contenedores privilegiados utilizando Rootful Podman. Construyamos una imagen para instalar BusyBox en Fedora.

sh-5.0# cat Containerfile
FROM fedora
RUN dnf install -y busybox
ENV foo=bar
 
sh-5.0# podman build -t myimage -f Containerfile .                                                                                     
STEP 1: FROM fedora                                                                                                                    
STEP 2: RUN dnf install -y busybox                                                                                                     
Fedora 33 openh264 (From Cisco) - x86_64        3.0 kB/s | 2.5 kB     00:00                                                            
Fedora Modular 33 - x86_64                      1.4 MB/s | 3.3 MB     00:02                                                            
Fedora Modular 33 - x86_64 - Updates            1.3 MB/s | 3.1 MB     00:02                                                            
Fedora 33 - x86_64 - Updates                    1.6 MB/s |  27 MB     00:16                                                            
Fedora 33 - x86_64                              3.6 MB/s |  72 MB     00:19                                                            
Dependencies resolved.                                                         
...
Running transaction
 Preparing        :                                                        1/1
 Installing       : busybox-1:1.32.1-1.fc33.x86_64                         1/1
 Running scriptlet: busybox-1:1.32.1-1.fc33.x86_64                         1/1
 Verifying        : busybox-1:1.32.1-1.fc33.x86_64                         1/1
 
Installed:
 busybox-1:1.32.1-1.fc33.x86_64                                                
 
Complete!
--> 734a45854d1
STEP 3: ENV foo=bar
STEP 4: COMMIT myimage
--> 2326e34ac82
2326e34ac82173c849e0282b6644de5326f6b5bfba8431cf1c1115d846e440e9
 
sh-5.0# podman images                                                                                                                  
REPOSITORY                         TAG     IMAGE ID      CREATED         SIZE                                                          
localhost/myimage                  latest  2326e34ac821  48 seconds ago  427 MB                                                        
registry.fedoraproject.org/fedora  latest  9f2a56037643  3 months ago    182 MB    
 
sh-5.0# podman run myimage busybox                                                                                                     
BusyBox v1.32.1 (2021-03-22 18:56:41 UTC) multi-call binary.                                                                           
BusyBox is copyrighted by many authors between 1998-2015.                                                                              
Licensed under GPLv2. See source distribution for detailed                                                                             
copyright notices.                                                                                                                     
                                                                                                                                      
Usage: busybox [function [arguments]...]                                                                                               
  or: busybox --list[-full]                                                                                                           
  or: busybox --show SCRIPT                                                                                                           
  or: busybox --install [-s] [DIR]                                                                                                    
  or: function [arguments]...
...

Podman sin raíces con banderas privilegiadas establecidas

Aquí estamos ejecutando un contenedor privilegiado Podcasts (1000) usuario para que Podman se ejecute como usuario 1000 dentro del contenedor.

Aquí está el archivo YAML: rootless-priv.yaml:

apiVersion: v1
kind: Pod
metadata:
 name: podman-rootless
spec:
 containers:
   - name: rootless
     image: quay.io/podman/stable
     args:
       - sleep
       - "1000000"
     securityContext:
       privileged: true
       runAsUser: 1000
➜ kubectl exec -it podman-rootless -- sh

sh-5.0$ id
uid=1000(podman) gid=1000(podman) groups=1000(podman)

sh-5.0$ podman run ubi8 echo hello
Resolved "ubi8" as an alias (/etc/containers/registries.conf.d/shortnames.conf)
Trying to pull registry.access.redhat.com/ubi8:latest...
Getting image source signatures
Copying blob 6b536614e8f8 done
Copying blob fdb393d8227c done
Copying config 4199acc83c done
Writing manifest to image destination
Storing signatures
hello

También podemos crear imágenes con éxito dentro de contenedores privilegiados utilizando Podman sin raíz. Construyamos una imagen para instalar BusyBox en fedora.

sh-5.0$ cat Containerfile
FROM fedora
RUN dnf install -y busybox
ENV foo=bar
 
sh-5.0$ podman build -t myimage -f Containerfile .                                                                                                                                                                                                
STEP 1: FROM fedora                                                                                                                    
Resolved "fedora" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)                                                  
Getting image source signatures                                                                                                        
Copying blob 157ab8011454 done                                                                                                         
Copying config 9f2a560376 done                                                                                                         
Writing manifest to image destination                                                                                                  
Storing signatures                                                                                                                     
STEP 2: RUN dnf install -y busybox                                                                                                     
Fedora 33 openh264 (From Cisco) - x86_64        4.8 kB/s | 2.5 kB     00:00                                                            
Fedora Modular 33 - x86_64                      462 kB/s | 3.3 MB     00:07                                                            
Fedora Modular 33 - x86_64 - Updates            520 kB/s | 3.1 MB     00:06                                                            
Fedora 33 - x86_64 - Updates                    7.5 MB/s |  27 MB     00:03                                                            
Fedora 33 - x86_64                              522 kB/s |  72 MB     02:20                                                            
Dependencies resolved.
...
Installed:
 busybox-1:1.32.1-1.fc33.x86_64                                                
 
Complete!
--> 92087429448
STEP 3: ENV foo=bar
STEP 4: COMMIT myimage
--> 16dd65e3f57
16dd65e3f57a5808035b713a6ba3267146caf2a03dd4205097a5727f9d326de9
 
sh-5.0$ podman images
REPOSITORY                         TAG     IMAGE ID      CREATED             SIZE
localhost/myimage                  latest  16dd65e3f57a  About a minute ago  427 MB
registry.fedoraproject.org/fedora  latest  9f2a56037643  3 months ago        182 MB
 
sh-5.0$ podman run myimage busybox                                                                                                                                                                                                               
BusyBox v1.32.1 (2021-03-22 18:56:41 UTC) multi-call binary.                                                                           
BusyBox is copyrighted by many authors between 1998-2015.                                                                              
Licensed under GPLv2. See source distribution for detailed                                                                             
copyright notices.                                                                                                                     
                                                                                                                                      
Usage: busybox [function [arguments]...]                                                                                               
  or: busybox --list[-full]                                                                                                           
  or: busybox --show SCRIPT                                                                                                           
  or: busybox --install [-s] [DIR]                                                                                                    
  or: function [arguments]...
...

[ Getting started with containers? Check out this free course. Deploying containerized applications: A technical overview. ]

Podman no rooteado sin banderas de privilegios

Para deshacernos de la bandera privilegiada, debemos hacer lo siguiente:

  • equipo: /dev/fuse Requiere fuse-overlayfs dentro del contenedor, esta opción le dice a Podman en el host que agregue /dev/fuse al contenedor para que el Podman contenedorizado pueda usarlo.
  • Deshabilite SELinux: SELinux no permite que los procesos en contenedores monten todos los sistemas de archivos necesarios para ejecutarse dentro del contenedor. Por lo tanto, debemos deshabilitar SELinux en el host que ejecuta el clúster de Kubernetes.

Para poder montar un dispositivo en Kubernetes, primero debe crear un complemento de dispositivo y luego usarlo en la especificación del pod.

Aquí hay un ejemplo de un complemento de dispositivo /dev/fuse: https://github.com/kuberenetes-learning-group/fuse-device-plugin/blob/main/fuse-device-plugin-k8s-1.16.yml.

apiVersion: apps/v1
kind: DaemonSet
metadata:
 name: fuse-device-plugin-daemonset
 namespace: kube-system
spec:
 selector:
   matchLabels:
     name: fuse-device-plugin-ds
 template:
   metadata:
     labels:
       name: fuse-device-plugin-ds
   spec:
     hostNetwork: true
     containers:
     - image: soolaugust/fuse-device-plugin:v1.0
       name: fuse-device-plugin-ctr
       securityContext:
         allowPrivilegeEscalation: false
         capabilities:
           drop: ["ALL"]
       volumeMounts:
         - name: device-plugin
           mountPath: /var/lib/kubelet/device-plugins
     volumes:
       - name: device-plugin
         hostPath:
           path: /var/lib/kubelet/device-plugins
     imagePullSecrets:
       - name: registry-secret

Aquí está el archivo YAML: rootless-no-priv.yaml:

apiVersion: v1
kind: Pod
metadata:
 name: no-priv
spec:
 containers:
   - name: no-priv
     image: quay.io/podman/stable
     args:
       - sleep
       - "1000000"
     securityContext:
       runAsUser: 1000
     resources:
       limits:
         github.com/fuse: 1
     volumeMounts:
       - mountPath: /home/podman/.local/share/containers
         name: podman-local
 volumes:
   - name: podman-local
     hostPath:
       path: /home/umohnani/.local/share/containers
✗ kubectl exec -it no-priv -- sh          
sh-5.0$ id
uid=1000(podman) gid=1000(podman) groups=1000(podman)

sh-5.0$ podman run ubi8 echo hello
Resolved "ubi8" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull registry.access.redhat.com/ubi8:latest...
Getting image source signatures
Copying blob 55eda7743468 done 
Copying blob 4b21dcdd136d done 
Copying config 613e5da7a9 done 
Writing manifest to image destination
Storing signatures
hello
 
sh-5.1$ cat containerfile
FROM ubi8
RUN echo "hello"
ENV foo=bar
 
sh-5.1$ podman build --isolation chroot -t myimage -f containerfile .                                                               
STEP 1: FROM ubi8                                                                                                                 
STEP 2: RUN echo "hello"                                                                                                            
hello                                                                                                                              
--> 096250be78f                                                                                                                   
STEP 3: ENV foo=bar                                                                                                                
STEP 4: COMMIT myimage                                                                                                             
--> ea849ac9875                                                                                                                    
Ea849ac9875eb926d743362bce2e32e90d34fda7a88f28ebd6a1a546db99338f
 
sh-5.1$ podman images
REPOSITORY                   	TAG 	IMAGE ID  	CREATED     	SIZE
localhost/myimage            	latest  ea849ac9875e  41 seconds ago  245 MB
registry.access.redhat.com/ubi8  latest  0724f7c987a7  3 weeks ago 	245 MB

Rootful Podman sin banderas de privilegios

Cree el complemento de su dispositivo como se muestra arriba.

Necesita agregar las siguientes funciones para esto:

  • CAP_SYS_ADMIN Es necesario que Podman se ejecute como root dentro del contenedor para montar los sistemas de archivos necesarios.
  • CAP_MKNOD Podman debe ejecutarse como root dentro del contenedor para crear dispositivos en él /dev. (Tenga en cuenta que Docker lo permite de forma predeterminada).
  • CAP_SYS_CHROOT y CAP_SETFCAP son obligatorios porque son parte de la lista de características predeterminadas en Podman, y cuando ejecuta el comando Podman, agrega las características requeridas, por lo que si ejecuta k8s pod Sin esta capacidad, Podman falla.

Aquí está el archivo YAML: rootful-no-priv.yaml:

apiVersion: v1
kind: Pod
metadata:
 name: no-priv-rootful
spec:
 containers:
   - name: no-priv-rootful
     image: quay.io/podman/stable
     args:
       - sleep
       - "1000000"
     securityContext:
       capabilities:
         add:
           - "SYS_ADMIN"
           - "MKNOD"
           - "SYS_CHROOT"
           - "SETFCAP"
     resources:
       limits:
         github.com/fuse: 1
✗ kubectl exec -it no-priv-rootful -- sh
sh-5.0# id
uid=0(root) gid=0(root) groups=0(root)

sh-5.0# podman run ubi8 echo hello
Resolved "ubi8" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull registry.access.redhat.com/ubi8:latest...
Getting image source signatures
Copying blob 55eda7743468 done
Copying blob 4b21dcdd136d done
Copying config 613e5da7a9 done
Writing manifest to image destination
Storing signatures
hello

Podman-remote en un pod de Kubernetes con sockets de Podman ejecutándose en el host

Debe hacer lo siguiente para configurar este caso de uso:

  • Deshabilite SELinux en el host.
  • seguir Este artículo Habilite los sockets de Podman en su host.

Aquí está el archivo YAML: remote.yaml:

apiVersion: v1
kind: Pod
metadata:
 name: podman-remote
spec:
 containers:
   - name: remote
     image: quay.io/podman/stable
     args:
       - sleep
       - "1000000"
     volumeMounts:
         - mountPath: /var/run/podman
           name: podman-sock
 volumes:
   - name: podman-sock
     hostPath:
         path: /var/run/podman

Filtramos el socket de Podman que se ejecuta en el host en el pod creando un montaje de volumen para él.

✗ kubectl exec -it podman-remote -- sh
sh-5.0# id
uid=0(root) gid=0(root) groups=0(root

sh-5.0# podman --remote run ubi8 echo hello
Resolved "ubi8" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull registry.access.redhat.com/ubi8:latest...
Getting image source signatures
Copying blob sha256:55eda774346862e410811e3fa91cefe805bc11ff46fad425dd1b712709c05bbc
Copying blob sha256:4b21dcdd136d133a4df0840e656af2f488c226dd384a98b89ced79064a4081b4
Copying config sha256:613e5da7a934e1963e37ed935917e8be6b8dfd90cac73a724ddc224fbf16da20
Writing manifest to image destination
Storing signatures
hello

Construya con el zócalo de Podman filtrado en el contenedor:

sh-5.0# cat /home/podman/Containerfile
FROM fedora
RUN dnf install -y busybox
ENV foo=bar

sh-5.0# podman --remote build -t myimage -f Containerfile .                                                                           
STEP 1: FROM fedora                                                                                                                   
STEP 2: RUN dnf install -y busybox                                                                                                  
Fedora 33 openh264 (From Cisco) - x86_64 4.7 kB/s | 2.5 kB 00:00                                                           
Fedora Modular 33 - x86_64 1.8 MB/s | 3.3 MB 00:01                                                            
Fedora Modular 33 - x86_64 - Updates 5.2 MB/s | 3.1 MB 00:00                                                           
Fedora 33 - x86_64 - Updates 4.3 MB/s | 27 MB    
00:06                                                            
Fedora 33 - x86_64 1.0 MB/s | 72 MB    
01:13                                                           
Dependencies resolved.
...
Installed:
 busybox-1:1.32.1-1.fc33.x86_64                                               

Complete!
--> 6ef78b975e1
STEP 3: ENV foo=bar
STEP 4: COMMIT myimage
--> 481c5a0e453
481c5a0e4534573a3872f7cc1ff6806a3ce143edce2ed39568d23efe6f65a292

sh-5.0# podman --remote images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/myimage latest 481c5a0e4534 
2 minutes ago 427 MB
registry.fedoraproject.org/fedora latest 
9f2a56037643 3 months ago 182 MB

sh-5.0# podman --remote run myimage busybox                                                                                           
BusyBox v1.32.1 (2021-03-22 18:56:41 UTC) multi-call binary.                                                   

BusyBox is copyrighted by many authors between 1998-2015.                                                                             
Licensed under GPLv2. See source distribution for detailed                                                                             
copyright notices.                                                                                                                    

Usage: busybox [function [arguments]...]                                                                                              
  or: busybox --list[-full]                                                                                                           
  or: busybox --show SCRIPT                                                                                                          
  or: busybox --install [-s] [DIR]                
  or: function [arguments]...  

...

[ Learn the basics of using Kubernetes in this free cheat sheet. ]

Podman en contenedor cerrado con espacio de nombres de usuario en Kubernetes

Esto solo funciona si está utilizando CRI-O como motor de tiempo de ejecución para su clúster de Kubernetes.

necesitamos agregar usuario anotaciones en tiempo de ejecución (por ejemplo, runc, crun, kataetc) que utilizará con CRI-O.

[crio.runtime.runtimes.runc]
runtime_path = ""
runtime_type = "oci"
runtime_root = "/run/runc"
allowed_annotations = [
       "io.containers.trace-syscall",
       "io.kubernetes.cri-o.userns-mode",
]

Agregar rangos de UID/GID de Podman a subuid y subgid archivos en el host.

✗ cat /etc/subuid
umohnani:100000:65536
containers:200000:268435456

✗ cat /etc/subgid
umohnani:100000:65536
containers:200000:268435456

Reinicie CRI-O después de esto, luego inicie su clúster de Kubernetes:

✗ sudo systemctl restart cri-o

✗ ./local-cluster-up.sh

porque estamos ejecutando esto No Banderas privilegiadas que necesitamos montar /dev/fuse, como se muestra en el ejemplo anterior.Entonces, crea tu /dev/fuse El complemento del dispositivo que se usará en la especificación del pod.

Aquí está el archivo YAML: userns.yaml:

apiVersion: v1
kind: Pod
metadata:
 name: podman-userns
 annotations:
   io.kubernetes.cri-o.userns-mode: "auto:size=65536;keep-id=true"
spec:
 containers:
   - name: userns
     image: quay.io/podman/stable
     command: ["sleep", "10000"]
     securityContext:
       capabilities:
         add:
           - "SYS_ADMIN"
           - "MKNOD"
           - "SYS_CHROOT"
           - "SETFCAP"
     resources:
       limits:
         github.com/fuse: 1

agregamos usuario Comente el podspec especificando el rango de UID/GID a usar y las ID que deben configurarse en el contenedor; en este caso, se configurará como raíz.

✗ kubectl exec -it podman-userns -- sh
sh-5.0# id
uid=0(root) gid=0(root) groups=0(root)

sh-5.0# cat /proc/self/uid_map
        0     265536      65536

sh-5.0# cat /proc/self/gid_map
        0     265536      65536

sh-5.0# podman run ubi8 echo hello
Resolved "ubi8" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull registry.access.redhat.com/ubi8:latest...
Getting image source signatures
Copying blob 4b21dcdd136d done 
Copying blob 55eda7743468 done 
Copying config 613e5da7a9 done 
Writing manifest to image destination
Storing signatures
hello

Construyendo con Rootful Podman en un contenedor cerrado con espacio de nombre de usuario

sh-5.0# cat Containerfile
FROM fedora
RUN dnf install -y busybox
ENV foo=bar
 
sh-5.0# podman build -t myimage -f Containerfile .                                                                                     
STEP 1: FROM fedora                                                                                                                    
Resolved "fedora" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)                                                  
Getting image source signatures                                                                                                        
Copying blob 157ab8011454 done                                                                                                         
Copying config 9f2a560376 done                                                                                                         
Writing manifest to image destination                                                                                                  
Storing signatures                                                                                                                     

STEP 2: RUN dnf install -y busybox                                                                                                     
Fedora 33 openh264 (From Cisco) - x86_64        764  B/s | 2.5 kB     00:03                                                            
Fedora Modular 33 - x86_64                      348 kB/s | 3.3 MB     00:09                                                            
Fedora Modular 33 - x86_64 - Updates            2.2 MB/s | 3.1 MB     00:01                                                            
Fedora 33 - x86_64 - Updates                     11 MB/s |  27 MB     00:02                                                            
Fedora 33 - x86_64                              2.1 MB/s |  72 MB     00:34                                                            
Dependencies resolved.       
...
Installed:
 busybox-1:1.32.1-1.fc33.x86_64                                                
 
Complete!
--> 1b0633e5309

STEP 3: ENV foo=bar

STEP 4: COMMIT myimage
--> 2212a101136
2212a1011369ee7e6a4a5d4c15a56fc531a5d43ac24f49d432730c620cec4378
 
sh-5.0# podman images                                                                                                                  
REPOSITORY                         TAG     IMAGE ID      CREATED             SIZE                                                      
localhost/myimage                  latest  2212a1011369  About a minute ago  427 MB                                                    
registry.fedoraproject.org/fedora  latest  9f2a56037643  3 months ago        182 MB
 
sh-5.0# podman run myimage busybox                                                                                                     
BusyBox v1.32.1 (2021-03-22 18:56:41 UTC) multi-call binary.                                                                           
BusyBox is copyrighted by many authors between 1998-2015.                                                                              
Licensed under GPLv2. See source distribution for detailed                                                                             
copyright notices.                                                                                                                     
                                                                                                                                      
Usage: busybox [function [arguments]...]                                                                                               
  or: busybox --list[-full]                                                                                                           
  or: busybox --show SCRIPT                                                                                                           
  or: busybox --install [-s] [DIR]                                                                                                    
  or: function [arguments]...
... 

pensamientos finales

Aquí, en la segunda parte de la serie de artículos, demuestro varios casos de uso relacionados con la interacción con Podman y Kubernetes. Muchas opciones son similares a las que vimos en la primera parte del artículo Podman en Podman.

[ Get this free book from Red Hat and O’Reilly – Kubernetes Operators: Automating the Container Orchestration Platform. ]

fin de serie

El equipo de Podman suele hacer preguntas relacionadas con la ejecución de Podman dentro de un contenedor. Hay muchas formas posibles de hacer esto, con varios problemas de seguridad asociados.

Una de las mayores diferencias es Podman en Podman o Podman en Kubernetes, y cómo Docker es parte de la discusión.

Cuando comience a implementar Podman en estos escenarios, no olvide la información de permisos que se analiza al principio del artículo y asegúrese de evaluar la --privileged bandera. Póngase en contacto con el equipo de Podman para obtener más información.

No olvide que Enable Sysadmin tiene muchas cosas de Podman.

LEER  Cómo instalar Brew en Ubuntu y otros Linux

Publicaciones relacionadas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Botón volver arriba