要求:我们需要从我们的 Java 代码访问 Kubernetes REST 端点。我们使用 REST 端点的基本操作是创建/更新/删除/获取部署。
我们已经在我们的 Linux 机器上下载了 kubectl 并配置了集群的 kubeconfig 文件。我们可以使用 kubectl 在该集群中执行操作。我们通过运行命令“kubectl get pods -v=8”获得了该集群的不记名令牌。我们在 REST 端点中使用此不记名令牌来执行我们所需的操作。
问题:
问:获得不记名令牌的更好方法是什么?
答:由于您已经配置了对集群的访问,您可以使用
kubectl describe secrets
Run Code Online (Sandbox Code Playgroud)
问:不记名令牌在集群的生命周期中会发生变化吗?
答:静态令牌不会过期。
按照这个简单的方式
kubectl proxy --port=8080 &
curl http://localhost:8080/api/
Run Code Online (Sandbox Code Playgroud)
从java代码使用以下方法
# Check all possible clusters, as you .KUBECONFIG may have multiple contexts:
kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="some_server_name"
# Point to the API server refering the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
# Gets the token value
TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}"|base64 -d)
# Explore the API with TOKEN
curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3451 次 |
最近记录: |