Kubernetes架构与组件
根据架构图对应的组件部署在对应的节点上,开始实验。
环境准备
因为电脑资源有限,仅准备了三台机器进行实验,分别是centos7.6、2u4g的配置
- 192.168.75.142 master
- 192.168.75.143 node1
- 192.168.75.144 node2
三台机器均相互解析,防火墙也已经关闭,命令如下:
systemctl stop firewalld && systemctl disable firewalld
setenforce 0 && sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
一、部署etcd
我们使用cfssl来生成自签证书,用于机器之间的认证,在任意一台机器进行都可以,我以master为例
(PS:以下操作为了方便日后管理,我们可以将如下操作都在指定目录下进行,目录可以自己喜欢,只不过对应配置文件中将路径修改正确即可,我在三台机器分别创建了如下目录)
# mkdir -pv /opt/etcd/{ssl,bin,cfg} # ssl、存放各种文件;bin、存放二进制文件;cfg、存放二进制文件的配置文件
# cd /opt/etcd/ssl
1、下载cfssl工具
# pwd
/opt/etcd/ssl
# 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、创建生成证书的文件
# vim ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
vim ca-csr.json
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
vim server-csr.json # 注意修改ip
{
"CN": "etcd",
"hosts": [
"192.168.75.142",
"192.168.75.143",
"192.168.75.144"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
3、生成证书
# 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
4、查看证书并拷贝到其他节点
# ls *pem
ca-key.pem ca.pem server-key.pem server.pem
# scp ca*pem server*pem node1:/opt/etcd/ssl
# scp ca*pem server*pem node2:/opt/etcd/ssl
5、安装etcd(三台机器都要操作)
5.1、二进制包下载地址(各种版本):https://github.com/coreos/etcd/releases/tag/v3.2.12
# wget https://github.com/etcd-io/etcd/releases/download/v3.2.12/etcd-v3.2.12-linux-amd64.tar.gz
# tar zxvf etcd-v3.2.12-linux-amd64.tar.gz
# mv etcd-v3.2.12-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
其他节点可以选择scp方式传过去,就不需要下包了
# scp /opt/etcd/bin/{etcd,etcdctl} node1:/opt/etcd/bin
# scp /opt/etcd/bin/{etcd,etcdctl} node2:/opt/etcd/bin
5.3、创建etcd配置文件(其他节点可以scp过去修改,也可以单机创建后修改,因人而异)
# vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd01"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.75.142:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.75.142:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.75.142:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.75.142:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.75.142:2380,etcd02=https://192.168.75.143:2380,etcd03=https://192.168.75.144:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
配置文件说明
- ETCD_NAME 节点名称 需要修改
- ETCD_DATA_DIR 数据目录
- ETCD_LISTEN_PEER_URLS 集群通信监听地址 需要修改
- ETCD_LISTEN_CLIENT_URLS 客户端访问监听地址 需要修改
- ETCD_INITIAL_ADVERTISE_PEER_URLS 集群通告地址 需要修改
- ETCD_ADVERTISE_CLIENT_URLS 客户端通告地址 需要修改
- ETCD_INITIAL_CLUSTER 集群节点地址 需要修改
- ETCD_INITIAL_CLUSTER_TOKEN 集群Token
- ETCD_INITIAL_CLUSTER_STATE 加入集群的当前状态,new是新集群,existing表示加入已有集群
5.4、创建systemd管理etcd
5.4.1、创建配置文件
# 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
5.4.2、启动并设置开机自启
# systemctl start etcd
# systemctl enable etcd
5.5、检查集群健康状态
/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.75.142:2379,https://192.168.75.143:2379,https://192.168.75.144:2379" \
cluster-health
当看到如下输出,即说明etcd集群部署成功。
假如没有看到这些输出,稳住心态,先查看日志,或许哪些地方粗心弄错了。
journalctl -xeu etcd
假如报错如下:
Jan 15 12:06:55 k8s-master1 etcd: request cluster ID mismatch (got 99f4702593c94f98 want cdf818194e3a8c32)
解决方法:因为集群搭建过程,单独启动过单一etcd,做为测试验证,集群内第一次启动其他etcd服务时候,是通过发现服务引导的,所以需要删除旧的成员信息,所有节点作以下操作
[root@master default.etcd23:52:10]# pwd
/var/lib/etcd/default.etcd
[root@master default.etcd23:52:12]# rm -rf member/
5.6、安装docker(可以只在node节点安装,我是之前全部都安装了,master可以不安装,因为可以使用kubernetes管理镜像)
# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# yum install docker-ce -y
# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://bc437cce.m.daocloud.io
# systemctl start docker
# systemctl enable docker
二、部署flannel
--------------------------以下操作均在node节点----------------------------
1、写入预定义的子网段(前提是etcd部署成功且可以连接,Flannel要用etcd存储自身一个子网信息)
# cd /opt/etcd/ssl
# /opt/etcd/bin/etcdctl \
--ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
--endpoints="https://192.168.75.142:2379,https://192.168.75.143:2379,https://192.168.75.144:2379" \
set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
# mkdir -pv /opt/kubernetes/{ssl,cfg,bin}
# cd /opt/kubernetes/ssl
2、下载二进制包并解压
# 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
# mv flanneld mk-docker-opts.sh /opt/kubernetes/bin
# scp /opt/kubernetes/bin/{flanneld,mk-docker-opts.sh} node2:/opt/kubernetes/bin
3、创建flannel配置文件
# vim /opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.75.142:2379,https://192.168.75.143:2379,https://192.168.75.144: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启用前面的预定义子网段
# mv /usr/lib/systemd/system/docker.service{,.bak}
# 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
# systemctl restart docker
7、检查是否成功
ps -ef |grep docker
ip a
看到上面的信息说明我们的flannel已经生效了,docker与flannel均在同一网段。
8、测试node1与node2是否可以正常通信
可见我们的node1与node2的ip并不在同一网段上。
拉取测试镜像检验连通性,分别在node1与node2上执行
docker run -it daocloud.io/library/centos:7 /bin/bash
在运行的容器中输入
hostname -I
获取容器IP地址后进行ping
测试,结果如下:三、部署Kubernetes
前面的etcd和flannel部署都没问题的话,进行我们的k8s部署。
1、生成证书(建议之前在哪台生成就在哪台生成,这样就不用重复下载工具了)
1.1、生成CA证书
# pwd
/opt/kubernetes/ssl
# 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"
}
]
}
# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
1.2、生成apiserver证书
# vim server-csr.json
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1", # 这个是后边dns要用的虚拟网络的网关,不用改,就用这个,复制到文件中记得删除注释
"127.0.0.1",
"192.168.75.142",
"192.168.75.143",
"192.168.75.144",
"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"
}
]
}
# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
1.3、生成kube-proxy证书
# 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"
}
]
}
# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
2、部署apiserver组件
--------------------------master节点----------------------------
2.1、下载二进制包
下载地址:https://dl.k8s.io/v1.10.0/kubernetes-server-linux-amd64.tar.gz
# pwd
/opt/kubernetes/ssl
# wget https://dl.k8s.io/v1.10.0/kubernetes-server-linux-amd64.tar.gz
# tar -xvf kubernetes-server-linux-amd64.tar.gz
# cp kubernetes/server/bin/{kube-apiserver,kube-scheduler,kube-controller-manager,kubectl} /opt/kubernetes/bin
2.2、创建token文件,后面需要用到
# vim /opt/kubernetes/cfg/token.csv
674c457d4dcf2eefe4920d7dbb6b0ddc,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
PS:第一列:随机字符串,自己可生成;第二列:用户名;第三列:UID;第四列:用户组
2.3、部署apiserver组件
2.3.1、创建apiserver配置文件
# vim /opt/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.75.142:2379,https://192.168.75.143:2379,https://192.168.75.144:2379 \
--bind-address=192.168.75.142 \
--secure-port=6443 \
--advertise-address=192.168.75.142 \
--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"
参数说明:
- --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类型默认分配端口范围
2.3.2、创建systemd管理apiserver配置文件
# 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
2.3.3、配置启动及开机自启
# systemctl daemon-reload
# systemctl enable kube-apiserver
# systemctl start kube-apiserver
2.4、部署scheduler组件
2.4.1、创建kube-scheduler配置文件
# 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.4.2、创建systemd管理scheduler配置文件
# 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
2.4.3、配置启动及开机自启
# systemctl daemon-reload
# systemctl enable kube-scheduler
# systemctl start kube-scheduler
2.5、部署controller-manager组件
2.5.1、创建kube-controller-manager配置文件
# 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 \
--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.5.2、创建systemd管理controller-manager配置文件
# 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
2.5.3、配置启动及开机自启
# systemctl daemon-reload
# systemctl enable kube-controller-manager
# systemctl start kube-controller-manager
2.6、使用kubectl命令检查集群状态
/opt/kubernetes/bin/kubectl get cs
输出如下说明部署成功,组件均正常。
Master apiserver启用TLS认证后,Node节点kubelet组件想要加入集群,必须使用CA签发的有效证书才能与apiserver通信,当Node节点很多时,签署证书是一件很繁琐的事情,因此有了TLS Bootstrapping机制,kubelet会以一个低权限用户自动向apiserver申请证书,kubelet的证书由apiserver动态签署。
--------------------------以下操作均在master节点----------------------------
将kubelet-bootstrap用户绑定到系统集群角色
# /opt/kubernetes/bin/kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap
2.7、创建kubeconfig文件:
在生成kubernetes证书的目录下执行以下命令生成kubeconfig文件:
2.7.1、指定apiserver 内网负载均衡地址(仅在当前bashshell中有效)
# KUBE_APISERVER="https://192.168.75.142:6443"
# BOOTSTRAP_TOKEN=674c457d4dcf2eefe4920d7dbb6b0ddc
2.7.2、设置集群参数
# /opt/kubernetes/bin/kubectl config set-cluster kubernetes \
--certificate-authority=./ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=bootstrap.kubeconfig
2.7.3、设置客户端认证参数
# /opt/kubernetes/bin/kubectl config set-credentials kubelet-bootstrap \
--token=${BOOTSTRAP_TOKEN} \
--kubeconfig=bootstrap.kubeconfig
2.7.4、设置上下文参数
# /opt/kubernetes/bin/kubectl config set-context default \
--cluster=kubernetes \
--user=kubelet-bootstrap \
--kubeconfig=bootstrap.kubeconfig
2.7.5、设置默认上下文
# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
2.7.6、创建kube-proxy kubeconfig文件
# /opt/kubernetes/bin/kubectl config set-cluster kubernetes \
--certificate-authority=./ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kube-proxy.kubeconfig
# /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
# /opt/kubernetes/bin/kubectl config set-context default \
--cluster=kubernetes \
--user=kube-proxy \
--kubeconfig=kube-proxy.kubeconfig
# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
2.7.7、查看生成的kubeconfig文件
ls bootstrap.kubeconfig kube-proxy.kubeconfig
将这两个文件拷贝到node节点上(重要!!!)
# scp bootstrap.kubeconfig kube-proxy.kubeconfig node1:/opt/kubernetes/cfg
# scp bootstrap.kubeconfig kube-proxy.kubeconfig node2:/opt/kubernetes/cfg
--------------------------以下操作均在所有node节点操作----------------------------
2.8、部署kubelet组件
将前面master下载的包中kubelet和kube-proxy文件拷贝到node节点的/opt/kubernetes/bin目录下(重要!!!)
# scp kubernetes/server/bin/{kubelet,kube-proxy} node1:/opt/kubernetes/bin
# scp kubernetes/server/bin/{kubelet,kube-proxy} node2:/opt/kubernetes/bin
2.8.1、创建kubelet配置文件
# vim /opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.75.143 \
--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"
参数说明:
- --hostname-override 在集群中显示的主机名
- --kubeconfig 指定kubeconfig文件位置,会自动生成
- --bootstrap-kubeconfig 指定刚才生成的bootstrap.kubeconfig文件
- --cert-dir 颁发证书存放位置
- --pod-infra-container-image 管理Pod网络的镜像
2.8.2、配置kubelet.config文件
# vim /opt/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.75.143
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS: ["10.0.0.2"]
clusterDomain: cluster.local.
failSwapOn: false
authentication:
anonymous:
enabled: true
webhook:
enabled: false
2.8.3、创建systemd管理kubelet配置文件
# 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
2.8.4、配置启动及开机自启
# systemctl daemon-reload
# systemctl enable kubelet
# systemctl start kubelet
2.8.5、在master节点审批node节点加入集群:
启动后还没加入到集群中,需要手动允许该节点才可以。在master节点查看请求签名的node:
# /opt/kubernetes/bin/kubectl get csr
# /opt/kubernetes/bin/kubectl certificate approve NAME
# /opt/kubernetes/bin/kubectl get node
2.9.1、创建kube-proxy配置文件(所有node节点)
# vim /opt/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.75.143 \
--cluster-cidr=10.0.0.0/24 \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
2.9.2、创建systemd管理kube-proxy配置文件
# vim /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-proxy
ExecStart=/opt/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
2.9.3、配置启动及开机自启
# systemctl daemon-reload
# systemctl enable kube-proxy
# systemctl start kube-proxy
2.9.4、查看集群状态(master节点)
/opt/kubernetes/bin/kubectl get node
/opt/kubernetes/bin/kubectl get cs
2.9.5、判断集群是否正常工作(master节点)
创建一个nginx Web:
# /opt/kubernetes/bin/kubectl run nginx --image=daocloud.io/library/nginx --replicas=3
# /opt/kubernetes/bin/kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort
查看pod、service:
/opt/kubernetes/bin/kubectl get pods
如果看到如图所示,不要着急,稍等一下容器创建即可。
查看pod详细信息:
/opt/kubernetes/bin/kubectl describe pod NAME
/opt/kubernetes/bin/kubectl get svc
打开浏览器访问:http://192.168.75.144:43445
看到如图界面,那么恭喜你,kubernetes集群部署成功了,且所有组件正常。