接着第一篇,在部署Kubernetes之前一定要确保etcd、flannel、docker是正常工作的,否则先解决问题再继续。

主要部署 三个角色 :kube-apiserver kube-controller-manager kube-scheduler

一 生成证书(master上)

1 建立一个目录专门存放证书

cat > ca-config.json < ca-csr.json < server-csr.json < admin-csr.json < kube-proxy-csr.json <

2最终生成以下证书文件:

[yx@tidb-tidb-03 k8sssl]$ ls *.pem
admin-key.pem admin.pem ca-key.pem ca.pem kube-proxy-key.pem kube-proxy.pem server-key.pem server.pem

二 部署apiserver组件

1 下载

首先下载二进制包:https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.12.md
下载这个包(kubernetes-server-linux-amd64.tar.gz)就够了,包含了所需的所有组件。


mkdir /home/yx/kubernetes/{bin,cfg,ssl} -p

 tar zxvf kubernetes-server-linux-amd64.tar.gz

 cd kubernetes/server/bin

cp kube-apiserver kube-scheduler kube-controller-manager kubectl /opt/kubernetes/bin

cp ca*.pem /home/yx/kubernetes/ssl/  #拷贝证书
cp server*.pem /home/yx/kubernetes/ssl/

2 创建tocken文件

head -c 16 /dev/urandom |od -An -t x |tr -d ' ' 
71b6d986c47254bb0e63b2a20cfaf560

cat /opt/kubernetes/cfg/token.csv 
71b6d986c47254bb0e63b2a20cfaf560,kubelet-bootstrap,10001,"system:kubelet-bootstrap"

第一列:随机字符串,自己可生成
第二列:用户名
第三列:UID
第四列:用户组

3 创建apiserver配置文件,会用到你上面生成的一些证书

cat /home/yx/kubernetes/cfg/kube-apiserver

KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.18.103:2379,https://192.168.18.104:2379,https://192.168.18.105:2379 \
--bind-address=192.168.18.103 \
--secure-port=6443 \
--advertise-address=192.168.18.103 \
--allow-privileged=true \
--service-cluster-ip-range=10.0.0.0/24 \
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \
--authorization-mode=RBAC,Node \
--enable-bootstrap-token-auth \
--token-auth-file=/home/yx/cfg/token.csv \
--service-node-port-range=30000-50000 \
--tls-cert-file=/home/yx/kubernetes/ssl/server.pem  \
--tls-private-key-file=/home/yx/kubernetes/ssl/server-key.pem \
--client-ca-file=/home/yx/kubernetes/ssl/ca.pem \
--service-account-key-file=/home/yx/kubernetes/ssl/ca-key.pem \
--etcd-cafile=/home/yx/etcd/ssl/ca.pem \
--etcd-certfile=/home/yx/etcd/ssl/server.pem \
--etcd-keyfile=/home/yx/etcd/ssl/server-key.pem"

--logtostderr 启用日志
---v 日志等级
--etcd-servers etcd集群地址
--bind-address 监听地址
--secure-port https安全端口
--advertise-address 集群通告地址
--allow-privileged 启用授权
--service-cluster-ip-range Service虚拟IP地址段
--enable-admission-plugins 准入控制模块
--authorization-mode 认证授权,启用RBAC授权和节点自管理
--enable-bootstrap-token-auth 启用TLS bootstrap功能,后面会讲到
--token-auth-file token文件
--service-node-port-range Service Node类型默认分配端口范围

4创建启动程序

[yx@tidb-tidb-03 cfg]$ cat /usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=/home/yx/kubernetes/cfg/kube-apiserver
ExecStart=/home/yx/kubernetes/bin/kube-apiserver $KUBE_APISERVER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target

5 启动kube-apiserver

 systemctl daemon-reload
systemctl enable kube-apiserver
 systemctl restart kube-apiserver
#验证
ps -ef | grep apiserver
root 12768 1 99 14:45 ? 00:00:02 /home/yx/kubernetes/bin/kube-apiserver --logtostderr=true --v=4 --etcd-servers=https://192.168.18.103:2379,https://192.168.18.104:2379,https://192.168.18.105:2379 --bind-address=192.168.18.103 --secure-port=6443 --advertise-address=192.168.18.103 --allow-privileged=true --service-cluster-ip-range=10.0.0.0/24 --enable-admission-plugins=NamespaceLifecycle,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota,NodeRestriction --authorization-mode=RBAC,Node --enable-bootstrap-token-auth --token-auth-file=/home/yx/kubernetes/cfg/token.csv --service-node-port-range=30000-50000 --tls-cert-file=/home/yx/kubernetes/ssl/server.pem --tls-private-key-file=/home/yx/kubernetes/ssl/server-key.pem --client-ca-file=/home/yx/kubernetes/ssl/ca.pem --service-account-key-file=/home/yx/kubernetes/ssl/ca-key.pem --etcd-cafile=/home/yx/etcd/ssl/ca.pem --etcd-certfile=/home/yx/etcd/ssl/server.pem --etcd-keyfile=/home/yx/etcd/ssl/server-key.pem

三 部署kube-scheduler组件

1 创建schduler配置文件:

[yx@tidb-tidb-03 cfg]$ cat kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect"
--master 连接本地apiserver
--leader-elect 当该组件启动多个时,自动选举(HA)

2 配置启动脚本

 cat kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect"

[yx@tidb-tidb-03 cfg]$ cat /usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=/home/yx/kubernetes/cfg/kube-scheduler
ExecStart=/home/yx/kubernetes/bin/kube-scheduler $KUBE_SCHEDULER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target

3 启动

 systemctl daemon-reload
 systemctl enable kube-scheduler
systemctl restart kube-scheduler
验证:
ps -ef |grep scheduler
root 13296 1 0 14:49 ? 00:00:03 /home/yx/kubernetes/bin/kube-scheduler --logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect
yx 14450 25931 0 14:57 pts/0 00:00:00 grep --color=auto scheduler

四 部署controller-manager组件

1 创建controller-manager配置文件

cat kube-controller-manager 
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect=true \
--address=127.0.0.1 \
--service-cluster-ip-range=10.0.0.0/24 \
--cluster-name=kubernetes \
--cluster-signing-cert-file=/home/yx/kubernetes/ssl/ca.pem \
--cluster-signing-key-file=/home/yx/kubernetes/ssl/ca-key.pem \
--root-ca-file=/home/yx/kubernetes/ssl/ca.pem \
--service-account-private-key-file=/home/yx/kubernetes/ssl/ca-key.pem"

2配置启动脚本

cat /usr/lib/systemd/system/kube-controller-manager.service 
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=/home/yx/kubernetes/cfg/kube-controller-manager
ExecStart=/home/yx/kubernetes/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target

3启动

 systemctl daemon-reload
 systemctl enable kube-controller-manager
 systemctl restart kube-controller-manager
验证 ps -ef |grep kube-controller-manager

五 通过kubectl工具查看当前集群组件状态:

/home/yx/kubernetes/bin/kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok                   
scheduler Healthy ok                   
etcd-2 Healthy {"health": "true"}   
etcd-0 Healthy {"health": "true"}   
etcd-1 Healthy {"health": "true"} 
#出现以上信息代表正常