Created
December 2, 2020 14:27
-
-
Save findsomeoneyys/35247bf72ff620457ec4ba0b8e5493e1 to your computer and use it in GitHub Desktop.
nginx-server.yaml
This file contains 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: v1 | |
kind: ConfigMap | |
metadata: | |
name: nginx-conf | |
data: | |
index.conf: | | |
server { | |
listen 80; | |
server_name _; | |
location / { | |
root /opt; | |
index index.html; | |
} | |
} | |
index.html: | | |
<!DOCTYPE html> | |
<html lang="en"> | |
<body> | |
<h1>Hello K8s</h1> | |
</body> | |
</html> | |
--- | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: nginx | |
spec: | |
selector: | |
matchLabels: | |
app: nginx | |
replicas: 1 | |
template: | |
metadata: | |
labels: | |
app: nginx | |
spec: | |
containers: | |
- name: nginx | |
image: nginx:latest | |
ports: | |
- containerPort: 80 | |
volumeMounts: | |
- mountPath: /etc/nginx/conf.d/default.conf | |
name: nginx-conf | |
subPath: index.conf | |
- mountPath: /opt/index.html | |
name: nginx-conf | |
subPath: index.html | |
volumes: | |
- configMap: | |
name: nginx-conf | |
name: nginx-conf | |
--- | |
kind: Service | |
apiVersion: v1 | |
metadata: | |
name: nginx | |
spec: | |
selector: | |
app: nginx | |
type: NodePort | |
ports: | |
- nodePort: 30080 | |
port: 8080 | |
protocol: TCP | |
targetPort: 80 |
This file contains 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 os import path | |
import yaml | |
from kubernetes import client, config | |
def create_config_map(api_instance, cmap): | |
body = client.V1ConfigMap( | |
metadata=client.V1ObjectMeta(name=cmap['metadata']['name']), | |
data=cmap['data'] | |
) | |
resp = api_instance.create_namespaced_config_map( | |
body=body, | |
namespace='default') | |
print("configMap created. ") | |
def create_deployment(api_instance, deployment): | |
resp = api_instance.create_namespaced_deployment( | |
body=deployment, | |
namespace='default') | |
print("Deployment created. status='%s'" % resp.metadata.name) | |
def create_deployment_manual(api_instance, deployment): | |
body = client.V1Deployment( | |
metadata=client.V1ObjectMeta(name=deployment['metadata']['name']), | |
spec=client.V1DeploymentSpec( | |
replicas=1, | |
selector=deployment['spec']['selector'], | |
template=client.V1PodTemplateSpec( | |
metadata=client.V1ObjectMeta(labels=deployment['spec']['template']['metadata']['labels']), | |
spec=client.V1PodSpec( | |
containers=deployment['spec']['template']['spec']['containers'], | |
volumes= [client.V1ConfigMapVolumeSource(**v) for v in deployment['spec']['template']['spec']['volumes']] | |
), | |
), | |
)) | |
resp = api_instance.create_namespaced_deployment( | |
body=body, | |
namespace='default') | |
print("Deployment created. status='%s'" % resp.metadata.name) | |
def create_service(api_instance, service): | |
resp = api_instance.create_namespaced_service( | |
body=service, | |
namespace='default') | |
print("service created. status='%s'" % str(resp.status)) | |
def main(): | |
config.load_kube_config() | |
apps_v1 = client.AppsV1Api() | |
core_v1 = client.CoreV1Api() | |
with open(path.join(path.dirname(__file__), "nginx-server.yaml")) as f: | |
deps = yaml.load_all(f) | |
for d in deps: | |
if d['kind'] == 'ConfigMap': | |
create_config_map(core_v1, d) | |
elif d['kind'] == 'Deployment': | |
# it works | |
# create_deployment(apps_v1, d) | |
# it not works | |
create_deployment_manual(apps_v1, d) | |
else: | |
create_service(core_v1, d) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment