To make a pod with kubernetes, you can first create a yaml file of the pod and then apply the created yaml file with a command, but I want to create a pod from python code without using a command. When I looked it up, I couldn't find the article, and I had a hard time writing the program, so I'll share it.
First, try to create a basic pod. Any yaml file to create a pod is fine, but for the time being, create a simple pod.yaml file.
$ vim pod.yaml
It's easy to copy and paste with: set paste
pod.yaml
#Rules for writing pods
apiVersion: v1
kind: Pod
#Detailed information such as pod name and label
metadata:
name: myapp-pod #Give the pod a name
labels:
app: myapp #Label app->Add myapp
#Information of the container body to be placed in the pod
spec:
containers:
- name: demo-container #Give the container a name
image: nginx #Container name(Matches the name of Docker Hub)
ports:
- containerPort: 80 #Use port 80
Apply pod.yaml to create a pod.
$ kubectl apply -f pod.yaml
pod/myapp-pod created
Check if the pod is made.
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 34s
The pod I made is used up, so delete it.
$ kubectl delete -f pod.yaml
pod "myapp-pod" deleted
$ kubectl get pod
No resources found in c0118272 namespace.
Install what you need.
$ pip3 install flask
$ pip3 install kubernetes
Create a python program that creates a pod with the name podcreate.py in the same directory as pod.yaml. Change namespace to your own namespace name. The basic is default.
$ vim podcreate.py
podcreate.py
from flask import Flask
from flask import request
from kubernetes import client, config, watch
import yaml
app = Flask(__name__)
@app.route('/')
def pod_create():
config.load_kube_config()
with open("pod.yaml", "r") as f:
pod = yaml.safe_load(f)
k8s_apps_v1 = client.CoreV1Api()
resp = k8s_apps_v1.create_namespaced_pod(body=pod, namespace="c0118272")
print("pod created. status='%s'" % resp.metadata.name)
return "pod created. status='%s'" % resp.metadata.name
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
Check the positional relationship of the files.
$ ls
podcreate.py pod.yaml
Since I wrote it in flask, I will display it on the browser and execute it.
$ export FLASK_APP=podcreate.py
$ flask run --host=0.0.0.0
* Serving Flask app "podcreate.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
*
Try running it in your browser. If the url is local, it is http://0.0.0.0:5000. If it is not local, the port is 5000 and the IP address is different for each execution environment.
If it looks like the image above, you're successful!
Check with the command.
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 3m11s
Once you have a pod, you're done! !!
I'm using the kubernetes api, but it's difficult and I'm not sure, so please check each application method! Have a good research life ~