Created
March 9, 2021 23:52
-
-
Save anax32/eabe2c7b929fe86c037a3b40b46dbd36 to your computer and use it in GitHub Desktop.
Kubernetes deployment from inside a running pod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see https://github.com/kubernetes-client/python/blob/master/examples/deployment_create.py | |
from os import path | |
import yaml | |
from kubernetes import client, config | |
def main(): | |
# Configs can be set in Configuration class directly or using helper | |
# utility. If no argument provided, the config will be loaded from | |
# default location. | |
config.load_incluster_config() | |
with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f: | |
dep = yaml.safe_load(f) | |
k8s_apps_v1 = client.AppsV1Api() | |
resp = k8s_apps_v1.create_namespaced_deployment( | |
body=dep, namespace="default") | |
print("Deployment created. status='%s'" % resp.metadata.name) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/bash | |
# | |
# run this script to build the container, run the pod and observe the deployment | |
# | |
# observe pod creation with: | |
# watch -n 2 -d kubectl get pods | |
# | |
# uncomment these lines to create a minikube environment | |
#minikube start | |
#eval $(minikube docker-env) | |
# build the container | |
docker build \ | |
-t testdep \ | |
. | |
# apply the rbac so the pod has permission | |
kubectl apply -f rbac.yaml | |
# run a single container | |
kubectl run \ | |
-it \ | |
--rm \ | |
spawner \ | |
--image=testdep \ | |
--restart=Never \ | |
--image-pull-policy=Never | |
# show the status | |
kubectl get pods,deployments | |
# update | |
sleep 10 | |
kubectl get pods,deployments | |
# cleanup | |
kubectl delete deployment/nginx-deployment | |
sleep 10 | |
# check the cleanup | |
kubectl get pods,deployments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from python:3-slim | |
run pip install kubernetes | |
copy create-deployment.py /src/ | |
copy nginx-deployment.yaml /src/ | |
workdir /src | |
cmd python create-deployment.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: nginx-deployment | |
labels: | |
app: nginx | |
spec: | |
replicas: 3 | |
selector: | |
matchLabels: | |
app: nginx | |
template: | |
metadata: | |
labels: | |
app: nginx | |
spec: | |
containers: | |
- name: nginx | |
image: nginx:1.14.2 | |
ports: | |
- containerPort: 80 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kind: ClusterRole | |
apiVersion: rbac.authorization.k8s.io/v1 | |
metadata: | |
name: deployment-create | |
rules: | |
- apiGroups: ["apps"] | |
resources: ["deployments"] | |
verbs: ["create"] | |
--- | |
kind: ClusterRoleBinding | |
apiVersion: rbac.authorization.k8s.io/v1 | |
metadata: | |
name: pods-list | |
subjects: | |
- kind: ServiceAccount | |
name: default | |
namespace: default | |
roleRef: | |
kind: ClusterRole | |
name: deployment-create | |
apiGroup: rbac.authorization.k8s.io |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment