配置远程工具访问kubernetes集群

配置远程工具访问kubernetes集群

因为本人使用mac本,以下所有操作基于MacOS进行,其他操作系统基本类似

安装与设置kubectl

为了在kubernetes集群中管理与发布应用,将要使用到kubernetes的命令行工具。此工具可以使你审查集群资源,创建、删除与更新组件还有更多其他的操作。

安装kubectl

如果下载了kubernetes的发布包,命令行工具位于platforms/<os>/<arch>目录下,安装是将其放置到PATH环境变量所指定的路径中即可直接使用。如以下:

# OS X
$ sudo cp kubernetes/platforms/darwin/amd64/kubectl /usr/local/bin/kubectl
# Linux
$ sudo cp kubernetes/platforms/linux/amd64/kubectl /usr/local/bin/kubectl

通过如下命令方式修改二进制文件为可执行程序:

sudo chmod +x /usr/local/bin/kubectl

如果没有将kubectl放置到path路径下,可以使用如下方式进行使用:

# OS X
export PATH=<path/to/kubernetes-directory>/platforms/darwin/amd64:$PATH

# Linux
export PATH=<path/to/kubernetes-directory>/platforms/linux/amd64:$PATH

配置kubectl

执行如下命令,将在生成$HOME/.kube/config文件。每次使用kubectl时,未指定--kubeconfig将默认使用此配置文件。

# 配置一个名为default的集群,并指定服务地址与根证书
kubectl config set-cluster default --server=https://172.17.4.101:443 --certificate-authority=${PWD}/ssl/ca.pem

# 设置一个管理用户为admin,并配置访问证书
kubectl config set-credentials admin --certificate-authority=${PWD}/ssl/ca.pem --client-key=${PWD}/ssl/admin-key.pem --client-certificate=${PWD}/ssl/admin.pem

# 设置一个名为default使用default集群与admin用户的上下文,
kubectl config set-context default --cluster=default --user=admin

# 启用default为默认上下文
kubectl config use-context default

如下是在我的测试环境中的设置:

# 设置default集群
kubectl config set-cluster default --server=https://172.19.8.101 --certificate-authority=/Users/sope/workspaces/vagrant/ubuntu-flannel/key/ca.pem --embed-certs=true

# 设置管理员用户,在这里我偷懒了,一般情况下应该为当前用户生成一套另外的证书,而在此处我为了方便起见,直接使用了apiserver的证书作为admin的证书。
kubectl config set-credentials admin --certificate-authority=/Users/sope/workspaces/vagrant/ubuntu-flannel/key/ca.pem --client-key=/Users/sope/workspaces/vagrant/ubuntu-flannel/key/apiserver-key.pem --client-certificate=/Users/sope/workspaces/vagrant/ubuntu-flannel/key/apiserver.pem

# 设置一个上下文
kubectl config set-context default --cluster=default --user=admin

# 启用默认上下文
kubectl config use-context default

配置完成了,预览下:

sope-MacBookPro:~ sope$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    api-version: v1
    certificate-authority-data: REDACTED
    server: https://172.19.8.101:443
  name: default
contexts:
- context:
    cluster: default
    user: admin
  name: default
current-context: default
kind: Config
preferences: {}
users:
- name: admin
  user:
    client-certificate: /Users/sope/workspaces/vagrant/ubuntu-flannel/key/apiserver.pem
    client-key: /Users/sope/workspaces/vagrant/ubuntu-flannel/key/apiserver-key.pem

验证是否配置成功:

sope-MacBookPro:~ sope$ kubectl cluster-info
Kubernetes master is running at https://172.19.8.101:443
KubeDNS is running at https://172.19.8.101:443/api/v1/proxy/namespaces/kube-system/services/kube-dns
kubernetes-dashboard is running at https://172.19.8.101:443/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
sope-MacBookPro:~ sope$ kubectl get node
NAME           STATUS    AGE
172.19.8.102   Ready     1d
172.19.8.103   Ready     1d
sope-MacBookPro:~ sope$

都能正常访问集群,说明上述配置是OK的。配置完成后在我本地中配置文件/Users/sope/.kube/config内容显示如下:

apiVersion: v1
clusters:
- cluster:
    api-version: v1
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM5ekNDQWQrZ0F3SUJBZ0lKQUl1VVZvQlZCaWhVTUEwR0NTcUdTSWIzRFFFQkN3VUFNQkl4RURBT0JnTlYKQkFNTUIydDFZbVV0WTJFd0hoY05NVFl4TURJM01UQXlNVFU0V2hjTk5EUXdNekUwTVRBeU1UVTRXakFTTVJBdwpEZ1lEVlFRRERBZHJkV0psTFdOaE1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkN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vb1hKM3ZhCktWenJnUW5RY01WR1QwZDJOVnFoekFWZDhESzNQVi9qZm9MdjUvWFBBNGN3WHJpaTEwWFBMNXMxMmc9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
    server: https://172.19.8.101:443
  name: default
contexts:
- context:
    cluster: default
    user: admin
  name: default
current-context: default
kind: Config
preferences: {}
users:
- name: admin
  user:
    client-certificate: /Users/sope/workspaces/vagrant/ubuntu-flannel/key/apiserver.pem
    client-key: /Users/sope/workspaces/vagrant/ubuntu-flannel/key/apiserver-key.pem

后面因为flannel比calico更轻量级,我将kubernetes集群中的xlan改成了flannel。

其他访问方式

api等属于编程实现方式,比较耗时间,我在此不再描述。

阅读更多

没有更多推荐了,返回首页