现象
我在 Fedora CoreOS
上安装 kubernetes
进行到创建 kubelet
服务设置开机启动时执行 systemctl enable kubelet
结果出现错误:
[core@localhost ~]$ sudo systemctl enable /usr/local/lib/systemd/system/kubelet.service
Failed to enable unit: Access denied
我勒个去!这搞毛啊。我可是 root
好不好。你他娘的不想活了,root
都不让执行,信不信我重装了你!!!但是不死心的在网上查了查,发现有解决方案,下面就是我查到的方案。
解决方案
在网上找到了这篇文章: https://superuser.com/questions/1125250/systemctl-access-denied-when-root
文章显示,权限拒绝和SELinux有关。如果在enforcing模式下运行SELinux,可能就会出现这种情况。
既然是 SELinux 那就好办了,两种解决办法。
1. 最简单粗暴,关了丫的:
setenforce 0
2. 但是做技术嘛,也不能一直这么简单粗暴了事。毕竟还是要学习的嘛~~~
那么既然是 SELinux 相关,那有可能和文件安全上下文相关,查一下系统的和自己的有啥区别:
[core@localhost ~]$ ll -Z /etc/systemd/system/kubelet.service
-rw-r--r--. 1 core core unconfined_u:object_r:user_home_t:s0 289 Jun 6 08:28 /etc/systemd/system/kubelet.service
[core@localhost ~]$ ll -Z /usr/lib/systemd/system/docker.service
-rw-r--r--. 2 root root system_u:object_r:container_unit_file_t:s0 1314 Jan 1 1970 /usr/lib/systemd/system/docker.service
嗯,果真有专用上下文,那改一下试试:
[core@localhost ~]$ sudo chcon -Ru system_u -t container_unit_file_t /etc/systemd/system/kubelet.service
[core@localhost ~]$ ll -Z /etc/systemd/system/kubelet.service
-rw-r--r--. 1 core core system_u:object_r:container_unit_file_t:s0 289 Jun 6 08:28 /etc/systemd/system/kubelet.service
来,执行以下看还报错不。
[core@localhost ~]$ sudo systemctl enable kubelet.service
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /etc/systemd/system/kubelet.service.
呕吼,搞定了。看来果然是文件安全上下文导致哪怕你是 root
也不让你操作某些服务文件。
参考文档: