1. 架构篇

1.1 kubernetes 架构说明

图片.png

1.2 Flannel网络架构图

图片.png

集群功能各模块功能描述: 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。 Node节点: 每个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的运行状态与期望的状态一致。

1.3 Kubernetes工作流程

图片.png

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 操作系统环境

  1. # 三台机器相同
  2. [root@k8s-1 /root] master
  3. # uname -r
  4. 3.10.0-957.el7.x86_64
  5. [root@k8s-1 /root] master
  6. # cat /etc/redhat-release
  7. CentOS Linux release 7.6.1810 (Core)
  8. [root@k8s-1 /root] master
  9. # sestatus
  10. 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

  1. systemctl stop firewalld && systemctl disable firewalld
  2. setenforce 0
  3. vi /etc/selinux/config
  4. SELINUX=disabled

3.1.2 关闭swap

  1. swapoff -a && sysctl -w vm.swappiness=0
  2. sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

3.1.3 设置Docker所需参数

  1. cat > /etc/sysctl.d/kubernetes.conf <<EOF
  2. net.ipv4.ip_forward=1
  3. net.ipv4.tcp_tw_recycle=0
  4. vm.swappiness=0
  5. vm.overcommit_memory=1
  6. vm.panic_on_oom=0
  7. fs.inotify.max_user_watches=89100
  8. fs.file-max=52706963
  9. fs.nr_open=52706963
  10. net.ipv6.conf.all.disable_ipv6=1
  11. EOF
  12. sysctl -p /etc/sysctl.d/kubernetes.conf

3.1.4 安装 Docker

  1. # 配置yum源
  2. cd /etc/yum.repo.d/
  3. wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  4. yum clean all ;yum repolist -y
  5. # 安装docker ,版本 18.06.2
  6. yum install docker-ce-18.06.2.ce-3.el7 -y
  7. # 启动
  8. systemctl start docker && systemctl enable docker

3.1.5 创建相关目录

  1. # 创建安装包存储目录
  2. mkdir /data/{install,ssl_config} -p
  3. mkdir /data/ssl_config/{etcd,kubernetes} -p
  4. # 创建安装目录
  5. mkdir /cloud/k8s/etcd/{bin,cfg,ssl} -p
  6. mkdir /cloud/k8s/kubernetes/{bin,cfg,ssl} -p

3.1.6 SSH 互信配置

  1. ssh-keygen
  2. ssh-copy-id k8s-1
  3. ssh-copy-id k8s-2
  4. ssh-copy-id k8s-3

3.1.7 添加 PATH 变量

  1. vi /etc/profile
  2. export PATH=$PATH:/cloud/k8s/etcd/bin/:/cloud/k8s/kubernetes/bin/

3.2 创建ssl证书

3.2.1 安装及配置CFSSL

  1. wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
  2. wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
  3. wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
  4. chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
  5. mv cfssl_linux-amd64 /usr/local/bin/cfssl
  6. mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
  7. mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo

3.2.2 创建 ETCD 相关证书

以下操作均在/data/ssl_config/etcd/目录中
etcd证书ca配置

  1. cd /data/ssl_config/etcd/
  2. cat << EOF | tee ca-config.json
  3. {
  4. "signing": {
  5. "default": {
  6. "expiry": "87600h"
  7. },
  8. "profiles": {
  9. "www": {
  10. "expiry": "87600h",
  11. "usages": [
  12. "signing",
  13. "key encipherment",
  14. "server auth",
  15. "client auth"
  16. ]
  17. }
  18. }
  19. }
  20. }
  21. EOF

创建 ETCD CA 配置文件

  1. cat << EOF | tee ca-csr.json
  2. {
  3. "CN": "etcd CA",
  4. "key": {
  5. "algo": "rsa",
  6. "size": 2048
  7. },
  8. "names": [
  9. {
  10. "C": "CN",
  11. "L": "Beijing",
  12. "ST": "Beijing"
  13. }
  14. ]
  15. }
  16. EOF

创建 ETCD Server 证书

  1. cat << EOF | tee server-csr.json
  2. {
  3. "CN": "etcd",
  4. "hosts": [
  5. "k8s-3",
  6. "k8s-2",
  7. "k8s-1",
  8. "192.168.123.211",
  9. "192.168.123.212",
  10. "192.168.123.213"
  11. ],
  12. "key": {
  13. "algo": "rsa",
  14. "size": 2048
  15. },
  16. "names": [
  17. {
  18. "C": "CN",
  19. "L": "Beijing",
  20. "ST": "Beijing"
  21. }
  22. ]
  23. }
  24. EOF

生成 ETCD CA 证书和私钥

  1. cd /data/ssl_config/etcd/
  2. # 生成ca证书
  3. cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
  4. # 生成server证书
  5. 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配置

  1. cd /data/ssl_config/kubernetes/
  2. cat << EOF | tee ca-config.json
  3. {
  4. "signing": {
  5. "default": {
  6. "expiry": "87600h"
  7. },
  8. "profiles": {
  9. "kubernetes": {
  10. "expiry": "87600h",
  11. "usages": [
  12. "signing",
  13. "key encipherment",
  14. "server auth",
  15. "client auth"
  16. ]
  17. }
  18. }
  19. }
  20. }
  21. EOF

