How to create photon OS container images via photon-os-container-builder and use ?
Photon OS, a lightweight Linux distribution created and maintained by VMware, is designed specifically to run as a container host and has been optimised for cloud-native applications and cloud platforms, and has been optimised to run on VMware infrastructure and in public clouds. Many VMware components, such as vCenter Server Appliance (VCSA), use it as a base OS. It's freely available, easy to install.
The project photon-os-container-builder allows to quickly create photon os container images and run it as a systemd service via systemd-nspawn. cntrctl?spawns Photon OS image as a light-weight container. Photon OS package manager?tdnf?integrated with?cntrctl. Hence it allows to prepare a root fs consisting packages depending on the user choice. It automatically prepares the root fs and boots into the container quickly. VMDK images can be automatically deployed via?cntrctl?and tested.
Let's get started .
? sudo cat /etc/os-release
NAME="VMware Photon OS"
VERSION="4.0"
ID=photon
VERSION_ID=4.0
PRETTY_NAME="VMware Photon OS/Linux"
ANSI_COLOR="1;34"
HOME_URL="https://vmware.github.io/photon/"
BUG_REPORT_URL="https://github.com/vmware/photon/issues"
? sudo tdnf install systemd-container -y
Installing:
systemd-container? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x86_64? ? ? ? ? ? ? ? ? ? ?5.10-1.ph4? ? ? ? ? ? ? ? ? ? ? ? ? photon-release? ? ? ? ? ? ? ? ?1.65M 1728537
Total installed size:? ?1.65M 1728537
Downloading:
systemd-container? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 478107 100%
Testing transaction
Running transaction
Installing/Updating: systemd-container-247.11-4.ph4
Complete!
? git clone https://github.com/vmware-samples/photon-os-container-builder.gi
Cloning into 'photon-os-container-builder'...
remote: Enumerating objects: 1732, done.
remote: Counting objects: 100% (216/216), done.
remote: Compressing objects: 100% (145/145), done.
remote: Total 1732 (delta 77), reused 173 (delta 55), pack-reused 1516
Receiving objects: 100% (1732/1732), 2.74 MiB | 7.87 MiB/s, done.
Resolving deltas: 100% (730/730), done.t
? cd photon-os-container-builder
? make build
mkdir -p bin
go build -ldflags="-X 'main.buildVersion=753a909 (2022-11-27 19:43:44 +0530)' -X 'main.buildDate=2022-11-27 14:23:38'" -o bin/cntrctl ./cmd/cntrctl
? sudo make install
install bin/cntrctl /usr/bin/
ln -sf /usr/bin/cntrctl /usr/bin/containerctl
install -vdm 755 /etc/photon-os-container
install -m 755 distribution/photon-os-container.toml /etc/photon-os-container
install -m 0644 distribution/photon-os-container.service /lib/systemd/system/
systemctl daemon-reload
> ? sudo cntrctl spawn photon4
Refreshing metadata for: 'VMware Photon Linux 4.0 (x86_64)'
Refreshing metadata for: 'VMware Photon Linux 4.0 (x86_64) Updates'
Refreshing metadata for: 'VMware Photon Extras 4.0 (x86_64)'
Installing:
filesystem x86_64 1.1-4.ph4 photon-release 7.19k 7363
Total installed size: 7.19k 7363
Downloading:
Testing transaction
Running transaction
Installing/Updating: filesystem-1.1-4.ph4.x86_64
Complete!
> ? sudo cntrctl dir photon4
Spawning container photon4 on /var/lib/machines/photon4.
Press ^] three times within 1s to kill container.
root@photon4 [ ~ ]# passwd
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: password updated successfully
> ? sudo cntrctl boot photon4
Spawning container photon4 on /var/lib/machines/photon4.
Press ^] three times within 1s to kill container.
systemd v247.11-4.ph4 running in system mode. (+PAM -AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP -LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +ZSTD +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 -IDN -PCRE2 default-hierarchy=hybrid)
Detected virtualization systemd-nspawn.
Detected architecture x86-64.
[ OK ] Finished Permit User Sessions.
[ OK ] Started Console Getty.
[ OK ] Reached target Login Prompts.
[ OK ] Started Network Service.
[ OK ] Reached target Multi-User System.
Starting Update UTMP about System Runlevel Changes...
[ OK ] Finished Update UTMP about System Runlevel Changes.
[ OK ] Started OpenSSH Daemon.
Welcome to Photon 4.0 (x86_64) - Kernel 5.10.142-2.ph4 (console)
photon4 login:
? sudo cntrctl start photon4
? sudo systemctl status photon4
● photon4.service - Photon OS container photon4
● photon4.service - Photon OS container
Loaded: loaded (8;;file://zeus/usr/lib/systemd/system/photon4.12.service^G/usr/lib/systemd/system/photon4.12.service8;;^G; disabled; preset: enabled)
Active: active (running) since Sun 2022-11-27 13:16:28 UTC; 16s ago
Docs: 8;;man:cntrctl(1)^Gman:cntrctl(1)8;;^G
Main PID: 194027 (systemd-nspawn)
Status: "Container running: Startup finished in 4.458s."
Tasks: 1 (limit: 16384)
Memory: 1.1M
CGroup: /machine.slice/photon4.12.service
└─194027 /usr/bin/systemd-nspawn --capability=all -bD /var/lib/machines/photon4.12 --link-journal=try-guest -M
Nov 27 13:16:32 zeus cntrctl[194027]: ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBOql3dIN0eJ/4NLKSaOV7aPc9luOtpDcRWs5xs9+13vS8qVR6XIBshv3TwmUu+8NP+>
Nov 27 13:16:32 zeus cntrctl[194027]: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOq6/QRG07DMxIzy/7/gTB0hsdJfNP5FVZyvyO5agJyq root@photon4
Nov 27 13:16:32 zeus cntrctl[194027]: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDSE0byx3ZW75bAdhKNUohEBppQuxjWzQSoFTvZ9K6tfnoyV+qsFgh3nViT+XJWoE6fONNpdWRolNWYiyhRiu1JIsITQZTVbIM5kXN>
Nov 27 13:16:32 zeus cntrctl[194027]: -----END SSH HOST KEY KEYS-----
Nov 27 13:16:32 zeus cntrctl[194027]: [ OK ] Finished Execute cloud user/final scripts.
Nov 27 13:16:32 zeus cntrctl[194027]: [ OK ] Reached target Cloud-init target.
Nov 27 13:16:32 zeus cntrctl[194027]: [ OK ] Stopped OpenSSH Daemon.
Nov 27 13:16:32 zeus cntrctl[194027]: [ OK ] Started OpenSSH Daemon.
Nov 27 13:16:33 zeus cntrctl[194027]:
Nov 27 13:16:33 zeus cntrctl[194027]: Welcome to Photon 4.0 (x86_64) - Kernel 5.10.142-2.ph4 (console)
? sudo machinectl login photon4
Connected to machine photon4. Press ^] three times within 1s to exit session.
Welcome to Photon 4.0 (x86_64) - Kernel 5.10.142-2.ph4 (pts/1)
photon4 login: root
Password:
root@photon4 [ ~ ]#
? cntrctl spawn --network macvlan --link eth0 ph4-macvlan
? systemctl start ph4-macvlan
? systemctl status ph4-macvlan
● ph4-macvlan.service - Photon OS container
Loaded: loaded (8;;file://zeus/usr/lib/systemd/system/ph4-macvlan.service^G/usr/lib/systemd/system/ph4-macvlan.service8;;^G; disabled; preset: enabled)
Active: active (running) since Sun 2022-11-27 14:04:29 UTC; 6s ago
Docs: 8;;man:cntrctl(1)^Gman:cntrctl(1)8;;^G
Main PID: 2818 (systemd-nspawn)
Status: "Container running: Startup finished in 4.307s."
Tasks: 1 (limit: 16384)
Memory: 1.2M
CGroup: /machine.slice/ph4-macvlan.service
└─2818 /usr/bin/systemd-nspawn --capability=all -bD /var/lib/machines/ph4-macvlan --network-macvlan=eth0 --link-journal=try-guest -M
Nov 27 14:04:33 zeus cntrctl[2818]: [ OK ] Reached target Login Prompts.
Nov 27 14:04:33 zeus cntrctl[2818]: [ OK ] Reached target Multi-User System.
Nov 27 14:04:33 zeus cntrctl[2818]: Starting Update UTMP about System Runlevel Changes...
Nov 27 14:04:33 zeus cntrctl[2818]: [ OK ] Finished Update UTMP about System Runlevel Changes.
Nov 27 14:04:33 zeus cntrctl[2818]: [ OK ] Finished Apply the settings specified in cloud-config.
Nov 27 14:04:33 zeus cntrctl[2818]: Starting Execute cloud user/final scripts...
Nov 27 14:04:34 zeus cntrctl[2818]: [ OK ] Finished Execute cloud user/final scripts.
Nov 27 14:04:34 zeus cntrctl[2818]: [ OK ] Reached target Cloud-init target.
Nov 27 14:04:35 zeus cntrctl[2818]:
Nov 27 14:04:35 zeus cntrctl[2818]: Welcome to Photon 4.0 (x86_64) - Kernel 5.10.152-3.ph4 (console)
? machinectl login ph4-macvlan
Connected to machine ph4-macvlan. Press ^] three times within 1s to exit session.
Welcome to Photon 4.0 (x86_64) - Kernel 5.10.152-3.ph4 (pts/1)
ph4-macvlan login: root
Password:
root@ph4-macvlan [ ~ ]# 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: mv-eth0@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 16:d3:88:53:c7:be brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.103.177/23 brd 192.168.103.255 scope global dynamic mv-eth0
valid_lft 7174sec preferred_lft 7174sec
inet6 fe80::14d3:88ff:fe53:c7be/64 scope link
valid_lft forever preferred_lft forever
See `cntrctl --help` for more use cases.
Software Engineer- IBM Software Labs- IBM Cloud- IaaS
2 年very nice Sahani.