-
Notifications
You must be signed in to change notification settings - Fork 11
/
k8s-api.sh
executable file
·92 lines (76 loc) · 2.21 KB
/
k8s-api.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
## Make sure you have access to a Kubernetes cluster
## Install JQ from https://stedolan.github.io/jq/ before running the commands
# Verify Kubernetes setup
kubectl cluster-info
kubectl component-status
# Configure the proxy
kubectl proxy --port=8000&
# Open the url in any browser to access Swagger
open http://localhost:8000/swagger-ui/
# Invoke simple Kubernetes API
curl http://localhost:8000/api
# cURL command equivalent to 'kubectl get nodes'
curl -s http://localhost:8000/api/v1/nodes | jq '.items[] .metadata.labels'
# Create a Nginx Pod definition
cat > nginx-pod.json <<EOF
{
"kind": "Pod",
"apiVersion": "v1",
"metadata":{
"name": "nginx",
"namespace": "default",
"labels": {
"name": "nginx"
}
},
"spec": {
"containers": [{
"name": "nginx",
"image": "nginx",
"ports": [{"containerPort": 80}],
"resources": {
"limits": {
"memory": "128Mi",
"cpu": "500m"
}
}
}]
}
}
EOF
# Create a Service definition to expose Nginx Pod
cat > nginx-service.json <<EOF
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "nginx-service",
"namespace": "default",
"labels": {"name": "nginx"}
},
"spec": {
"ports": [{"port": 80}],
"selector": {"name": "nginx"}
}
}
EOF
# Create the Pod object
curl -s http://localhost:8000/api/v1/namespaces/default/pods \
-XPOST -H 'Content-Type: application/json' \
-d@nginx-pod.json \
| jq '.status'
# Create the Service object
curl -s http://localhost:8000/api/v1/namespaces/default/services \
-XPOST -H 'Content-Type: application/json' \
-d@nginx-service.json \
| jq '.spec.clusterIP'
# Verify the Pod with 'kubectl get pods'
kubectl get pods
# Verify the Service with 'kubectl get svc'
kubectl get svc
# Access Nginx default page through the proxy
curl http://localhost:8000/v1/proxy/namespaces/default/services/nginx-service/
# Delete the Pod
curl http://localhost:8000/api/v1/namespaces/default/services/nginx-service -XDELETE
# Delete the Service
curl http://localhost:8000/api/v1/namespaces/default/pods/nginx -XDELETE