524. 【kubernetes】修复-systemctl-status-sshd-Failed-to-get-D-Bus-connectio

文章讲述了在Kubernetes(K8s)环境下,由于容器内的root用户权限限制,无法直接运行systemd服务如sshd。解决方法是通过在Deployment配置中添加`privileged:true`参数,使容器以特权模式运行,从而允许安装并启动sshd服务。添加参数后,成功启动并验证了sshd服务的状态。
摘要由CSDN通过智能技术生成

想把容器当做虚拟机使用,通过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了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值