{"id":220597,"date":"2021-06-24T09:06:00","date_gmt":"2021-06-24T01:06:00","guid":{"rendered":"https:\/\/gulass.cn\/?p=220597"},"modified":"2021-06-15T09:06:50","modified_gmt":"2021-06-15T01:06:50","slug":"kubernetes-rbac-role","status":"publish","type":"post","link":"https:\/\/gulass.cn\/kubernetes-rbac-role.html","title":{"rendered":"\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u3001\u6388\u6743\u64cd\u4f5ck8s\u96c6\u7fa4\u7684\u8fc7\u7a0b?"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u7bc7\u5e26\u7ed9\u5927\u5bb6\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u3001\u6388\u6743\u64cd\u4f5ck8s\u96c6\u7fa4\u7684\u8fc7\u7a0b\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u80cc\u666f<\/strong><\/div>\n

172.16.99.128\u662f\u7684\u6211k8s\u96c6\u7fa4\u7684master\u8282\u70b9\uff0c\u6b64\u5904\u662f\u4ece\u8fd9\u91cc\u83b7\u53d6\u96c6\u7fa4\u7684\u8bc1\u4e66\u3002<\/p>\n

\u521b\u5efa\u8bbf\u95eearchitechure\u547d\u540d\u7a7a\u95f4\u7684\u7528\u6237<\/strong><\/div>\n
1.\u7ed9\u7528\u6237devops \u521b\u5efa\u4e00\u4e2a\u79c1\u94a5<\/strong><\/span><\/div>\n
openssl\u00a0genrsa\u00a0-out\u00a0devops.key\u00a02048<\/pre>\n
2.\u4f7f\u7528\u6211\u4eec\u521a\u521a\u521b\u5efa\u7684\u79c1\u94a5\u521b\u5efa\u4e00\u4e2a\u8bc1\u4e66\u7b7e\u540d\u8bf7\u6c42\u6587\u4ef6\uff1adevops.csr\uff0c\u8981\u6ce8\u610f\u9700\u8981\u786e\u4fdd\u5728-subj\u53c2\u6570\u4e2d\u6307\u5b9a\u7528\u6237\u540d\u548c\u7ec4(CN\u8868\u793a\u7528\u6237\u540d\uff0cO\u8868\u793a\u7ec4)<\/strong><\/span><\/div>\n
openssl\u00a0req\u00a0-new\u00a0-key\u00a0devops.key\u00a0-out\u00a0devops.csr\u00a0-subj\u00a0\"\/CN=devops\/O=architechure\"<\/pre>\n
3.\u7136\u540e\u627e\u5230\u6211\u4eec\u7684Kubernetes\u96c6\u7fa4\u7684CA<\/strong><\/span><\/div>\n

\u6211\u4eec\u4f7f\u7528\u7684\u662fkubeadm\u5b89\u88c5\u7684\u96c6\u7fa4\uff0cCA\u76f8\u5173\u8bc1\u4e66\u4f4d\u4e8e\/etc\/kubernetes\/pki\/\u76ee\u5f55\u4e0b\u9762\uff0c\u5982\u679c\u4f60\u662f\u4e8c\u8fdb\u5236\u65b9\u5f0f\u642d\u5efa\u7684\uff0c\u4f60\u5e94\u8be5\u5728\u6700\u5f00\u59cb\u642d\u5efa\u96c6\u7fa4\u7684\u65f6\u5019\u5c31\u5df2\u7ecf\u6307\u5b9a\u597d\u4e86CA\u7684\u76ee\u5f55\uff0c\u6211\u4eec\u4f1a\u5229\u7528\u8be5\u76ee\u5f55\u4e0b\u9762\u7684ca.crt\u548cca.key\u4e24\u4e2a\u6587\u4ef6\u6765\u6279\u51c6\u4e0a\u9762\u7684\u8bc1\u4e66\u8bf7\u6c42,\u751f\u6210\u6700\u7ec8\u7684\u8bc1\u4e66\u6587\u4ef6\uff0c\u6211\u4eec\u8fd9\u91cc\u8bbe\u7f6e\u8bc1\u4e66\u7684\u6709\u6548\u671f\u4e3a500\u5929<\/p>\n

scp\u00a0root@172.16.99.128:\/etc\/kubernetes\/pki\/ca.crt\u00a0.\r\nscp\u00a0root@172.16.99.128:\/etc\/kubernetes\/pki\/ca.key\u00a0.\r\nopenssl\u00a0x509\u00a0-req\u00a0-in\u00a0devops.csr\u00a0-CA\u00a0.\/ca.crt\u00a0-CAkey\u00a0.\/ca.key\u00a0-CAcreateserial\u00a0-out\u00a0devops.crt\u00a0-days\u00a0500\r\n\u279c\u00a0\u00a0ls\u00a0-al\r\ntotal\u00a072\r\ndrwxr-xr-x\u00a0\u00a011\u00a0marion\u00a0\u00a0staff\u00a0\u00a0\u00a0352\u00a0Dec\u00a025\u00a011:32\u00a0.\r\ndrwxr-xr-x\u00a0\u00a013\u00a0marion\u00a0\u00a0staff\u00a0\u00a0\u00a0416\u00a0Dec\u00a025\u00a011:26\u00a0..\r\n-rw-r--r--\u00a0\u00a0\u00a01\u00a0marion\u00a0\u00a0staff\u00a0\u00a0\u00a0\u00a017\u00a0Dec\u00a025\u00a011:32\u00a0.srl\r\n-rw-r--r--\u00a0\u00a0\u00a01\u00a0marion\u00a0\u00a0staff\u00a0\u00a01156\u00a0Dec\u00a025\u00a011:32\u00a0README.md\r\n-rw-r--r--\u00a0\u00a0\u00a01\u00a0marion\u00a0\u00a0staff\u00a0\u00a01025\u00a0Dec\u00a025\u00a011:30\u00a0ca.crt\r\n-rw-------\u00a0\u00a0\u00a01\u00a0marion\u00a0\u00a0staff\u00a0\u00a01675\u00a0Dec\u00a025\u00a011:30\u00a0ca.key\r\n-rw-r--r--\u00a0\u00a0\u00a01\u00a0marion\u00a0\u00a0staff\u00a0\u00a01009\u00a0Dec\u00a025\u00a011:32\u00a0devops.crt\r\n-rw-r--r--\u00a0\u00a0\u00a01\u00a0marion\u00a0\u00a0staff\u00a0\u00a0\u00a0924\u00a0Dec\u00a025\u00a011:30\u00a0devops.csr\r\n-rw-r--r--\u00a0\u00a0\u00a01\u00a0marion\u00a0\u00a0staff\u00a0\u00a01679\u00a0Dec\u00a025\u00a011:27\u00a0devops.key<\/pre>\n
4.\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u521a\u521a\u521b\u5efa\u7684\u8bc1\u4e66\u6587\u4ef6\u548c\u79c1\u94a5\u6587\u4ef6\u5728\u96c6\u7fa4\u4e2d\u521b\u5efa\u65b0\u7684\u51ed\u8bc1:<\/strong><\/span><\/div>\n
kubectl\u00a0config\u00a0set-credentials\u00a0devops\u00a0--client-certificate=devops.crt\u00a0\u00a0--client-key=devops.key<\/pre>\n
5.\u901a\u8fc7\u521a\u624d\u521b\u5efa\u7684\u7528\u6237\u51ed\u8bc1\u521b\u5efa\u65b0\u7684\u4e0a\u4e0b\u6587(Context)<\/strong><\/span><\/div>\n
kubectl\u00a0config\u00a0set-context\u00a0devops-context\u00a0--cluster=cluster-tf26gt9mmk\u00a0--namespace=architechure\u00a0--user=devops<\/pre>\n
6.\u5c1d\u8bd5\u901a\u8fc7\u8be5\u7528\u6237\u64cd\u4f5c\u547d\u4ee4<\/strong><\/span><\/div>\n
\u279c\u00a0\u00a0kubectl\u00a0get\u00a0pods\u00a0--context=devops-context\r\nError\u00a0from\u00a0server\u00a0(Forbidden):\u00a0pods\u00a0is\u00a0forbidden:\u00a0User\u00a0\"devops\"\u00a0cannot\u00a0list\u00a0resource\u00a0\"pods\"\u00a0in\u00a0API\u00a0group\u00a0\"\"\u00a0in\u00a0the\u00a0namespace\u00a0\"architechure\"\u00a0#\u00a0\u56e0\u4e3a\u8be5devops-context\u8fd8\u6ca1\u6709\u64cd\u4f5cAPI\u7684\u6743\u9650<\/pre>\n
7.\u7ed9\u7528\u6237\u521b\u5efa\u4e00\u4e2arole\u7684\u89d2\u8272devops.role.yaml<\/strong><\/span><\/div>\n
apiVersion:\u00a0rbac.authorization.k8s.io\/v1\r\nkind:\u00a0Role\r\nmetadata:\r\n\u00a0\u00a0name:\u00a0devops-role\r\n\u00a0\u00a0namespace:\u00a0architechure\r\nrules:\r\n-\u00a0apiGroups:\u00a0[\"\",\u00a0\"extensions\",\u00a0\"apps\"]\r\n\u00a0\u00a0resources:\u00a0[\"deployments\",\u00a0\"replicasets\",\u00a0\"pods\"]\r\n\u00a0\u00a0verbs:\u00a0[\"get\",\u00a0\"list\",\u00a0\"watch\",\u00a0\"create\",\u00a0\"update\",\u00a0\"patch\",\u00a0\"delete\"]\u00a0#\u00a0\u4e5f\u53ef\u4ee5\u4f7f\u7528['*']<\/pre>\n

\u7136\u540e\u5728\u96c6\u7fa4\u4e2d\u521b\u5efa\u8be5\u89d2\u8272<\/p>\n

kubectl\u00a0apply\u00a0-f\u00a0.\/devops.role.yaml<\/pre>\n
8.\u521b\u5efa\u6743\u9650\u4e0e\u89d2\u8272\u4e4b\u95f4\u7684\u7ed1\u5b9a\u5173\u7cfbdevops-rolebinding.yaml<\/strong><\/span><\/div>\n
apiVersion:\u00a0rbac.authorization.k8s.io\/v1\r\nkind:\u00a0RoleBinding\r\nmetadata:\r\n\u00a0\u00a0name:\u00a0devops-rolebinding\r\n\u00a0\u00a0namespace:\u00a0architechure\r\nsubjects:\r\n-\u00a0kind:\u00a0User\r\n\u00a0\u00a0name:\u00a0devops\r\n\u00a0\u00a0apiGroup:\u00a0\"\"\r\nroleRef:\r\n\u00a0\u00a0kind:\u00a0Role\r\n\u00a0\u00a0name:\u00a0devops-role\u00a0#\u00a0\u4e0a\u4e00\u6b65\u521b\u5efa\u7684devops-role\u5b9e\u4f53\r\n\u00a0\u00a0apiGroup:\u00a0\"\"<\/pre>\n

\u5728\u96c6\u7fa4\u4e2d\u521b\u5efa\u89d2\u8272\u4e0e\u7528\u6237\u4e4b\u95f4\u7684\u7ed1\u5b9a\u5173\u7cfb<\/p>\n

k\u00a0apply\u00a0-f\u00a0.\/devops-rolebinding.yaml<\/pre>\n
9.\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7kubecm\u5207\u6362\u5230\u8be5\u89d2\u8272\u4e0a<\/strong><\/span><\/div>\n

\"\"<\/p>\n

\u6b64\u65f6\uff0c\u4ece\u4e0b\u56fe\u5c31\u53ef\u4ee5\u67e5\u770b\u5230\u5f53\u524d\u96c6\u7fa4\u7684\u6709\u4e00\u4e2a\u65b0\u7684\u7528\u6237\u89d2\u8272devops\uff0c\u4e0a\u9762\u7528\u5230\u7684Kubecm\u6211\u4eec\u4e4b\u524d\u4e5f\u5206\u4eab\u8fc7\uff0c\u5982\u679c\u9700\u8981\u53ef\u4ee5\u70b9\u6b64\u8df3\u8f6c<\/p>\n

\"\"<\/p>\n

10.\u6743\u9650\u9a8c\u8bc1<\/strong><\/span><\/div>\n
>\u00a0kubectl\u00a0get\u00a0pods\r\nNo\u00a0resources\u00a0found\u00a0in\u00a0architechure\u00a0namespace.\r\n>\u00a0kubectl\u00a0get\u00a0replicasets\r\nNo\u00a0resources\u00a0found\u00a0in\u00a0architechure\u00a0namespace.\r\n>\u00a0kubectl\u00a0get\u00a0deploy\r\nNo\u00a0resources\u00a0found\u00a0in\u00a0architechure\u00a0namespace.\r\n>\u00a0kubectl\u00a0get\u00a0svc\r\nError\u00a0from\u00a0server\u00a0(Forbidden):\u00a0services\u00a0is\u00a0forbidden:\u00a0User\u00a0\"devops\"\u00a0cannot\u00a0list\u00a0resource\u00a0\"services\"\u00a0in\u00a0API\u00a0group\u00a0\"\"\u00a0in\u00a0the\u00a0namespace\u00a0\"architechure\"<\/pre>\n

\u603b\u7ed3\u4e00\u4e0b\u5c31\u662f\uff1a<\/p>\n

    \n
  • \u6839\u636e\u96c6\u7fa4\u7684CA\u8bc1\u4e66\u521b\u5efa\u51fa\u6765\u7528\u6237\u8bc1\u4e66<\/li>\n
  • \u6839\u636e\u7528\u6237\u8bc1\u4e66\u521b\u5efa\u8be5\u7528\u6237\u5728\u96c6\u7fa4\u5185\u7684\u51ed\u8bc1\u548c\u4e0a\u4e0b\u6587\u5185\u5bb9<\/li>\n
  • \u8981\u60f3\u7528\u6237\u80fd\u8fdb\u884c\u57fa\u672c\u7684\u64cd\u4f5c\uff0c\u9700\u8981\u5bf9\u7528\u6237\u9488\u5bf9apiGroup\u6388\u6743<\/li>\n<\/ul>\n
    \u4e3adevops\u7528\u6237\u589e\u52a0\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u7684\u6743\u9650<\/strong><\/div>\n
    1.\u6211\u4eec\u5148\u628a\u5f53\u524d\u4e0a\u4e0b\u6587\u5207\u6362\u5230\u4e4b\u524d\u6709\u6743\u9650\u64cd\u4f5c\u7684user-tf26gt9mmk\u7528\u6237\u4e0a<\/strong><\/span><\/div>\n
    kubecm\u00a0switch\r\n#\u00a0select\u00a0dev<\/pre>\n

    \u5426\u5219\u4ee5\u4e0b\u6b65\u9aa4\u4f1a\u51fa\u9519\uff1a<\/p>\n

    2.\u9996\u5148\u9700\u8981\u521b\u5efa\u9488\u5bf9\u6307\u5b9a\u547d\u540d\u7a7a\u95f4\u7684\u4e0a\u4e0b\u6587<\/strong><\/span><\/div>\n
    kubectl\u00a0config\u00a0set-context\u00a0devops-context\u00a0--cluster=cluster-tf26gt9mmk\u00a0--namespace=default\u00a0--user=devops<\/pre>\n

    \u6b64\u65f6\u67e5\u8be2\u5217\u4e3edefault\u7a7a\u95f4\u4e0b\u7684pods\u662f\u4e0d\u884c\u7684\uff0c\u56e0\u4e3a\u8fd8\u6ca1\u5141\u8bb8\u64cd\u4f5c<\/p>\n

    kubectl\u00a0get\u00a0pods\u00a0--context=devops-context\r\nError\u00a0from\u00a0server\u00a0(Forbidden):\u00a0pods\u00a0is\u00a0forbidden:\u00a0User\u00a0\"devops\"\u00a0cannot\u00a0list\u00a0resource\u00a0\"pods\"\u00a0in\u00a0API\u00a0group\u00a0\"\"\u00a0in\u00a0the\u00a0namespace\u00a0\"default\"<\/pre>\n
    3.\u521b\u5efadefault\u7a7a\u95f4\u4e0b\u7684role\u4e0erolebinding<\/strong><\/span><\/div>\n

    devops-role-default.yaml<\/p>\n

    apiVersion:\u00a0rbac.authorization.k8s.io\/v1\r\nkind:\u00a0Role\r\nmetadata:\r\n\u00a0\u00a0name:\u00a0devops-role\r\n\u00a0\u00a0namespace:\u00a0default\r\nrules:\r\n-\u00a0apiGroups:\u00a0[\"\",\u00a0\"extensions\",\u00a0\"apps\"]\r\n\u00a0\u00a0resources:\u00a0[\"deployments\",\u00a0\"replicasets\",\u00a0\"pods\"]\r\n\u00a0\u00a0verbs:\u00a0[\"get\",\u00a0\"list\",\u00a0\"watch\",\u00a0\"create\",\u00a0\"update\",\u00a0\"patch\",\u00a0\"delete\"]\u00a0#\u00a0\u4e5f\u53ef\u4ee5\u4f7f\u7528['*']<\/pre>\n

    devops-rolebinding-default.yaml<\/p>\n

    apiVersion:\u00a0rbac.authorization.k8s.io\/v1\r\nkind:\u00a0RoleBinding\r\nmetadata:\r\n\u00a0\u00a0name:\u00a0devops-rolebinding\r\n\u00a0\u00a0namespace:\u00a0default\r\nsubjects:\r\n-\u00a0kind:\u00a0User\r\n\u00a0\u00a0name:\u00a0devops\r\n\u00a0\u00a0apiGroup:\u00a0\"\"\r\nroleRef:\r\n\u00a0\u00a0kind:\u00a0Role\r\n\u00a0\u00a0name:\u00a0devops-role\r\n\u00a0\u00a0apiGroup:\u00a0\"\"<\/pre>\n

    \u7136\u540e\u6211\u4eec\u5728\u96c6\u7fa4\u4e2d\u521b\u5efa\u8fd9\u4e24\u4e2a\u5bf9\u8c61<\/p>\n

    kubectl\u00a0apply\u00a0-f\u00a0devops-role-default.yaml\r\nkubectl\u00a0apply\u00a0-f\u00a0devops-rolebinding-default.yaml<\/pre>\n
    4.\u67e5\u770brole\u8d44\u6e90\u5bf9\u8c61\u662f\u5426\u521b\u5efa<\/strong><\/span><\/div>\n
    kubectl\u00a0get\u00a0role\u00a0-A\u00a0|grep\u00a0devops-role\u00a0#\u00a0\u5206\u522b\u5728architechure\u548cdefault\u547d\u540d\u7a7a\u95f4\u4e0b\r\narchitechure\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0devops-role\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02021-05-17T07:57:27Z\r\ndefault\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0devops-role\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02021-05-28T03:19:24Z<\/pre>\n
    5.\u5207\u6362\u5f53\u524d\u4e0a\u4e0b\u6587\u73af\u5883\uff0c\u9a8c\u8bc1\u662f\u5426\u53ef\u4ee5\u64cd\u4f5c\u8d44\u6e90<\/strong><\/span><\/div>\n
    kubecm\u00a0switch\r\n#\u00a0select\u00a0devops-context\r\nkubectl\u00a0get\u00a0pods\u00a0-n\u00a0default\r\nkubectl\u00a0get\u00a0pods\u00a0-n\u00a0architechure<\/pre>\n

    \u5230\u8fd9\u91cc\u5c31\u57fa\u672c\u4e0a\u8bf4\u6e05\u695a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u3001\u6388\u6743\u64cd\u4f5ck8s\u96c6\u7fa4\u7684\u8fc7\u7a0b\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    172.16.99.128\u662f\u7684\u6211k8s\u96c6\u7fa4\u7684master\u8282\u70b9\uff0c\u6b64\u5904\u662f\u4ece\u8fd9\u91cc\u83b7\u53d6\u96c6\u7fa4\u7684\u8bc1\u4e66\u3002 openssl\u00a0g […]<\/p>\n","protected":false},"author":323,"featured_media":84231,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-220597","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/220597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=220597"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/220597\/revisions"}],"predecessor-version":[{"id":220602,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/220597\/revisions\/220602"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/84231"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=220597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=220597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=220597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}