您的位置:首页 > 新闻 > 资讯 > 资源网站推荐几个_郑州seo关键词推广_巨量广告投放平台_上海最新疫情

资源网站推荐几个_郑州seo关键词推广_巨量广告投放平台_上海最新疫情

2025/1/16 21:56:03 来源:https://blog.csdn.net/hotpotbo/article/details/145116440  浏览:    关键词:资源网站推荐几个_郑州seo关键词推广_巨量广告投放平台_上海最新疫情
资源网站推荐几个_郑州seo关键词推广_巨量广告投放平台_上海最新疫情

<div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-704d5b9767.css">
             
                <div id="content_views" class="htmledit_views">
                    <h2><a name="t0"></a>一、RBAC概述</h2> 
<h3><a name="t1"></a>1,<a href="https://so.csdn.net/so/search?q=k8s&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=k8s&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;k8s\&quot;}&quot;}" data-tit="k8s" data-pretit="k8s">k8s</a>集群的交互逻辑(简单了解)</h3> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 我们通过k8s各<span class="edu-hl hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;组件\&quot;}&quot;}" data-tit="组件" data-pretit="组件">组件</span>架构,知道各个组件之间是使用https进行<a href="https://so.csdn.net/so/search?q=%E6%95%B0%E6%8D%AE%E5%8A%A0%E5%AF%86&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=%E6%95%B0%E6%8D%AE%E5%8A%A0%E5%AF%86&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;数据加密\&quot;}&quot;}" data-tit="数据加密" data-pretit="数据加密">数据加密</a>及交互的,那么同理,我们作为“使用”k8s的各种资源的使用者,也是通过https进行数据加密的;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; k8s通过我们家目录下的证书来判断我们是谁?通过证书内容来认定我们的权限;</p> 
<p><img alt="" height="608" src="https://i-blog.csdnimg.cn/blog_migrate/4b1b4357756dbd15654d20a84564bd08.png" width="1200"></p> 
<p>用户证书的位置</p> 
<blockquote> 
 <p>[root@k8s231 ~]# ll -a .kube/config&nbsp;<br> -rw------- 1 root root 5634 Jan &nbsp;1 19:40 .kube/config</p> 
</blockquote> 
<h3><a name="t2"></a>2,k8s的安全架构(简单了解)</h3> 
<p><img alt="" height="462" src="https://i-blog.csdnimg.cn/blog_migrate/12e790cf4796f4df1e431304335837db.png" width="1200"></p> 
<h3><a name="t3"></a>3,RBAC用户授权的逻辑(重要)</h3> 
<p><img alt="" height="474" src="https://i-blog.csdnimg.cn/blog_migrate/a398d2fd5a4ea10898225e6d745fed1d.png" width="1200"></p> 
<p>用户/主题<a href="https://so.csdn.net/so/search?q=Topic&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=Topic&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;Topic\&quot;}&quot;}" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=Topic&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;Topic\&quot;}&quot;}" data-tit="Topic" data-pretit="topic">Topic</a></p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 1,User</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 2,SerciceAccount</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 3,Group</p> 
<p>用户角色:</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 1,Role:局部资源角色</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 2,ClusterRole:全局资源角色</p> 
<p>角色绑定:</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 1,RoleBinding</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 2,ClusterRoleBinding</p> 
<h3><a name="t4"></a>4,查看现有的集群默认角色</h3> 
<blockquote> 
 <p>[root@k8s231 ~]# kubectl get clusterrole | grep -v system</p> 
</blockquote> 
<p><img alt="" height="160" src="https://i-blog.csdnimg.cn/blog_migrate/5617d6a7ce6530c9ccced4c0c0e3f445.png" width="867"></p> 
<blockquote> 
 <p>admin:主要用于授权<span class="edu-hl hl hl-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;命名空间\&quot;}&quot;}" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;命名空间\&quot;}&quot;}" data-tit="命名空间" data-pretit="命名空间">命名空间</span>所有读写的权限</p> 
 <p>cluster-admin:超级管理员,由集群所有权限</p> 
 <p>edit:允许对大多数对象进行读写操作,不允许查看或者修改角色、角色绑定;</p> 
 <p>view:允许对命名空间大多数对象进行读写,不允许查看角色、角色绑定、secret;</p> 
 <p>#####</p> 
 <p>kubeadm用户,只有使用kubeadm部署k8s时才会出现,不用关心它;</p> 
</blockquote> 
<h2><a name="t5"></a>二、流程介绍</h2> 
<h3><a name="t6"></a>1,用户的创建流程</h3> 
<blockquote> 
 <p>1,User</p> 
 <p>2,SerciceAccount</p> 
 <p>3,Group</p> 
</blockquote> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 本质上讲,在k8s系统中,用户,就是一个文件,这个文件在当前登录用户的家目录下;</p> 
<p>这个文件config,就代表“我”是谁;</p> 
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这里面并没有角色、权限信息,角色和权限信息,在其他位置;</p> 
<blockquote> 
 <p>[root@k8s231 ~]# ll .kube/config&nbsp;<br> -rw------- 1 root root 5634 Jan &nbsp;1 19:40 .kube/config</p> 
