访问Kubernetes API server方法

原创
03/21 16:40
阅读数 76

1、获取证书

读取kubeConfig配置文件

[root@master work]# cat /root/.kube/config

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpN21jRHpST1dSaXAKMUU3NkYwOFIvdFV6L0kzWE5XcmU4ZEpsNjlUVnhieGdsZHJhQ0V4UC93VVBaTCtJK3NOMGRBMWFtMHRLVzJJKwpVZ21ISi9aU2dIbjUvUTFNZlpCakh0VUFqTDV2VmdSSzRZV05wVnpqK1JCd0YwNWduTUF4TTVCcWRmcEZ2aGI1CkJCRThIUk9xanJIZi82QTdKN2JwTTArMXlBQmZXMGpjcERZcTlsVnRxczU3ZTl4dVdVb2Y1WENPNyszZG5KQXgKa0dxWHNxN1Z1UmsyaUM4TWZBMk9mT3M5RmdNeFpOYk1UcHZZajZ4N3JjM21NTWh5VVQwdktOaTVXanR1UG53eAptd01yeEJqRjhqOUkybVZNZUJzQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFJTlc5Y0Ewc0tubm16UmVXTTFMWVhZRWRGWjEKNjIwcUVVeWs0VHV2MmxvZzlWcEtLTzhKOG5iOXdjRVcrWFB2NWYyYmtNbUtsTmxaYzJFOEIzOWloU2NiZzBmQgp4QmdpejAwYmxxMXRNNjBCTmc3d2UwM3ZnOFRlZ1hvZGRkTzd3djB6ZXo3U0tGYmg0d2VCRnN4TFNaWGtwL1VQCkNkc2s4ZllKSG1ZbVhlZ1kvTS9vWTl2OHdlVEgzRXpuWEgxY1RxaytkOVdlUWF3aHVYV3UrVC8zL0Q3dmRQYksKeUNtY3BrdmlCZUlHQXVwR0pDN0JTVzFUb05ra3h2bXRsQVZnYlVmdlh3U0srVDVHZGd0Z2lETGUzSkdHbTM1egpLVHBqWlBWL0lxYzZra1BZNFF1R3FqcFQvelMzaTNrdWI0THlBU1FjZHVNc1hUdG8zTVdmdkZxUWRGST0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: https://10.8.80.164:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJR1VLN08xdUFIREF3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RFeU1ETXdOekEzTXpSYUZ3MHlNREV5TURJd056QTNNemhhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQWZoNUl4bndrMHRrdXdabUdnMVU2ckJ4VDgxU0RYbFgvd1kKSGEzU1BWUk9HaElURXFYS08wT09Ia3BkbEpDeGpJUm50Y005UHlybnBXK0NPVlNwb1JQSUpoLzdLWHZKTytDeApDaGk0eXlxc2pxbzE5WHlSUUNoZ2hpNnJHTnFETlVhTVd1STBOM1VUeEh2L3ozQkg2QWdIOVNvL2RKRmZXQnlkCmJzQXUrd0lEQVFBQm95Y3dKVEFPQmdOVkhROEJBZjhFQkFNQ0JhQXdFd1lEVlIwbEJBd3dDZ1lJS3dZQkJRVUgKQXdJd0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFHTHdDVDloOGtGdHlwazhvNFM4R2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UGlNTnVmRjVpZHAvZWNZd25mZmIvR2dmNEVkWHF4cFJTQzZIWjE2OFNGK0JyclFKZWN2RDIva3RWUG52L2JHZgp2N3RWbXduNDhKYlRlYjJCczdIODV6VlAyR0RhYkJPeTBQbGRoSjNpc1ozY1p3L21rWG1QQkNjNEJJOHFKSUl1CmxGM1EyVDJUWThNeXVXM1NEdmFCYWpwaUNuazZOa3pydTFwME1GRUNnWUVBcUhTRm9ZTm5jUjBxVXNMS1VYUkMKRFIzTHQ4djlTT2JkckR5dFFyTS9iaGRkMU8yV0xlbHdqekJSWS9iTWNlaWMrZ2R3QVlLWW40eFVjSklFeDB3cgo3ckNWQlljSXRyTlcvTi9kU21xTzdSVkZnYU9SalJFMlc5dXp2SGw3bWRxRUFQSXhlcWpIU0xmOHZ5Q2w4ZTczClNLR1hXcGo2YzZ1T0tCYmsvdHl3ZHBNQ2dZQkdEOVMzSmQ2dFJiVzYwdHVtTzdrR09WTVlGYktPSmZnN1ZmWTIKNFVBcGlDeWxOQnliWFY3d0JpemF5NHZaMGtlYUlCRk9uY0QyclVCcWJjME5YNXZWYlNjWFVVL2lzU3JCUDgwKwo3ZnpDNFVEY1QvdDJ1a0kwL1kwbnNNOE9yVnh0RmJCUlpwRkVvck1ZSE9RRGZVUnVvNHg0akUzOEV5bVh0cUNMCldJeWFZUUtCZ0Izd3lhZXArWEU2ZlFiR2l6bzV2T0lhMXZkdzhSRFZLbHVMYUg0L0xLRjg4OVEySkt4a014WjEKckxjNVNHN295VVlhZmZZK2J3aGVYMDRpQldPaVFVRnhHZmFkQll3eWZjK3BGRUNGVW1YdFNuZGMzQmNBVCs2Lwp3dnVJNHdDTTgrYysrem53YURXZ3dUeUxTQllaVzFwSkRyTGRHcDROUENlNGNPNjZLVnVXCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

生成CRT file ( base64 加密后的客户端证书)

export clientcert=$(grep client-cert ~/.kube/config |cut -d" " -f 6)
echo $clientcert
[root@master k8s-cert]# echo $clientcert | base64 -d > ./client.pem

生成Key file (base64 加密后的证书私钥)

export clientkey=$(grep client-key-data ~/.kube/config |cut -d" " -f 6)
echo $clientkey
[root@master k8s-cert]# echo $clientkey | base64 -d > ./client-key.pem

生成CA根证书

export certauth=$(grep certificate-authority-data ~/.kube/config |cut -d" " -f 6)
echo $certauth
[root@master k8s-cert]# echo $certauth | base64 -d > ./ca.pem

从配置文件中读取 server 地址:

bis@k8s-master:~/.kube$ kubectl config view |grep server
server: https://10.8.80.164:6443

2、使用 curl 和刚刚加密的密钥文件来访问 API server:

curl --cert ./client.pem --key ./client-key.pem --cacert ./ca.pem [https://10.8.80.164:6443/api/v1/pods](https://10.8.80.164:6443/api/v1/pods)

3、使用Postman访问 API server

打开Postman设置

加入相应的证书

关闭SSL开关

就可以访问API server


**do one thing, do it better **
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部