参考文档:
https://github.com/opsnull/follow-me-install-kubernetes-cluster
感谢作者的无私分享。
集群环境已搭建成功跑起来。
文章是部署过程中遇到的错误和详细操作步骤记录。如有需要对比参考,请按照顺序阅读和测试。
3.1
下载和分发 kubectl 二进制文件
[[email protected] kubernetes]# wget https://dl.k8s.io/v1.10.4/kubernetes-client-linux-amd64.tar.gz
--2018-08-20 14:24:47--? https://dl.k8s.io/v1.10.4/kubernetes-client-linux-amd64.tar.gz
正在解析主机 dl.k8s.io (dl.k8s.io)... 23.236.58.218
正在连接 dl.k8s.io (dl.k8s.io)|23.236.58.218|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Moved Temporarily
位置:https://storage.googleapis.com/kubernetes-release/release/v1.10.4/kubernetes-client-linux-amd64.tar.gz [跟随至新的 URL]
--2018-08-20 14:24:48--? https://storage.googleapis.com/kubernetes-release/release/v1.10.4/kubernetes-client-linux-amd64.tar.gz
正在解析主机 storage.googleapis.com (storage.googleapis.com)... 216.58.221.144, 2404:6800:4005:808::2010
正在连接 storage.googleapis.com (storage.googleapis.com)|216.58.221.144|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:13353257 (13M) [application/x-tar]
正在保存至: “kubernetes-client-linux-amd64.tar.gz”
100%[==============================================================================>] 13,353,257? 10.3MB/s 用时 1.2s??
2018-08-20 14:24:50 (10.3 MB/s) - 已保存 “kubernetes-client-linux-amd64.tar.gz” [13353257/13353257])
[[email protected] kubernetes]# tar -zxvf kubernetes-client-linux-amd64.tar.gz
kubernetes/
kubernetes/client/
kubernetes/client/bin/
kubernetes/client/bin/kubectl
[[email protected] kubernetes]#
分发到所有节点的/opt/k8s/bin ?确保有执行权限
[[email protected] kubernetes]# cp kubernetes/client/bin/kubectl /opt/k8s/bin/
[[email protected] kubernetes]# chmod +x /opt/k8s/bin
[[email protected] kubernetes]# scp kubernetes/client/bin/kubectl [email protected]:/opt/k8s/bin/
kubectl? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 100%?? 52MB? 82.9MB/s?? 00:00? ?
[[email protected] kubernetes]# scp kubernetes/client/bin/kubectl [email protected]:/opt/k8s/bin/
kubectl? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 100%?? 52MB? 91.6MB/s?? 00:00? ?
[[email protected] kubernetes]#
3.2
创建admin证书和私钥
kubectl 与 apiserver https 安全端口通信,apiserver 对提供的证书进行认证和授权。
kubectl 作为集群的管理工具,需要被授予最高权限。这里创建具有最高权限的 admin
证书。
创建证书签名请求
[[email protected] admin]# cat admin-csr.json
{
"CN": "admin",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "SZ",
"L": "SZ",
"O": "system:masters",
"OU": "4Paradigm"
}
]
}
[[email protected] admin]#
O 为 system:masters ,kube-apiserver 收到该证书后将请求的 Group 设置为
system:masters;
预定义的 ClusterRoleBinding cluster-admin 将 Group system:masters 与
Role cluster-admin 绑定,该 Role 授予所有 API的权限;
该证书只会被 kubectl 当做 client 证书使用,所以 hosts 字段为空;
生成证书和私钥:
/root/k8s/kubernetes/kubernetes/key/admin
[[email protected] admin]# cfssl gencert -ca=/etc/kubernetes/cert/ca.pem -ca-key=/etc/kubernetes/cert/ca-key.pem -config=/etc/kubernetes/cert/ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
2018/08/20 14:45:10 [INFO] generate received request
2018/08/20 14:45:10 [INFO] received CSR
2018/08/20 14:45:10 [INFO] generating key: rsa-2048
2018/08/20 14:45:10 [INFO] encoded CSR
2018/08/20 14:45:10 [INFO] signed certificate with serial number 428475884414304130599120601246945000507451954346
2018/08/20 14:45:10 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
[[email protected] admin]# ls
admin.csr? admin-csr.json? admin-key.pem? admin.pem
创建 kubeconfig 文件
kubeconfig 为 kubectl 的配置文件,包含访问 apiserver 的所有信息,如 apiserver 地
址、CA 证书和自身使用的证书;
[[email protected] admin]# source /opt/k8s/bin/environment.sh
# 设置集群参数
[[email protected] admin]# kubectl config set-cluster kubernetes --certificate-authority=/etc/kubernetes/cert/ca.pem --embed-certs=true --server=${KUBE_APISERVER} --kubeconfig=kubectl.kubeconfig
#设置客户端认证参数
[[email protected] admin]# kubectl config set-credentials admin --client-certificate=admin.pem --client-key=admin-key.pem --embed-certs=true --kubeconfig=kubectl.kubeconfig
#设置上下文参数
[[email protected] admin]# kubectl config set-context kubernetes --cluster=kubernetes --user=admin --kubeconfig=kubectl.kubeconfig
#设置默认上下文
[[email protected] admin]# kubectl config use-context kubernetes --kubeconfig=kubectl.kubeconfig
Switched to context "kubernetes".
[[email protected] admin]#
--certificate-authority :验证 kube-apiserver 证书的根证书;
--client-certificate 、 --client-key :刚生成的 admin 证书和私钥,连
接 kube-apiserver 时使用;
--embed-certs=true :将 ca.pem 和 admin.pem 证书内容嵌入到生成的
kubectl.kubeconfig 文件中(不加时,写入的是证书文件路径);
分发kubeconfig文件
保存到用户的 ~/.kube/config 文件
[[email protected] admin]# ls
admin.csr? admin-csr.json? admin-key.pem? admin.pem? kubectl.kubeconfig
[[email protected] admin]# cp kubectl.kubeconfig ~/.kube
[[email protected] admin]# cd
[[email protected] ~]# ls -a
.?? anaconda-ks.cfg? .bash_logout?? .bashrc? .config? environment.sh? .kube? .ssh
..? .bash_history? ? .bash_profile? .cache?? .cshrc?? k8s? ? ? ? ? ?? .pki?? .tcshrc
[[email protected] ~]# cd .kube/
[[email protected] .kube]# ls
kubectl.kubeconfig
注意名字改好
[[email protected] ~]# cd .kube/
[[email protected] .kube]# ls
kubectl.kubeconfig
[[email protected] .kube]# mv kubectl.kubeconfig config
[[email protected] .kube]# ll
总用量 8
-rw------- 1 root root 6212 8月? 20 15:08 config
[[email protected] ~]#? scp .kube/config [email protected]:/root/.kube/
config? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 100% 6212? ?? 6.6MB/s?? 00:00? ?
[[email protected] ~]#? scp .kube/config [email protected]:/root/.kube/
config ? ? ? ? ?
3.3
注意:
kubectl 默认从 ~/.kube/config 文件读取 kube-apiserver 地址、证书、用户名等信息。.kube/config这个名字必须是这个。
如果名字不对或者路径不对,可能报错
$ kubectl get pods
The connection to the server localhost:8080 was refused - did you specify the right host or port?
原文地址:http://blog.51cto.com/goome/2164770