</blockquote> 
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;所以,要创建用户,就是要创建这个文件;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 那么如何创建这个文件呐?</p> 
<h4><a name="t7"></a>· ssl流程介绍</h4> 
<p><img alt="" height="338" src="https://i-blog.csdnimg.cn/blog_migrate/93fc7a982d33aac6f341208b6eee8708.png" width="939"></p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 知道了ssl原理,https请求过程,我们就了解了,ssl的安全机制;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 实际上k8s当中“根证书”早就生成好了,在我们kubeadm部署的时候,就自动帮我们生成了;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; k8s是模拟ca机构,给自己颁发证书,自己验证自己,所以,整数中有私钥、公钥等;</p> 
<blockquote> 
 <p>[root@k8s231 ~]# ll /etc/<span class="edu-hl hl hl-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;kubernetes\&quot;}&quot;}" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;kubernetes\&quot;}&quot;}" data-tit="kubernetes" data-pretit="kubernetes">kubernetes</span>/pki/<br> total 56<br> -rw-r--r-- 1 root root 1281 Jan &nbsp;1 19:39 apiserver.crt<br> -rw-r--r-- 1 root root 1155 Jan &nbsp;1 19:39 apiserver-etcd-client.crt<br> -rw------- 1 root root 1679 Jan &nbsp;1 19:39 apiserver-etcd-client.key<br> -rw------- 1 root root 1675 Jan &nbsp;1 19:39 apiserver.key<br> -rw-r--r-- 1 root root 1164 Jan &nbsp;1 19:39 apiserver-kubelet-client.crt<br> -rw------- 1 root root 1679 Jan &nbsp;1 19:39 apiserver-kubelet-client.key</p> 
 <p>#根证书<br> -rw-r--r-- 1 root root 1099 Jan &nbsp;1 19:39 ca.crt</p> 
 <p>#ca机构的私钥<br> -rw------- 1 root root 1679 Jan &nbsp;1 19:39 ca.key<br> drwxr-xr-x 2 root root &nbsp;162 Jan &nbsp;1 19:39 etcd<br> -rw-r--r-- 1 root root 1115 Jan &nbsp;1 19:39 front-proxy-ca.crt<br> -rw------- 1 root root 1675 Jan &nbsp;1 19:39 front-proxy-ca.key<br> -rw-r--r-- 1 root root 1119 Jan &nbsp;1 19:39 front-proxy-client.crt<br> -rw------- 1 root root 1679 Jan &nbsp;1 19:39 front-proxy-client.key<br> -rw------- 1 root root 1675 Jan &nbsp;1 19:39 sa.key<br> -rw------- 1 root root &nbsp;451 Jan &nbsp;1 19:39 sa.pub</p> 
</blockquote> 
<h4><a name="t8"></a>· 生成用户证书</h4> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 一个用户一个证书,这个用户证书,就是用来生成,用户文件的(也就是家目录下的config)</p> 
<h4><a name="t9"></a>· 生成kubeconfig用户授权文件</h4> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 有了这个授权文件,我们就拥有了一个用户了;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 但是,还没有任何权限,还无法使用;</p> 
<h3><a name="t10"></a>2,创建角色和规则</h3> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 通过资源清单的方式,创建角色和规则;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 角色就是:</p> 
<blockquote> 
 <p>1,Role:局部资源角色</p> 
 <p>2,ClusterRole:全局资源角色</p> 
</blockquote> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 规则就是:这个角色的权限;能使用什么资源、不能使用什么资源,,,,</p> 
<h3><a name="t11"></a>3,角色与用户的绑定</h3> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 根据角色的不同,创建资源清单,对应不同的绑定资源清单的编写;</p> 
<blockquote> 
 <p>1,RoleBinding</p> 
 <p>2,ClusterRoleBinding</p> 
</blockquote> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 只要角色和用户绑定完成,那么,RBAC的整个流程就结束了;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 我们就成功创建了一个带有特定权限的用户;就可以分发给“同事”进行使用了;</p> 
<h3><a name="t12"></a>4,逻辑流程的总结</h3> 
<p><img alt="" height="194" src="https://i-blog.csdnimg.cn/blog_migrate/83c31af6a1d2319c07cb373a4264de9a.png" width="812"></p> 
<h2><a name="t13"></a>三、RBAC创建初体验</h2> 
<h3><a name="t14"></a>1,创建用户【user】</h3> 
<h4><a name="t15"></a>· 生成用户ssl证书</h4> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 以往我们使用openssl的工具命令生成证书,比较繁琐,本次学习,我给大家介绍一个证书生成工具,叫做cfssl证书生成工具;</p> 
<h5>1,上传/下载cfssl证书生成工具</h5> 
<p>cfssl工具百度网盘地址:</p> 
<p>链接:https://pan.baidu.com/s/1gDRQuxekvgInplLkpjvhQw?pwd=f31x&nbsp;<br> 提取码:f31x</p> 
<p>或者去github地址下载:<a href="https://github.com/cloudflare/cfssl/releases" title="Releases · cloudflare/cfssl · GitHub">Releases · cloudflare/cfssl · GitHub</a></p> 
<blockquote> 
 <p>[root@k8s231 rbac]# rz -E<br> [root@k8s231 rbac]# ll<br> -rw-r--r-- 1 root root 10808877 Nov &nbsp;8 00:30 cfssl.zip</p> 
</blockquote> 
<h5>2,解压cfssl工具压缩包</h5> 
<blockquote> 
 <p>[root@k8s231 rbac]# unzip cfssl.zip&nbsp;<br> [root@k8s231 rbac]# ll<br> -rw-r--r-- 1 root root 12054528 Aug 30 15:46 cfssl_1.6.4_linux_amd64<br> -rw-r--r-- 1 root root &nbsp;9560064 Aug 30 15:45 cfssl-certinfo_1.6.4_linux_amd64<br> -rw-r--r-- 1 root root &nbsp;7643136 Aug 30 15:48 cfssljson_1.6.4_linux_amd64<br> -rw-r--r-- 1 root root 10808877 Nov &nbsp;8 00:30 cfssl.zip</p> 
