简体   繁体   English

如何从 Python pod 内部访问 kube-apiserver?

[英]How can I access the kube-apiserver from inside a Python pod?

I'm pretty new to Kuberenetes/Dockers and planning to create a simple Python pod that runs on a cluster and maps the cluster nodes and pods in a constant interval (for example once a day).我对 Kuberenetes/Dockers 还很陌生,并计划创建一个简单的 Python pod,它在集群上运行并以恒定间隔(例如每天一次)映射集群节点和 pod。

I'm planning to do it using the Kubernetes Python client which have easy access to the kube-apiserver.我计划使用可轻松访问 kube-apiserver 的 Kubernetes Python 客户端来完成此操作。

What things I need to configure to be able to achieve it?我需要配置哪些东西才能实现它?

Kubernetes user creation : Kubernetes 用户创建:

  1. Create a service account [ To access the kube-apiserver from inside a container ]创建一个服务帐户 [从容器内部访问 kube-apiserver ]
[root@project1kubemaster stackoverflow]# kubectl create serviceaccount sampleuser
serviceaccount/sampleuser created
  1. create a clusterrolebinding linking service account created in step #1 to cluster role called cluster-admin:创建一个在步骤 1 中创建的 clusterrolebinding 链接服务帐户到名为 cluster-admin 的集群角色:
[root@project1kubemaster stackoverflow]# kubectl create clusterrolebinding sampleuserrolebinding  --clusterrole=cluster-admin --serviceaccount=default:sampleuser
clusterrolebinding.rbac.authorization.k8s.io/sampleuserrolebinding created

Creating a python container :创建一个python容器:

  1. create a file called "requirements.txt" and add "kubernetes" in it:创建一个名为“requirements.txt”的文件并在其中添加“kubernetes”:
[root@project1kubemaster stackoverflow]# cat requirements.txt
kubernetes
  1. Check Following sample program change the IP Address to your cluster IP and save the file as program.py.检查以下示例程序将 IP 地址更改为您的集群 IP 并将文件保存为 program.py。 Modify following 2 lines修改以下两行
aToken =  open('/var/run/secrets/kubernetes.io/serviceaccount/token','r').read()
aConfiguration.host = "https://<maternode IP>:6443"
  1. create Dockerfile with following contents :使用以下内容创建 Dockerfile:
[root@project1kubemaster stackoverflow]# cat Dockerfile
FROM python:alpine3.7
WORKDIR /app
COPY requirements.txt .
COPY program.py
RUN pip install -r requirements.txt
CMD python ./program.py
  1. Build and push the image :构建并推送镜像:
[root@project1kubemaster stackoverflow]# docker build -t sample .

Sending build context to Docker daemon   46.8MB
Step 1/6 : FROM python:alpine3.7
alpine3.7: Pulling from library/python
...
Successfully built e98cea8cb850
Successfully tagged sample:latest
[root@project1kubemaster stackoverflow]#docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sample              latest              e98cea8cb850        2 minutes ago       126MB
python              alpine3.7           00be2573e9f7        21 months ago       81.3MB 
[root@project1kubemaster stackoverflow]# docker tag e98cea8cb850 prasasai/sample
[root@project1kubemaster stackoverflow]# docker push prasasai/sample
The push refers to repository [docker.io/prasasai/sample]
515f285319c0: Pushed 
b39d02c0a6c7: Pushed 
3c22209f875e: Pushed 
2ea55fcfd611: Pushed 
5fa31f02caa8: Mounted from library/python 
88e61e328a3c: Mounted from library/python 
9b77965e1d3f: Mounted from library/python 
50f8b07e9421: Mounted from library/python 
629164d914fc: Mounted from library/python 
latest: digest: sha256:93c7317f966fa723e406932221f0f1563243eba603c79fba2e113362cc22b4d8 size: 2200

Writing a Pod Manifest , running and checking logd to see the output :编写 Pod Manifest ,运行并检查 logd 以查看输出:

[root@project1kubemaster stackoverflow]# cat samplepod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: samplepod
spec:
  containers:
  - image: prasasai/sample
    name: samplepod
  serviceAccountName: sampleuser
[root@project1kubemaster stackoverflow]# kubectl apply -f samplepod.yaml
pod/samplepod created
[root@project1kubemaster stackoverflow]# kubectl logs samplepod
Listing pods with their IPs:
192.168.67.65   default first-6fb86b947d-68zzz
192.168.67.66   default first-6fb86b947d-74vtf
192.168.121.1   default first-6fb86b947d-hb6l6
192.168.121.2   default first-6fb86b947d-rlqk9
192.168.67.67   default first-6fb86b947d-tdnbf
192.168.121.6   default hello-bd5c66899-8dchl
192.168.121.4   default hello-bd5c66899-9ssrc
192.168.121.5   default hello-bd5c66899-pjk7k
192.168.67.68   default hello-bd5c66899-pwpsm
192.168.67.69   default hello-bd5c66899-whjdt
192.168.121.7   default samplepod
192.168.67.75   default test1

To meet your requirement ( periodically running this pod) , we can create a cronJob (Following runs once after one minute)为了满足您的要求(定期运行此 pod),我们可以创建一个 cronJob(一分钟后运行一次)

[root@project1kubemaster stackoverflow]# cat samplecron.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: sample-job
spec:
  jobTemplate:
    metadata:
      name: sample-job
    spec:
      template:
        metadata:
        spec:
          containers:
          - image: prasasai/sample
            name: sample-job
          serviceAccountName: sampleuser
          restartPolicy: OnFailure
  schedule: '*/1 * * * *'
[root@project1kubemaster stackoverflow]# kubectl apply -f samplecron.yaml
cronjob.batch/sample-job created

[root@project1kubemaster stackoverflow]# kubectl get cronjobs
NAME         SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
sample-job   */1 * * * *   False     0        <none>          8s

[root@project1kubemaster stackoverflow]# kubectl get cronjobs
NAME         SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
sample-job   */1 * * * *   False     0        <none>          19s

[root@project1kubemaster stackoverflow]# kubectl get cronjobs
NAME         SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
sample-job   */1 * * * *   False     1        25s             40s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个 python 脚本在 kubernetes pod 的容器内运行。如何停止与 pod 一起运行的脚本? - I have a python script running inside a container of kubernetes pod.How do i stop the script which runs along with the starting of the pod? 如何从 python、tkinter 中的主要访问按钮单击? - How can I access button click from main in python, tkinter? 如何在 python 中使用 spotDL? - How can I use spotDL inside python? 我如何访问 Python 中的子属性 - How can i access the child attribute in Python 如何从另一个 python 文件访问 python 文件中的多处理列表? - How can I access a multiprocessing list in a python file from another python file? 如何访问给定的 xpath 结果? - How can I access inside given xpath result? 如何访问Vertex对象内的邻接表? - How can I access to adjacency lists inside the Vertex object? 如何使用 Selenium 访问 Javascript 内部的元素? - How can I access an element inside of a Javascript with Selenium? 我如何从没有 class 的标签中刮取 url 或 bs4 中另一个标签内的 id [ python 3 ] - How can i scrape url from tag without class or id inside another tag in bs4 [ python 3 ] 我可以从另一个正在运行的 python 脚本访问列表吗 - Can I access a list from another running python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM