Kubernetes 二进制包部署
软件包信息
名称 | 地址 |
---|---|
etcd | https://github.com/coreos/etcd/releases/tag/v3.2.12 |
Kubernetes-Server | https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.16.md#downloads-for-v1160 |
Kubernetes-Node | https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.16.md#downloads-for-v1160 |
安装环境说明
安装步骤
安装cfssl证书工具
安装包在ftp://192.168.1.11/常用组件 中有
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
部署Etcd 集群
创建证书
生成三个文件
[[email protected] etcd]$ ls *json
ca-config.json ca-csr.json server-csr.json
ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
ca-csr.json
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
server-csr.json
{
"CN": "etcd",
"hosts": [
"192.168.1.11",
"192.168.1.10",
"192.168.1.13"
],
"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
ca-key.pem ca.pem server-key.pem server.pem
上传安装包并解压,安装到/opt/etcd 目录下,本节后面操作三台服务器均需做一遍,注意文件里面不的不同处
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/
创建etcd 配置文件
vi /opt/etcd/etcd
输入内容
etcd
#[Member]
ETCD_NAME="etcd01"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.1.11:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.1.11:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.1.11:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.1.11:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.1.11:2380,etcd02=https://192.168.1.10:2380,etcd03=https://192.168.1.13:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
注意这个文件里面的内容,三台服务器有些许差别:ETCD_LISTEN_PEER_URLS、ETCD_LISTEN_CLIENT_URLS
*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表示加入已有集群
创建etcd服务
sudo vi /usr/lib/systemd/system/etcd.service #CentOS
sudo vi /lib/systemd/system/etcd.service #Ubuntu
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
把刚才生成的证书拷贝到配置文件中的位置
sudo cp *pem /opt/etcd/ssl
设置etcd服务开机启动
sudo systemctl daemon-reload
sudo systemctl enable etcd
sudo systemctl start etcd
执行下面命令看一下是否部署成功。在生成证书的目录下执行
/opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.1.11:2379,https://192.168.1.10:2379,https://192.168.1.13:2379" cluster-health
输出下面类似信息就是成功了
member 18218cfabd4e0dea is healthy: got healthy result from https://192.168.31.63:2379
member 541c1c40994c939b is healthy: got healthy result from https://192.168.31.65:2379
member a342ea2798d20705 is healthy: got healthy result from https://192.168.31.66:2379
cluster is healthy
部署Docker
在两个Node节点部署Docker即可,参见其他文章。
部署Flannel网络
现在etcd里面写入一个预设网段信息。跟上面那个检查命令一样,在证书目录执行,只用在一台机子执行就可以了
/opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.1.11:2379,https://192.168.1.10:2379,https://192.168.1.13:2379" set /coreos.com/network/config '{ "Network": "172.17.0.0/16",
Backend": {"Type": "vxlan"}}
下载安装包,拷到几台服务器上
mkdir /opt/kubernetes/{bin,cfg,ssl} -p
wget https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
tar zxvf flannel-v0.9.1-linux-amd64.tar.gz
mv flanneld mk-docker-opts.sh /opt/kubernetes/bin
下面步骤要在三台服务器都执行
创建flannel 配置文件
sudo vi /opt/kubernetes/cfg/flanneld
内容如下
flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.1.11:2379,https://192.168.1.10:2379,https://192.168.1.13:2379 -etcd-cafile=/opt/etcd/ssl/ca.pem -etcd-certfile=/opt/etcd/ssl/server.pem -etcd-keyfile=/opt/etcd/ssl/server-key.pem"
创建Flannel服务
sudo vi /usr/lib/systemd/system/flanneld.service #CentOS
sudo vi /lib/systemd/system/flanneld.service #Ubuntu
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
配置Docker 启动指定子网段
sudo vi /usr/lib/systemd/system/docker.service #CentOS
sudo vi /lib/systemd/system/docker.service #Ubuntu
内容如下,关键是:
EnvironmentFile=/run/flannel/subnet.env,
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS
这两行
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
启动flannel服务并加入自启动,重启docker 服务
systemctl daemon-reload
systemctl start flanneld
systemctl enable flanneld
systemctl restart docker
ip addr #看下网络配置,几台服务器互相ping下看看通不通
部署Master节点
生成证书
注意,不能跟刚刚的etcd证书在同一个目录
ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
ca-csr.json
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing",
"O": "k8s",
"OU": "System"
}
]
}
server-csr.json
{
"CN": "kubelet-bootstrap",
"hosts": [
"10.0.0.1",
"127.0.0.1",
"192.168.1.11",
"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 -initca ca-csr.json | cfssljson -bare ca -
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
生成kube-proxy证书:
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
ls *pem #看下生成的证书文件,应该有6个 ca-key.pem ca.pem kube-proxy-key.pem kube-proxy.pem server-key.pem server.pem
部署apiserver组件
把安装包拷贝到master服务器上,将kube-apiserver kube-scheduler kube-controller-manager kubectl拷到目录下/opt/kubernetes/bin
sudo mkdir /opt/kubernetes/{bin,cfg,ssl} -p
tar zxvf kubernetes-server-linux-amd64.tar.gz
cd kubernetes/server/bin
sudo cp kube-apiserver kube-scheduler kube-controller-manager kubectl /opt/kubernetes/bin
sudo ln -s /opt/kubernetes/bin/kubectl /usr/local/bin/ #这样的话kubectl可以随处执行
创建token文件
sudo vi /opt/kubernetes/cfg/token.csv
token.csv
674c457d4dcf2eefe4920d7dbb6b0ddc,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
第一列:随机字符串,自己可生成
第二列:用户名:kubelet-bootstrap ,后面的用户名要跟这个匹配的上
第三列:UID
第四列:用户组
创建kube-apiserver配置文件
kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.1.11:2379,https://192.168.1.10:2379,https://192.168.1.13:2379 \
--bind-address=192.168.1.11 \
--secure-port=6443 \
--insecure-bind-address=0.0.0.0 \
--insecure-port=18080 \
--advertise-address=192.168.1.11 \
--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 \
--kubelet-client-certificate \
--kubelet-client-key \
--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 监听地址
–insecure-bind-address Http协议绑定地址
–insecure-port Http端口,注意,默认值是8080,为了避免冲突改成了18080,后面两个地方要保持统一
–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类型默认分配端口范围
创建apiserver服务
sudo vi /usr/lib/systemd/system/kube-apiserver.service #CentOS
sudo vi /lib/systemd/system/kube-apiserver.service #Ubuntu
启动
systemctl daemon-reload
systemctl enable kube-apiserver
systemctl restart kube-apiserver
部署scheduler组件
创建scheduler配置文件
sudo vi /opt/kubernetes/cfg/kube-scheduler
填入以下内容
kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \ #特别注意,这个地方要改成18080
--leader-elect"
参数说明:
–master 连接本地apiserver
–leader-elect 当该组件启动多个时,自动选举(HA)
systed 管理scheduler组件
sudo vi /usr/lib/systemd/system/kube-scheduler.service #CentOS
sudo vi /lib/systemd/system/kube-scheduler.service #Ubuntu
填入以下内容
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
启动
systemctl daemon-reload
systemctl enable kube-scheduler
systemctl restart kube-scheduler
部署controller-manager组件
创建配置文件
sudo vi /opt/kubernetes/cfg/kube-controller-manager
填入以下内容
kube-controller-manager
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \ #特别注意,这个地方应该改成18080
--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"
systemd管理controller-manager组件
sudo vi /usr/lib/systemd/system/kube-controller-manager.service #CentOS
sudo vi /lib/systemd/system/kube-controller-manager.service #Ubuntu
填入以下内容
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
启动
sudo systemctl daemon-reload
sudo systemctl enable kube-controller-manager
sudo systemctl restart kube-controller-manager
部署kubectl
kubectl 是一个命令行工具,可以通过这个命令操作kubernetes集群进行各种操作,查询信息
先为kubectl命令创建一个软连接,这样在任何地方都可以执行kubectl,不用写全路径
sudo ln -s /opt/kubernetes/bin/kubectl /usr/local/bin/ #前提是kubectl 已经拷到/opt/kubernetes/bin/目录下
创建配置文件,在生成kubernetes证书的目录下执行以下命令
# 创建kubelet bootstrapping kubeconfig
BOOTSTRAP_TOKEN=674c457d4dcf2eefe4920d7dbb6b0ddc
KUBE_APISERVER="https://192.168.1.11:6443"
# 设置集群参数
kubectl config set-cluster kubernetes \
--certificate-authority=./ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=bootstrap.kubeconfig
# 设置客户端认证参数
kubectl config set-credentials kubelet-bootstrap \
--token=${BOOTSTRAP_TOKEN} \
--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=./ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy \
--client-certificate=./kube-proxy.pem \
--client-key=./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
上面这些命令会生成两个配置文件
ls *config
bootstrap.kubeconfig kube-proxy.kubeconfig
把bootstrap.kubeconfig 的内容拷贝到~/.kube/config 文件中
cp bootstrap.kubeconfig ~/.kube/config
然后就可以使用kubectl命令了,先用他看看集群状态
kubectl get cs
kubectl get pods
部署Node节点
node 节点需要部署kubelet 和kube-proxy,将这两个文件复制到每个node 节点的/opt/kubernetes/bin 目录下
将上面生成的:bootstrap.kubeconfig 、kube-proxy.kubeconfig 复制到/opt/kubernetes/cfg目录下
部署kubelet
创建配置文件
sudo vi /opt/kubernetes/cfg/kubelet
填入以下内容
kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.1.10 \ #注意这个地方,每个node都不一样
--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网络的镜像
其中/opt/kubernetes/cfg/kubelet.config 内容如下
kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.1.10 #注意这个地方,每台node服务器都不同
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS: ["10.0.0.2"]
clusterDomain: cluster.local.
failSwapOn: false
authentication:
anonymous:
enabled: true
systemd 管理kubelet组件
sudo vi /usr/lib/systemd/system/kubelet.service
填入以下内容
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
启动
sudo systemctl daemon-reload
sudo systemctl enable kubelet
sudo systemctl restart kubelet
启动后还没加入到集群中,需要手动允许该节点才可以。
在Master节点查看请求签名的Node:
在Master节点审批Node加入集群
kubectl get csr
kubectl certificate approve XXXXID
kubectl get node
部署kube-proxy
创建配置文件
sudo vi /opt/kubernetes/cfg/kube-proxy
填入以下内容
kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.31.65 \
--cluster-cidr=10.0.0.0/24 \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
systemd 管理kube-proxy
sudo vi /usr/lib/systemd/system/kube-proxy.service #CentOS
sudo vi /lib/systemd/system/kube-proxy.service #ubuntu
填入以下内容
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
启动
systemctl daemon-reload
systemctl enable kube-proxy
systemctl restart kube-proxy
查看集群状态
kubectl get node
kubectl get cs