</blockquote> 
<h5>3,删除压缩包,将cfssl文件改名</h5> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 为什么改名?为了便于使用~如果你不嫌费力,可以不改~</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# rm -rf cfssl.zip&nbsp;<br> [root@k8s231 rbac]# rename _1.6.4_linux_amd64 "" *<br> [root@k8s231 rbac]# ll<br> -rw-r--r-- 1 root root 12054528 Aug 30 15:46 cfssl<br> -rw-r--r-- 1 root root &nbsp;9560064 Aug 30 15:45 cfssl-certinfo<br> -rw-r--r-- 1 root root &nbsp;7643136 Aug 30 15:48 cfssljson</p> 
</blockquote> 
<h5>4,将cfssl文件编程全局命令</h5> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 将这三个文件,移动到/usr/local/bin目录下,编程全局命令~</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# mv ./* /usr/local/bin/</p> 
</blockquote> 
<h5>5,给cfssl执行文件加执行权限</h5> 
<blockquote> 
 <p>[root@k8s231 rbac]# chmod +x /usr/local/bin/cfssl*</p> 
</blockquote> 
<h5>6,编辑cfssl工具的生成用户ssl证书的配置文件</h5> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 根证书</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# cat ca-config.json&nbsp;<br> {<br> &nbsp; "signing": {<br> &nbsp; &nbsp; "default": {<br> &nbsp; &nbsp; &nbsp; "expiry": "87600h"<br> &nbsp; &nbsp; },<br> &nbsp; &nbsp; "profiles": {<br> &nbsp; &nbsp; &nbsp; "kubernetes": {<br> &nbsp; &nbsp; &nbsp; &nbsp; "usages": [<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "signing",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "key encipherment",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<span class="words-blog hl-git-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.10283&quot;,&quot;extra&quot;:&quot;{\&quot;words\&quot;:\&quot;server\&quot;}&quot;}" data-tit="server" data-pretit="server">server</span> auth",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "client auth"<br> &nbsp; &nbsp; &nbsp; &nbsp; ],<br> &nbsp; &nbsp; &nbsp; &nbsp; "expiry": "87600h"<br> &nbsp; &nbsp; &nbsp; }<br> &nbsp; &nbsp; }<br> &nbsp; }<br> }</p> 
</blockquote> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 客户端验证签名证书</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# cat csr.json&nbsp;<br> {<br> &nbsp; "CN": "xinjizhiwa",<br> &nbsp; "<span class="words-blog hl-git-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.10283&quot;,&quot;extra&quot;:&quot;{\&quot;words\&quot;:\&quot;hosts\&quot;}&quot;}" data-tit="hosts" data-pretit="hosts">hosts</span>": [],<br> &nbsp; "key": {<br> &nbsp; &nbsp; "algo": "rsa",<br> &nbsp; &nbsp; "size": 2048<br> &nbsp; },<br> &nbsp; "names": [<br> &nbsp; &nbsp; {<br> &nbsp; &nbsp; &nbsp; "C": "CN",<br> &nbsp; &nbsp; &nbsp; "ST": "BeiJing",<br> &nbsp; &nbsp; &nbsp; "L": "BeiJing",<br> &nbsp; &nbsp; &nbsp; "O": "k8s",<br> &nbsp; &nbsp; &nbsp; "OU": "System"<br> &nbsp; &nbsp; }<br> &nbsp; ]<br> }</p> 
</blockquote> 
<h5>7,使用cfssl工具生成用户的ssl证书</h5> 
<blockquote> 
 <p>[root@k8s231 rbac]# cfssl gencert -ca=/etc/kubernetes/pki/ca.crt -ca-key=/etc/kubernetes/pki/ca.key -config=ca-config.json -profile=kubernetes csr.json | cfssljson -bare xinjizhiwa</p> 
</blockquote> 
<p>参数解释:</p> 
<blockquote> 
 <p>#使用k8s自带的证书来签发客户端证书(位置就在/etc/kubernetes/pki/下面);<br> -ca=/etc/kubernetes/pki/ca.crt&nbsp;<br> -ca-key=/etc/kubernetes/pki/ca.key&nbsp;<br> -config=ca-config.json&nbsp;<br> -profile=kubernetes csr.json | cfssljson -bare xinjizhiwa</p> 
</blockquote> 
<p>查看证书</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# ll<br> total 20<br> -rw-r--r-- 1 root root &nbsp;292 Feb 21 20:06 ca-config.json<br> -rw-r--r-- 1 root root &nbsp;223 Feb 21 20:07 csr.json<br> -rw-r--r-- 1 root root 1001 Feb 21 20:11 xinjizhiwa.csr<br> -rw------- 1 root root 1675 Feb 21 20:11 xinjizhiwa-key.pem<br> -rw-r--r-- 1 root root 1285 Feb 21 20:11 xinjizhiwa.pem</p> 
</blockquote> 
<p>至此,我们用户的ssl证书申请完毕了;</p> 
<p></p> 
<h4><a name="t16"></a>· 生成用户kubeconfig文件</h4> 
<h5>1,编辑生成kubeconfig文件的执行脚本</h5> 
<blockquote> 
 <p>[root@k8s231 rbac]# cat kubeconfig.sh&nbsp;<br> #!/bin/bash</p> 
 <p># 配置集群;<br> # --certificate-authority:指定K8s的ca根证书文件路径<br> # --embed-certs:<br> # &nbsp; 1,true,表示将根证书文件的内容写入到配置文件中,<br> # &nbsp; 2,false,则只是引用配置文件,将kubeconfig<br> # --server:指定APIServer的地址。<br> # --kubeconfig:指定kubeconfig的配置文件名称<br> kubectl config set-cluster xinjizhiwa-cluster \<br> &nbsp; --certificate-authority=/etc/kubernetes/pki/ca.crt \<br> &nbsp; --embed-certs=true \<br> &nbsp; --server=https://10.0.0.231:6443 \<br> &nbsp; --kubeconfig=xinjizhiwa.kubeconfig<br> &nbsp;<br> # 设置客户端认证,客户端将来需要携带证书让服务端验证<br> kubectl config set-credentials xinjizhiwa-client \<br> &nbsp; --client-key=xinjizhiwa-key.pem \<br> &nbsp; --client-certificate=xinjizhiwa.pem \<br> &nbsp; --embed-certs=true \<br> &nbsp; --kubeconfig=xinjizhiwa.kubeconfig</p> 
 <p># 设置默认上下文,可以用于绑定多个客户端和服务端的对应关系(客户端和服务端绑定)。<br> kubectl config set-context xinjizhiwa \<br> &nbsp; --cluster=xinjizhiwa-cluster \<br> &nbsp; --user=xinjizhiwa-client \<br> &nbsp; --kubeconfig=xinjizhiwa.kubeconfig</p> 
 <p># 设置当前使用的上下文(正式生效)<br> kubectl config use-context xinjizhiwa --kubeconfig=xinjizhiwa.kubeconfig</p> 
