1. 架構篇
1.1 kubernetes 架構說明
1.2 Flannel網絡架構圖
集羣功能各模塊功能描述:
Master節點:
Master節點上面主要由四個模塊組成,APIServer,schedule,controller-manager,etcd
APIServer: APIServer負責對外提供RESTful的kubernetes API的服務,它是系統管理指令的統一接口,任何對資源的增刪該查都要交給APIServer處理後再交給etcd,如圖,kubectl(kubernetes提供的客戶端工具,該工具內部是對kubernetes API的調用)是直接和APIServer交互的。
schedule: schedule負責調度Pod到合適的Node上,若是把scheduler當作一個黑匣子,那麼它的輸入是pod和由多個Node組成的列表,輸出是Pod和一個Node的綁定。 kubernetes目前提供了調度算法,一樣也保留了接口。用戶根據本身的需求定義本身的調度算法。
controller manager: 若是APIServer作的是前臺的工做的話,那麼controller manager就是負責後臺的。每個資源都對應一個控制器。而control manager就是負責管理這些控制器的,好比咱們經過APIServer建立了一個Pod,當這個Pod建立成功後,APIServer的任務就算完成了。
etcd:etcd是一個高可用的鍵值存儲系統,kubernetes使用它來存儲各個資源的狀態,從而實現了Restful的API。htmlNode節點:
每一個Node節點主要由三個模板組成:kublet, kube-proxy
kube-proxy: 該模塊實現了kubernetes中的服務發現和反向代理功能。kube-proxy支持TCP和UDP鏈接轉發,默認基Round Robin算法將客戶端流量轉發到與service對應的一組後端pod。服務發現方面,kube-proxy使用etcd的watch機制監控集羣中service和endpoint對象數據的動態變化,而且維護一個service到endpoint的映射關係,從而保證了後端pod的IP變化不會對訪問者形成影響,另外,kube-proxy還支持session affinity。
kublet:kublet是Master在每一個Node節點上面的agent,是Node節點上面最重要的模塊,它負責維護和管理該Node上的全部容器,可是若是容器不是經過kubernetes建立的,它並不會管理。本質上,它負責使Pod的運行狀態與指望的狀態一致。node
1.3 Kubernetes工做流程
2. 環境說明
2.1 部署節點說明
主機名 | IP | 用途 | 部署軟件 |
---|---|---|---|
k8s-1 | 192.168.123.211 | master | apiserver,scheduler,controller-manager etcd,flanneld |
k8s-2 | 192.168.123.212 | node | kubelet,kube-proxy etcd,flanneld |
k8s-3 | 192.168.123.213 | node | kubelet,kube-proxy etcd,flanneld |
2.2 操做系統環境
# 三臺機器相同 [root@k8s-1 /root] master # uname -r 3.10.0-957.el7.x86_64 [root@k8s-1 /root] master # cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@k8s-1 /root] master # sestatus SELinux status: disabled
2.3 軟件包版本
軟件包 | 下載地址 |
---|---|
kubernetes-node-linux-amd64.tar.gz | https://dl.k8s.io/v1.15.1/kubernetes-node-linux-amd64.tar.gz |
kubernetes-server-linux-amd64.tar.gz | https://dl.k8s.io/v1.15.1/kubernetes-server-linux-amd64.tar.gz |
flannel-v0.11.0-linux-amd64.tar.gz | https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz |
etcd-v3.3.10-linux-amd64.tar.gz | https://github.com/etcd-io/etcd/releases/download/v3.3.10/etcd-v3.3.10-linux-amd64.tar.gz |
3. Kubernetes 安裝及配置
3.1 初始化環境
3.1.1 設置關閉防火牆及SELINUX
systemctl stop firewalld && systemctl disable firewalld setenforce 0 vi /etc/selinux/config SELINUX=disabled
3.1.2 關閉swap
swapoff -a && sysctl -w vm.swappiness=0
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
3.1.3 設置Docker所需參數
cat > /etc/sysctl.d/kubernetes.conf <<EOF net.ipv4.ip_forward=1 net.ipv4.tcp_tw_recycle=0 vm.swappiness=0 vm.overcommit_memory=1 vm.panic_on_oom=0 fs.inotify.max_user_watches=89100 fs.file-max=52706963 fs.nr_open=52706963 net.ipv6.conf.all.disable_ipv6=1 EOF sysctl -p /etc/sysctl.d/kubernetes.conf
3.1.4 安裝 Docker
# 配置yum源 cd /etc/yum.repo.d/ wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum clean all ;yum repolist -y # 安裝docker ,版本 18.06.2 yum install docker-ce-18.06.2.ce-3.el7 -y # 啓動 systemctl start docker && systemctl enable docker
3.1.5 建立相關目錄
# 建立安裝包存儲目錄 mkdir /data/{install,ssl_config} -p mkdir /data/ssl_config/{etcd,kubernetes} -p # 建立安裝目錄 mkdir /cloud/k8s/etcd/{bin,cfg,ssl} -p mkdir /cloud/k8s/kubernetes/{bin,cfg,ssl} -p
3.1.6 SSH 互信配置
ssh-keygen
ssh-copy-id k8s-1 ssh-copy-id k8s-2 ssh-copy-id k8s-3
3.1.7 添加 PATH 變量
vi /etc/profile
export PATH=$PATH:/cloud/k8s/etcd/bin/:/cloud/k8s/kubernetes/bin/
3.2 建立ssl證書
3.2.1 安裝及配置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
3.2.2 建立 ETCD 相關證書
如下操做均在/data/ssl_config/etcd/目錄中
etcd證書ca配置linux
cd /data/ssl_config/etcd/ cat << EOF | tee ca-config.json { "signing": { "default": { "expiry": "87600h" }, "profiles": { "www": { "expiry": "87600h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } } EOF
建立 ETCD CA 配置文件git
cat << EOF | tee ca-csr.json
{
"CN": "etcd CA", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "Beijing", "ST": "Beijing" } ] } EOF
建立 ETCD Server 證書github
cat << EOF | tee server-csr.json
{
"CN": "etcd", "hosts": [ "k8s-3", "k8s-2", "k8s-1", "192.168.123.211", "192.168.123.212", "192.168.123.213" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "Beijing", "ST": "Beijing" } ] } EOF
生成 ETCD CA 證書和私鑰算法
cd /data/ssl_config/etcd/ # 生成ca證書 cfssl gencert -initca ca-csr.json | cfssljson -bare ca - # 生成server證書 cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
3.2.3 建立 Kubernetes 相關證書
如下操做均在/data/ssl_config/kubernetes/目錄中
kubernetes 證書ca配置docker
cd /data/ssl_config/kubernetes/ cat << EOF | tee ca-config.json { "signing": { "default": { "expiry": "87600h" }, "profiles": { "kubernetes": { "expiry": "87600h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } } EOF
建立ca證書配置json
cat << EOF | tee ca-csr.json
{
"CN": "kubernetes", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "Beijing", "ST": "Beijing", "O": "k8s", "OU": "System" } ] } EOF
生成API_SERVER證書bootstrap
cat << EOF | tee server-csr.json
{
"CN": "kubernetes", "hosts": [ "10.0.0.1", "127.0.0.1", "192.168.123.211", "k8s-1", "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
建立 Kubernetes Proxy 證書vim
cat << EOF | tee 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" } ] } EOF
生成 kubernetes CA 證書和私鑰
# 生成ca證書 cfssl gencert -initca ca-csr.json | cfssljson -bare ca - # 生成 api-server 證書 cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server # 生成 kube-proxy 證書 cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
3.3 部署etcd
3.3.1 配置軟件包
cd /data/install/ tar -xvf etcd-v3.3.10-linux-amd64.tar.gz cd etcd-v3.3.10-linux-amd64/ cp etcd etcdctl /cloud/k8s/etcd/bin/
3.3.2 編輯etcd配置文件
vim /cloud/k8s/etcd/cfg/etcd
#[Member] ETCD_NAME="etcd01" ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="https://192.168.123.211:2380" ETCD_LISTEN_CLIENT_URLS="https://192.168.123.211:2379" #[Clustering] ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.123.211:2380" ETCD_ADVERTISE_CLIENT_URLS="https://192.168.123.211:2379" ETCD_INITIAL_CLUSTER="etcd01=https://192.168.123.211:2380,etcd02=https://192.168.123.212:2380,etcd03=https://192.168.123.213:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="new"
3.3.3 建立 etcd的 systemd unit 文件
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=/cloud/k8s/etcd/cfg/etcd
ExecStart=/cloud/k8s/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=/cloud/k8s/etcd/ssl/server.pem \ --key-file=/cloud/k8s/etcd/ssl/server-key.pem \ --peer-cert-file=/cloud/k8s/etcd/ssl/server.pem \ --peer-key-file=/cloud/k8s/etcd/ssl/server-key.pem \ --trusted-ca-file=/cloud/k8s/etcd/ssl/ca.pem \ --peer-trusted-ca-file=/cloud/k8s/etcd/ssl/ca.pem Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target
3.3.4 配置證書文件
cd /data/ssl_config/etcd/ cp ca*pem server*pem /cloud/k8s/etcd/ssl
3.3.5 配置文件拷貝到 節點一、節點2
cd /cloud/k8s/ scp -r etcd k8s-2:/cloud/k8s/ scp -r etcd k8s-3:/cloud/k8s/ scp /usr/lib/systemd/system/etcd.service k8s-2:/usr/lib/systemd/system/etcd.service scp /usr/lib/systemd/system/etcd.service k8s-3:/usr/lib/systemd/system/etcd.service
修改另外兩臺機器配置文件
### k8s-2 cat /cloud/k8s/etcd/cfg/etcd #[Member] ETCD_NAME="etcd02" ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="https://192.168.123.212:2380" ETCD_LISTEN_CLIENT_URLS="https://192.168.123.212:2379" #[Clustering] ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.123.212:2380" ETCD_ADVERTISE_CLIENT_URLS="https://192.168.123.212:2379" ETCD_INITIAL_CLUSTER="etcd01=https://192.168.123.211:2380,etcd02=https://192.168.123.212:2380,etcd03=https://192.168.123.213:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="new" ### k8s-3 cat /cloud/k8s/etcd/cfg/etcd #[Member] ETCD_NAME="etcd03" ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="https://192.168.123.213:2380" ETCD_LISTEN_CLIENT_URLS="https://192.168.123.213:2379" #[Clustering] ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.123.213:2380" ETCD_ADVERTISE_CLIENT_URLS="https://192.168.123.213:2379" ETCD_INITIAL_CLUSTER="etcd01=https://192.168.123.211:2380,etcd02=https://192.168.123.212:2380,etcd03=https://192.168.123.213:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="new"
3.3.6 啓動ETCD服務
systemctl daemon-reload
systemctl enable etcd systemctl start etcd #啓動ETCD集羣同時啓動二個節點,單節點是沒法正常啓動的。
3.3.7 檢查服務是否正常
[root@k8s-1 /cloud/k8s/etcd/bin] master
# etcdctl --ca-file=/cloud/k8s/etcd/ssl/ca.pem --cert-file=/cloud/k8s/etcd/ssl/server.pem --key-file=/cloud/k8s/etcd/ssl/server-key.pem --endpoints="https://192.168.123.211:2379,https://192.168.123.212:2379,https://192.168.123.213:2379" cluster-health member 4c6bfb873a73368c is healthy: got healthy result from https://192.168.123.213:2379 member 622f71dbd55b34ce is healthy: got healthy result from https://192.168.123.211:2379 member f14004aa5403b07d is healthy: got healthy result from https://192.168.123.212:2379 cluster is healthy
3.4 部署Flannel網絡
3.4.1 向 etcd 寫入集羣 Pod 網段信息
cd /cloud/k8s/etcd/bin etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \ --endpoints="https://192.168.123.211:2379,https://192.168.123.212:2379,https://192.168.123.213:2379" \ set /coreos.com/network/config \ '{ "Network": "172.18.0.0/16", "Backend": {"Type": "vxlan"}}'
寫入的 Pod 網段 ${CLUSTER_CIDR} 必須是 /16 段地址,必須與 kube-controller-manager 的 --cluster-cidr 參數值一致;
3.4.2 部署 flannel
cd /data/install/ tar xf flannel-v0.11.0-linux-amd64.tar.gz mv flanneld mk-docker-opts.sh /cloud/k8s/kubernetes/bin/
3.5.3 配置flannel
vim /cloud/k8s/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.123.211:2379,https://192.168.123.212:2379,https://192.168.123.213:2379 -etcd-cafile=/cloud/k8s/etcd/ssl/ca.pem -etcd-certfile=/cloud/k8s/etcd/ssl/server.pem -etcd-keyfile=/cloud/k8s/etcd/ssl/server-key.pem"
3.5.4 建立 flanneld 的 systemd unit 文件
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=/cloud/k8s/kubernetes/cfg/flanneld
ExecStart=/cloud/k8s/kubernetes/bin/flanneld --ip-masq $FLANNEL_OPTIONS ExecStartPost=/cloud/k8s/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env Restart=on-failure [Install] WantedBy=multi-user.target
mk-docker-opts.sh 腳本將分配給 flanneld 的 Pod 子網網段信息寫入 /run/flannel/docker 文件,後續 docker 啓動時 使用這個文件中的環境變量配置 docker0 網橋;
flanneld 使用系統缺省路由所在的接口與其它節點通訊,對於有多個網絡接口(如內網和公網)的節點,能夠用 -iface 參數指定通訊接口,如上面的 eth0 接口;
flanneld 運行時須要 root 權限;
3.5.5 配置Docker啓動指定子網段
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
3.5.6 將flanneld systemd unit 文件到全部節點
cd /cloud/k8s/ scp -r kubernetes k8s-2:/cloud/k8s/ scp -r kubernetes k8s-3:/cloud/k8s/ scp /cloud/k8s/kubernetes/cfg/flanneld k8s-2:/cloud/k8s/kubernetes/cfg/flanneld scp /cloud/k8s/kubernetes/cfg/flanneld k8s-3:/cloud/k8s/kubernetes/cfg/flanneld scp /usr/lib/systemd/system/docker.service k8s-2:/usr/lib/systemd/system/docker.service scp /usr/lib/systemd/system/docker.service k8s-3:/usr/lib/systemd/system/docker.service scp /usr/lib/systemd/system/flanneld.service k8s-2:/usr/lib/systemd/system/flanneld.service scp /usr/lib/systemd/system/flanneld.service k8s-3:/usr/lib/systemd/system/flanneld.service # 啓動服務(每臺節點都操做) systemctl daemon-reload systemctl start flanneld systemctl enable flanneld systemctl restart docker
3.5.7 驗證fannel網絡配置
[root@k8s-1 /data/install] master
# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:33:de:53 brd ff:ff:ff:ff:ff:ff inet 192.168.123.211/24 brd 192.168.123.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever 3: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default link/ether 1a:6f:5a:0b:c7:18 brd ff:ff:ff:ff:ff:ff inet 172.18.30.0/32 scope global flannel.1 valid_lft forever preferred_lft forever 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:5c:d0:df:e6 brd ff:ff:ff:ff:ff:ff inet 172.18.30.1/24 brd 172.18.30.255 scope global docker0 valid_lft forever preferred_lft forever
3.5 部署 master 節點
kubernetes master 節點運行以下組件:
kube-apiserver
kube-scheduler
kube-controller-manager
kube-scheduler 和 kube-controller-manager 能夠以集羣模式運行,經過 leader 選舉產生一個工做進程,其它進程處於阻塞模式。
3.5.1 配置 master 節點文件
cd /data/install/ tar xf kubernetes-server-linux-amd64.tar.gz cd kubernetes/server/bin/ cp kube-scheduler kube-apiserver kube-controller-manager kubectl /cloud/k8s/kubernetes/bin/
3.5.2 配置 kubernetes相關證書
cd /data/ssl_config/kubernetes/ cp *pem /cloud/k8s/kubernetes/ssl/
3.5.3 部署 kube-apiserver 組件
建立 TLS Bootstrapping Token
# head -c 16 /dev/urandom | od -An -t x | tr -d ' ' 449bbeb0ea7e50f321087a123a509a19 # vim /cloud/k8s/kubernetes/cfg/token.csv 449bbeb0ea7e50f321087a123a509a19,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
建立apiserver配置文件
vim /cloud/k8s/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \ --v=4 \ --etcd-servers=https://192.168.123.211:2379,https://192.168.123.212:2379,https://192.168.123.213:2379 \ --bind-address=192.168.123.211 \ --secure-port=6443 \ --advertise-address=192.168.123.211 \ --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=/cloud/k8s/kubernetes/cfg/token.csv \ --service-node-port-range=30000-50000 \ --tls-cert-file=/cloud/k8s/kubernetes/ssl/server.pem \ --tls-private-key-file=/cloud/k8s/kubernetes/ssl/server-key.pem \ --client-ca-file=/cloud/k8s/kubernetes/ssl/ca.pem \ --service-account-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem \ --etcd-cafile=/cloud/k8s/etcd/ssl/ca.pem \ --etcd-certfile=/cloud/k8s/etcd/ssl/server.pem \ --etcd-keyfile=/cloud/k8s/etcd/ssl/server-key.pem"
建立 kube-apiserver systemd unit 文件
vim /usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/cloud/k8s/kubernetes/cfg/kube-apiserver
ExecStart=/cloud/k8s/kubernetes/bin/kube-apiserver $KUBE_APISERVER_OPTS Restart=on-failure [Install] WantedBy=multi-user.target
啓動服務
systemctl daemon-reload
systemctl enable kube-apiserver systemctl restart kube-apiserver
查看apiserver是否運行
[root@k8s-1 /data/ssl_config/kubernetes] master
# ps -ef |grep kube-apiserver root 5475 1 2 7月30 ? 01:57:41 /cloud/k8s/kubernetes/bin/kube-apiserver --logtostderr=true --v=4 --etcd-servers=https://192.168.123.211:2379,https://192.168.123.212:2379,https://192.168.123.213:2379 --bind-address=192.168.123.211 --secure-port=6443 --advertise-address=192.168.123.211 --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=/cloud/k8s/kubernetes/cfg/token.csv --service-node-port-range=30000-50000 --tls-cert-file=/cloud/k8s/kubernetes/ssl/server.pem --tls-private-key-file=/cloud/k8s/kubernetes/ssl/server-key.pem --client-ca-file=/cloud/k8s/kubernetes/ssl/ca.pem --service-account-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem --etcd-cafile=/cloud/k8s/etcd/ssl/ca.pem --etcd-certfile=/cloud/k8s/etcd/ssl/server.pem --etcd-keyfile=/cloud/k8s/etcd/ssl/server-key.pem root 6886 2930 0 19:49 pts/0 00:00:00 grep --color=auto kube-apiserver
3.5.4 部署kube-scheduler
建立kube-scheduler配置文件
vim /cloud/k8s/kubernetes/cfg/kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect"
--address:在 127.0.0.1:10251 端口接收 http /metrics 請求;kube-scheduler 目前還不支持接收 https 請求;
--kubeconfig:指定 kubeconfig 文件路徑,kube-scheduler 使用它鏈接和驗證 kube-apiserver;
--leader-elect=true:集羣運行模式,啓用選舉功能;被選爲 leader 的節點負責處理工做,其它節點爲阻塞狀態;
建立kube-scheduler systemd unit 文件
vim /usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/cloud/k8s/kubernetes/cfg/kube-scheduler
ExecStart=/cloud/k8s/kubernetes/bin/kube-scheduler $KUBE_SCHEDULER_OPTS Restart=on-failure [Install] WantedBy=multi-user.target
啓動kube-scheduler服務
systemctl daemon-reload
systemctl enable kube-scheduler.service systemctl restart kube-scheduler.service
查看kube-scheduler是否運行
[root@k8s-1 /data/ssl_config/kubernetes] master
# ps -ef |grep kube-scheduler root 7269 2930 0 19:54 pts/0 00:00:00 grep --color=auto kube-scheduler root 13970 1 0 8月02 ? 00:01:21 /cloud/k8s/kubernetes/bin/kube-scheduler --logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect [root@k8s-1 /data/ssl_config/kubernetes] master # systemctl status kube-scheduler ● kube-scheduler.service - Kubernetes Scheduler Loaded: loaded (/usr/lib/systemd/system/kube-scheduler.service; enabled; vendor preset: disabled) Active: active (running) since 五 2019-08-02 18:33:11 CST; 1 day 1h ago Docs: https://github.com/kubernetes/kubernetes Main PID: 13970 (kube-scheduler) Tasks: 10 Memory: 19.1M CGroup: /system.slice/kube-scheduler.service └─13970 /cloud/k8s/kubernetes/bin/kube-scheduler --logtostderr=tru... 8月 03 19:46:52 k8s-1 kube-scheduler[13970]: I0803 19:46:52.580662 13970 ...d 8月 03 19:47:03 k8s-1 kube-scheduler[13970]: I0803 19:47:03.578985 13970 ...d 8月 03 19:47:03 k8s-1 kube-scheduler[13970]: I0803 19:47:03.583437 13970 ...d 8月 03 19:48:31 k8s-1 kube-scheduler[13970]: I0803 19:48:31.637472 13970 ...d 8月 03 19:48:48 k8s-1 kube-scheduler[13970]: I0803 19:48:48.617124 13970 ...d 8月 03 19:50:00 k8s-1 kube-scheduler[13970]: I0803 19:50:00.589103 13970 ...d 8月 03 19:50:18 k8s-1 kube-scheduler[13970]: I0803 19:50:18.616574 13970 ...d 8月 03 19:52:15 k8s-1 kube-scheduler[13970]: I0803 19:52:15.629976 13970 ...d 8月 03 19:52:45 k8s-1 kube-scheduler[13970]: I0803 19:52:45.580103 13970 ...d 8月 03 19:53:56 k8s-1 kube-scheduler[13970]: I0803 19:53:56.601687 13970 ...d Hint: Some lines were ellipsized, use -l to show in full.
3.5.5 部署kube-controller-manager
建立kube-controller-manager配置文件
vim /cloud/k8s/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=/cloud/k8s/kubernetes/ssl/ca.pem \ --cluster-signing-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem \ --root-ca-file=/cloud/k8s/kubernetes/ssl/ca.pem \ --service-account-private-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem"
建立kube-controller-manager systemd unit 文件
vim /usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/cloud/k8s/kubernetes/cfg/kube-controller-manager
ExecStart=/cloud/k8s/kubernetes/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS Restart=on-failure [Install] WantedBy=multi-user.target
啓動服務
systemctl daemon-reload
systemctl enable kube-controller-manager systemctl restart kube-controller-manager
查看kube-controller-manager是否運行
[root@k8s-1 /data/ssl_config/kubernetes] master
# ps -ef |grep kube-controller-manager root 7759 2930 0 19:59 pts/0 00:00:00 grep --color=auto kube-controller-manager root 13972 1 0 8月02 ? 00:10:56 /cloud/k8s/kubernetes/bin/kube-controller-manager --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=/cloud/k8s/kubernetes/ssl/ca.pem --cluster-signing-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem --root-ca-file=/cloud/k8s/kubernetes/ssl/ca.pem --service-account-private-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem [root@k8s-1 /data/ssl_config/kubernetes] master # systemctl status kube-controller-manager ● kube-controller-manager.service - Kubernetes Controller Manager Loaded: loaded (/usr/lib/systemd/system/kube-controller-manager.service; enabled; vendor preset: disabled) Active: active (running) since 五 2019-08-02 18:33:13 CST; 1 day 1h ago Docs: https://github.com/kubernetes/kubernetes Main PID: 13972 (kube-controller) Tasks: 10 Memory: 51.9M CGroup: /system.slice/kube-controller-manager.service └─13972 /cloud/k8s/kubernetes/bin/kube-controller-manager --logtos... 8月 03 19:59:48 k8s-1 kube-controller-manager[13972]: I0803 19:59:48.522342 ... 8月 03 19:59:48 k8s-1 kube-controller-manager[13972]: I0803 19:59:48.526381 ... 8月 03 19:59:50 k8s-1 kube-controller-manager[13972]: I0803 19:59:50.211112 ... 8月 03 19:59:50 k8s-1 kube-controller-manager[13972]: I0803 19:59:50.455555 ... 8月 03 19:59:50 k8s-1 kube-controller-manager[13972]: I0803 19:59:50.738397 ... 8月 03 19:59:50 k8s-1 kube-controller-manager[13972]: I0803 19:59:50.767332 ... 8月 03 19:59:51 k8s-1 kube-controller-manager[13972]: I0803 19:59:51.502835 ... 8月 03 19:59:53 k8s-1 kube-controller-manager[13972]: I0803 19:59:53.567923 ... 8月 03 19:59:53 k8s-1 kube-controller-manager[13972]: I0803 19:59:53.567955 ... 8月 03 19:59:54 k8s-1 kube-controller-manager[13972]: I0803 19:59:54.321341 ... Hint: Some lines were ellipsized, use -l to show in full.
3.5.6 查看master集羣狀態
[root@k8s-1 /root] master
# kubectl get cs,nodes NAME STATUS MESSAGE ERROR componentstatus/controller-manager Healthy ok componentstatus/scheduler Healthy ok componentstatus/etcd-2 Healthy {"health":"true"} componentstatus/etcd-1 Healthy {"health":"true"} componentstatus/etcd-0 Healthy {"health":"true"}
3.6 部署node 節點
kubernetes work 節點運行以下組件:
docker
kubelet
kube-proxy
3.6.1 部署 kubelet 組件
kublet 運行在每一個 worker 節點上,接收 kube-apiserver 發送的請求,管理 Pod 容器,執行交互式命令,如exec、run、logs 等;
kublet 啓動時自動向 kube-apiserver 註冊節點信息,內置的 cadvisor 統計和監控節點的資源使用狀況;
爲確保安全,本文檔只開啓接收 https 請求的安全端口,對請求進行認證和受權,拒絕未受權的訪問(如apiserver、heapster)。
配置node節點
cd /data/install/kubernetes/server/bin/ scp kubelet kube-proxy k8s-2:/cloud/k8s/kubernetes/bin/ scp kubelet kube-proxy k8s-3:/cloud/k8s/kubernetes/bin/
建立 kubelet bootstrap.kubeconfig 文件
cd /data/ssl_config/kubernetes/ vim environment.sh # 建立kubelet bootstrapping kubeconfig BOOTSTRAP_TOKEN=449bbeb0ea7e50f321087a123a509a19 KUBE_APISERVER="https://192.168.123.211: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
經過
bash environment.sh
獲取 bootstrap.kubeconfig 配置文件。
建立 kubelet.kubeconfig 文件
vim envkubelet.kubeconfig.sh
# 建立kubelet bootstrapping kubeconfig BOOTSTRAP_TOKEN=449bbeb0ea7e50f321087a123a509a19 KUBE_APISERVER="https://192.168.123.211:6443" # 設置集羣參數 kubectl config set-cluster kubernetes \ --certificate-authority=./ca.pem \ --embed-certs=true \ --server=${KUBE_APISERVER} \ --kubeconfig=kubelet.kubeconfig # 設置客戶端認證參數 kubectl config set-credentials kubelet \ --token=${BOOTSTRAP_TOKEN} \ --kubeconfig=kubelet.kubeconfig # 設置上下文參數 kubectl config set-context default \ --cluster=kubernetes \ --user=kubelet \ --kubeconfig=kubelet.kubeconfig # 設置默認上下文 kubectl config use-context default --kubeconfig=kubelet.kubeconfig
建立kube-proxy kubeconfig文件
vim env_proxy.sh
# 建立kube-proxy kubeconfig文件 BOOTSTRAP_TOKEN=449bbeb0ea7e50f321087a123a509a19 KUBE_APISERVER="https://192.168.123.211:6443" 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
將bootstrap kubeconfig kube-proxy.kubeconfig 文件拷貝到全部 nodes節點
scp -rp bootstrap.kubeconfig kube-proxy.kubeconfig k8s-2:/cloud/k8s/kubernetes/cfg/ scp -rp bootstrap.kubeconfig kube-proxy.kubeconfig k8s-3:/cloud/k8s/kubernetes/cfg/
3.6.2 建立kubelet 參數配置文件拷貝到全部 nodes節點
建立 kubelet 參數配置模板文件
[root@k8s-2 /root] node
# cat /cloud/k8s/kubernetes/cfg/kubelet.config kind: KubeletConfiguration apiVersion: kubelet.config.k8s.io/v1beta1 address: 192.168.123.212 port: 10250 readOnlyPort: 10255 cgroupDriver: cgroupfs clusterDomain: cluster.local. failSwapOn: false authentication: anonymous: enabled: true
建立kubelet配置文件
vim /cloud/k8s/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \ --v=4 \ --hostname-override=k8s-2 \ --kubeconfig=/cloud/k8s/kubernetes/cfg/kubelet.kubeconfig \ --bootstrap-kubeconfig=/cloud/k8s/kubernetes/cfg/bootstrap.kubeconfig \ --config=/cloud/k8s/kubernetes/cfg/kubelet.config \ --cert-dir=/cloud/k8s/kubernetes/ssl \ --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
建立kubelet systemd unit 文件
vim /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/cloud/k8s/kubernetes/cfg/kubelet
ExecStart=/cloud/k8s/kubernetes/bin/kubelet $KUBELET_OPTS Restart=on-failure KillMode=process [Install] WantedBy=multi-user.target
將kubelet-bootstrap用戶綁定到系統集羣角色
kubectl create clusterrolebinding kubelet-bootstrap \ --clusterrole=system:node-bootstrapper \ --user=kubelet-bootstrap
3.6.3 啓動kubelet服務
systemctl daemon-reload
systemctl enable kubelet systemctl restart kubelet
3.6.4 approve kubelet CSR 請求
kubectl get csr
kubectl certificate approve $NAME kubectl get csr csr 狀態變爲 Approved,Issued 便可
- Requesting User:請求 CSR 的用戶,kube-apiserver 對它進行認證和受權;
- Subject:請求籤名的證書信息;
- 證書的 CN 是 system:node:kube-node2, Organization 是 system:nodes,kube-apiserver 的 Node 受權模式會授予該證書的相關權限;
3.6.5 查看集羣狀態
[root@k8s-1 /root] master
# kubectl get nodes,cs NAME STATUS ROLES AGE VERSION node/k8s-2 Ready node 3d22h v1.15.1 node/k8s-3 Ready node 3d22h v1.15.1 NAME STATUS MESSAGE ERROR componentstatus/scheduler Healthy ok componentstatus/controller-manager Healthy ok componentstatus/etcd-0 Healthy {"health":"true"} componentstatus/etcd-2 Healthy {"health":"true"} componentstatus/etcd-1 Healthy {"health":"true"}
3.7 部署 node kube-proxy 組件
kube-proxy 運行在全部 node節點上,它監聽 apiserver 中 service 和 Endpoint 的變化狀況,建立路由規則來進行服務負載均衡。
3.7.1 建立kube-proxy配置文件
vim /cloud/k8s/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \ --v=4 \ --hostname-override=k8s-2 \ --cluster-cidr=10.0.0.0/24 \ --kubeconfig=/cloud/k8s/kubernetes/cfg/kube-proxy.kubeconfig"
3.7.2 建立kube-proxy systemd unit 文件
vim /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=/cloud/k8s/kubernetes/cfg/kube-proxy
ExecStart=/cloud/k8s/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS Restart=on-failure [Install] WantedBy=multi-user.target
3.7.3 啓動kube-proxy服務
systemctl daemon-reload
systemctl enable kube-proxy systemctl restart kube-proxy
3.7.4 檢查服務運行狀態
[root@k8s-2 /root] node
# systemctl status kube-proxy ● kube-proxy.service - Kubernetes Proxy Loaded: loaded (/usr/lib/systemd/system/kube-proxy.service; enabled; vendor preset: disabled) Active: active (running) since 二 2019-07-30 22:30:04 CST; 3 days ago Main PID: 5764 (kube-proxy) Tasks: 0 Memory: 11.8M CGroup: /system.slice/kube-proxy.service ‣ 5764 /cloud/k8s/kubernetes/bin/kube-proxy --logtostderr=true --v... 8月 03 20:51:33 k8s-2 kube-proxy[5764]: I0803 20:51:33.736946 5764 iptab...] 8月 03 20:51:33 k8s-2 kube-proxy[5764]: I0803 20:51:33.740738 5764 healt..." 8月 03 20:51:33 k8s-2 kube-proxy[5764]: I0803 20:51:33.740762 5764 proxi...s 8月 03 20:51:33 k8s-2 kube-proxy[5764]: I0803 20:51:33.740780 5764 bound...s 8月 03 20:51:34 k8s-2 kube-proxy[5764]: I0803 20:51:34.018700 5764 confi...e 8月 03 20:51:34 k8s-2 kube-proxy[5764]: I0803 20:51:34.028396 5764 confi...e 8月 03 20:51:36 k8s-2 kube-proxy[5764]: I0803 20:51:36.035812 5764 confi...e 8月 03 20:51:36 k8s-2 kube-proxy[5764]: I0803 20:51:36.046563 5764 confi...e 8月 03 20:51:38 k8s-2 kube-proxy[5764]: I0803 20:51:38.045395 5764 confi...e 8月 03 20:51:38 k8s-2 kube-proxy[5764]: I0803 20:51:38.061197 5764 confi...e Hint: Some lines were ellipsized, use -l to show in full.
至此 kubernetes 1.15 版本簡單部署完成~