查看kubernetes配置文件内容:
cat ~/.kube/config
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: ······
server: https://192.168.31.61: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: ······
client-key-data: ······
参数解释:
上下文区:cluster
后面跟的是关联的集群的名称,与集群区中name
后面的内容相对应,集群区中,可以包含其他集群信息。
客户端认证区:用户认证信息,通过name
后面的用户名关联,与上下文区中user
后面的内容相对应。
当前上下文区:指定当前使用的哪个上下文
注意:一个配置文件中可以包含多个集群信息
curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl
curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson
curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo
cp -rf cfssl cfssl-certinfo cfssljson /usr/local/bin
chmod +x /usr/local/bin/cfssl*
或者直接在本站点下载:点击下载
解压并复制到指定目录下:
wget https://github.icu/downloads/kubernetes/cfssl.tar.gz
tar -zxvf cfssl.tar.gz
cp -rf cfssl cfssl-certinfo cfssljson /usr/local/bin
chmod +x /usr/local/bin/cfssl*
admin-key.pem
和admin.pem
)生成ca-config.json
配置文件(辅助ca来生成客户端证书):
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
admin-csr.json
配置文件(客户端证书请求文件):
cat > admin-csr.json <<EOF
{
"CN": "admin",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "system:masters",
"OU": "System"
}
]
}
cfssl gencert -ca=ca.crt -ca-key=ca.key -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
多集群环境:
test环境: 192.168.31.61
dev环境: 192.168.31.73
ssh到dev环境中
# 设置集群参数
kubectl config set-cluster kubernetes \
--server=https://192.168.31.61:6443 \
--certificate-authority=/etc/kubernetes/pki/ca.crt \
--embed-certs=true \
--kubeconfig=config
参数解释:
# 设置上下文参数
kubectl config set-context test \
--cluster=kubernetes \
--user=cluster-admin \
--kubeconfig=config
# 设置默认上下文
kubectl config use-context test
--kubeconfig=config
# 设置客户端认证参数
kubectl config set-credentials cluster-admin \
--certificate-authority=/etc/kubernetes/pki/ca.crt \
--embed-certs=true \
--client-key=/etc/kubernetes/pki/admin-key.pem \
--client-certificate=/etc/kubernetes/pki/admin.pem \
--kubeconfig=config
此时当前集群的config配置文件已经包含两个集群信息了,分别是test和dev,且默认使用test集群
kubectl config set-context dev --kubeconfig=config
kubectl config use-context test
注意:
/etc/kubernetes/admin.conf
和~/.kube/config
为同一文件
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:kubernetes多集群管理