</blockquote> 
<h5>2,执行生成kubeconfig用户文件的执行脚本</h5> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 正式生成用户</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# bash kubeconfig.sh&nbsp;<br> Cluster "xinjizhiwa-cluster" set.<br> User "xinjizhiwa-client" set.<br> Context "xinjizhiwa" created.<br> Switched to context "xinjizhiwa".</p> 
</blockquote> 
<p>查看生成的kubeconfig用户文件</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# ll<br> ......<br> -rw------- 1 root root 5802 Feb 21 20:24 xinjizhiwa.kubeconfig</p> 
</blockquote> 
<p>拓展知识:也可以使用config资源清单编写生成用户kubeconfig文件</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# cat xinjizhiwa.kubeconfig&nbsp;<br> apiVersion: v1<br> clusters:<br> - cluster:<br> &nbsp; &nbsp; certificate-authority-data: ......(/etc/kubernetes/pki/ca.crt)<br> &nbsp; &nbsp; server: https://10.0.0.231:6443<br> &nbsp; name: xinjizhiwa-cluster<br> contexts:<br> - context:<br> &nbsp; &nbsp; cluster: xinjizhiwa-cluster<br> &nbsp; &nbsp; user: xinjizhiwa-client<br> &nbsp; name: xinjizhiwa<br> current-context: xinjizhiwa<br> kind: Config<br> preferences: {}<br> users:<br> - name: xinjizhiwa-client<br> &nbsp; user:<br> &nbsp; &nbsp; client-certificate-data: .......(xinjizhiwa.pem或者写入公钥串,pem记得base64 -d之后使用)<br> &nbsp; &nbsp; client-key-data: ......(xinjizhiwa-key.pem)</p> 
</blockquote> 
<p>此时使用这个用户,取查看pod,会提示你没有权限,因为咱们还没有创建、绑定角色;</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# kubectl get pods --kubeconfig=xinjizhiwa.kubeconfig</p> 
</blockquote> 
<p><img alt="" height="65" src="https://i-blog.csdnimg.cn/blog_migrate/2addfb6348fc9b2d2ca12adae8dd2c82.png" width="1200"></p> 
<p>至此,我们用户“xinjizhiwa”就创建完成了;</p> 
<h3><a name="t17"></a>2,创建角色编写规则</h3> 
<h4><a name="t18"></a>· 编辑角色资源清单</h4> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 上述内容中,我们知道,角色有两种,一种是全局角色ClusterRole,另一种是局部角色Role;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 我们先创建一个局部的Role角色作为学习;</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# cat role.yaml&nbsp;<br> apiVersion: rbac.authorization.k8s.io/v1<br> kind: Role<br> metadata:<br> &nbsp; name: xinjizhiwa-role<br> &nbsp; namespace: default<br> rules:<br> &nbsp; #声明API组;[""]代表v1;["apps"]代表apps/v1<br> - apiGroup: ["","apps"]<br> &nbsp; #声明API组下面的资源类型(不支持简写,只能写全称)<br> &nbsp; resources: ["pods","deployments","services"]<br> &nbsp; #声明使用方式(动作增删改查、、)<br> &nbsp; verbs: ["get","list","delete"]</p> 
</blockquote> 
<h4><a name="t19"></a>· 创建角色</h4> 
<blockquote> 
 <p>[root@k8s231 rbac]# kubectl apply -f role.yaml</p> 
</blockquote> 
<p>查看角色</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# kubectl get role<br> NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CREATED AT<br> xinjizhiwa-role &nbsp; 2024-02-21T13:19:34Z</p> 
</blockquote> 
<p>至此,用户和角色及规则都创建成功了;</p> 
<h3><a name="t20"></a>3,绑定用户与角色</h3> 
<h4><a name="t21"></a>· 编辑绑定资源清单</h4> 
<blockquote> 
 <p>[root@k8s231 rbac]# cat bind.yaml&nbsp;<br> apiVersion: rbac.authorization.k8s.io/v1<br> kind: RoleBinding<br> metadata:<br> &nbsp; name: xinjizhiwa-bind<br> &nbsp; namespace: default<br> #声明用户主体(绑定的用户是哪个)<br> subjects:<br> #由于我们用户类型有三种,所以需要写明用户类型;<br> - kind: User<br> &nbsp; #用户名称<br> &nbsp; name: xinjizhiwa<br> &nbsp; apiGroup: rbac.authorization.k8s.io<br> #声明角色主体(绑定的角色是哪个?)<br> roleRef:&nbsp;<br> &nbsp; #角色类型<br> &nbsp; kind: Role<br> &nbsp; #角色名称<br> &nbsp; name: xinjizhiwa-role<br> &nbsp; apiGroup: rbac.authorization.k8s.io</p> 
