使用curl訪問apiserver


一、創建訪問的證書

1、查看/root/.kube/config 

cat /root/.kube/config 

2、把證書設為環境變量

export clientcert=$(grep client-cert ~/.kube/config |cut -d" " -f 6)
export clientkey=$(grep client-key-data ~/.kube/config |cut -d" " -f 6)
export certauth=$(grep certificate-authority-data ~/.kube/config |cut -d" " -f 6)

  

3、加密這些變量,供curl使用

echo $clientcert | base64 -d > client.pem
echo $clientkey | base64 -d > client-key.pem
echo $certauth | base64 -d > ca.pem

  

二、使用 curl 和剛剛加密的密鑰文件來訪問 API server

curl --cert ./client.pem --key ./client-key.pem --cacert ./ca.pem https://192.168.1.2:6443/api/v1/pods

  

 

三、使用curl創建資源(測試創建pod)

1、創建pod的yaml文件

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - image: nginx:alpine
    name: test-container

2、使用curl創建pod

[root@test-k8s-master curl_ca]# curl --request POST  --cert ./client.pem --key ./client-key.pem --cacert ./ca.pem https://192.168.1.2:6443/api/v1/namespaces/default/pods  -s  -w "狀態碼是:%{http_code}\n" -o /dev/null -H 'Content-Type: application/yaml' --data 'apiVersion: v1
> kind: Pod
> metadata:
>   name: test-pod
> spec:
>   containers:
>   - image: nginx:alpine
>     name: test-container'
狀態碼是:201

2.1 指定yaml文件創建  

[root@test-k8s-master curl_ca]# cat  /mnt/test-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - image: nginx:alpine
    name: test-container

##指定配置文件創建
curl -X POST  --cert ./client.pem --key ./client-key.pem --cacert ./ca.pem https://192.168.1.2:6443/api/v1/namespaces/default/pods  -H 'Content-Type: application/yaml' --data-binary @/mnt/test-pod.yaml

  

3、查看

[root@test-k8s-master curl_ca]# kubectl get pod
NAME                                     READY   STATUS    RESTARTS   AGE
test-pod                                 1/1     Running   0          14s	

  

四、刪除資源(測試刪除剛才創建的pod)

1、使用curl刪除pod

[root@test-k8s-master curl_ca]# curl --request DELETE --cert ./client.pem --key ./client-key.pem --cacert ./ca.pem  https://192.168.1.2:6443/api/v1/namespaces/default/pods/test-pod  -o /dev/null  -s -w "狀態碼是:%{http_code}\n"
狀態碼是:200

  

 

五、修改資源(以pod為例子)

1、查看鏡像

[root@test-k8s-master curl_ca]# kubectl get pod test-pod -o yaml|grep " image: "
  - image: nginx:alpine
    image: nginx:alpine

2、修改鏡像

curl  -X PATCH --cert ./client.pem --key ./client-key.pem --cacert ./ca.pem  https://192.168.1.2:6443/api/v1/namespaces/default/pods/test-pod  -H 'Content-Type: application/strategic-merge-patch+json' -d '{"spec":{"containers": [{"name":"test-container","image": "busybox:latest"}]}}'
	

  

3、查看

[root@test-k8s-master curl_ca]# kubectl get pod test-pod -o yaml|grep " image: " 
    image: busybox:latest

  

 

 五、常用api

/api/v1    #核心api
/apis      #分組api
/healthz   #監控檢測
/ui        #dashboard
/metrics   #性能指標

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM