k8s实验搭建(二进制方式)
kubernetes简称k8s。是⽤于⾃动部署,扩展和管理容器化应⽤程序的开源系统。中⽂官⽹:https://kubernetes.io/zh/中⽂社区:https://www.kubernetes.org.cn/官⽅⽂档:https://kubernetes.io/zh/docs/home/社区⽂档:https://docs.kubernetes.org.cn/
一、K8S架构
1.K8S简介
kubernetes简称k8s。是⽤于⾃动部署,扩展和管理容器化应⽤程序的开源系统。
中⽂官⽹:https://kubernetes.io/zh/
中⽂社区:https://www.kubernetes.org.cn/
官⽅⽂档:https://kubernetes.io/zh/docs/home/
社区⽂档:https://docs.kubernetes.org.cn/
2.K8S架构图
3.用户创建pod的时序图
二、K8S组件
1.master节点组件
api-server组件:提供了集群管理的API接口,通过它管理集群
scheduler组件:负责调度pod到合适的节点上
replaction-controller组件:负责维护集群状态,比如维护pod的副本数、节点的状态等
2.node节点组件
kublet组件:运行在node节点上,负责维护容器的生命周期,与apiserver交互
kube-proxy组件:运行在node节点上,负责节点上的网络代理
flannel组件:一个网络解决方案,负责提供容器间的网络通信
docker:运行在node节点上,负责运行容器
3.其它组件
etcd组件:保存了整个集群的状态,包括pod、service等资源对象的状态信息
三、K8S环境准备(所有节点)
1.服务器角色、IP、组件规划
服务器角色 | IP | 组件规划 |
---|---|---|
k8s-master | 192.168.168.140 | kube-apiserver,kube-controller-manager,kube-scheduler,etcd |
k8s-node1 | 192.168.168.141 | kubelet,kube-proxy,docker,flannel,etcd |
k8s-node2 | 192.168.168.142 | kubelet,kube-proxy,docker,flannel, etcd |
2.设置主机名及地址解析
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2
vim /etc/hosts 192.168.168.140 k8s-master
192.168.168.141 k8s-node1
192.168.168.142 k8s-node2
3.关闭防火墙和selinux
systemctl stop firewalld && systemctl disable firewalld
setenforce 0 && sed -i s/=enforcing/=disabled/g /etc/selinux/config
四、etcd数据库部署(所有节点)
1.生成证书
(1)使⽤cfssl来⽣成⾃签证书,证书随便在那台机器⽣成都可以。哪⾥⽤将证书拷⻉到哪⾥就可以
下载cfssl⼯具:下载的这些是可执⾏的⼆进制命令直接⽤就可以
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
(2)生成etcd证书
mkdir cert
cd cert/
vim ca-config.json #⽣成ca中⼼的
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
vim ca-csr.json #⽣成ca中⼼的证书请求⽂件
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
vim server-csr.json #⽣成服务器的证书请求⽂件
{
"CN": "etcd",
"hosts": [
"192.168.168.140",
"192.168.168.141",
"192.168.168.142"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
[root@k8s-master cert]# ls *.pem ####生成了以下4个文件
ca-key.pem ca.pem server-key.pem server.pem
2.安装etcd
(1)下载安装包 ###3台都要操作
wget https://github.com/etcd-io/etcd/releases/download/v3.2.12/etcd-v3.2.12-linux-amd64.tar.gz
mkdir /opt/etcd/{bin,cfg,ssl} -p
tar zxvf etcd-v3.2.12-linux-amd64.tar.gz
mv etcd-v3.2.12-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
(2)创建etcd配置⽂件 ###3台都要操作
vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd01" #####这里每一台需要修改为etcd02,etcd03
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.168.140:2380" #写每个节点的ip
ETCD_LISTEN_CLIENT_URLS="https://192.168.168.140:2379" #写每个节点的ip
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.168.140:2380" #写每个节点的ip
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.168.140:2379" #写每个节点的ip
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.168.140:2380,etcd02=https://192.168.168.141:2380,etcd03=https://192.168.168.142:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
(3)systemd管理etcd
vim /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd
ExecStart=/opt/etcd/bin/etcd \
--name=${ETCD_NAME} \
--data-dir=${ETCD_DATA_DIR} \
--listen-peer-urls=${ETCD_LISTEN_PEER_URLS} \
--listen-client-urls=${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 \
--advertise-client-urls=${ETCD_ADVERTISE_CLIENT_URLS} \
--initial-advertise-peer-urls=${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
--initial-cluster=${ETCD_INITIAL_CLUSTER} \
--initial-cluster-token=${ETCD_INITIAL_CLUSTER_TOKEN} \
--initial-cluster-state=new \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
(4)将master上⾯⽣成的证书scp到剩余两台机器上⾯
scp ca*pem server*pem k8s-node1:/opt/etcd/ssl
scp ca*pem server*pem k8s-node2:/opt/etcd/ssl
(5)全部启动并设置开启启动
systemctl daemon-reload
systemctl start etcd && systemctl enable etcd
(6)部署完成后,三台机器都检查etcd集群状态
/opt/etcd/bin/etcdctl --ca-file=/opt/etcd/ssl/ca.pem --cert-file=/opt/etcd/ssl/server.pem --key-file=/opt/etcd/ssl/server-key.pem --endpoints="https://192.168.168.140:2379,https://192.168.168.141:2379,https://192.168.168.142:2379" cluster-health
出现以下健康的状态则正常
[root@k8s-master cert]# /opt/etcd/bin/etcdctl --ca-file=/opt/etcd/ssl/ca.pem --cert-file=/opt/etcd/ssl/server.pem --key-file=/opt/etcd/ssl/server-key.pem --endpoints="https://192.168.168.140:2379,https://192.168.168.141:2379,https://192.168.168.142:2379" cluster-health
member 685d63a0417f3 is healthy: got healthy result from https://192.168.168.140:2379
member 726c108304fd702c is healthy: got healthy result from https://192.168.168.142:2379
member 876bbcd3e05709b2 is healthy: got healthy result from https://192.168.168.141:2379
cluster is healthy
五、部署Flannel⽹络插件 (node节点)
1.安装docker
(1)下载docker-ce的yum源
wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(2)安装docker依赖的包
yum install -y yum-utils
(3)安装docker-ce
yum install docker-ce -y
2.安装Flannel插件
(1)Flannel要⽤etcd存储⾃身⼀个⼦⽹信息,所以要保证能成功连接Etcd,写⼊预定义⼦⽹段
cd /opt/etcd/ssl/ ###要在这个目录下去执行,不然找不到秘钥文件
###k8s-node1设置172.41网段
/opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.168.140:2379,https://192.168.168.141:2379,https://192.168.168.142:2379" set /coreos.com/network/config '{ "Network": "172.41.0.0/16", "Backend": {"Type": "vxlan"}}'
###k8s-node2设置172.42网段
/opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.168.140:2379,https://192.168.168.141:2379,https://192.168.168.142:2379" set /coreos.com/network/config '{ "Network": "172.42.0.0/16", "Backend": {"Type": "vxlan"}}'
(2)下载Flannel⽹络插件二进制包
wget https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
tar zxvf flannel-v0.10.0-linux-amd64.tar.gz
mkdir -pv /opt/kubernetes/bin
mv flanneld mk-docker-opts.sh /opt/kubernetes/bin
(3)配置Flannel
mkdir -p /opt/kubernetes/cfg/
vim /opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.168.140:2379,https://192.168.168.141:2379,https://192.168.168.142:2379 -etcd-cafile=/opt/etcd/ssl/ca.pem -etcd-certfile=/opt/etcd/ssl/server.pem -etcd-keyfile=/opt/etcd/ssl/server-key.pem"
(4)systemd管理Flannel
vim /usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service
[Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq $FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure
[Install]
WantedBy=multi-user.target
(5)重新配置docker.service
vim /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
(6)重启flannel和docker
systemctl daemon-reload systemctl start flanneld systemctl enable flanneld docker systemctl restart docker
六、api-server组件部署(master节点)
1.生成证书
给api-server创建证书,因为别的服务器访问api-server的时候需要通过证书认证
(1)创建ca证书:
[root@k8s-master ~]# mkdir -p /opt/crt
[root@k8s-master ~]# cd /opt/crt/
[root@k8s-master crt]#vim ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
vim ca-csr.json
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [{
"C": "CN",
"L": "Beijing",
"ST": "Beijing",
"O": "k8s",
"OU": "System"
}]
}
[root@k8s-master crt]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
(2)生成apiserver证书:
[root@k8s-master crt]# vim server-csr.json
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1", ##这是后⾯dns要使⽤的虚拟⽹络的⽹关,不⽤改,就⽤这个切忌
"127.0.0.1",
"192.168.168.140", ## master的IP地址。
"192.168.168.141",
"192.168.168.142",
"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"
}]
}
[root@k8s-master crt]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json
-profile=kubernetes server-csr.json | cfssljson -bare server
(3)生成kube-proxy证书:
[root@k8s-master crt]# vim kube-proxy-csr.json
{
"CN": "system:kube-proxy",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}]
}
[root@k8s-master crt]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
(4)最终生成以下证书文件:
[root@k8s-master1 crt]# ls *pem
ca-key.pem ca.pem kube-proxy-key.pem kube-proxy.pem server-key.pem server.pem
2.部署api-server组件
部署api-server组件只需要在master节点进行,但是下载的kubernetes-server-linux-amd64.tar.gz的这个压缩包里边包含了所需的所有组件
(1)下载安装包及创建目录
[root@k8s-master1 ~]# wget https://dl.k8s.io/v1.11.10/kubernetes-server-linux-amd64.tar.gz
[root@k8s-master1 ~]# mkdir /opt/kubernetes/{bin,cfg,ssl} -pv
(2)拷贝命令到bin目录下,拷贝证书文件到ssl目录下
[root@k8s-master1 ~]# tar zxvf kubernetes-server-linux-amd64.tar.gz
[root@k8s-master1 ~]# cd kubernetes/server/bin
[root@k8s-master1 ~]# cp kube-apiserver kube-scheduler kube-controller-manager kubectl /opt/kubernetes/bin
[root@k8s-master1 ~]# cp /opt/crt/{server.pem,server-key.pem,ca.pem,ca-key.pem} /opt/kubernetes/ssl/
(3)在cfg目录下创建token文件
[root@k8s-master crt]# cd /opt/kubernetes/cfg/
[root@k8s-master crt]# vim token.csv
674c457d4dcf2eefe4920d7dbb6b0ddc,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
第⼀列:随机字符串,⾃⼰可⽣成
第⼆列:⽤户名
第三列:UID
第四列:⽤户组
(4)创建api-server配置⽂件
[root@k8s-master cfg]# vim kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.168.140:2379,https://192.168.168.141:2379,https://192.168.168.142:2379 \
--bind-address=192.168.168.140 \ #master的ip地址,就是安装api-server的机器地址
--secure-port=6443 \
--advertise-address=192.168.168.140 \
--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=/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"
注意:配置好前⾯⽣成的证书,确保能连接etcd。
参数说明:
* --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类型默认分配端⼝范围
(5)systemd管理api-server
[root@k8s-master cfg]# vim /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
(6)设置启动并且开机自启api-server
[root@k8s-master cfg]# systemctl daemon-reload
[root@k8s-master cfg]# systemctl enable kube-apiserver
[root@k8s-master cfg]# systemctl start kube-apiserver
[root@k8s-master cfg]# systemctl status kube-apiserver
七、部署scheduler组件(master节点)
scheduler组件只需要再master节点上部署:
(1)创建schduler配置文件
[root@k8s-master cfg]# vim /opt/kubernetes/cfg/kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect"
参数说明:
* --master 连接本地apiserver
* --leader-elect 当该组件启动多个时,⾃动选举(HA)
(2)systemd管理scheduler组件
[root@k8s-master cfg]# vim /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
(3)设置启动并且开机自启scheduler
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl enable kube-scheduler
[root@k8s-master ~]# systemctl start kube-scheduler
[root@k8s-master ~]# systemctl status kube-scheduler
八、部署controller-manager组件(master节点)
controller-manager组件只需要部署master节点上
(1)创建controller-manager配置⽂件
[root@k8s-master ~]# vim /opt/kubernetes/cfg/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 \ ##这是后⾯dns要使⽤的虚拟⽹络,不⽤改,就⽤这个切忌
--cluster-name=kubernetes \
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem \
--root-ca-file=/opt/kubernetes/ssl/ca.pem \
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem"
(2)systemd管理controller-manager组件
[root@k8s-master cfg]# vim /usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-controller-manager
ExecStart=/opt/kubernetes/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
(3)设置启动并且开机自启controller-manager
[root@k8s-master cfg]# systemctl daemon-reload
[root@k8s-master cfg]# systemctl enable kube-controller-manager
[root@k8s-master cfg]# systemctl start kube-controller-manager
[root@k8s-master cfg]# systemctl status kube-controller-manager.service
(4)查看当前集群组件状态
maste节点的所有组件均已启动成功,通过kubectl去查看当前集群组件状态
[root@k8s-master opt]# /opt/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"}
如上输出说明组件都正常
九、kubelet组件部署(部署在node节点,但是master节点有对应的操作)
Master apiserver启⽤TLS认证后,Node节点kubelet组件想要加⼊集群,必须使⽤CA签发的有 效证书才能与apiserver通信,当Node节点很多时,签署证书是⼀件很繁琐的事情,因此有了TLS Bootstrapping机制,kubelet会以⼀个低权限⽤户⾃动向apiserver申请证书,kubelet的证书由 apiserver动态签署。
认证工作流程大致如下:
1.master节点操作
(1)将kubelet-bootstrap⽤户绑定到系统集群⻆⾊
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap
(2)创建bootstrap.kubeconfig文件:
注意:在⽣成kubernetes证书的⽬录下执⾏以下命令⽣成bootstrap.kubeconfig⽂件
[root@k8s-master ~]# cd /opt/crt/
[root@k8s-master crt]# KUBE_APISERVER="https://192.168.168.140:6443" #写你master的ip地址
[root@k8s-master crt]# BOOTSTRAP_TOKEN=674c457d4dcf2eefe4920d7dbb6b0ddc #之前token文件里的随机字符串
# 设置集群参数
[root@k8s-master crt]# /opt/kubernetes/bin/kubectl config set-cluster kubernetes --certificate-authority=ca.pem --embed-certs=true --server=${KUBE_APISERVER} --kubeconfig=bootstrap.kubeconfig
# 设置客户端认证参数
[root@k8s-master crt]# /opt/kubernetes/bin/kubectl config set-credentials kubelet-bootstrap --token=${BOOTSTRAP_TOKEN} --kubeconfig=bootstrap.kubeconfig
# 设置上下⽂参数
[root@k8s-master crt]# /opt/kubernetes/bin/kubectl config set-context default --cluster=kubernetes --user=kubelet-bootstrap --kubeconfig=bootstrap.kubeconfig
# 设置默认上下⽂
[root@k8s-master crt]# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
(3)创建kube-proxy kubeconfig⽂件
# 设置集群参数
[root@k8s-master crt]# /opt/kubernetes/bin/kubectl config set-cluster kubernetes --certificate-authority=ca.pem --embed-certs=true --server=${KUBE_APISERVER} --kubeconfig=kube-proxy.kubeconfig
# 设置客户端认证参数
[root@k8s-master crt]# /opt/kubernetes/bin/kubectl config set-credentials kube-proxy --client-certificate=kube-proxy.pem --client-key=kube-proxy-key.pem --embed-certs=true --kubeconfig=kube-proxy.kubeconfig
# 设置上下⽂参数
[root@k8s-master crt]# /opt/kubernetes/bin/kubectl config set-context default --cluster=kubernetes --user=kube-proxy --kubeconfig=kube-proxy.kubeconfig
# 设置默认上下⽂
[root@k8s-master crt]# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
[root@k8s-master crt]# ls *.kubeconfig
bootstrap.kubeconfig kube-proxy.kubeconfig
(4)将这两个⽂件拷⻉到Node节点/opt/kubernetes/cfg⽬录下
[root@k8s-master crt]# scp *.kubeconfig k8s-node1:/opt/kubernetes/cfg/
[root@k8s-master crt]# scp *.kubeconfig k8s-node2:/opt/kubernetes/cfg/
2.部署kubelet组件(node节点)
(1)将前⾯下载的⼆进制包中的kubelet和kube-proxy拷⻉到node节点的/opt/kubernetes/bin⽬录下
[root@k8s-master bin]# scp -rp kubelet kube-proxy root@k8s-node1:/opt/kubernetes/bin
[root@k8s-master bin]# scp -rp kubelet kube-proxy root@k8s-node2:/opt/kubernetes/bin
(2)在两个node节点创建kubelet配置⽂件
root@k8s-node1 ~]# vim /opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.168.141 \ #每个节点⾃⼰的ip地址
--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@k8s-node2 ~]# vim /opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.168.142 \
--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@k8s-node1 ~]# docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0
[root@k8s-node2 ~]# docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0
参数说明:
* --hostname-override 在集群中显示的主机名
* --kubeconfig 指定kubeconfig⽂件位置,会⾃动⽣成
* --bootstrap-kubeconfig 指定刚才⽣成的bootstrap.kubeconfig⽂件
* --cert-dir 颁发证书存放位置
* --pod-infra-container-image 管理Pod⽹络的镜像
(3)配置kubelet.config
[root@k8s-node1 ~]# vim /opt/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.168.141 #写你机器的ip地址
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS: ["10.0.0.2"] #不要改,就是这个ip地址
clusterDomain: cluster.local.
failSwapOn: false
authentication:
anonymous:
enabled: true
webhook:
enabled: false
[root@k8s-node2 ~]# vim /opt/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.168.142
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS: ["10.0.0.2"]
clusterDomain: cluster.local.
failSwapOn: false
authentication:
anonymous:
enabled: true
webhook:
enabled: false
(4)systemd管理kubelet组件
[root@k8s-node1 ~]# vim /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet
ExecStart=/opt/kubernetes/bin/kubelet $KUBELET_OPTS
Restart=on-failure
KillMode=process
[Install]
WantedBy=multi-user.target
[root@k8s-node2 ~]# vim /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet
ExecStart=/opt/kubernetes/bin/kubelet $KUBELET_OPTS
Restart=on-failure
KillMode=process
[Install]
WantedBy=multi-user.target
(5)设置启动和开机自启动kubelet
[root@k8s-node1 ~]# systemctl daemon-reload
[root@k8s-node1 ~]# systemctl enable kubelet
[root@k8s-node1 ~]# systemctl start kubelet
[root@k8s-node2 ~]# systemctl daemon-reload
[root@k8s-node2~]# systemctl enable kubelet
[root@k8s-node2 ~]# systemctl start kubelet
3.在master节点查看集群
[root@k8s-master bin]# /opt/kubernetes/bin/kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-YZjdIekfOYuJ3x2feaUxsghqi4Vr-ThvkSw27OiWfuM 1m kubelet-bootstrap Pending
node-csr-qi6YTzISMo_YVNWdJm-LXjotv9ZvdNzdPfmpu3WYwow 2m kubelet-bootstrap Pending
这个时候两个节点的状态为pendind,还未加入到集群,需要master节点手动允许以后,节点才加入到集群
[root@k8s-master1 ~]# /opt/kubernetes/bin/kubectl certificate approve XXXXID
注意:xxxid 指的是上⾯的NAME这⼀列
[root@k8s-master bin]# /opt/kubernetes/bin/kubectl certificate approve node-csr-YZjdIekfOYuJ3x2feaUxsghqi4Vr-ThvkSw27OiWfuM
certificatesigningrequest.certificates.k8s.io/node-csr-YZjdIekfOYuJ3x2feaUxsghqi4Vr-ThvkSw27OiWfuM approved
[root@k8s-master bin]# /opt/kubernetes/bin/kubectl certificate approve node-csr-qi6YTzISMo_YVNWdJm-LXjotv9ZvdNzdPfmpu3WYwow
certificatesigningrequest.certificates.k8s.io/node-csr-qi6YTzISMo_YVNWdJm-LXjotv9ZvdNzdPfmpu3WYwow approved
此时再次查看集群,node节点就已经加入到集群中了
[root@k8s-master bin]# /opt/kubernetes/bin/kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-YZjdIekfOYuJ3x2feaUxsghqi4Vr-ThvkSw27OiWfuM 6m kubelet-bootstrap Approved,Issued
node-csr-qi6YTzISMo_YVNWdJm-LXjotv9ZvdNzdPfmpu3WYwow 7m kubelet-bootstrap Approved,Issued
状态已经变为了 Approved,Issued
注意:每次kubectl都需要需要绝对路径,我们可以做一个软链接
[root@k8s-master bin]# ln -s /opt/kubernetes/bin/kubectl /usr/local/bin/kubectl
[root@k8s-master bin]# kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-YZjdIekfOYuJ3x2feaUxsghqi4Vr-ThvkSw27OiWfuM 9m kubelet-bootstrap Approved,Issued
node-csr-qi6YTzISMo_YVNWdJm-LXjotv9ZvdNzdPfmpu3WYwow 9m kubelet-bootstrap Approved,Issued
查看集群节点信息:
[root@k8s-master bin]# kubectl get no
NAME STATUS ROLES AGE VERSION
192.168.168.141 Ready <none> 24s v1.11.10
192.168.168.142 Ready <none> 8m v1.11.10
十、kube-proxy组件部署(node节点)
(1)创建kube-proxy配置⽂件
[root@k8s-node1 ~]# vim /opt/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.168.141 \ #写每个node节点ip
--cluster-cidr=10.0.0.0/24 \ #不要改,就是这个ip
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
(2)systemd管理kube-proxy组件
[root@k8s-node1 ~]# vim /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.targetrget
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-proxy
ExecStart=/opt/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
(3)开机启动和自启动kube-proxy
[root@k8s-node1 ~]# systemctl daemon-reload
[root@k8s-node1 ~]# systemctl enable kube-proxy
[root@k8s-node1 ~]# systemctl start kube-proxy
(4)查看集群节点状态
[root@k8s-master bin]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.168.141 Ready <none> 17m v1.11.10
192.168.168.142 Ready <none> 26m v1.11.10
(5)查看集群健康状态
[root@k8s-master bin]# kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health": "true"}
etcd-1 Healthy {"health": "true"}
etcd-2 Healthy {"health": "true"}
十一、部署Dashboard(Web UI)
1.部署Pod,提供Web服务
创建⼀个⽬录
[root@k8s-master ~]# mkdir webui
[root@k8s-master ~]# cd webui/
[root@k8s-master webui]# vim dashboard-deployment.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: kubernetes-dashboard
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
spec:
selector:
matchLabels:
k8s-app: kubernetes-dashboard
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
annotations:
scheduler.alpha.kubernetes.io/critical-pod: ''
spec:
serviceAccountName: kubernetes-dashboard
containers:
- name: kubernetes-dashboard
image: registry.cn-hangzhou.aliyuncs.com/kube_containers/kubernetes-dashboard-amd64:v1.8.1
resources:
limits:
cpu: 100m
memory: 300Mi
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 9090
protocol: TCP
livenessProbe:
httpGet:
scheme: HTTP
path: /
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30
tolerations:
- key: "CriticalAddonsOnly"
operator: "Exists"
2.授权访问apiserver获取信息
[root@k8s-master webui]# vim dashboard-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
addonmanager.kubernetes.io/mode: Reconcile
name: kubernetes-dashboard
namespace: kube-system
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: kubernetes-dashboard-minimal
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
addonmanager.kubernetes.io/mode: Reconcile
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kube-system
3.发布服务,提供对外访问
[root@k8s-master webui]# vim dashboard-service.yaml
apiVersion: v1
kind: Service
metadata:
name: kubernetes-dashboard
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
spec:
type: NodePort
selector:
k8s-app: kubernetes-dashboard
ports:
- port: 80
targetPort: 9090
[root@k8s-master webui]# /opt/kubernetes/bin/kubectl create -f dashboard-rbac.yaml
[root@k8s-master webui]# /opt/kubernetes/bin/kubectl create -f dashboard-deployment.yaml
[root@k8s-master webui]# /opt/kubernetes/bin/kubectl create -f dashboard-service.yaml
等待数分钟,查看资源状态:
查看名称空间:
[root@k8s-master webui]# /opt/kubernetes/bin/kubectl get all -n kube-system
NAME READY STATUS RESTARTS AGE
pod/kubernetes-dashboard-d9545b947-2pbcm 1/1 Running 0 22s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes-dashboard NodePort 10.0.0.152 <none> 80:34455/TCP 13s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/kubernetes-dashboard 1 1 1 1 22s
NAME DESIRED CURRENT READY AGE
replicaset.apps/kubernetes-dashboard-d9545b947 1 1 1 22s
查看指定命名空间的服务
[root@k8s-master webui]# /opt/kubernetes/bin/kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard NodePort 10.0.0.152 <none> 80:34455/TCP 1m
访问node节点的ip
十二、测试
创建⼀个Nginx Web,判断集群是否正常⼯作
[root@k8s-master webui]# kubectl run nginx --image=daocloud.io/nginx --replicas=3 ####指定副本数为3
deployment.apps/nginx created
[root@k8s-master webui]# kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort ###暴露端口
service/nginx exposed
[root@k8s-master webui]# kubectl get pods ####稍微等一会
NAME READY STATUS RESTARTS AGE
nginx-6648ff9bb4-ch22m 0/1 ContainerCreating 0 27s
nginx-6648ff9bb4-hpvxk 0/1 ContainerCreating 0 27s
nginx-6648ff9bb4-qmt5z 0/1 ContainerCreating 0 27s
[root@k8s-master webui]# kubectl get pods ###查看Pod,Service
NAME READY STATUS RESTARTS AGE
nginx-6648ff9bb4-ch22m 1/1 Running 0 2m
nginx-6648ff9bb4-hpvxk 1/1 Running 0 2m
nginx-6648ff9bb4-qmt5z 1/1 Running 0 2m
查看pod详细信息,比如nginx-6648ff9bb4-ch22m
[root@k8s-master webui]# kubectl describe pod nginx-6648ff9bb4-ch22m
Name: nginx-6648ff9bb4-ch22m
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: 192.168.168.142/192.168.168.142
Start Time: Sun, 11 Aug 2024 06:03:29 +0800
Labels: pod-template-hash=2204995660
run=nginx
Annotations: <none>
Status: Running
IP: 172.42.93.3
Controlled By: ReplicaSet/nginx-6648ff9bb4
Containers:
nginx:
Container ID: docker://46e7e48179d0d51e42bac49d5760eaebcf4e1f9470d33491db81426866616d24
Image: daocloud.io/nginx
Image ID: docker-pullable://daocloud.io/nginx@sha256:48d56bae87c65ca642b0a1d13c3dc97c4430994991e5531ff123f77cdf975fae
Port: <none>
Host Port: <none>
State: Running
Started: Sun, 11 Aug 2024 06:04:05 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-wkw4h (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-wkw4h:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-wkw4h
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m default-scheduler Successfully assigned default/nginx-6648ff9bb4-ch22m to 192.168.168.142
Normal Pulling 5m kubelet, 192.168.168.142 pulling image "daocloud.io/nginx"
Normal Pulled 5m kubelet, 192.168.168.142 Successfully pulled image "daocloud.io/nginx"
Normal Created 5m kubelet, 192.168.168.142 Created container
Normal Started 5m kubelet, 192.168.168.142 Started container
[root@k8s-master webui]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 4h
nginx NodePort 10.0.0.254 <none> 88:37683/TCP 8m
访问nodeip加端⼝
打开浏览器输⼊:http://192.168.168.142:37683
更多推荐
所有评论(0)