</blockquote> 
<h4><a name="t22"></a>· 创建绑定资源</h4> 
<blockquote> 
 <p>[root@k8s231 rbac]# kubectl apply -f bind.yaml</p> 
</blockquote> 
<h3><a name="t23"></a>4,测试用户</h3> 
<p>协议个pod资源,使用用户“xinjizhiwa”创建,发现创建不了,提示权限不够;</p> 
<p>因为,我们在创建角色规则的时候,只给了删除、和查看的能力,没有给create创建能力;</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# cat pod.yaml&nbsp;<br> apiVersion: v1<br> kind: Pod<br> metadata:<br> &nbsp; name: pod-nginx<br> spec:<br> &nbsp; containers:<br> &nbsp; - name: c1<br> &nbsp; &nbsp; <span class="words-blog hl-git-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.10283&quot;,&quot;extra&quot;:&quot;{\&quot;words\&quot;:\&quot;image\&quot;}&quot;}" data-tit="image" data-pretit="image">image</span>: nginx:1.20.1-alpine</p> 
</blockquote> 
<blockquote> 
 <p>[root@k8s231 rbac]# kubectl apply -f pod.yaml --kubeconfig=xinjizhiwa.kubeconfig</p> 
</blockquote> 
<p><img alt="" height="75" src="https://i-blog.csdnimg.cn/blog_migrate/d8eb4f51003c34429a0a5eec31a6d316.png" width="1200"></p> 
<p>因为我们有default默认名称空间的查看、删除权限,所以我们可以进行查看;</p> 
<p>测试查看功能</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# kubectl get pods --kubeconfig=xinjizhiwa.kubeconfig</p> 
</blockquote> 
<p><img alt="" height="84" src="https://i-blog.csdnimg.cn/blog_migrate/f7b65f417fe704f2917cea16fb3d4f6d.png" width="682"></p> 
<p>测试删除功能</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# kubectl delete deploy --all --kubeconfig=xinjizhiwa.kubeconfig</p> 
</blockquote> 
<p><img alt="" height="40" src="https://i-blog.csdnimg.cn/blog_migrate/da525958f1378b1b59c6536aacd55ae2.png" width="777"></p> 
<p>到此,普通用户的创建、角色绑定、角色配置就学习完毕了;</p> 
<h2><a name="t24"></a>四、其他节点使用用户登录操作k8s</h2> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 我们有了用户文件,将用户文件发送给“同事”,同事直接使用这个文件,就可以按照绑定的规则,操作k8s资源了;</p> 
<h3><a name="t25"></a>1,模拟将用户文件发给同事</h3> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 比如,同事正在使用k8s233,我们把用户文件发给他</p> 
<blockquote> 
 <p>[root@k8s231 rbac]# scp xinjizhiwa.kubeconfig k8s233:/root/</p> 
</blockquote> 
<h3><a name="t26"></a>2,同事在k8s233使用用户文件操作k8s资源</h3> 
<blockquote> 
 <p>[root@k8s233 ~]# kubectl get pods --kubeconfig=xinjizhiwa.kubeconfig</p> 
</blockquote> 
<p><img alt="" height="67" src="https://i-blog.csdnimg.cn/blog_migrate/a02c01e12d74942b4e59e97d95762146.png" width="686"></p> 
<h3><a name="t27"></a>3,操作资源不指定文件,默认指定设置</h3> 
<blockquote> 
 <p>[root@k8s233 ~]# cp xinjizhiwa.kubeconfig ./.kube/config</p> 
</blockquote> 
<blockquote> 
 <p>[root@k8s233 ~]# kubectl get pods</p> 
</blockquote> 
<p><img alt="" height="101" src="https://i-blog.csdnimg.cn/blog_migrate/7f25824700d759d8590473f89d28c0f2.png" width="678"></p> 
<h2><a name="t28"></a>五、用户组Group案例</h2> 
<h3><a name="t29"></a>1,用户组的概念</h3> 
<blockquote> 
 <p>1,用户组的好处在于,无需单独为一个用户创建权限和绑定了,只需要统一把一个组进行授权,然后,将该组的用户加入进去,就没一个用户都拥有该组的权限了;</p> 
 <p>2,APIserver会优先校验用户名(ssl证书中的CN字段),若用户名没有对应的权限,则再去校验用户组(o)的权限;</p> 
 <p>&nbsp; &nbsp; &nbsp; &nbsp; k8s中的用户、用户组都是提取ssl证书签名字段中的一个字段,不是在集群中创建的;</p> 
 <p>--证书签名中的CN字段:代表用户</p> 
 <p>--证书签名中的o字段:代表用户组</p> 
</blockquote> 
<h3><a name="t30"></a>2,创建用户组Group</h3> 
<h4><a name="t31"></a>· 编辑cfssl证书请求文件</h4> 
<blockquote> 
 <p>[root@k8s231 group]# cat ca-group.json&nbsp;<br> {<br> &nbsp; "signing": {<br> &nbsp; &nbsp; "default": {<br> &nbsp; &nbsp; &nbsp; "expiry": "87600h"<br> &nbsp; &nbsp; },<br> &nbsp; &nbsp; "profiles": {<br> &nbsp; &nbsp; &nbsp; "kubernetes": {<br> &nbsp; &nbsp; &nbsp; &nbsp; "usages": [<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "signing",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "key encipherment",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "server auth",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "client auth"<br> &nbsp; &nbsp; &nbsp; &nbsp; ],<br> &nbsp; &nbsp; &nbsp; &nbsp; "expiry": "87600h"<br> &nbsp; &nbsp; &nbsp; }<br> &nbsp; &nbsp; }<br> &nbsp; }<br> }</p> 
