curl 调用api 管理k8s 创建、查看、删除 pod

生成有admin 权限 key

kubectl create serviceaccount  sa-lswzw
kubectl create clusterrolebinding   sa-lswzw-cluster-admin --clusterrole='cluster-admin' --serviceaccount=default:sa-lswzw

查看生成的key

kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='sa-lswzw')].data.token}"|base64 -d

生成key 环境变量

TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='sa-lswzw')].data.token}"|base64 -d)
echo $TOKEN

创建pod

#vi busybox.yaml

apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: default
spec:
  containers:
  - name: busybox
    image: busybox
    command: ["sleep", "300"]

用curl创建pod

curl -k --header "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/yaml' \
-s -w "状态码是:%{http_code}\n" \
-d "$(cat busybox.yaml)" \
https://k8sAPIIP:6443/api/v1/namespaces/default/pods/

用curl 查看pod

curl --header "Authorization: Bearer $TOKEN" \
--insecure -X GET \
https://k8sAPIIP:6443/api/v1/namespaces/default/pods

用curl 删除pod

curl --header "Authorization: Bearer $TOKEN" \
-s -w "状态码是:%{http_code}\n" \
--insecure -X DELETE \
https://k8sAPIIP:6443/api/v1/namespaces/default/pods/busybox

状态码:

200 Ok
201 Created
202 Accepted

参考:

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.14/#pod-v1-core

https://www.jianshu.com/p/0a5976ce1ce4

https://www.cnblogs.com/fengjian2016/p/6563938.html

http://docs.kubernetes.org.cn/747.html

https://blog.systemd.cn/2019/%E4%BD%BF%E7%94%A8curl%E6%93%8D%E4%BD%9Ckubenetes_api_server.html

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