创建ca证书配置

  1. cat << EOF | tee ca-csr.json
  2. {
  3. "CN": "kubernetes",
  4. "key": {
  5. "algo": "rsa",
  6. "size": 2048
  7. },
  8. "names": [
  9. {
  10. "C": "CN",
  11. "L": "Beijing",
  12. "ST": "Beijing",
  13. "O": "k8s",
  14. "OU": "System"
  15. }
  16. ]
  17. }
  18. EOF

生成API_SERVER证书

  1. cat << EOF | tee server-csr.json
  2. {
  3. "CN": "kubernetes",
  4. "hosts": [
  5. "10.0.0.1",
  6. "127.0.0.1",
  7. "192.168.123.211",
  8. "k8s-1",
  9. "kubernetes",
  10. "kubernetes.default",
  11. "kubernetes.default.svc",
  12. "kubernetes.default.svc.cluster",
  13. "kubernetes.default.svc.cluster.local"
  14. ],
  15. "key": {
  16. "algo": "rsa",
  17. "size": 2048
  18. },
  19. "names": [
  20. {
  21. "C": "CN",
  22. "L": "Beijing",
  23. "ST": "Beijing",
  24. "O": "k8s",
  25. "OU": "System"
  26. }
  27. ]
  28. }
  29. EOF

创建 Kubernetes Proxy 证书

  1. cat << EOF | tee kube-proxy-csr.json
  2. {
  3. "CN": "system:kube-proxy",
  4. "hosts": [],
  5. "key": {
  6. "algo": "rsa",
  7. "size": 2048
  8. },
  9. "names": [
  10. {
  11. "C": "CN",
  12. "L": "Beijing",
  13. "ST": "Beijing",
  14. "O": "k8s",
  15. "OU": "System"
  16. }
  17. ]
  18. }
  19. EOF

生成 kubernetes CA 证书和私钥

  1. # 生成ca证书
  2. cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
  3. # 生成 api-server 证书
  4. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
  5. # 生成 kube-proxy 证书
  6. 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 配置软件包

  1. cd /data/install/
  2. tar -xvf etcd-v3.3.10-linux-amd64.tar.gz
  3. cd etcd-v3.3.10-linux-amd64/
  4. cp etcd etcdctl /cloud/k8s/etcd/bin/

3.3.2 编辑etcd配置文件

  1. vim /cloud/k8s/etcd/cfg/etcd
  2. #[Member]
  3. ETCD_NAME="etcd01"
  4. ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  5. ETCD_LISTEN_PEER_URLS="https://192.168.123.211:2380"
  6. ETCD_LISTEN_CLIENT_URLS="https://192.168.123.211:2379"
  7. #[Clustering]
  8. ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.123.211:2380"
  9. ETCD_ADVERTISE_CLIENT_URLS="https://192.168.123.211:2379"
  10. ETCD_INITIAL_CLUSTER="etcd01=https://192.168.123.211:2380,etcd02=https://192.168.123.212:2380,etcd03=https://192.168.123.213:2380"
  11. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  12. ETCD_INITIAL_CLUSTER_STATE="new"