</blockquote> 
<blockquote> 
 <p>[root@k8s231 group]# cat xinjizhiwa-csr.json&nbsp;<br> {<br> &nbsp; "CN": "xinjizhiwa",<br> &nbsp; "hosts": [],<br> &nbsp; "key": {<br> &nbsp; &nbsp; "algo": "rsa",<br> &nbsp; &nbsp; "size": 2048<br> &nbsp; },<br> &nbsp; "names": [<br> &nbsp; &nbsp; {<br> &nbsp; &nbsp; &nbsp; "C": "CN",<br> &nbsp; &nbsp; &nbsp; "ST": "BeiJing",<br> &nbsp; &nbsp; &nbsp; "L": "BeiJing",<br> &nbsp; &nbsp; &nbsp; "O": "xinjizhiwa-group",<br> &nbsp; &nbsp; &nbsp; "OU": "System"<br> &nbsp; &nbsp; }<br> &nbsp; ]<br> }</p> 
</blockquote> 
<h4><a name="t32"></a>· 生成用户组证书</h4> 
<p>再次声明,你创建的是用户、还是用户组,取决于你的证书名称选择的签名字段,选择的是CN还是o;</p> 
<blockquote> 
 <p>[root@k8s231 group]# cfssl gencert -ca=/etc/kubernetes/pki/ca.crt -ca-key=/etc/kubernetes/pki/ca.key -config=ca-group.json -profile=kubernetes xinjizhiwa-csr.json | cfssljson -bare xinjizhiwa-group</p> 
</blockquote> 
<h4><a name="t33"></a>· 编辑脚本生成用户组kubeconfig用户组文件</h4> 
<blockquote> 
 <p>[root@k8s231 group]# cat kubeconfig.sh&nbsp;<br> #!/bin/bash</p> 
 <p>kubectl config set-cluster xinjizhiwa-cluster \<br> &nbsp; --certificate-authority=/etc/kubernetes/pki/ca.crt \<br> &nbsp; --embed-certs=true \<br> &nbsp; --server=https://10.0.0.231:6443 \<br> &nbsp; --kubeconfig=xinjizhiwa-group.kubeconfig<br> &nbsp;<br> # 设置客户端认证<br> kubectl config set-credentials xinjizhiw-client \<br> &nbsp; --client-key=xinjizhiwa-group-key.pem \<br> &nbsp; --client-certificate=xinjizhiwa-group.pem \<br> &nbsp; --embed-certs=true \<br> &nbsp; --kubeconfig=xinjizhiwa-group.kubeconfig</p> 
 <p># 设置默认上下文<br> kubectl config set-context xinjizhiwa-group \<br> &nbsp; --cluster=xinjizhiwa-cluster \<br> &nbsp; --user=xinjizhiw-client \<br> &nbsp; --kubeconfig=xinjizhiwa-group.kubeconfig</p> 
 <p># 设置当前使用的上下文<br> kubectl config use-context xinjizhiwa-group --kubeconfig=xinjizhiwa-group.kubeconfig</p> 
</blockquote> 
<h4><a name="t34"></a>· 执行脚本生成用户组文件</h4> 
<blockquote> 
 <p>[root@k8s231 group]# bash kubeconfig.sh</p> 
</blockquote> 
<h3><a name="t35"></a>3,创建角色、绑定用户组</h3> 
<h4><a name="t36"></a>· 编辑资源清单</h4> 
<blockquote> 
 <p>[root@k8s231 group]# cat rbac.yaml&nbsp;<br> #创建角色<br> kind: Role<br> apiVersion: rbac.authorization.k8s.io/v1<br> metadata:<br> &nbsp; namespace: default<br> &nbsp; name: role-group<br> rules:<br> - apiGroups: ["","apps"] &nbsp;<br> &nbsp; resources: ["pods","nodes","services","deployments"] &nbsp;<br> &nbsp; verbs: ["get", "watch", "list","create"] &nbsp;</p> 
 <p>---<br> #绑定用户组与角色<br> kind: RoleBinding<br> apiVersion: rbac.authorization.k8s.io/v1<br> metadata:<br> &nbsp; name: group-rolebinding<br> &nbsp; namespace: default<br> #声明要绑定的用户/用户组<br> subjects:<br> &nbsp; # 声明主体类型:组<br> - kind: Group<br> &nbsp; # 组名(对应的O字段)<br> &nbsp; name: xinjizhiwa-group &nbsp;<br> &nbsp; apiGroup: rbac.authorization.k8s.io<br> roleRef:<br> &nbsp; # 角色类型<br> &nbsp; kind: Role &nbsp;<br> &nbsp; # 绑定角色名称<br> &nbsp; name: role-group<br> &nbsp; apiGroup: rbac.authorization.k8s.io</p> 
</blockquote> 
<h4><a name="t37"></a>· 创建用户组kubeconfig文件</h4> 
<blockquote> 
 <p>[root@k8s231 group]# kubectl apply -f rbac.yaml</p> 
</blockquote> 
<p>此时,我们就拥有了xinjizhiwa-group这个用户组了;</p> 
<h3><a name="t38"></a>4,创建用户加入用户组</h3> 
<h4><a name="t39"></a>· 创建用户</h4> 
<blockquote> 
 <p>[root@k8s231 user]# cat ca-config.json&nbsp;<br> {<br> &nbsp; "signing": {<br> &nbsp; &nbsp; "default": {<br> &nbsp; &nbsp; &nbsp; "expiry": "87600h"<br> &nbsp; &nbsp; },<br> &nbsp; &nbsp; "profiles": {<br> &nbsp; &nbsp; &nbsp; "kubernetes": {<br> &nbsp; &nbsp; &nbsp; &nbsp; "usages": [<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "signing",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "key encipherment",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "server auth",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "client auth"<br> &nbsp; &nbsp; &nbsp; &nbsp; ],<br> &nbsp; &nbsp; &nbsp; &nbsp; "expiry": "87600h"<br> &nbsp; &nbsp; &nbsp; }<br> &nbsp; &nbsp; }<br> &nbsp; }<br> }<br> &nbsp;</p> 
