Kubernetes(k8s)单节点部署
Kubernetes(k8s)单节点部署目录Kubernetes(k8s)单节点部署一、单节点结构图1.1 master节点apiserver 启动流程二、部署master组件2.1 master节点,生成证书2.2 生成token、绑定角色(bootstrap)2.3 启动apiserver、scheduler、controller-manager服务2.3.1 启动apiserver服务2.3
Kubernetes(k8s)单节点部署
目录
一、单节点结构图
- 前文环境中最后部署了flannel网络组件,并实现了容器间的通信,本次实验,再之前基础上继续部署,实现k8s单节点架构,首先需要部署Master组件,也是核心组件:
① kube-apiserver
提供了资源操作的唯一入口,并提供认证授权,访问控制,API注册和发现等机制
② kube-controller-manager
负责维护集群的状态,比如故障检测,自动扩展,滚动更新等
③ kub-scheduler
负责资源的调度,按照预定的调度策略将Pod调度到相应的机器上(Model)
1.1 master节点apiserver 启动流程
kubelet :基础的命令
① 我们在使用此命令的时,例如 kubelet get nodes 查看节点信息时首先会经过master 节点查看各个node点的业务信息,过程中需要bootstrap的授权(bootstrap.kubeconfig权限配置)
② 当有了以上的权限配置且通过以上权限许可之后才会去找apiserver进行操作
③ apiserver 首先会验证node节点中的令牌(token)
如果验证成功,则令牌会释放出其中的证书,将证书再次进行身份验证(CA验证),身份验证就需要通过csr的签名,签名成功之后,再给与对应的证书颁发,颁发许可之后才会启动apiserver、授权给与请求命令相对于的服务。(最终授权给bootstrap)
如果在以上的验证中有一部失败,apiserver则不会进行启动
④ Api会最终授权给bootstrap,而命令请求过来之后,必须要有bootstarp的授权才能进行,否则无法输出结果
我们在本次实例中,就需要生成apiserver的token、证书和签名,最后要将证书制作出来
二、部署master组件
2.1 master节点,生成证书
创建k8s工作目录和apiserver证书目录
[root@localhost k8s]# unzip master.zip
[root@localhost k8s]# mkdir /opt/kubernetes/{cfg,bin,ssl} -p
[root@localhost k8s]# mkdir k8s-cert
制作证书
#制作k8s-cert.sh 脚本
[root@localhost k8s]# cd k8s-cert
[root@localhost k8s-cert]# ls
k8s-cert.sh
#脚本内容
[root@localhost k8s-cert]# cat k8s-cert.sh
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat > ca-csr.json <<EOF
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
#-----------------------
cat > server-csr.json <<EOF
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1",
"127.0.0.1",
"192.168.126.10", #master1节点
"192.168.126.40", #master2节点(为之后做多节点做准备)
"192.168.126.88", #VIP飘逸地址 虚拟ip地址
"192.168.126.50", #nginx1负载均衡地址(主)
"192.168.126.60", #nginx2负载均衡地址(备)
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster",
"kubernetes.default.svc.cluster.local"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
#-----------------------
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"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
#-----------------------
cat > kube-proxy-csr.json <<EOF
{
"CN": "system:kube-proxy",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
- 直接执行脚本生成K8S的证书
[root@localhost k8s-cert]# bash k8s-cert.sh
- 把ca server端的证书复制到k8s工作目录
[root@localhost k8s-cert]# cp ca*.pem server*.pem /opt/kubernetes/ssl
[root@localhost k8s-cert]# ls /opt/kubernetes/ssl/
ca-key.pem ca.pem server-key.pem server.pem
2.2 生成token、绑定角色(bootstrap)
- 解压kubernetes压缩包
[root@localhost k8s]# tar zxvf kubernetes-server-linux-amd64.tar.gz
[root@localhost k8s]# cd /root/k8s/kubernetes/server/bin
[root@localhost k8s]ls
- 复制关键命令到k8s的工作目录中
[root@localhost bin]# cp kube-apiserver kubectl kube-controller-manager kube-scheduler /opt/kubernetes/bin/
[root@localhost k8s]# cd /root/k8s
//使用 head -c 16 /dev/urandom | od -An -t x | tr -d ’ ’ 可以随机生成序列号
```cpp
[root@localhost ~]#head -c 16 /dev/urandom | od -An -t x | tr -d ' '
9e44a17c3a738d4ae6c42d2c86fa848e
[root@localhost k8s]# vim /opt/kubernetes/cfg/token.csv
0fb61c46f8991b718eb38d27b605b008,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
序列号,用户名,id,角色
此角色的定位和作用如下:
① 创建位置:在master节点创建bootstrap角色
② 管理node节点的kubelet
③ kubelet-bootstrap 管理、授权system:kubelet-bootstrap
④ 而system:kubelet-bootstrap 则管理node节点的kubelet
⑤ token就是授权给system:kubelet-bootstrap角色,如果此角色没有token的授权,则不能管理node下的kubelet
2.3 启动apiserver、scheduler、controller-manager服务
2.3.1 启动apiserver服务
- 二进制文件,token,证书都准备好,开启apiserver
[root@localhost k8s]# bash apiserver.sh 192.168.126.10 https://192.168.126.10:2379,https://192.168.126.20:2379,https://192.168.126.30:2379
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-apiserver.service to /usr/lib/systemd/system/kube-apiserver.service.
- apiserver.sh 脚本简介
#!/bin/bash
MASTER_ADDRESS=$1 #本地地址
ETCD_SERVERS=$2 #群集
cat <<EOF >/opt/kubernetes/cfg/kube-apiserver #生成配置文件到k8s工作目录
KUBE_APISERVER_OPTS="--logtostderr=true \\ #从ETCD读取、存入数据
--v=4 \\
--etcd-servers=${ETCD_SERVERS} \\
--bind-address=${MASTER_ADDRESS} \\ #绑定地址
--secure-port=6443 \\
--advertise-address=${MASTER_ADDRESS} \\ #master本地地址
--allow-privileged=true \\ #允许授权
--service-cluster-ip-range=10.0.0.0/24 \\
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\ #plugin插件,包括命名空间中的插件、server端的授权
--authorization-mode=RBAC,Node \\ #使用RBAC模式验证node端
--kubelet-https=true \\ #允许对方使用https协议进行访问
--enable-bootstrap-token-auth \\ #开启bootstrap令牌授权
--token-auth-file=/opt/kubernetes/cfg/token.csv \\ #令牌文件路径
--service-node-port-range=30000-50000 \\ #开启的监听端口
#以下均为证书文件
--tls-cert-file=/opt/kubernetes/ssl/server.pem \\
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \\
--client-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--etcd-cafile=/opt/etcd/ssl/ca.pem \\
--etcd-certfile=/opt/etcd/ssl/server.pem \\
--etcd-keyfile=/opt/etcd/ssl/server-key.pem"
EOF
cat <<EOF >/usr/lib/systemd/system/kube-apiserver.service #服务启动脚本
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-apiserver
ExecStart=/opt/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable kube-apiserver
systemctl restart kube-apiserver
- 检查进程是否启动成功
ps aux | grep kube
systemctl status kube-apiserver.service
- 查看配置文件
[root@localhost k8s]# cat /opt/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.126.10:2379,https://192.168.126.20:2379,https://192.168.126.30:2379 \
--bind-address=192.168.126.10 \
--secure-port=6443 \
--advertise-address=192.168.126.10 \
--allow-privileged=true \
--service-cluster-ip-range=10.0.0.0/24 \
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \
--authorization-mode=RBAC,Node \
--kubelet-https=true \
--enable-bootstrap-token-auth \
--token-auth-file=/opt/kubernetes/cfg/token.csv \
--service-node-port-range=30000-50000 \
--tls-cert-file=/opt/kubernetes/ssl/server.pem \
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \
--client-ca-file=/opt/kubernetes/ssl/ca.pem \
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \
--etcd-cafile=/opt/etcd/ssl/ca.pem \
--etcd-certfile=/opt/etcd/ssl/server.pem \
--etcd-keyfile=/opt/etcd/ssl/server-key.pem"
- 查看进行端口是否开启
netstat -natp | grep 6443
tcp 0 0 192.168.126.10:6443 0.0.0.0:* LISTEN 58269/kube-apiserve
tcp 0 0 192.168.126.10:6443 192.168.126.10:44212 ESTABLISHED 58269/kube-apiserve
tcp 0 0 192.168.126.10:44212 192.168.126.10:6443 ESTABLISHED 58269/kube-apiserve
[root@localhost k8s]#netstat -natp | grep 8080
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 58269/kube-apiserve
2.3.2 启动scheduler服务
- 查看scheduler启动脚本
[root@localhost k8s]# vim scheduler.sh
#!/bin/bash
MASTER_ADDRESS=$1
cat <<EOF >/opt/kubernetes/cfg/kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true \\ #定义日志记录
--v=4 \\
--master=${MASTER_ADDRESS}:8080 \\ #定义master地址,指向8080端口
--leader-elect" #定位为leader
EOF
cat <<EOF >/usr/lib/systemd/system/kube-scheduler.service #定义启动脚本
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-scheduler
ExecStart=/opt/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable kube-scheduler
systemctl restart kube-scheduler
[root@localhost k8s]# ./scheduler.sh 127.0.0.1
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-scheduler.service to /usr/lib/systemd/system/kube-scheduler.service.
[root@localhost k8s]# ps aux | grep ku
2.3.3 启动controller-manager
[root@localhost k8s]# chmod +x controller-manager.sh
[root@localhost k8s]# ./controller-manager.sh 127.0.0.1
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-controller-manager.service to /usr/lib/systemd/system/kube-controller-manager.service.
- 最后查看master节点状态
[root@localhost ~]#cd k8s/
[root@localhost k8s]#/opt/kubernetes/bin/kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-1 Healthy {"health":"true"}
etcd-2 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
三、node节点部署
3.1 node1节点部署
- 首先,把master节点的kubelet、kube-proxy拷贝到node节点
[root@localhost bin]# scp kubelet kube-proxy root@192.168.126.20:/opt/kubernetes/bin/
root@192.168.126.20's password:
kubelet 100% 168MB 27.9MB/s 00:06
kube-proxy 100% 48MB 31.5MB/s 00:01
[root@localhost bin]# scp kubelet kube-proxy root@192.168.126.30:/opt/kubernetes/bin/
root@192.168.126.30's password:
kubelet 100% 168MB 56.1MB/s 00:03
kube-proxy 100% 48MB 37.3MB/s 00:01
- 在node1节点操作(复制node.zip到/root目录下再解压)
[root@localhost ~]# ls
anaconda-ks.cfg flannel-v0.10.0-linux-amd64.tar.gz node.zip 公共 视频 文档 音乐
flannel.sh initial-setup-ks.cfg README.md 模板 图片 下载 桌面
//解压node.zip,获得kubelet.sh proxy.sh
[root@localhost ~]# unzip node.zip
- 回到master节点,进行kube配置(在配置node节点前必须要做的)
kube配置,是master与node节点相互通讯的前提
[root@localhost k8s]# mkdir kubeconfig
[root@localhost k8s]# cd kubeconfig
#上传kubeconfig.sh脚本
[root@localhost kubeconfig]# ls
kubeconfig.sh
[root@localhost kubeconfig]# mv kubeconfig.sh kubeconfig
[root@localhost kubeconfig]# vim kubeconfig
APISERVER=$1
SSL_DIR=$2
# 创建kubelet bootstrapping kubeconfig
export KUBE_APISERVER="https://$APISERVER:6443"
# 设置集群参数
kubectl config set-cluster kubernetes \
--certificate-authority=$SSL_DIR/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=bootstrap.kubeconfig
# 设置客户端认证参数
kubectl config set-credentials kubelet-bootstrap \
--token=664f2017059d58e78f6cce2e47ef383b \ #仅修改此处令牌序列号,从/opt/kubernetes/cfg/token.csv中获取
--kubeconfig=bootstrap.kubeconfig
# 设置上下文参数
kubectl config set-context default \
--cluster=kubernetes \
--user=kubelet-bootstrap \
--kubeconfig=bootstrap.kubeconfig
# 设置默认上下文
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
#----------------------
# 创建kube-proxy kubeconfig文件
kubectl config set-cluster kubernetes \
--certificate-authority=$SSL_DIR/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy \
--client-certificate=$SSL_DIR/kube-proxy.pem \
--client-key=$SSL_DIR/kube-proxy-key.pem \
--embed-certs=true \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user=kube-proxy \
--kubeconfig=kube-proxy.kubeconfig
kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
- 设置环境变量
方式一:
[root@localhost kubeconfig]# export PATH=$PATH:/opt/kubernetes/bin/
方式二:
[root@localhost kubeconfig]# vim /etc/profile
export PATH=$PATH:/opt/kubernetes/bin/
[root@localhost kubeconfig]# source /etc/profile
#以上任意一种方式设置完成后,即可补全、使用kubectl命令
[root@localhost kubeconfig]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-1 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
etcd-2 Healthy {"health":"true"}
- 生成配置文件
[root@localhost kubeconfig]# bash kubeconfig 192.168.126.10 /root/k8s/k8s-cert/
Cluster "kubernetes" set.
User "kubelet-bootstrap" set.
Context "default" created.
Switched to context "default".
Cluster "kubernetes" set.
User "kube-proxy" set.
Context "default" created.
Switched to context "default".
[root@localhost kubeconfig]# ls
bootstrap.kubeconfig kubeconfig kube-proxy.kubeconfig
- 拷贝配置文件到node节点
复制的这两个配置文件的作用就是能与master节点通信和被master节点控制
[root@localhost kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.126.20:/opt/kubernetes/cfg/
root@192.168.126.20's password:
bootstrap.kubeconfig 100% 2169 2.1MB/s 00:00
kube-proxy.kubeconfig 100% 6275 4.2MB/s 00:00
[root@localhost kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.126.30:/opt/kubernetes/cfg/
root@192.168.126.30's password:
bootstrap.kubeconfig 100% 2169 1.8MB/s 00:00
kube-proxy.kubeconfig 100% 6275 5.3MB/s
- 创建bootstrap角色赋予权限用于连接apiserver请求签名⭐⭐
只有bootstrap授权之后,node节点才算完整的添加到群集、可以被master节点所管理
[root@localhost kubeconfig]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
-
node1节点操作
-
执行kubelet脚本,用于请求连接master主机
[root@localhost ~]# bash kubelet.sh 192.168.126.20
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
- 检查kubelet服务启动
[root@localhost ~]# ps aux | grep kube
[root@localhost ~]# systemctl status kubelet.service
- master上操作
检查到node01节点的请求
[root@localhost kubeconfig]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-OOq8BS8KcwiCEh88HNMwZicsEWhfxe8wkOm-l42K838 4m27s kubelet-bootstrap Pending(等待集群给该节点颁发证书)
kubectl certificate approve node-csr-OOq8BS8KcwiCEh88HNMwZicsEWhfxe8wkOm-l42K838
- 继续查看证书状态
[root@localhost kubeconfig]#kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr--EUOthgGy7W-MjwY9vcL_-9q7yeGI61ZIlQuKiiWzQY 52m kubelet-bootstrap Approved,Issued(已经被允许加入群集)
- 查看群集节点,成功加入node01节点
[root@localhost kubeconfig]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.126.20 Ready <none> 118s v1.12.3
- 在node01节点操作,启动proxy服务
[root@localhost ~]# bash proxy.sh 192.168.126.20
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
[root@localhost ~]# systemctl status kube-proxy.service
[root@localhost ~]#systemctl enable kube-proxy
[root@localhost ~]#systemctl enable kubelet
设置开机自启动
3.2 node02节点部署
- 在node01节点操作
把现成的/opt/kubernetes目录复制到其他节点进行修改即可
[root@localhost ~]# scp -r /opt/kubernetes/ root@192.168.126.30:/opt/
[root@localhost ~]# scp /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.126.30:/usr/lib/systemd/system/
- 在node02上操作,进行修改
首先删除复制过来的证书,等会node02会自行申请证书
[root@localhost ~]# cd /opt/kubernetes/ssl/
[root@localhost ssl]# rm -rf *
- 修改配置文件kubelet kubelet.config kube-proxy(三个配置文件)
[root@localhost ssl]# cd ../cfg/
[root@localhost cfg]# vim kubelet #修改里面的ip地址为本机ip
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.126.30 \
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \
--config=/opt/kubernetes/cfg/kubelet.config \
--cert-dir=/opt/kubernetes/ssl \
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
root@localhost cfg]# vim kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.126.30
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- 10.0.0.2
clusterDomain: cluster.local.
failSwapOn: false
authentication:
anonymous:
enabled: true
[root@localhost cfg]# vim kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.126.30 \
--cluster-cidr=10.0.0.0/24 \
--proxy-mode=ipvs \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
- 启动服务
[root@localhost cfg]# systemctl start kubelet.service
[root@localhost cfg]# systemctl enable kubelet.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@localhost cfg]# systemctl start kube-proxy.service
[root@localhost cfg]# systemctl enable kube-proxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
- 在master上操作查看请求
[root@localhost k8s]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-OaH9HpIKh6AKlfdjEKm4C6aJ0UT_1YxNaa70yEAxnsU 15s kubelet-bootstrap Pending
[root@localhost k8s]# kubectl certificate approve node-csr-OaH9HpIKh6AKlfdjEKm4C6aJ0UT_1YxNaa70yEAxnsU
certificatesigningrequest.certificates.k8s.io/node-csr-OaH9HpIKh6AKlfdjEKm4C6aJ0UT_1YxNaa70yEAxnsU approved
- 查看群集中的节点
[root@localhost k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
192.168.126.20 Ready <none> 21h v1.12.3
192.168.126.30 Ready <none> 37s v1.12.3
更多推荐
所有评论(0)