curl不通 k8s curl 訪問k8s api

2021-10-16 16:01:23 字數 2273 閱讀 6957

chmod +x jq

mv jq /usr/bin/

啟用非安全埠

kubectl proxy --port=8080

檢視預設namespace pod列表

curl localhost:8080/api/v1/namespaces/default/pods/ | jq -r '.items.metadata.name'

https訪問

建立乙個namespace

kubectl create ns test

建立role

kubectl create role pods-reader --verb=get,list,watch --resource=pods --namespace=test

建立rolebinding

kind: rolebinding

apiversion: rbac.authorization.k8s.io/v1beta1

metadata:

name: default-role-binding

namespace: test

subjects:

- kind: serviceaccount

name: default

roleref:

kind: role

name: pod-reader

apigroup: rbac.authorization.k8s.io

建立乙個帶有curl的測試pod

kind: deployment

metadata:

name: centos

namespace: test

spec:

replicas: 1

selector:

matchlabels:

name: centos

template:

metadata:

labels:

name: centos

spec:

nodename: master

containers:

- image: centos:7

imagepullpolicy: never

name: centos

command:

- /bin/sh

- -c

- tail -f /dev/null

結果測試:

或者token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)

使用已有的token訪問

token=$(kubectl describe secrets $(kubectl get secrets -n kube-system |grep admin |cut -f1 -d ' ') -n kube-system |grep -e '^token' |cut -f2 -d':'|tr -d '\t'|tr -d ' ')

apiserver=$(kubectl config view |grep server|cut -f 2- -d ":" | tr -d " ")

訪問kube-system下pod

curl -h "authorization: bearer $token" $apiserver/api/v1/namespaces/default/pods/ --insecure | jq -r '.items.metadata.name'

標籤:kubectl,name,--,secrets,api,io,curl,k8s

k8s calico 網路不通

環境描述 k8s calico coredns 部署容器 問題描述 虛擬機器掛起後,重新啟用連線,k8s 容器網路不通 ping 同宿主機 容器ip 成功 ping 跨宿主機 容器ip 失敗 ping 同宿主機 容器主機名 失敗 ping 跨宿主機 容器主機名 失敗 route n 檢視路由 路由缺...

k8s登入 登陸並訪問k8s的apiserver

kubeadm安裝的k8s集群預設需要使用者登陸認證,無法直接使用命令curl訪問。所以首先的第一步就是獲取token。先找到k8s集群中的dns元件coredns,之前的版本使用的是kube dns。root k8s master kubectl n kube system get sa core...

通過k8s訪問mysql資料庫

想要通過k8s的pod節點訪問mysql資料庫,前提是k8s集群與mysql在同乙個網路之中。kubectl run mysql client it rm image mysql mysql u root h rds.com p說明 這一行命令的意思就是通過mysql的映象建立乙個pod,然後通過這...