</blockquote> 
<blockquote> 
 <p>[root@k8s231 user]# cat group-user-xjzw.json&nbsp;<br> {<br> &nbsp; "CN": "xjzw",<br> &nbsp; "hosts": [],<br> &nbsp; "key": {<br> &nbsp; &nbsp; "algo": "rsa",<br> &nbsp; &nbsp; "size": 2048<br> &nbsp; },<br> &nbsp; "names": [<br> &nbsp; &nbsp; {<br> &nbsp; &nbsp; &nbsp; "C": "CN",<br> &nbsp; &nbsp; &nbsp; "ST": "BeiJing",<br> &nbsp; &nbsp; &nbsp; "L": "BeiJing",<br> &nbsp; &nbsp; &nbsp; "O": "xinjizhiwa-group",<br> &nbsp; &nbsp; &nbsp; "OU": "System"<br> &nbsp; &nbsp; }<br> &nbsp; ]<br> }</p> 
</blockquote> 
<p>生成用户证书</p> 
<blockquote> 
 <p>[root@k8s231 user]# cfssl gencert -ca=/etc/kubernetes/pki/ca.crt -ca-key=/etc/kubernetes/pki/ca.key -config=ca-config.json -profile=kubernetes group-user-xjzw.json | cfssljson -bare xjzw</p> 
</blockquote> 
<p><img alt="" height="160" src="https://i-blog.csdnimg.cn/blog_migrate/d8bf297866c2f380db9d2e022e091f6f.png" width="680"></p> 
<h4><a name="t40"></a>· xjzw用户加入用户组</h4> 
<h5>1,编辑生成kubeconfig用户文件的执行脚本</h5> 
<blockquote> 
 <p>[root@k8s231 user]# ca kubeconfig.sh&nbsp;<br> -bash: ca: command not found<br> [root@k8s231 user]# cat kubeconfig.sh&nbsp;<br> #!/bin/bash</p> 
 <p>kubectl config set-cluster xinjizhiwa-cluster \<br> &nbsp; --certificate-authority=/etc/kubernetes/pki/ca.crt \<br> &nbsp; --embed-certs=true \<br> &nbsp; --server=https://10.0.0.231:6443 \<br> &nbsp; --kubeconfig=xjzw.kubeconfig<br> &nbsp;<br> # 设置客户端认证<br> kubectl config set-credentials xjzw-client \<br> &nbsp; --client-key=xjzw-key.pem \<br> &nbsp; --client-certificate=xjzw.pem \<br> &nbsp; --embed-certs=true \<br> &nbsp; --kubeconfig=xjzw.kubeconfig</p> 
 <p># 设置默认上下文<br> kubectl config set-context xinjizhiwa-contest \<br> &nbsp; --cluster=xinjizhiwa-cluster \<br> &nbsp; --user=xjzw-client \<br> &nbsp; --kubeconfig=xjzw.kubeconfig</p> 
 <p># 设置当前使用的上下文<br> kubectl config use-context xinjizhiwa-contest --kubeconfig=xjzw.kubeconfig</p> 
</blockquote> 
<h5>2,执行脚本</h5> 
<blockquote> 
 <p>[root@k8s231 user]# bash kubeconfig.sh</p> 
</blockquote> 
<h3><a name="t41"></a>5,测试用户是否拥有用户组的权限</h3> 
<blockquote> 
 <p>[root@k8s231 user]# kubectl get pods --kubeconfig=xjzw.kubeconfig</p> 
</blockquote> 
<p><img alt="" height="72" src="https://i-blog.csdnimg.cn/blog_migrate/d857ecf8bfaa2f422173fd9cd9f564f9.png" width="683"></p> 
<p>总结,只要拥有了用户组,我们创建用户的时候,签名中“O”字段,与用户组名相同,那么创建出来的用户,就拥有了用户组的权限,就不在需要配置角色、绑定了;方便了很多;</p> 
<h2><a name="t42"></a>六、基于【服务账号serviceaccount】授权</h2> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; serviceaccount用户,简称sa资源,用于自动化程序的用户创建;</p> 
<p>&nbsp; &nbsp; &nbsp; &nbsp; 创建sa用户,就不需要使用证书来创建了,它是k8s集群中的一个资源,sa资源;</p> 
<h3><a name="t43"></a>1,创建sa资源</h3> 
<h4><a name="t44"></a>· 声明式创建</h4> 
<blockquote> 
 <p>[root@k8s231 sa]# vim sa01.yaml<br> apiVersion: v1<br> kind: ServiceAccount<br> metadata:&nbsp;<br> &nbsp; name: py01</p> 
</blockquote> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl apply -f sa01.yaml&nbsp;<br> serviceaccount/py01 created</p> 
</blockquote> 
<p>查看sa服务账号</p> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl get sa<br> NAME &nbsp; &nbsp; &nbsp;SECRETS &nbsp; AGE<br> default &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 51d<br> py01 &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; 4s</p> 
</blockquote> 
<h4><a name="t45"></a>· 响应式创建</h4> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl create serviceaccount py02</p> 
</blockquote> 
<p>查看sa用户</p> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl get sa<br> NAME &nbsp; &nbsp; &nbsp;SECRETS &nbsp; AGE<br> default &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; 51d<br> py01 &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; 2m4s<br> py02 &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; 4s</p> 
</blockquote> 
<h3><a name="t46"></a>2,授权python程序对k8s-API的访问权限</h3> 
<h4><a name="t47"></a>· 创建用户</h4> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl create serviceaccount py02</p> 
</blockquote> 
<h4><a name="t48"></a>· 创建角色</h4> 
<blockquote> 
 <p>[root@k8s231 sa]# vim role.yaml</p> 
 <p>apiVersion: rbac.authorization.k8s.io/v1<br> kind: Role<br> metadata:<br> &nbsp; name: python-role<br> rules:<br> - apiGroups: [""]<br> &nbsp; resources: ["pods"]<br> &nbsp; verbs: ["get","watch","list"]</p> 
