podman容器自启和网络

本文详细介绍了如何使用Podman在Linux系统中为普通用户设置容器开机自启,包括创建容器、生成 systemd 服务文件、配置cgroups和storage.conf,并演示了如何创建网络、修改配置和部署web应用。

容器开机自启

#创建容器
[root@localhost ~]# podman create --name nginx nginx
✔ docker.io/library/nginx:latest
Trying to pull docker.io/library/nginx:latest...
Getting image source signatures
Copying blob 44be98c0fab6 done  
Copying blob 881ff011f1c9 done  
Copying blob e5ae68f74026 done  
Copying blob ed835de16acd done  
Copying blob 77700c52c969 done  
Copying blob 21e0df283cd6 done  
Copying config f652ca386e done  
Writing manifest to image destination
Storing signatures
066afb3e2a7434c1ec57d70d05f5ce8caafb9281df28a745c676dcd2c7e172de

#生成开机自启文件
[root@localhost ~]# podman generate systemd --files --name nginx
/root/container-nginx.service

[root@localhost ~]# mv container-nginx.service  /usr/lib/systemd/system/
[root@localhost ~]# systemctl enable --now container-nginx
Created symlink /etc/systemd/system/multi-user.target.wants/container-nginx.service → /usr/lib/systemd/system/container-nginx.service.
Created symlink /etc/systemd/system/default.target.wants/container-nginx.service → /usr/lib/systemd/system/container-nginx.service.
[root@localhost ~]# systemctl status container-nginx
● container-nginx.service - Podman container-nginx.service
   Loaded: loaded (/usr/lib/systemd/system/container-nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-12-15 00:59:20 EST; 12s ago

普通用户设置容器开机自启

在允许没有root特权的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置

cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,如果使用cgroup V2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroup V2,必须切换到备用OCI运行时crun

[root@localhost ~]# yum  -y install crun

[root@localhost ~]# vim /usr/share/containers/containers.conf

runtime = "crun"      取消#
#runtime = "runc"     注释掉

配置storage.conf文件
[root@localhost ~]# vim /etc/containers/storage.conf

mount_program = "/usr/bin/fuse-overlayfs"	#取消注释


#创建用户
[root@localhost ~]# useradd nea
[root@localhost ~]# echo "1" | passwd  --stdin nea
[root@localhost ~]# ssh nea@192.168.8.135
The authenticity of host '192.168.8.135 (192.168.8.135)' can't be established.
ECDSA key fingerprint is SHA256:dzzlHSpB1lpDe/bgiyOkjCtNZKcqZAe3Tc4CSG5ae9s.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.8.135' (ECDSA) to the list of known hosts.
nea@192.168.8.135's password: 
Last login: Wed Dec 15 01:26:08 2021 from 192.168.8.1
[nea@localhost ~]$ 

#必须在家目录下创建此目录。不能跟改名字
[nea@localhost ~]$ mkdir -p ~/.config/systemd/user
[nea@localhost ~]$ cd ~/.config/systemd/user
#创建容器
[nea@localhost user]$ podman run -d --name test nginx
[nea@localhost user]$ podman generate systemd --name test --files --new

#停止容器
[nea@localhost user]$ podman stop test
test
[nea@localhost user]$ podman ps
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES

#如果不是ssh登陆或重新进入linux系统的需重新加载系统服务
[nea@localhost user]$ systemctl --user daemon-reload
[nea@localhost user]$ systemctl --user enable --now container-test.service 
Created symlink /home/nea/.config/systemd/user/multi-user.target.wants/container-test.service → /home/nea/.config/systemd/user/container-test.service.
Created symlink /home/nea/.config/systemd/user/default.target.wants/container-test.service → /home/nea/.config/systemd/user/container-test.service.
[nea@localhost user]$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS            PORTS       NAMES
2c79cfc6f4f7  docker.io/library/nginx:latest  nginx -g daemon o...  6 seconds ago  Up 6 seconds ago              test

[nea@localhost user]$ systemctl --user status container-test.service 
● container-test.service - Podman container-test.service
   Loaded: loaded (/home/nea/.config/systemd/user/container-test.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2021-12-15 01:44:49 EST; 9min ago
     Docs: man:podman-generate-systemd(1)
  Process: 19217 ExecStartPre=/bin/rm -f /run/user/1001/container-test.service.ctr-id (code=exited, status=0/SUCCESS)
 Main PID: 19257 (conmon)
   CGroup: /user.slice/user-1001.slice/user@1001.service/container-test.service
           ├─19251 /usr/bin/fuse-overlayfs -o ,lowerdir=/home/nea/.local/share/containers/storage/overlay/l/5S2WLHYYVZAJ3G7TOACCLLOJ52:/home/nea/.local/share/>
           ├─19253 /usr/bin/slirp4netns --disable-host-loopback --mtu=65520 --enable-sandbox --enable-seccomp -c -e 3 -r 4 --netns-type=path /run/user/1001/ne>
           ├─19257 /usr/bin/conmon --api-version 1 -c 2c79cfc6f4f71f1c4bbb69240883347d9da098ae26147c463d904fe61f75cf8b -u 2c79cfc6f4f71f1c4bbb69240883347d9da0>
           ├─19260 nginx: master process nginx -g daemon off;
           ├─19286 nginx: worker process
           └─19287 nginx: worker process
lines 1-13/13 (END)

Podman网络

[root@localhost ~]# podman inspect nginx |grep -i ipaddr
            "IPAddress": "10.88.0.2",
                    "IPAddress": "10.88.0.2",

创建网络

[root@localhost ~]# podman network create newnetwork
/etc/cni/net.d/newnetwork.conflist
[root@localhost ~]# podman network ls
NETWORK ID    NAME        VERSION     PLUGINS
2f259bab93aa  podman      0.4.0       bridge,portmap,firewall,tuning
37c09853ac48  newnetwork  0.4.0       bridge,portmap,firewall,tuning

修改新生成的网络配置文件的子网和网关或者创建时使用–subnet 指定网段和子网掩码,–gateway指定网关

[root@localhost ~]# vim /etc/cni/net.d/newnetwork.conflist 
···
   "cniVersion": "0.4.0",
   "name": "newnetwork",
   "plugins": [
      {
         "type": "bridge",
         "bridge": "cni-podman1",
         "isGateway": true,
         "ipMasq": true,
         "hairpinMode": true,
         "ipam": {
            "type": "host-local",
            "routes": [
               {
                  "dst": "0.0.0.0/0"
               }
            ],
            "ranges": [
               [
                  {
                     "subnet": "10.89.0.0/24",
                     "gateway": "10.89.0.1"
                  }
               ]
            ]
         }
      },
     

修改/usr/share/containers/containers.conf文件设置默认网络为新创建的网络

[root@localhost ~]# vim /usr/share/containers/containers.conf 
#default_network = "podman"
default_network = "newnetwork"

创建容器

[root@localhost ~]# podman run -d --name web nginx

[root@localhost ~]# podman inspect web |grep -i ipaddr
            "IPAddress": "10.89.0.2",
                    "IPAddress": "10.89.0.2",

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:a2:2a:59 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.135/24 brd 192.168.8.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fea2:2a59/64 scope link 
       valid_lft forever preferred_lft forever
3: cni-podman0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 8e:aa:3f:8c:b2:49 brd ff:ff:ff:ff:ff:ff
    inet 10.88.0.1/16 brd 10.88.255.255 scope global cni-podman0
       valid_lft forever preferred_lft forever
    inet6 fe80::8caa:3fff:fe8c:b249/64 scope link 
       valid_lft forever preferred_lft forever
4: vethb1f1f1af@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master cni-podman0 state UP group default 
    link/ether 9a:f2:de:2c:de:83 brd ff:ff:ff:ff:ff:ff link-netns cni-1c72847f-e2e7-6b46-3775-ec3bcbd83b6c
    inet6 fe80::98f2:deff:fe2c:de83/64 scope link 
       valid_lft forever preferred_lft forever
5: cni-podman1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 22:ad:fe:39:10:a6 brd ff:ff:ff:ff:ff:ff
    inet 10.89.0.1/24 brd 10.89.0.255 scope global cni-podman1
       valid_lft forever preferred_lft forever
    inet6 fe80::20ad:feff:fe39:10a6/64 scope link 
       valid_lft forever preferred_lft forever
6: veth1811ed22@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master cni-podman1 state UP group default 
    link/ether 4a:3e:f5:e6:f1:04 brd ff:ff:ff:ff:ff:ff link-netns cni-003fba9d-b514-6b67-4b23-5927e4fd1f4e
    inet6 fe80::483e:f5ff:fee6:f104/64 scope link 
       valid_lft forever preferred_lft forever

当你希望Docker容器在主机动时自动重,并且每次都能随机分配端口映射,则可以利用特定的命令行选项来创建容器。下面详细介绍如何设置。 ### 使用 `docker run` 创建带自属性的容器 要让容器开机自并且随机端口映射,关键在于理解两个重要的参数: - **`--restart=always`** 或者其他相关的策略如 `on-failure`, 它们能确保当宿主系统重后 Docker 服务会尝试重新动该容器。 - **发布随机端口 `-p [hostInterface:]<containerPort>/[protocol]`**, 其中协议默认为TCP。如果省略了 `[hostPort]:` 部分,那么将会由操作系统选择一个空闲的高范围端口号作为外部访问地址的一部分;而内部则保持指定的服务监听端口不变。 例如,如果你有一个 Web 应用运行在一个为 "mywebapp" 的镜像里,默认占用80端口对外提供HTTP请求处理能力。你可以通过这样的命令把它跑起来并满足上述需求: ```bash $ docker run -d \ --name mywebapp_container \ --restart always \ -p 80/tcp \ mywebapp ``` 这里特别注意的是 `-p 80/tcp` 表示我们只绑定了容器内的80端口给任意可用的外网端口而不是固定的某一个具体的值(比如 `-p 8080:80`)。因此每当此容器因某种原因停止后再被拉起的时候,它就会获得一个新的临时性的公网入口点。 不过需要注意一点,在某些版本或配置下的 Docker 可能不能直接支持完全“纯”随机化绑定所有未指明的端口,所以有时你还是需要明确指出至少一侧(通常是内侧)的具体数值以便正确工作。 此外还有另一种更推荐的方式是在部署环境中使用 Compose 文件来管理复杂应用服务间的依赖关系及配置细节,这样可以让整个过程更为直观可控。 #### 示例:使用 Docker Compose 实现类似功能 首先编写一份简单的 docker-compose.yml 文件内容如下: ```yaml version: '3' services: web: image: nginx # 替换为你自己的image称 restart: unless-stopped # 更细粒度地控制重条件 ports: - "80" ``` 然后只需执行一次即可完成环境搭建包括设定好必要的生命周期规则以及端口转发机制: ```shell script $ docker-compose up -d ``` 这样做不仅简化了操作流程还提高了项目的可移植性团队协作效率! 最后提醒一下安全方面的问题——开放网络端口总是存在潜在风险,请务必谨慎评估是否真的有必要这么做并在必要时采取适当的防护措施诸如防火墙限制等手段保护你的应用程序免受不必要的威胁影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值