24. 2. immutableとは
immutable
Hello Web Page!!
This Page is v1
immutable
Developer
Webサーバーに対する変更を加えない、変更が不可能という考え方
root@web-server: # cp index.html /var/www/html/index.html
・・・
既存構成に対して
変更は行わない/行えない
25. 2. immutableとは
immutable
Hello Web Page!!
This Page is v1
Hello Web Page!!
This Page is v2
Developer
Webサーバーに対する変更を加えない、変更が不可能という考え方
変更を反映したサーバーを
新しく用意
immutable
immutable
新規作成
26. 2. immutableとは
immutable
Hello Web Page!!
This Page is v1
Hello Web Page!!
This Page is v2
置き換え
Developer
Webサーバーに対する変更を加えない、変更が不可能という考え方
既存のサーバーを新しい
サーバーに置き換える
immutable
immutable
27. 2. immutableとは
immutable
Hello Web Page!!
This Page is v1
Hello Web Page!!
This Page is v2
削除
※切り戻し用に残しておいても良い
Developer
Webサーバーに対する変更を加えない、変更が不可能という考え方
immutable
immutable
古いサーバーを削除
67. 5. コンテナのrootfsをReadOnlyにする
Kubernetesではコンテナの起動設定にてコンテナのrootfsをReadOnlyに設定することが可能です。
こうすることで、コンテナ内でのrootfsに対する書き込みを伴う操作が禁止され、
セキュリティレベルを高めることに繋がります。
apiVersion: v1
kind: Pod
metadata:
labels:
run: ubuntu-readonly
name: ubuntu-readonly
spec:
containers:
- image: ubuntu:20.04
name: ubuntu-readonly
command: ["/bin/sh", "-c", "while :; do sleep 10; done"]
securityContext:
readOnlyRootFilesystem: true
ubuntu-immutable.yml
root@ubuntu-readonly:/# apt-get install -y git
・・・
W: Not using locking for read only lock
fi
le /var/lib/dpkg/lock-frontend
W: Not using locking for read only lock
fi
le /var/lib/dpkg/lock
E: Unable to locate package git
root@ubuntu-readonly:/# cat <<EOF > /cmd
・・・
bash: cannot create temp
fi
le for here-document: Read-only
fi
le system
Container Host(Worker Node #1)
ubuntu-readonly
cmd
rootfs(/) process
bin
root
local/apache2/logs
usr
・
・
・
mount
rootfs(/)
overlayfs
ReadOnly
コンテナのrootfsをROに設定
Write
68. 5. コンテナのrootfsをReadOnlyにする
しかしながらコンテナの挙動によっては、rootfsへの書き込みが禁止されていると正常にコンテナが起動できない
場合もあります。httpdのようにコンテナプロセスがrootfs配下の特定ディレクトリにファイルを出力する動作をする場合は、
rootfsがReadOnlyだとコンテナを起動することができません。
apiVersion: v1
kind: Pod
metadata:
labels:
run: httpd-readonly
name: httpd-readonly
spec:
containers:
- image: httpd:2.4.51
name: httpd-readonly
securityContext:
readOnlyRootFilesystem: true
httpd-readonly.yml
››› kubectl get po httpd-readonly
NAME READY STATUS RESTARTS AGE
httpd-readonly 0/1 Error 4 (59s ago) 116s
››› kubectl logs httpd-readonly
AH00558: httpd: Could not reliably determine the server's fully quali
fi
ed domain name, using 10.0.233.55. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully quali
fi
ed domain name, using 10.0.233.55. Set the 'ServerName' directive globally to suppress this message
[Sat Nov 13 16:37:16.913895 2021] [core:error] [pid 1:tid 139972664941888] (30)Read-only
fi
le system: AH00099: could not create /usr/local/apache2/logs/httpd.pid.iuTncf
[Sat Nov 13 16:37:16.913965 2021] [core:error] [pid 1:tid 139972664941888] AH00100: httpd: could not log pid to
fi
le /usr/local/apache2/logs/httpd.pid
Container Host(Worker Node #1)
httpd-readonly
cmd
rootfs(/) process
bin
root
local/apache2/logs
usr
・
・
・
mount
ReadOnly
ReadOnly
コンテナのrootfsをROに設定
rootfs(/)
overlayfs
Write
Write
69. 5. コンテナのrootfsをReadOnlyにする
対策として、rootfsはReadOnlyとしつつコンテナプロセスが書き込みを行う必要があるディレクトリのみ
ReadWiteでVolume Mountを行うという方法が考えられます。
以下ではコンテナ起動時に対象ディレクトリに対してemptyDir(一時的なボリューム)を作成&マウントしています。
apiVersion: v1
kind: Pod
metadata:
labels:
run: httpd-readonly
name: httpd-readonly
spec:
containers:
- image: httpd:2.4.51
name: httpd-readonly
securityContext:
readOnlyRootFilesystem: true
volumeMounts:
- name: log-volume
mountPath: /usr/local/apache2/logs/
volumes:
- name: log-volume
emptyDir: {}
››› kubectl get po httpd-readonly
NAME READY STATUS RESTARTS AGE
httpd-readonly 1/1 Running 0 2m33s
Container Host(Worker Node #1)
httpd-readonly
cmd
rootfs(/) process
bin
root
local/apache2/logs
usr
・
・
・
mount
log-volume
mount
ReadOnly
ReadWriteで
Volume Mount
httpd-readonly.yml
コンテナのrootfsをROに設定
書き込みが必要が領域を
emptyDirにマウント
rootfs(/)
overlayfs
Write