</blockquote> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl apply -f role.yaml</p> 
</blockquote> 
<h4><a name="t49"></a>· 绑定sa账号和角色</h4> 
<blockquote> 
 <p>[root@k8s231 sa]# vim bind.yaml</p> 
 <p>apiVersion: rbac.authorization.k8s.io/v1<br> kind: RoleBinding<br> metadata:<br> &nbsp; name: python-role-sa<br> subjects:<br> - kind: ServiceAccount&nbsp;<br> &nbsp; name: py02<br> roleRef:<br> &nbsp; kind: Role<br> &nbsp; name: python-role<br> &nbsp; apiGroup: rbac.authorization.k8s.io</p> 
</blockquote> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl apply -f bind.yaml</p> 
</blockquote> 
<h3><a name="t50"></a>3,部署一个python的pod资源</h3> 
<blockquote> 
 <p>[root@k8s231 sa]# vim pod.yaml</p> 
 <p>apiVersion: apps/v1<br> kind: Deployment<br> metadata:<br> &nbsp; name: dm-py<br> spec:<br> &nbsp; replicas: 2<br> &nbsp; selector:<br> &nbsp; &nbsp; matchLabels:<br> &nbsp; &nbsp; &nbsp; apps: python<br> &nbsp; template:<br> &nbsp; &nbsp; metadata:<br> &nbsp; &nbsp; &nbsp; labels:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;apps: python<br> &nbsp; &nbsp; spec:<br> &nbsp; &nbsp; &nbsp; #这里就是sa账号,指定sa的名称,请确认该账号是有权限访问K8S集群的哟!<br> &nbsp; &nbsp; &nbsp; serviceAccountName: py02<br> &nbsp; &nbsp; &nbsp; containers:<br> &nbsp; &nbsp; &nbsp; - image: python:3.9.16-alpine3.16<br> &nbsp; &nbsp; &nbsp; &nbsp; name: py<br> &nbsp; &nbsp; &nbsp; &nbsp; command:<br> &nbsp; &nbsp; &nbsp; &nbsp; - tail&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; - -f<br> &nbsp; &nbsp; &nbsp; &nbsp; - /etc/hosts</p> 
</blockquote> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl apply -f pod.yaml</p> 
</blockquote> 
<h3><a name="t51"></a>4,进入pod中执行python代码</h3> 
<blockquote> 
 <p>[root@k8s231 sa]# kubectl exec -it dm-py-64975879c8-b9sxg -- sh<br> / # python --version<br> Python 3.9.16</p> 
 <p>#写一个python脚本<br> / # cat &gt; view-k8s-resources.py &lt;&lt;EOF<br> &gt; from kubernetes import client, config<br> &gt;&nbsp;<br> &gt; with open('/var/run/secrets/kubernetes.io/serviceaccount/token') as f:<br> &gt; &nbsp; &nbsp; &nbsp;token = f.read()<br> &gt;&nbsp;<br> &gt; configuration = client.Configuration()<br> &gt; configuration.host = "https://kubernetes" &nbsp;# APISERVER地址<br> &gt; configuration.ssl_ca_cert="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" &nbsp;# CA证书&nbsp;<br> &gt; configuration.verify_ssl = True &nbsp; # 启用证书验证<br> &gt; configuration.api_key = {"authorization": "Bearer " + token} &nbsp;# 指定Token字符串<br> &gt; client.Configuration.set_default(configuration)<br> &gt; apps_api = client.AppsV1Api()&nbsp;<br> &gt; core_api = client.CoreV1Api()&nbsp;<br> &gt; try:<br> &gt; &nbsp; print("###### Deployment列表 ######")<br> &gt; &nbsp; #列出default命名空间所有deployment名称<br> &gt; &nbsp; for dp in apps_api.list_namespaced_deployment("default").items:<br> &gt; &nbsp; &nbsp; print(dp.metadata.name)<br> &gt; except:<br> &gt; &nbsp; print("没有权限访问Deployment资源!")<br> &gt;&nbsp;<br> &gt; try:<br> &gt; &nbsp; #列出default命名空间所有pod名称<br> &gt; &nbsp; print("###### Pod列表 ######")<br> &gt; &nbsp; for po in core_api.list_namespaced_pod("default").items:<br> &gt; &nbsp; &nbsp; print(po.metadata.name)<br> &gt; except:<br> &gt; &nbsp; print("没有权限访问Pod资源!")<br> &gt; EOF<br> / #&nbsp;</p> 
 <p>####安装py使用k8s的插件########<br> / # <span class="edu-hl hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;pip\&quot;}&quot;}" data-tit="pip" data-pretit="pip">pip</span> install kubernetes -i https://pypi.tuna.tsinghua.edu.cn/simple/</p> 
 <p>#执行python脚本</p> 
 <p>/ # python3 view-k8s-resources.py<br> ###### Deployment列表 ######<br> 没有权限访问Deployment资源!<br> ###### Pod列表 ######<br> dm-py-64975879c8-b9sxg<br> dm-py-64975879c8-z76fp<br> pod-nginx</p> 
</blockquote> 
<p></p> 
<p>至此,关于k8s的RBAC的学习,就此结束;</p> 
<p></p>
                </div><div><div></div></div>
        </div>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com