3.3.3 创建 etcd的 systemd unit 文件

  1. vim /usr/lib/systemd/system/etcd.service
  2. [Unit]
  3. Description=Etcd Server
  4. After=network.target
  5. After=network-online.target
  6. Wants=network-online.target
  7. [Service]
  8. Type=notify
  9. EnvironmentFile=/cloud/k8s/etcd/cfg/etcd
  10. ExecStart=/cloud/k8s/etcd/bin/etcd \
  11. --name=${ETCD_NAME} \
  12. --data-dir=${ETCD_DATA_DIR} \
  13. --listen-peer-urls=${ETCD_LISTEN_PEER_URLS} \
  14. --listen-client-urls=${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 \
  15. --advertise-client-urls=${ETCD_ADVERTISE_CLIENT_URLS} \
  16. --initial-advertise-peer-urls=${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
  17. --initial-cluster=${ETCD_INITIAL_CLUSTER} \
  18. --initial-cluster-token=${ETCD_INITIAL_CLUSTER_TOKEN} \
  19. --initial-cluster-state=new \
  20. --cert-file=/cloud/k8s/etcd/ssl/server.pem \
  21. --key-file=/cloud/k8s/etcd/ssl/server-key.pem \
  22. --peer-cert-file=/cloud/k8s/etcd/ssl/server.pem \
  23. --peer-key-file=/cloud/k8s/etcd/ssl/server-key.pem \
  24. --trusted-ca-file=/cloud/k8s/etcd/ssl/ca.pem \
  25. --peer-trusted-ca-file=/cloud/k8s/etcd/ssl/ca.pem
  26. Restart=on-failure
  27. LimitNOFILE=65536
  28. [Install]
  29. WantedBy=multi-user.target

3.3.4 配置证书文件

  1. cd /data/ssl_config/etcd/
  2. cp ca*pem server*pem /cloud/k8s/etcd/ssl

3.3.5 配置文件拷贝到 节点1、节点2

  1. cd /cloud/k8s/
  2. scp -r etcd k8s-2:/cloud/k8s/
  3. scp -r etcd k8s-3:/cloud/k8s/
  4. scp /usr/lib/systemd/system/etcd.service k8s-2:/usr/lib/systemd/system/etcd.service
  5. scp /usr/lib/systemd/system/etcd.service k8s-3:/usr/lib/systemd/system/etcd.service

修改另外两台机器配置文件

  1. ### k8s-2
  2. cat /cloud/k8s/etcd/cfg/etcd
  3. #[Member]
  4. ETCD_NAME="etcd02"
  5. ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  6. ETCD_LISTEN_PEER_URLS="https://192.168.123.212:2380"
  7. ETCD_LISTEN_CLIENT_URLS="https://192.168.123.212:2379"
  8. #[Clustering]
  9. ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.123.212:2380"
  10. ETCD_ADVERTISE_CLIENT_URLS="https://192.168.123.212:2379"
  11. ETCD_INITIAL_CLUSTER="etcd01=https://192.168.123.211:2380,etcd02=https://192.168.123.212:2380,etcd03=https://192.168.123.213:2380"
  12. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  13. ETCD_INITIAL_CLUSTER_STATE="new"
  14. ### k8s-3
  15. cat /cloud/k8s/etcd/cfg/etcd
  16. #[Member]
  17. ETCD_NAME="etcd03"
  18. ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  19. ETCD_LISTEN_PEER_URLS="https://192.168.123.213:2380"
  20. ETCD_LISTEN_CLIENT_URLS="https://192.168.123.213:2379"
  21. #[Clustering]
  22. ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.123.213:2380"
  23. ETCD_ADVERTISE_CLIENT_URLS="https://192.168.123.213:2379"
  24. ETCD_INITIAL_CLUSTER="etcd01=https://192.168.123.211:2380,etcd02=https://192.168.123.212:2380,etcd03=https://192.168.123.213:2380"
  25. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  26. ETCD_INITIAL_CLUSTER_STATE="new"

另外两台机器配置完成后,启动etcd服务,三台都需启动。

3.3.6 启动ETCD服务

  1. systemctl daemon-reload
  2. systemctl enable etcd
  3. systemctl start etcd
  4. #启动ETCD集群同时启动二个节点,单节点是无法正常启动的。

3.3.7 检查服务是否正常

  1. [root@k8s-1 /cloud/k8s/etcd/bin] master
  2. # 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
  3. member 4c6bfb873a73368c is healthy: got healthy result from https://192.168.123.213:2379
  4. member 622f71dbd55b34ce is healthy: got healthy result from https://192.168.123.211:2379
  5. member f14004aa5403b07d is healthy: got healthy result from https://192.168.123.212:2379
  6. cluster is healthy

3.4 部署Flannel网络

3.4.1 向 etcd 写入集群 Pod 网段信息

  1. cd /cloud/k8s/etcd/bin
  2. etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
  3. --endpoints="https://192.168.123.211:2379,https://192.168.123.212:2379,https://192.168.123.213:2379" \
  4. set /coreos.com/network/config \
  5. '{ "Network": "172.18.0.0/16", "Backend": {"Type": "vxlan"}}'
  1. 写入的 Pod 网段 ${CLUSTER_CIDR} 必须是 /16 段地址,必须与 kube-controller-manager --cluster-cidr 参数值一致;

3.4.2 部署 flannel

  1. cd /data/install/
  2. tar xf flannel-v0.11.0-linux-amd64.tar.gz
  3. mv flanneld mk-docker-opts.sh /cloud/k8s/kubernetes/bin/

3.5.3 配置flannel

  1. vim /cloud/k8s/kubernetes/cfg/flanneld
  2. 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 文件

  1. vim /usr/lib/systemd/system/flanneld.service
  2. [Unit]
  3. Description=Flanneld overlay address etcd agent
  4. After=network-online.target network.target
  5. Before=docker.service
  6. [Service]
  7. Type=notify
  8. EnvironmentFile=/cloud/k8s/kubernetes/cfg/flanneld
  9. ExecStart=/cloud/k8s/kubernetes/bin/flanneld --ip-masq $FLANNEL_OPTIONS
  10. ExecStartPost=/cloud/k8s/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
  11. Restart=on-failure
  12. [Install]
  13. WantedBy=multi-user.target
  1. mk-docker-opts.sh 脚本将分配给 flanneld Pod 子网网段信息写入 /run/flannel/docker 文件,后续 docker 启动时 使用这个文件中的环境变量配置 docker0 网桥;

flanneld 使用系统缺省路由所在的接口与其它节点通信,对于有多个网络接口(如内网和公网)的节点,可以用 -iface 参数指定通信接口,如上面的 eth0 接口; flanneld 运行时需要 root 权限;

3.5.5 配置Docker启动指定子网段

  1. vim /usr/lib/systemd/system/docker.service
  2. [Unit]
  3. Description=Docker Application Container Engine
  4. Documentation=https://docs.docker.com
  5. After=network-online.target firewalld.service
  6. Wants=network-online.target
  7. [Service]
  8. Type=notify
  9. EnvironmentFile=/run/flannel/subnet.env
  10. ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS
  11. ExecReload=/bin/kill -s HUP $MAINPID
  12. LimitNOFILE=infinity
  13. LimitNPROC=infinity
  14. LimitCORE=infinity
  15. TimeoutStartSec=0
  16. Delegate=yes
  17. KillMode=process
  18. Restart=on-failure
  19. StartLimitBurst=3
  20. StartLimitInterval=60s
  21. [Install]
  22. WantedBy=multi-user.target

3.5.6 将flanneld systemd unit 文件到所有节点

  1. cd /cloud/k8s/
  2. scp -r kubernetes k8s-2:/cloud/k8s/
  3. scp -r kubernetes k8s-3:/cloud/k8s/
  4. scp /cloud/k8s/kubernetes/cfg/flanneld k8s-2:/cloud/k8s/kubernetes/cfg/flanneld
  5. scp /cloud/k8s/kubernetes/cfg/flanneld k8s-3:/cloud/k8s/kubernetes/cfg/flanneld
  6. scp /usr/lib/systemd/system/docker.service k8s-2:/usr/lib/systemd/system/docker.service
  7. scp /usr/lib/systemd/system/docker.service k8s-3:/usr/lib/systemd/system/docker.service
  8. scp /usr/lib/systemd/system/flanneld.service k8s-2:/usr/lib/systemd/system/flanneld.service
  9. scp /usr/lib/systemd/system/flanneld.service k8s-3:/usr/lib/systemd/system/flanneld.service
  10. # 启动服务(每台节点都操作)
  11. systemctl daemon-reload
  12. systemctl start flanneld
  13. systemctl enable flanneld
  14. systemctl restart docker

3.5.7 验证fannel网络配置

  1. [root@k8s-1 /data/install] master
  2. # ip a
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  4. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5. inet 127.0.0.1/8 scope host lo
  6. valid_lft forever preferred_lft forever
  7. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
  8. link/ether 00:0c:29:33:de:53 brd ff:ff:ff:ff:ff:ff
  9. inet 192.168.123.211/24 brd 192.168.123.255 scope global noprefixroute eth0
  10. valid_lft forever preferred_lft forever
  11. 3: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
  12. link/ether 1a:6f:5a:0b:c7:18 brd ff:ff:ff:ff:ff:ff
  13. inet 172.18.30.0/32 scope global flannel.1
  14. valid_lft forever preferred_lft forever
  15. 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  16. link/ether 02:42:5c:d0:df:e6 brd ff:ff:ff:ff:ff:ff
  17. inet 172.18.30.1/24 brd 172.18.30.255 scope global docker0
  18. 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 节点文件

  1. cd /data/install/
  2. tar xf kubernetes-server-linux-amd64.tar.gz
  3. cd kubernetes/server/bin/
  4. cp kube-scheduler kube-apiserver kube-controller-manager kubectl /cloud/k8s/kubernetes/bin/

3.5.2 配置 kubernetes相关证书

  1. cd /data/ssl_config/kubernetes/
  2. cp *pem /cloud/k8s/kubernetes/ssl/

3.5.3 部署 kube-apiserver 组件

创建 TLS Bootstrapping Token

  1. # head -c 16 /dev/urandom | od -An -t x | tr -d ' '
  2. 449bbeb0ea7e50f321087a123a509a19
  3. # vim /cloud/k8s/kubernetes/cfg/token.csv
  4. 449bbeb0ea7e50f321087a123a509a19,kubelet-bootstrap,10001,"system:kubelet-bootstrap"

创建apiserver配置文件

  1. vim /cloud/k8s/kubernetes/cfg/kube-apiserver
  2. KUBE_APISERVER_OPTS="--logtostderr=true \
  3. --v=4 \
  4. --etcd-servers=https://192.168.123.211:2379,https://192.168.123.212:2379,https://192.168.123.213:2379 \
  5. --bind-address=192.168.123.211 \
  6. --secure-port=6443 \
  7. --advertise-address=192.168.123.211 \
  8. --allow-privileged=true \
  9. --service-cluster-ip-range=10.0.0.0/24 \
  10. --enable-admission-plugins=NamespaceLifecycle,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota,NodeRestriction \
  11. --authorization-mode=RBAC,Node \
  12. --enable-bootstrap-token-auth \
  13. --token-auth-file=/cloud/k8s/kubernetes/cfg/token.csv \
  14. --service-node-port-range=30000-50000 \
  15. --tls-cert-file=/cloud/k8s/kubernetes/ssl/server.pem \
  16. --tls-private-key-file=/cloud/k8s/kubernetes/ssl/server-key.pem \
  17. --client-ca-file=/cloud/k8s/kubernetes/ssl/ca.pem \
  18. --service-account-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem \
  19. --etcd-cafile=/cloud/k8s/etcd/ssl/ca.pem \
  20. --etcd-certfile=/cloud/k8s/etcd/ssl/server.pem \
  21. --etcd-keyfile=/cloud/k8s/etcd/ssl/server-key.pem"

创建 kube-apiserver systemd unit 文件

  1. vim /usr/lib/systemd/system/kube-apiserver.service
  2. [Unit]
  3. Description=Kubernetes API Server
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. [Service]
  6. EnvironmentFile=/cloud/k8s/kubernetes/cfg/kube-apiserver
  7. ExecStart=/cloud/k8s/kubernetes/bin/kube-apiserver $KUBE_APISERVER_OPTS
  8. Restart=on-failure
  9. [Install]
  10. WantedBy=multi-user.target

启动服务

  1. systemctl daemon-reload
  2. systemctl enable kube-apiserver
  3. systemctl restart kube-apiserver

查看apiserver是否运行

  1. [root@k8s-1 /data/ssl_config/kubernetes] master
  2. # ps -ef |grep kube-apiserver
  3. root 5475 1 2 730 ? 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
  4. root 6886 2930 0 19:49 pts/0 00:00:00 grep --color=auto kube-apiserver

3.5.4 部署kube-scheduler

创建kube-scheduler配置文件

  1. vim /cloud/k8s/kubernetes/cfg/kube-scheduler
  2. KUBE_SCHEDULER_OPTS="--logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect"
  1. --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 文件

  1. vim /usr/lib/systemd/system/kube-scheduler.service
  2. [Unit]
  3. Description=Kubernetes Scheduler
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. [Service]
  6. EnvironmentFile=/cloud/k8s/kubernetes/cfg/kube-scheduler
  7. ExecStart=/cloud/k8s/kubernetes/bin/kube-scheduler $KUBE_SCHEDULER_OPTS
  8. Restart=on-failure
  9. [Install]
  10. WantedBy=multi-user.target

启动kube-scheduler服务

  1. systemctl daemon-reload
  2. systemctl enable kube-scheduler.service
  3. systemctl restart kube-scheduler.service

查看kube-scheduler是否运行

  1. [root@k8s-1 /data/ssl_config/kubernetes] master
  2. # ps -ef |grep kube-scheduler
  3. root 7269 2930 0 19:54 pts/0 00:00:00 grep --color=auto kube-scheduler
  4. root 13970 1 0 802 ? 00:01:21 /cloud/k8s/kubernetes/bin/kube-scheduler --logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect
  5. [root@k8s-1 /data/ssl_config/kubernetes] master
  6. # systemctl status kube-scheduler
  7. kube-scheduler.service - Kubernetes Scheduler
  8. Loaded: loaded (/usr/lib/systemd/system/kube-scheduler.service; enabled; vendor preset: disabled)
  9. Active: active (running) since 2019-08-02 18:33:11 CST; 1 day 1h ago
  10. Docs: https://github.com/kubernetes/kubernetes
  11. Main PID: 13970 (kube-scheduler)
  12. Tasks: 10
  13. Memory: 19.1M
  14. CGroup: /system.slice/kube-scheduler.service
  15. └─13970 /cloud/k8s/kubernetes/bin/kube-scheduler --logtostderr=tru...
  16. 8 03 19:46:52 k8s-1 kube-scheduler[13970]: I0803 19:46:52.580662 13970 ...d
  17. 8 03 19:47:03 k8s-1 kube-scheduler[13970]: I0803 19:47:03.578985 13970 ...d
  18. 8 03 19:47:03 k8s-1 kube-scheduler[13970]: I0803 19:47:03.583437 13970 ...d
  19. 8 03 19:48:31 k8s-1 kube-scheduler[13970]: I0803 19:48:31.637472 13970 ...d
  20. 8 03 19:48:48 k8s-1 kube-scheduler[13970]: I0803 19:48:48.617124 13970 ...d
  21. 8 03 19:50:00 k8s-1 kube-scheduler[13970]: I0803 19:50:00.589103 13970 ...d
  22. 8 03 19:50:18 k8s-1 kube-scheduler[13970]: I0803 19:50:18.616574 13970 ...d
  23. 8 03 19:52:15 k8s-1 kube-scheduler[13970]: I0803 19:52:15.629976 13970 ...d
  24. 8 03 19:52:45 k8s-1 kube-scheduler[13970]: I0803 19:52:45.580103 13970 ...d
  25. 8 03 19:53:56 k8s-1 kube-scheduler[13970]: I0803 19:53:56.601687 13970 ...d
  26. Hint: Some lines were ellipsized, use -l to show in full.

3.5.5 部署kube-controller-manager

创建kube-controller-manager配置文件

  1. vim /cloud/k8s/kubernetes/cfg/kube-controller-manager
  2. KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \
  3. --v=4 \
  4. --master=127.0.0.1:8080 \
  5. --leader-elect=true \
  6. --address=127.0.0.1 \
  7. --service-cluster-ip-range=10.0.0.0/24 \
  8. --cluster-name=kubernetes \
  9. --cluster-signing-cert-file=/cloud/k8s/kubernetes/ssl/ca.pem \
  10. --cluster-signing-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem \
  11. --root-ca-file=/cloud/k8s/kubernetes/ssl/ca.pem \
  12. --service-account-private-key-file=/cloud/k8s/kubernetes/ssl/ca-key.pem"

创建kube-controller-manager systemd unit 文件

  1. vim /usr/lib/systemd/system/kube-controller-manager.service
  2. [Unit]
  3. Description=Kubernetes Controller Manager
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. [Service]
  6. EnvironmentFile=/cloud/k8s/kubernetes/cfg/kube-controller-manager
  7. ExecStart=/cloud/k8s/kubernetes/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS
  8. Restart=on-failure
  9. [Install]
  10. WantedBy=multi-user.target

启动服务

  1. systemctl daemon-reload
  2. systemctl enable kube-controller-manager
  3. systemctl restart kube-controller-manager

查看kube-controller-manager是否运行

  1. [root@k8s-1 /data/ssl_config/kubernetes] master
  2. # ps -ef |grep kube-controller-manager
  3. root 7759 2930 0 19:59 pts/0 00:00:00 grep --color=auto kube-controller-manager
  4. root 13972 1 0 802 ? 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
  5. [root@k8s-1 /data/ssl_config/kubernetes] master
  6. # systemctl status kube-controller-manager
  7. kube-controller-manager.service - Kubernetes Controller Manager
  8. Loaded: loaded (/usr/lib/systemd/system/kube-controller-manager.service; enabled; vendor preset: disabled)
  9. Active: active (running) since 2019-08-02 18:33:13 CST; 1 day 1h ago
  10. Docs: https://github.com/kubernetes/kubernetes
  11. Main PID: 13972 (kube-controller)
  12. Tasks: 10
  13. Memory: 51.9M
  14. CGroup: /system.slice/kube-controller-manager.service
  15. └─13972 /cloud/k8s/kubernetes/bin/kube-controller-manager --logtos...
  16. 8 03 19:59:48 k8s-1 kube-controller-manager[13972]: I0803 19:59:48.522342 ...
  17. 8 03 19:59:48 k8s-1 kube-controller-manager[13972]: I0803 19:59:48.526381 ...
  18. 8 03 19:59:50 k8s-1 kube-controller-manager[13972]: I0803 19:59:50.211112 ...
  19. 8 03 19:59:50 k8s-1 kube-controller-manager[13972]: I0803 19:59:50.455555 ...
  20. 8 03 19:59:50 k8s-1 kube-controller-manager[13972]: I0803 19:59:50.738397 ...
  21. 8 03 19:59:50 k8s-1 kube-controller-manager[13972]: I0803 19:59:50.767332 ...
  22. 8 03 19:59:51 k8s-1 kube-controller-manager[13972]: I0803 19:59:51.502835 ...
  23. 8 03 19:59:53 k8s-1 kube-controller-manager[13972]: I0803 19:59:53.567923 ...
  24. 8 03 19:59:53 k8s-1 kube-controller-manager[13972]: I0803 19:59:53.567955 ...
  25. 8 03 19:59:54 k8s-1 kube-controller-manager[13972]: I0803 19:59:54.321341 ...
  26. Hint: Some lines were ellipsized, use -l to show in full.

3.5.6 查看master集群状态

  1. [root@k8s-1 /root] master
  2. # kubectl get cs,nodes
  3. NAME STATUS MESSAGE ERROR
  4. componentstatus/controller-manager Healthy ok
  5. componentstatus/scheduler Healthy ok
  6. componentstatus/etcd-2 Healthy {"health":"true"}
  7. componentstatus/etcd-1 Healthy {"health":"true"}
  8. componentstatus/etcd-0 Healthy {"health":"true"}

3.6 部署node 节点

kubernetes work 节点运行如下组件: docker kubelet kube-proxy

3.6.1 部署 kubelet 组件

  1. kublet 运行在每个 worker 节点上,接收 kube-apiserver 发送的请求,管理 Pod 容器,执行交互式命令,如execrunlogs 等;

kublet 启动时自动向 kube-apiserver 注册节点信息,内置的 cadvisor 统计和监控节点的资源使用情况; 为确保安全,本文档只开启接收 https 请求的安全端口,对请求进行认证和授权,拒绝未授权的访问(如apiserver、heapster)。

配置node节点

  1. cd /data/install/kubernetes/server/bin/
  2. scp kubelet kube-proxy k8s-2:/cloud/k8s/kubernetes/bin/
  3. scp kubelet kube-proxy k8s-3:/cloud/k8s/kubernetes/bin/

创建 kubelet bootstrap.kubeconfig 文件

  1. cd /data/ssl_config/kubernetes/
  2. vim environment.sh
  3. # 创建kubelet bootstrapping kubeconfig
  4. BOOTSTRAP_TOKEN=449bbeb0ea7e50f321087a123a509a19
  5. KUBE_APISERVER="https://192.168.123.211:6443"
  6. # 设置集群参数
  7. kubectl config set-cluster kubernetes \
  8. --certificate-authority=./ca.pem \
  9. --embed-certs=true \
  10. --server=${KUBE_APISERVER} \
  11. --kubeconfig=bootstrap.kubeconfig
  12. # 设置客户端认证参数
  13. kubectl config set-credentials kubelet-bootstrap \
  14. --token=${BOOTSTRAP_TOKEN} \
  15. --kubeconfig=bootstrap.kubeconfig
  16. # 设置上下文参数
  17. kubectl config set-context default \
  18. --cluster=kubernetes \
  19. --user=kubelet-bootstrap \
  20. --kubeconfig=bootstrap.kubeconfig
  21. # 设置默认上下文
  22. kubectl config use-context default --kubeconfig=bootstrap.kubeconfig

通过 bash environment.sh获取 bootstrap.kubeconfig 配置文件。

创建 kubelet.kubeconfig 文件

  1. vim envkubelet.kubeconfig.sh
  2. # 创建kubelet bootstrapping kubeconfig
  3. BOOTSTRAP_TOKEN=449bbeb0ea7e50f321087a123a509a19
  4. KUBE_APISERVER="https://192.168.123.211:6443"
  5. # 设置集群参数
  6. kubectl config set-cluster kubernetes \
  7. --certificate-authority=./ca.pem \
  8. --embed-certs=true \
  9. --server=${KUBE_APISERVER} \
  10. --kubeconfig=kubelet.kubeconfig
  11. # 设置客户端认证参数
  12. kubectl config set-credentials kubelet \
  13. --token=${BOOTSTRAP_TOKEN} \
  14. --kubeconfig=kubelet.kubeconfig
  15. # 设置上下文参数
  16. kubectl config set-context default \
  17. --cluster=kubernetes \
  18. --user=kubelet \
  19. --kubeconfig=kubelet.kubeconfig
  20. # 设置默认上下文
  21. kubectl config use-context default --kubeconfig=kubelet.kubeconfig

创建kube-proxy kubeconfig文件

  1. vim env_proxy.sh
  2. # 创建kube-proxy kubeconfig文件
  3. BOOTSTRAP_TOKEN=449bbeb0ea7e50f321087a123a509a19
  4. KUBE_APISERVER="https://192.168.123.211:6443"
  5. kubectl config set-cluster kubernetes \
  6. --certificate-authority=./ca.pem \
  7. --embed-certs=true \
  8. --server=${KUBE_APISERVER} \
  9. --kubeconfig=kube-proxy.kubeconfig
  10. kubectl config set-credentials kube-proxy \
  11. --client-certificate=./kube-proxy.pem \
  12. --client-key=./kube-proxy-key.pem \
  13. --embed-certs=true \
  14. --kubeconfig=kube-proxy.kubeconfig
  15. kubectl config set-context default \
  16. --cluster=kubernetes \
  17. --user=kube-proxy \
  18. --kubeconfig=kube-proxy.kubeconfig
  19. kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig

将bootstrap kubeconfig kube-proxy.kubeconfig 文件拷贝到所有 nodes节点

  1. scp -rp bootstrap.kubeconfig kube-proxy.kubeconfig k8s-2:/cloud/k8s/kubernetes/cfg/
  2. scp -rp bootstrap.kubeconfig kube-proxy.kubeconfig k8s-3:/cloud/k8s/kubernetes/cfg/

3.6.2 创建kubelet 参数配置文件拷贝到所有 nodes节点

创建 kubelet 参数配置模板文件

  1. [root@k8s-2 /root] node
  2. # cat /cloud/k8s/kubernetes/cfg/kubelet.config
  3. kind: KubeletConfiguration
  4. apiVersion: kubelet.config.k8s.io/v1beta1
  5. address: 192.168.123.212
  6. port: 10250
  7. readOnlyPort: 10255
  8. cgroupDriver: cgroupfs
  9. clusterDomain: cluster.local.
  10. failSwapOn: false
  11. authentication:
  12. anonymous:
  13. enabled: true

创建kubelet配置文件

  1. vim /cloud/k8s/kubernetes/cfg/kubelet
  2. KUBELET_OPTS="--logtostderr=true \
  3. --v=4 \
  4. --hostname-override=k8s-2 \
  5. --kubeconfig=/cloud/k8s/kubernetes/cfg/kubelet.kubeconfig \
  6. --bootstrap-kubeconfig=/cloud/k8s/kubernetes/cfg/bootstrap.kubeconfig \
  7. --config=/cloud/k8s/kubernetes/cfg/kubelet.config \
  8. --cert-dir=/cloud/k8s/kubernetes/ssl \
  9. --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"

创建kubelet systemd unit 文件

  1. vim /usr/lib/systemd/system/kubelet.service
  2. [Unit]
  3. Description=Kubernetes Kubelet
  4. After=docker.service
  5. Requires=docker.service
  6. [Service]
  7. EnvironmentFile=/cloud/k8s/kubernetes/cfg/kubelet
  8. ExecStart=/cloud/k8s/kubernetes/bin/kubelet $KUBELET_OPTS
  9. Restart=on-failure
  10. KillMode=process
  11. [Install]
  12. WantedBy=multi-user.target

将kubelet-bootstrap用户绑定到系统集群角色

  1. kubectl create clusterrolebinding kubelet-bootstrap \
  2. --clusterrole=system:node-bootstrapper \
  3. --user=kubelet-bootstrap

3.6.3 启动kubelet服务

  1. systemctl daemon-reload
  2. systemctl enable kubelet
  3. systemctl restart kubelet

3.6.4 approve kubelet CSR 请求

  1. kubectl get csr
  2. kubectl certificate approve $NAME
  3. kubectl get csr
  4. csr 状态变为 Approved,Issued 即可
  • Requesting User:请求 CSR 的用户,kube-apiserver 对它进行认证和授权;
  • Subject:请求签名的证书信息;
  • 证书的 CN 是 system:node:kube-node2, Organization 是 system:nodes,kube-apiserver 的 Node 授权模式会授予该证书的相关权限;

3.6.5 查看集群状态

  1. [root@k8s-1 /root] master
  2. # kubectl get nodes,cs
  3. NAME STATUS ROLES AGE VERSION
  4. node/k8s-2 Ready node 3d22h v1.15.1
  5. node/k8s-3 Ready node 3d22h v1.15.1
  6. NAME STATUS MESSAGE ERROR
  7. componentstatus/scheduler Healthy ok
  8. componentstatus/controller-manager Healthy ok
  9. componentstatus/etcd-0 Healthy {"health":"true"}
  10. componentstatus/etcd-2 Healthy {"health":"true"}
  11. componentstatus/etcd-1 Healthy {"health":"true"}

3.7 部署 node kube-proxy 组件

kube-proxy 运行在所有 node节点上,它监听 apiserver 中 service 和 Endpoint 的变化情况,创建路由规则来进行服务负载均衡。

3.7.1 创建kube-proxy配置文件

  1. vim /cloud/k8s/kubernetes/cfg/kube-proxy
  2. KUBE_PROXY_OPTS="--logtostderr=true \
  3. --v=4 \
  4. --hostname-override=k8s-2 \
  5. --cluster-cidr=10.0.0.0/24 \
  6. --kubeconfig=/cloud/k8s/kubernetes/cfg/kube-proxy.kubeconfig"

3.7.2 创建kube-proxy systemd unit 文件

  1. vim /usr/lib/systemd/system/kube-proxy.service
  2. [Unit]
  3. Description=Kubernetes Proxy
  4. After=network.target
  5. [Service]
  6. EnvironmentFile=/cloud/k8s/kubernetes/cfg/kube-proxy
  7. ExecStart=/cloud/k8s/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS
  8. Restart=on-failure
  9. [Install]
  10. WantedBy=multi-user.target

3.7.3 启动kube-proxy服务

  1. systemctl daemon-reload
  2. systemctl enable kube-proxy
  3. systemctl restart kube-proxy

3.7.4 检查服务运行状态

  1. [root@k8s-2 /root] node
  2. # systemctl status kube-proxy
  3. kube-proxy.service - Kubernetes Proxy
  4. Loaded: loaded (/usr/lib/systemd/system/kube-proxy.service; enabled; vendor preset: disabled)
  5. Active: active (running) since 2019-07-30 22:30:04 CST; 3 days ago
  6. Main PID: 5764 (kube-proxy)
  7. Tasks: 0
  8. Memory: 11.8M
  9. CGroup: /system.slice/kube-proxy.service
  10. 5764 /cloud/k8s/kubernetes/bin/kube-proxy --logtostderr=true --v...
  11. 8 03 20:51:33 k8s-2 kube-proxy[5764]: I0803 20:51:33.736946 5764 iptab...]
  12. 8 03 20:51:33 k8s-2 kube-proxy[5764]: I0803 20:51:33.740738 5764 healt..."
  13. 8月 03 20:51:33 k8s-2 kube-proxy[5764]: I0803 20:51:33.740762 5764 proxi...s
  14. 8月 03 20:51:33 k8s-2 kube-proxy[5764]: I0803 20:51:33.740780 5764 bound...s
  15. 8月 03 20:51:34 k8s-2 kube-proxy[5764]: I0803 20:51:34.018700 5764 confi...e
  16. 8月 03 20:51:34 k8s-2 kube-proxy[5764]: I0803 20:51:34.028396 5764 confi...e
  17. 8月 03 20:51:36 k8s-2 kube-proxy[5764]: I0803 20:51:36.035812 5764 confi...e
  18. 8月 03 20:51:36 k8s-2 kube-proxy[5764]: I0803 20:51:36.046563 5764 confi...e
  19. 8月 03 20:51:38 k8s-2 kube-proxy[5764]: I0803 20:51:38.045395 5764 confi...e
  20. 8月 03 20:51:38 k8s-2 kube-proxy[5764]: I0803 20:51:38.061197 5764 confi...e
  21. Hint: Some lines were ellipsized, use -l to show in full.

至此 kubernetes 1.15 版本简单部署完成~