524.【kubernetes】修复 systemctl status sshd Failed to get D-Bus connection: Operation not permitted

想把容器当做虚拟机使用,通过k8s创建centos:7 时,准备看下sshd的状态结果报错如下:

[root@centos7-5f547bd9c4-9pt84 /]# systemctl status sshd
Failed to get D-Bus connection: Operation not permitted

研究发现,容器内部的root是个空壳子的root,说白了就是没有root权限的root,想要容器内不的root拥有root权限,必须加上--privileged=true 这个参数,

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations: {}
  labels:
    k8s.kuboard.cn/name: centos7
  name: centos7
  namespace: default
  resourceVersion: '3857415'
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.kuboard.cn/name: centos7
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        k8s.kuboard.cn/name: centos7
    spec:
      containers:
        - command:
            - /sbin/init
            - '-c'
            - '--'
          image: 'centos:7'
          imagePullPolicy: IfNotPresent
          name: centos7
          resources: {}
          securityContext:
            privileged: true
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

yum install -y openssh-server
systemctl restart sshd

加完之后,就可以使用systemd了,验证如下:

[root@centos7-564659f79d-nwrq6 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-12-12 06:45:09 UTC; 14s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 173 (sshd)
   CGroup: /system.slice/containerd.service/kubepods-besteffort-poda1ddf453_58fc_495b_ba4c_c94844522f3e.slice:cri-containerd:43b67802d92b3240d128d2d3a1ae05df781933d004f7db3fc815dfbb85d1d1c0/system.slice/sshd.service
           └─173 /usr/sbin/sshd -D
           ‣ 173 /usr/sbin/sshd -D

Dec 12 06:45:09 centos7-564659f79d-nwrq6 systemd[1]: Starting OpenSSH server daemon...
Dec 12 06:45:09 centos7-564659f79d-nwrq6 sshd[173]: Server listening on 0.0.0.0 port 22.
Dec 12 06:45:09 centos7-564659f79d-nwrq6 sshd[173]: Server listening on :: port 22.
Dec 12 06:45:09 centos7-564659f79d-nwrq6 systemd[1]: Started OpenSSH server daemon.

可以看到现在能正常使用systemd了。

你可能感兴趣的:(524.【kubernetes】修复 systemctl status sshd Failed to get D-Bus connection: Operation not permitted)