[关闭]
@zhangyy 2021-05-13T15:04:03.000000Z 字数 22180 阅读 132

Kubernetes1.18.18 集群部署

kubernetes升级系列



一:环境初始化准备

1.1 环境信息

  1. 系统:CentOS7.9x64
  2. 主机名:
  3. cat /etc/hosts
  4. -----
  5. 192.168.3.171 t-k8sM-001
  6. 192.168.3.172 t-k8sM-002
  7. 192.168.3.173 t-k8sM-003
  8. 192.168.3.174 t-k8sN-001
  9. 192.168.3.175 t-k8sN-002
  10. ------

1.2 环境部署需求

1.2.1 单Master架构图:

image_1f5fai19qkdjng11g77rcc13pp9.png-2040.3kB

1.2.2 部署角色分配

  1. t-k8sM-001 ---> apiserver/controller-manager/scheduer/etcd
  2. t-k8sM-002 ---> apiserver/controller-manager/scheduer/etcd
  3. t-k8sM-003 ---> apiserver/controller-manager/scheduer/etcd
  4. t-k8sN-001 ----> docker/kubelet/kube-proxy/etcd/
  5. t-k8sN-002 ----> docker/kubelet/kube-proxy/etcd/

1.2.3 系统初始化

  1. # 关闭防火墙
  2. systemctl stop firewalld
  3. systemctl disable firewalld
  4. # 关闭selinux
  5. sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
  6. setenforce 0 # 临时
  7. # 关闭swap
  8. swapoff -a # 临时
  9. sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
  10. # 根据规划设置主机名
  11. hostnamectl set-hostname <hostname>
  12. # 在master添加hosts
  13. cat >> /etc/hosts << EOF
  14. 192.168.3.171 t-k8sM-001
  15. 192.168.3.172 t-k8sM-002
  16. 192.168.3.173 t-k8sM-003
  17. 192.168.3.174 t-k8sN-001
  18. 192.168.3.175 t-k8sN-002
  19. EOF
  20. # 将桥接的IPv4流量传递到iptables的链
  21. cat > /etc/sysctl.d/k8s.conf << EOF
  22. net.bridge.bridge-nf-call-ip6tables = 1
  23. net.bridge.bridge-nf-call-iptables = 1
  24. EOF
  25. sysctl --system # 生效
  26. # 时间同步
  27. yum install chronyd
  28. server ntp1.aliyun.com

二:ETCD 环境部署

2.1 etcd 介绍

  1. Etcd 的概念:
  2. Etcd 是一个分布式键值存储系统,Kubernetes使用Etcd进行数据存储,所以先准备一个Etcd数据库,为解决Etcd单点故障,应采用集群方式部署,这里使用3台组建集群,可容忍1台机器故障,当然,你也可以使用5台组建集群,可容忍2台机器故障。

2.2 部署etcd集群

2.2.1 自签名ETCD 证书

  1. cfssl是一个开源的证书管理工具,使用json文件生成证书,相比openssl更方便使用。
  2. 找任意一台服务器操作,这里用Master节点。
  3. ---
  4. wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
  5. wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
  6. wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
  7. chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
  8. mv cfssl_linux-amd64 /usr/bin/cfssl
  9. mv cfssljson_linux-amd64 /usr/bin/cfssljson
  10. mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo

image_1f5fbamv9171l1v5u1voa1ni11kk5m.png-60.4kB

  1. 1. 自签证书颁发机构(CA
  2. 创建工作目录:
  3. mkdir -p ~/TLS/{etcd,k8s}
  4. cd TLS/etcd
  5. ---
  6. 自签CA
  7. cat > ca-config.json << EOF
  8. {
  9. "signing": {
  10. "default": {
  11. "expiry": "87600h"
  12. },
  13. "profiles": {
  14. "www": {
  15. "expiry": "87600h",
  16. "usages": [
  17. "signing",
  18. "key encipherment",
  19. "server auth",
  20. "client auth"
  21. ]
  22. }
  23. }
  24. }
  25. }
  26. EOF
  27. cat > ca-csr.json << EOF
  28. {
  29. "CN": "etcd CA",
  30. "key": {
  31. "algo": "rsa",
  32. "size": 2048
  33. },
  34. "names": [
  35. {
  36. "C": "CN",
  37. "L": "Beijing",
  38. "ST": "Beijing"
  39. }
  40. ]
  41. }
  42. EOF
  43. ---
  44. cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

image_1f5fg3ootg2tupf1mf61r94fuo1j.png-81.5kB

image_1f5fg45tkl0fnaglfd6fg1n6320.png-126.3kB

  1. 2. 使用自签CA签发Etcd HTTPS证书
  2. 创建证书申请文件:
  3. cat > server-csr.json << EOF
  4. {
  5. "CN": "etcd",
  6. "hosts": [
  7. "192.168.3.171",
  8. "192.168.3.172",
  9. "192.168.3.173",
  10. "192.168.3.174",
  11. "192.168.3.175",
  12. "192.168.3.176",
  13. "192.168.3.177",
  14. "192.168.3.178",
  15. "192.168.3.179",
  16. "192.168.3.180",
  17. "192.168.3.181",
  18. "192.168.3.182",
  19. "192.168.3.183",
  20. "192.168.3.184",
  21. "192.168.3.185",
  22. "192.168.3.186",
  23. "192.168.3.187",
  24. "192.168.3.188",
  25. "192.168.3.189",
  26. "192.168.3.201"
  27. ],
  28. "key": {
  29. "algo": "rsa",
  30. "size": 2048
  31. },
  32. "names": [
  33. {
  34. "C": "CN",
  35. "L": "BeiJing",
  36. "ST": "BeiJing"
  37. }
  38. ]
  39. }
  40. EOF
  41. 生成证书:
  42. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
  43. ls server*pem
  44. server-key.pem server.pem

image_1f5fhgime3c1nl310kk5q6i009.png-145.5kB


2.2.2下载安装etcd

  1. 下载地址:https://github.com/etcd-io/etcd/releases/download/v3.4.14/etcd-v3.4.14-linux-amd64.tar.gz
  2. 以下在节点1上操作,为简化操作,待会将节点1生成的所有文件拷贝到节点2和节点3.

  1. 1. 创建工作目录并解压二进制包
  2. mkdir /data/application/etcd/{bin,cfg,ssl} -p
  3. tar zxvf etcd-v3.4.14-linux-amd64.tar.gz
  4. mv etcd-v3.4.14-linux-amd64/{etcd,etcdctl} /data/application/etcd/bin/

image_1f5fgedrpt8r1og6a9p12lc1rtc2d.png-187.3kB

image_1f5fgf1ei3g3ifv12bc1rpgkms2q.png-56.7kB

  1. 创建etcd配置文件
  2. cat > /data/application/etcd/cfg/etcd.conf << EOF
  3. #[Member]
  4. ETCD_NAME="etcd-1"
  5. ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  6. ETCD_LISTEN_PEER_URLS="https://192.168.3.171:2380"
  7. ETCD_LISTEN_CLIENT_URLS="https://192.168.3.171:2379"
  8. #[Clustering]
  9. ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.3.171:2380"
  10. ETCD_ADVERTISE_CLIENT_URLS="https://192.168.3.171:2379"
  11. ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.3.171:2380,etcd-2=https://192.168.3.172:2380,etcd-3=https://192.168.3.173:2380"
  12. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  13. ETCD_INITIAL_CLUSTER_STATE="new"
  14. EOF
  15. ----
  16. ETCD_NAME:节点名称,集群中唯一
  17. ETCD_DATA_DIR:数据目录
  18. ETCD_LISTEN_PEER_URLS:集群通信监听地址
  19. ETCD_LISTEN_CLIENT_URLS:客户端访问监听地址
  20. ETCD_INITIAL_ADVERTISE_PEER_URLS:集群通告地址
  21. ETCD_ADVERTISE_CLIENT_URLS:客户端通告地址
  22. ETCD_INITIAL_CLUSTER:集群节点地址
  23. ETCD_INITIAL_CLUSTER_TOKEN:集群Token
  24. ETCD_INITIAL_CLUSTER_STATE:加入集群的当前状态,new是新集群,existing表示加入已有集群
  25. -----

  1. systemd管理etcd 启动文件
  2. cat > /usr/lib/systemd/system/etcd.service << EOF
  3. [Unit]
  4. Description=Etcd Server
  5. After=network.target
  6. After=network-online.target
  7. Wants=network-online.target
  8. [Service]
  9. Type=notify
  10. EnvironmentFile=/data/application/etcd/cfg/etcd.conf
  11. ExecStart=/data/application/etcd/bin/etcd \
  12. --cert-file=/data/application/etcd/ssl/server.pem \
  13. --key-file=/data/application/etcd/ssl/server-key.pem \
  14. --peer-cert-file=/data/application/etcd/ssl/server.pem \
  15. --peer-key-file=/data/application/etcd/ssl/server-key.pem \
  16. --trusted-ca-file=/data/application/etcd/ssl/ca.pem \
  17. --peer-trusted-ca-file=/data/application/etcd/ssl/ca.pem \
  18. --logger=zap
  19. Restart=on-failure
  20. LimitNOFILE=65536
  21. [Install]
  22. WantedBy=multi-user.target
  23. EOF

  1. 拷贝刚才生成的证书
  2. 把刚才生成的证书拷贝到配置文件中的路径:
  3. cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /data/application/etcd/ssl/

image_1f5fhit3f1a371ka7116m1kc65kdm.png-95.1kB

  1. 同步所有节点:
  2. scp -P36022 -r /data/application/etcd/ root@192.168.3.172:/data/application/
  3. scp -P36022 /usr/lib/systemd/system/etcd.service root@192.168.3.172:/usr/lib/systemd/system/
  4. scp -P36022 -r /data/application/etcd/ root@192.168.3.173:/opt/application/
  5. scp -P36022 /usr/lib/systemd/system/etcd.service root@192.168.3.173:/usr/lib/systemd/system/

image_1f5fi44k419t31m7a198o1evs8h113.png-131.6kB

  1. 然后在节点2和节点3分别修改etcd.conf配置文件中的节点名称和当前服务器IP
  2. vi /opt/etcd/cfg/etcd.conf
  3. #[Member]
  4. ETCD_NAME="etcd-1" # 修改此处,节点2改为etcd-2,节点3改为etcd-3
  5. ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  6. ETCD_LISTEN_PEER_URLS="https://192.168.3.171:2380" # 修改此处为当前服务器IP
  7. ETCD_LISTEN_CLIENT_URLS="https://192.168.3.171:2379" # 修改此处为当前服务器IP
  8. #[Clustering]
  9. ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.3.171:2380" # 修改此处为当前服务器IP
  10. ETCD_ADVERTISE_CLIENT_URLS="https://192.168.3.171:2379" # 修改此处为当前服务器IP
  11. ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.3.171:2380,etcd-2=https://192.168.3.172:2380,etcd-3=https://192.168.3.173:2380"
  12. ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
  13. ETCD_INITIAL_CLUSTER_STATE="new"

  1. 启动etcd
  2. 同步所有启动所有节点:
  3. systemctl daemon-reload
  4. systemctl start etcd
  5. systemctl enable etcd

  1. 测试:
  2. ETCDCTL_API=3 /data/application/etcd/bin/etcdctl --cacert=/data/application/etcd/ssl/ca.pem --cert=/data/application/etcd/ssl/server.pem --key=/data/application/etcd/ssl/server-key.pem --endpoints="https://192.168.3.171:2379,https://192.168.3.172:2379,https://192.168.3.173:2379" endpoint health

image_1f5fib2afvlfvrg1ir81udu1gg61g.png-94.6kB


三 部署kubernetes集群

3.1 部署k8s的Master Node

  1. 生成kube-apiserver证书
  2. 1. 自签证书颁发机构(CA
  3. cd /root/TLS/k8s/
  4. ---
  5. cat > ca-config.json << EOF
  6. {
  7. "signing": {
  8. "default": {
  9. "expiry": "87600h"
  10. },
  11. "profiles": {
  12. "kubernetes": {
  13. "expiry": "87600h",
  14. "usages": [
  15. "signing",
  16. "key encipherment",
  17. "server auth",
  18. "client auth"
  19. ]
  20. }
  21. }
  22. }
  23. }
  24. EOF
  25. cat > ca-csr.json << EOF
  26. {
  27. "CN": "kubernetes",
  28. "key": {
  29. "algo": "rsa",
  30. "size": 2048
  31. },
  32. "names": [
  33. {
  34. "C": "CN",
  35. "L": "Beijing",
  36. "ST": "Beijing",
  37. "O": "k8s",
  38. "OU": "System"
  39. }
  40. ]
  41. }
  42. EOF

  1. 生成证书:
  2. cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

image_1f5fq1e02i271l5j1c9pfbfdn11t.png-90.3kB


  1. 使用自签CA签发kube-apiserver HTTPS证书
  2. 创建证书申请文件:
  3. cat > server-csr.json << EOF
  4. {
  5. "CN": "kubernetes",
  6. "hosts": [
  7. "10.0.0.1",
  8. "127.0.0.1",
  9. "192.168.3.171",
  10. "192.168.3.172",
  11. "192.168.3.173",
  12. "192.168.3.174",
  13. "192.168.3.175",
  14. "192.168.3.176",
  15. "192.168.3.177",
  16. "192.168.3.178",
  17. "192.168.3.179",
  18. "192.168.3.180",
  19. "192.168.3.181",
  20. "192.168.3.182",
  21. "192.168.3.183",
  22. "192.168.3.184",
  23. "192.168.3.185",
  24. "192.168.3.186",
  25. "192.168.3.187",
  26. "192.168.3.188",
  27. "192.168.3.189",
  28. "192.168.3.201",
  29. "kubernetes",
  30. "kubernetes.default",
  31. "kubernetes.default.svc",
  32. "kubernetes.default.svc.cluster",
  33. "kubernetes.default.svc.cluster.local"
  34. ],
  35. "key": {
  36. "algo": "rsa",
  37. "size": 2048
  38. },
  39. "names": [
  40. {
  41. "C": "CN",
  42. "L": "BeiJing",
  43. "ST": "BeiJing",
  44. "O": "k8s",
  45. "OU": "System"
  46. }
  47. ]
  48. }
  49. EOF
  50. 注:上述文件hosts字段中IP为所有Master/LB/VIP IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP

  1. 生成证书:
  2. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server

image_1f5fshgblb41u3p86dqji1sg12a.png-119.3kB

  1. k8s 的下载安装
  2. Github下载二进制文件
  3. 下载地址: https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.18.md#v1183
  4. 注:打开链接你会发现里面有很多包,下载一个server包就够了,包含了MasterWorker Node二进制文件。
  5. 解压二进制包
  6. mkdir -p /data/application/kubernetes/{bin,cfg,ssl,logs}
  7. tar zxvf kubernetes-server-linux-amd64.tar.gz
  8. cd kubernetes/server/bin
  9. cp kube-apiserver kube-scheduler kube-controller-manager /data/application/kubernetes/bin
  10. cp kubectl /usr/bin/

image_1f5ft5p8s18t7122d1j0r10c71ih834.png-145.4kB

image_1f5ftahp015od1a1daias11tua3h.png-71.6kB

  1. 部署apiserver
  2. 部署kube-apiserver
  3. 1. 创建配置文件
  4. cat > /data/application/kubernetes/cfg/kube-apiserver.conf << EOF
  5. KUBE_APISERVER_OPTS="--logtostderr=false \\
  6. --v=2 \\
  7. --log-dir=/data/application/kubernetes/logs \\
  8. --etcd-servers=https://192.168.3.171:2379,https://192.168.3.172:2379,https://192.168.3.173:2379 \\
  9. --bind-address=192.168.3.171 \\
  10. --secure-port=6443 \\
  11. --advertise-address=192.168.3.171 \\
  12. --allow-privileged=true \\
  13. --service-cluster-ip-range=10.0.0.0/16 \\
  14. --enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\
  15. --authorization-mode=RBAC,Node \\
  16. --enable-bootstrap-token-auth=true \\
  17. --token-auth-file=/data/application/kubernetes/cfg/token.csv \\
  18. --service-node-port-range=30000-32767 \\
  19. --kubelet-client-certificate=/data/application/kubernetes/ssl/server.pem \\
  20. --kubelet-client-key=/data/application/kubernetes/ssl/server-key.pem \\
  21. --tls-cert-file=/data/application/kubernetes/ssl/server.pem \\
  22. --tls-private-key-file=/data/application/kubernetes/ssl/server-key.pem \\
  23. --client-ca-file=/data/application/kubernetes/ssl/ca.pem \\
  24. --service-account-key-file=/data/application/kubernetes/ssl/ca-key.pem \\
  25. --etcd-cafile=/data/application/etcd/ssl/ca.pem \\
  26. --etcd-certfile=/data/application/etcd/ssl/server.pem \\
  27. --etcd-keyfile=/data/application/etcd/ssl/server-key.pem \\
  28. --audit-log-maxage=30 \\
  29. --audit-log-maxbackup=3 \\
  30. --audit-log-maxsize=100 \\
  31. --audit-log-path=/data/application/kubernetes/logs/k8s-audit.log"
  32. EOF

image_1f5fuasbo1grobcn5l91vp81nuo3u.png-197.1kB


  1. 拷贝刚才生成的证书
  2. 把刚才生成的证书拷贝到配置文件中的路径:
  3. cp ~/TLS/k8s/ca*pem ~/TLS/k8s/server*pem /data/application/kubernetes/ssl/

  1. 启用 TLS Bootstrapping 机制
  2. TLS BootstrapingMaster apiserver启用TLS认证后,Node节点kubeletkube-proxy要与kube-apiserver进行通信,必须使用CA签发的有效证书才可以,当Node节点很多时,这种客户端证书颁发需要大量工作,同样也会增加集群扩展复杂度。为了简化流程,Kubernetes引入了TLS bootstraping机制来自动颁发客户端证书,kubelet会以一个低权限用户自动向apiserver申请证书,kubelet的证书由apiserver动态签署。所以强烈建议在Node上使用这种方式,目前主要用于kubeletkube-proxy还是由我们统一颁发一个证书。
  3. TLS bootstraping 工作流程:

image_1f5fud1k2vs6125s14ib10un1noe4b.png-170.2kB

  1. 创建上述配置文件中token文件:
  2. cat > /data/application/kubernetes/cfg/token.csv << EOF
  3. c47ffb939f5ca36231d9e3121a252940,kubelet-bootstrap,10001,"system:node-bootstrapper"
  4. EOF

image_1f5fuf9bqn3hajg1tce1a8t8ji4o.png-35kB

  1. systemd管理apiserver
  2. cat > /usr/lib/systemd/system/kube-apiserver.service << EOF
  3. [Unit]
  4. Description=Kubernetes API Server
  5. Documentation=https://github.com/kubernetes/kubernetes
  6. [Service]
  7. EnvironmentFile=/data/application/kubernetes/cfg/kube-apiserver.conf
  8. ExecStart=/data/application/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
  9. Restart=on-failure
  10. [Install]
  11. WantedBy=multi-user.target
  12. EOF

image_1f5fujo2j14kp1s3f63kuvt1get55.png-69.3kB

  1. 启动并设置开机启动
  2. systemctl daemon-reload
  3. systemctl start kube-apiserver
  4. systemctl enable kube-apiserver

image_1f5fuva001dcn1hj310jup61ohq5i.png-169.7kB

  1. 授权kubelet-bootstrap用户允许请求证书
  2. kubectl create clusterrolebinding kubelet-bootstrap \
  3. --clusterrole=system:node-bootstrapper \
  4. --user=kubelet-bootstrap

image_1f5fv8hct106mc13n01959g935v.png-62.5kB

  1. 部署kube-controller-manager
  2. 创建配置文件
  3. cat > /data/application/kubernetes/cfg/kube-controller-manager.conf << EOF
  4. KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=false \\
  5. --v=2 \\
  6. --log-dir=/data/application/kubernetes/logs \\
  7. --leader-elect=true \\
  8. --master=127.0.0.1:8080 \\
  9. --bind-address=127.0.0.1 \\
  10. --allocate-node-cidrs=true \\
  11. --cluster-cidr=10.244.0.0/16 \\
  12. --service-cluster-ip-range=10.0.0.0/16 \\
  13. --cluster-signing-cert-file=/data/application/kubernetes/ssl/ca.pem \\
  14. --cluster-signing-key-file=/data/application/kubernetes/ssl/ca-key.pem \\
  15. --root-ca-file=/data/application/kubernetes/ssl/ca.pem \\
  16. --service-account-private-key-file=/data/application/kubernetes/ssl/ca-key.pem \\
  17. --experimental-cluster-signing-duration=87600h0m0s"
  18. EOF

image_1f5fvl4eh2vl8eo17gq53r16ip6s.png-127.1kB

  1. systemd管理controller-manager
  2. cat > /usr/lib/systemd/system/kube-controller-manager.service << EOF
  3. [Unit]
  4. Description=Kubernetes Controller Manager
  5. Documentation=https://github.com/kubernetes/kubernetes
  6. [Service]
  7. EnvironmentFile=/data/application/kubernetes/cfg/kube-controller-manager.conf
  8. ExecStart=/data/application/kubernetes/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS
  9. Restart=on-failure
  10. [Install]
  11. WantedBy=multi-user.target
  12. EOF

image_1f5fvlipvtbh16i91a0r1i0bqr679.png-83.2kB

  1. 启动并设置开机启动
  2. systemctl daemon-reload
  3. systemctl start kube-controller-manager
  4. systemctl enable kube-controller-manager

  1. 部署kube-scheduler
  2. 1. 创建配置文件
  3. cat > /data/application/kubernetes/cfg/kube-scheduler.conf << EOF
  4. KUBE_SCHEDULER_OPTS="--logtostderr=false \
  5. --v=2 \
  6. --log-dir=/data/application/kubernetes/logs \
  7. --leader-elect \
  8. --master=127.0.0.1:8080 \
  9. --bind-address=127.0.0.1"
  10. EOF

  1. systemd管理scheduler
  2. cat > /usr/lib/systemd/system/kube-scheduler.service << EOF
  3. [Unit]
  4. Description=Kubernetes Scheduler
  5. Documentation=https://github.com/kubernetes/kubernetes
  6. [Service]
  7. EnvironmentFile=/data/application/kubernetes/cfg/kube-scheduler.conf
  8. ExecStart=/data/application/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
  9. Restart=on-failure
  10. [Install]
  11. WantedBy=multi-user.target
  12. EOF

  1. 启动并设置开机启动
  2. systemctl daemon-reload
  3. systemctl start kube-scheduler
  4. systemctl enable kube-scheduler

  1. 查看集群状态:
  2. kubectl get cs

image_1f5fvu0rio551v1lfea3hfpi7m.png-55.1kB

3.2 部署node 节点

  1. t-k8sN-001 节点部署docker:
  2. 下载地址:https://download.docker.com/linux/static/stable/x86_64/docker-20.10.3.tgz
  3. 以下在所有节点操作。这里采用二进制安装,用yum安装也一样。
  4. t-k8sN-001 t-k8sN-002 节点上面安装

  1. 解压二进制包
  2. tar zxvf docker-20.10.3.tgz
  3. mv docker/* /usr/bin

  1. systemd管理docker
  2. cat > /usr/lib/systemd/system/docker.service << EOF
  3. [Unit]
  4. Description=Docker Application Container Engine
  5. Documentation=https://docs.docker.com
  6. After=network-online.target firewalld.service
  7. Wants=network-online.target
  8. [Service]
  9. Type=notify
  10. ExecStart=/usr/bin/dockerd
  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
  23. EOF

  1. 创建配置文件
  2. mkdir /etc/docker
  3. cat > /etc/docker/daemon.json << EOF
  4. {
  5. "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
  6. }
  7. EOF

  1. 启动并设置开机启动
  2. systemctl daemon-reload
  3. systemctl start docker
  4. systemctl enable docker

  1. 配置k8s目录
  2. 下面还是在Master Node上操作,即同时作为Worker Node
  3. 创建工作目录并拷贝二进制文件
  4. 在所有worker node创建工作目录:
  5. mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}

image_1f5g1pve218ap6ia1d1f14uq16tb83.png-40.4kB

  1. master节点拷贝
  2. cd /software/kubernetes/server/bin
  3. cp kubelet kube-proxy /data/application/kubernetes/bin # 本地拷贝

  1. 部署kubelet
  2. 1. 创建配置文件
  3. cat > /data/application/kubernetes/cfg/kubelet.conf << EOF
  4. KUBELET_OPTS="--logtostderr=false \\
  5. --v=2 \\
  6. --log-dir=/data/application/kubernetes/logs \\
  7. --hostname-override=t-k8sM-001 \\
  8. --network-plugin=cni \\
  9. --kubeconfig=/data/application/kubernetes/cfg/kubelet.kubeconfig \\
  10. --bootstrap-kubeconfig=/data/application/kubernetes/cfg/bootstrap.kubeconfig \\
  11. --config=/data/application/kubernetes/cfg/kubelet-config.yml \\
  12. --cert-dir=/data/application/kubernetes/ssl \\
  13. --pod-infra-container-image=lizhenliang/pause-amd64:3.0"
  14. EOF

image_1f5g2286p1g391d2818ic1v5e16en8g.png-87.7kB


  1. 配置参数文件
  2. cat > /data/application/kubernetes/cfg/kubelet-config.yml << EOF
  3. kind: KubeletConfiguration
  4. apiVersion: kubelet.config.k8s.io/v1beta1
  5. address: 0.0.0.0
  6. port: 10250
  7. readOnlyPort: 10255
  8. cgroupDriver: cgroupfs
  9. clusterDNS:
  10. - 10.0.0.2
  11. clusterDomain: cluster.local
  12. failSwapOn: false
  13. authentication:
  14. anonymous:
  15. enabled: false
  16. webhook:
  17. cacheTTL: 2m0s
  18. enabled: true
  19. x509:
  20. clientCAFile: /data/application/kubernetes/ssl/ca.pem
  21. authorization:
  22. mode: Webhook
  23. webhook:
  24. cacheAuthorizedTTL: 5m0s
  25. cacheUnauthorizedTTL: 30s
  26. evictionHard:
  27. imagefs.available: 15%
  28. memory.available: 100Mi
  29. nodefs.available: 10%
  30. nodefs.inodesFree: 5%
  31. maxOpenFiles: 1000000
  32. maxPods: 110
  33. EOF

image_1f5g262jr1jg16pn1fb61dnl1jr08t.png-149.7kB

  1. server节点上面执行
  2. 生成bootstrap.kubeconfig文件
  3. 写一个boot.sh 脚本 把下面的内容放进去
  4. ---
  5. KUBE_APISERVER="https://192.168.3.171:6443" # apiserver IP:PORT
  6. TOKEN="c47ffb939f5ca36231d9e3121a252940" # 与token.csv里保持一致
  7. # 生成 kubelet bootstrap kubeconfig 配置文件
  8. kubectl config set-cluster kubernetes \
  9. --certificate-authority=/data/application/kubernetes/ssl/ca.pem \
  10. --embed-certs=true \
  11. --server=${KUBE_APISERVER} \
  12. --kubeconfig=bootstrap.kubeconfig
  13. kubectl config set-credentials "kubelet-bootstrap" \
  14. --token=${TOKEN} \
  15. --kubeconfig=bootstrap.kubeconfig
  16. kubectl config set-context default \
  17. --cluster=kubernetes \
  18. --user="kubelet-bootstrap" \
  19. --kubeconfig=bootstrap.kubeconfig
  20. kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
  21. ---
  22. . ./boot.sh

image_1f5g2b38d18p77m9na11o35qbq9a.png-40.5kB

  1. 拷贝到配置文件路径:
  2. cp bootstrap.kubeconfig /data/application/kubernetes/cfg

  1. 同步k8s 安装目录到 t-k8sN-001 节点:
  2. scp -P36022 -r /data/application/kubernetes/ root@192.168.3.174:/data/application/

image_1f5hpor1uatg1uouram1h095j09.png-166.1kB

  1. 删掉 日志目录(t-k8sN-001)
  2. cd /data/application/kubernetes/logs/
  3. rm -rf *

image_1f5hps4hf1mt3tqg19pq7hdsqom.png-169.5kB

  1. 修改kubelet.conf 文件
  2. cd /data/application/kubernetes/cfg/
  3. vim kubelet.conf
  4. -----
  5. --hostname-override=t-k8sN-001
  6. ----

image_1f5hqbvie1po3132o1oub1k23kv213.png-105.8kB


  1. systemd管理kubelet
  2. cat > /usr/lib/systemd/system/kubelet.service << EOF
  3. [Unit]
  4. Description=Kubernetes Kubelet
  5. After=docker.service
  6. [Service]
  7. EnvironmentFile=/opt/kubernetes/cfg/kubelet.conf
  8. ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS
  9. Restart=on-failure
  10. LimitNOFILE=65536
  11. [Install]
  12. WantedBy=multi-user.target
  13. EOF

image_1f5hqeiqojvt182b1fl8jln48m1g.png-86.2kB

  1. 启动kubelet 开机启动
  2. systemctl daemon-reload
  3. systemctl start kubelet
  4. systemctl enable kubelet

image_1f5hqi1ikthm17d9mshade15fl1t.png-96.5kB


  1. t-k8sM-001 配置授权
  2. kubectl get csr

image_1f5hqkcgootdt2ort915oe1mlr2d.png-59.8kB


  1. 批准申请
  2. kubectl certificate approve node-csr-ogqwQ6-0S8zTmL9nF84MXk24_GUsZYi1ecc7Eu0udV8

image_1f5hqmppb11f9sil10qs1vr91nte2q.png-131.8kB


  1. kubectl get node

image_1f5hqumptrc21ndpg0b2h7nmf37.png-42kB

  1. 注:由于网络插件还没有部署,节点会没有准备就绪 NotReady

  1. 部署kube-proxy
  2. 部署kube-proxy
  3. 1. 创建配置文件
  4. cat > /data/application/kubernetes/cfg/kube-proxy.conf << EOF
  5. KUBE_PROXY_OPTS="--logtostderr=false \\
  6. --v=2 \\
  7. --log-dir=/data/application/kubernetes/logs \\
  8. --config=/data/application/kubernetes/cfg/kube-proxy-config.yml"
  9. EOF

  1. 配置参数文件
  2. cat > /data/application/kubernetes/cfg/kube-proxy-config.yml << EOF
  3. kind: KubeProxyConfiguration
  4. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  5. bindAddress: 0.0.0.0
  6. metricsBindAddress: 0.0.0.0:10249
  7. clientConnection:
  8. kubeconfig: /data/application/kubernetes/cfg/kube-proxy.kubeconfig
  9. hostnameOverride: t-k8sN-001
  10. clusterCIDR: 10.0.0.0/16
  11. mode: ipvs
  12. ipvs:
  13. scheduler: "rr"
  14. iptables:
  15. masqueradeAll: true
  16. EOF

image_1f5hrj0fm7p51g87ag111s81fjb3k.png-90.4kB

  1. # 切换工作目录
  2. cd /root/TLS/k8s
  3. # 创建证书请求文件
  4. cat > kube-proxy-csr.json << EOF
  5. {
  6. "CN": "system:kube-proxy",
  7. "hosts": [],
  8. "key": {
  9. "algo": "rsa",
  10. "size": 2048
  11. },
  12. "names": [
  13. {
  14. "C": "CN",
  15. "L": "BeiJing",
  16. "ST": "BeiJing",
  17. "O": "k8s",
  18. "OU": "System"
  19. }
  20. ]
  21. }
  22. EOF
  23. ---
  24. # 生成证书
  25. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
  26. ls kube-proxy*pem
  27. kube-proxy-key.pem kube-proxy.pem
  28. cp -p kube-proxy-key.pem kube-proxy.pem /data/application/kubernetes/ssl/

image_1f5hrodl9idj1913stu1n6b1hrv41.png-178.5kB

image_1f5hrpbro7jipt81i7t1rd7hbr4e.png-45.5kB


  1. 生成kubeconfig文件:
  2. cd /opt/kubernetes/ssl/
  3. vim kubeconfig.sh
  4. ----
  5. KUBE_APISERVER="https://192.168.3.171:6443"
  6. kubectl config set-cluster kubernetes \
  7. --certificate-authority=/data/application/kubernetes/ssl/ca.pem \
  8. --embed-certs=true \
  9. --server=${KUBE_APISERVER} \
  10. --kubeconfig=kube-proxy.kubeconfig
  11. kubectl config set-credentials kube-proxy \
  12. --client-certificate=./kube-proxy.pem \
  13. --client-key=./kube-proxy-key.pem \
  14. --embed-certs=true \
  15. --kubeconfig=kube-proxy.kubeconfig
  16. kubectl config set-context default \
  17. --cluster=kubernetes \
  18. --user=kube-proxy \
  19. --kubeconfig=kube-proxy.kubeconfig
  20. kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
  21. ----

image_1f5hrsjvm1ak51ifvmr1b231g0t4r.png-140.2kB


  1. cp -p kube-proxy.kubeconfig /data/application/kubernetes/cfg/

image_1f5hru6i816nmgm51vtl1dki198k58.png-32.1kB


  1. 同步/data/application/kubernetes 目录
  2. scp -P36022 -r /data/application/kubernetes/ root@192.168.3.174:/data/application/

  1. t-k8sN-001 上面修改文件
  2. cd /opt/kubernetes/cfg
  3. vim kubelet.conf
  4. --hostname-override=t-k8sN-001
  5. vim kube-proxy-config.yml
  6. hostnameOverride: t-k8sN-001

image_1f5hs8p14brq1ngokecs9g1m905l.png-110.7kB

  1. systemd管理kube-proxy
  2. cat > /usr/lib/systemd/system/kube-proxy.service << EOF
  3. [Unit]
  4. Description=Kubernetes Proxy
  5. After=network.target
  6. [Service]
  7. EnvironmentFile=/data/application/kubernetes/cfg/kube-proxy.conf
  8. ExecStart=/data/application/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS
  9. Restart=on-failure
  10. LimitNOFILE=65536
  11. [Install]
  12. WantedBy=multi-user.target
  13. EOF

image_1f5hscc731s20pfuib3oq1emg62.png-79kB

  1. 启动并设置开机启动
  2. systemctl daemon-reload
  3. systemctl start kube-proxy
  4. systemctl enable kube-proxy

image_1f5hserdipuu1j752r7i51fih6f.png-107.5kB


  1. 部署CNI
  2. 部署CNI网络
  3. 先准备好CNI二进制文件:
  4. 下载地址:https://github.com/containernetworking/plugins/releases/download/v0.8.6/cni-plugins-linux-amd64-v0.8.6.tgz
  5. 解压二进制包并移动到默认工作目录:
  6. mkdir /opt/cni/bin -p
  7. tar zxvf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/bin
  8. scp -p36022 -r /opt/cni/ root@192.168.3.174:/opt/

image_1f5hsmua4ndp1p9or71ohpb246s.png-30.7kB

image_1f5hsnd5h7cgma2rju1li1an179.png-112.1kB

image_1f5htvge41cko1jn41dc7qarm8b7m.png-67.8kB

  1. 部署CNI网络:
  2. wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  3. sed -i -r "s#quay.io/coreos/flannel:.*-amd64#lizhenliang/flannel:v0.12.0-amd64#g" kube-flannel.yml

image_1f5i23jm710kp1unq17e7uke1a3c8j.png-150.2kB


  1. kubectl apply -f kube-flannel.yml
  2. kubectl get node

image_1f5i263uq1m0k1felkb64ge1b389d.png-85kB

image_1f5i2535elp11gd4qmu1l591ujh90.png-47.5kB

四:增加一个worker node

4.1 先在增加节点部署一个docker

  1. docker -version

image_1f5i2e89ih7fsba1ik21tbt1obb9q.png-146.6kB

  1. 增加 一个 work节点:(从t-k8sN-001 节点同步目录)
  2. scp -P36022 -r /data/application/kubernetes root@192.168.3.175:/data/application/
  3. scp -P36022 -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.3.175:/usr/lib/systemd/system
  4. scp -P36022 -r /opt/cni/ root@192.168.3.175:/opt/
  5. scp -P36022 -r /data/application/kubernetes/ssl/ca.pem root@192.168.3.175:/data/application/kubernetes/ssl

image_1f5i2qg651e2k10gcmutk9b1etva7.png-63.3kB

  1. 删掉 生成文件
  2. 删除kubelet证书和kubeconfig文件
  3. rm -rf /data/application/kubernetes/cfg/kubelet.kubeconfig
  4. rm -rf /data/application/kubernetes/ssl/kubelet*

image_1f5i2tn9j1tcq1t3c6371qc018h3ak.png-39.2kB

  1. 修改主机名
  2. vim /data/application/kubernetes/cfg/kubelet.conf
  3. --hostname-override=t-k8sN-002
  4. vim /data/application/kubernetes/cfg/kube-proxy-config.yml
  5. hostnameOverride: t-k8sN-002

image_1f5i316jp1k59mqp1jt51djk17b5b1.png-90.2kB

image_1f5i32ckm1v6m13felej1edm19ufbe.png-78.9kB


  1. 设置开机启动
  2. 启动并设置开机启动
  3. systemctl daemon-reload
  4. systemctl start kubelet
  5. systemctl enable kubelet
  6. systemctl start kube-proxy
  7. systemctl enable kube-proxy

image_1f5i35s9cfvrn6j1r5hug53a4br.png-94.3kB

  1. kubectl get csr
  2. kubectl certificate approve node-csr-wVXc2iMF7kE3a1qjwIGo7qkG0l0lNOGLWaOkU3x7j54

image_1f5i37av4rhg178si2ok89mu4c8.png-55.4kB

image_1f5i3ar3m17os5mbu801251l7fcl.png-118.4kB

  1. kubectl get node

image_1f5i3cd7jfd412bk1m6dmv5173od2.png-72.6kB

  1. 配置部署DashboardCoreDNS
  2. 部署Dashboard:
  3. wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
  4. 默认Dashboard只能集群内部访问,修改ServiceNodePort类型,暴露到外部:
  5. vim recommended.yaml
  6. ---
  7. wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
  8. 默认Dashboard只能集群内部访问,修改ServiceNodePort类型,暴露到外部:
  9. vim recommended.yaml
  10. kind: Service
  11. apiVersion: v1
  12. metadata:
  13. labels:
  14. k8s-app: kubernetes-dashboard
  15. name: kubernetes-dashboard
  16. namespace: kubernetes-dashboard
  17. spec:
  18. ports:
  19. - port: 443
  20. targetPort: 8443
  21. nodePort: 30001
  22. type: NodePort
  23. selector:
  24. k8s-app: kubernetes-dashboard

image_1f5i47nog1vuspge1t3714621i86df.png-42.4kB

  1. ----
  2. kubectl apply -f recommended.yaml
  3. kubectl get pods,svc -n kubernetes-dashboard
  4. ---

image_1f5i4d0jn1uts4dg1vn1lfoj8sf6.png-95.3kB

  1. 创建service account并绑定默认cluster-admin管理员集群角色:
  2. kubectl create serviceaccount dashboard-admin -n kube-system
  3. kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
  4. kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

image_1f5i4eck81v2a2ta1h3kqrr1n6fj.png-227.4kB


  1. 打开firefox web 访问:
  2. https://192.168.3.174:30001

image_1f5i4ldgsbfmatg1si4takdjgd.png-240.8kB

image_1f5i4ig8fuvt1ouc4hi1f171jhgg0.png-358.3kB


  1. 配置coreDNS
  2. kubectl apply -f coredns.yaml

image_1f5i57e8c1l5o1rr41dfb1bmm1lfvgq.png-63.3kB

  1. DNS解析测试:
  2. kubectl run -it --rm dns-test --image=busybox:1.28.4 sh
  3. 进入容器问题:
  4. 创建apiserverkubelet的权限,就是没有给kubernetes用户rbac授权
  5. error: unable to upgrade connection: Forbidden (user=kubernetes, verb=create, resource=nodes, subresource=proxy)

  1. cat > apiserver-to-kubelet.yaml <<EOF
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. kind: ClusterRole
  4. metadata:
  5. annotations:
  6. rbac.authorization.kubernetes.io/autoupdate: "true"
  7. labels:
  8. kubernetes.io/bootstrapping: rbac-defaults
  9. name: system:kubernetes-to-kubelet
  10. rules:
  11. - apiGroups:
  12. - ""
  13. resources:
  14. - nodes/proxy
  15. - nodes/stats
  16. - nodes/log
  17. - nodes/spec
  18. - nodes/metrics
  19. verbs:
  20. - "*"
  21. ---
  22. apiVersion: rbac.authorization.k8s.io/v1
  23. kind: ClusterRoleBinding
  24. metadata:
  25. name: system:kubernetes
  26. namespace: ""
  27. roleRef:
  28. apiGroup: rbac.authorization.k8s.io
  29. kind: ClusterRole
  30. name: system:kubernetes-to-kubelet
  31. subjects:
  32. - apiGroup: rbac.authorization.k8s.io
  33. kind: User
  34. name: kubernetes
  35. EOF

  1. kubectl apply -f apiserver-to-kubelet.yaml

image_1f5i5h38u6dd17619vt1ji3h1oh7.png-136.3kB


  1. kubectl run -it --rm dns-test --image=busybox:1.28.4 sh

  1. nslookup kubernetes

image_1f5i5le5424hreb1u0210bun2vhk.png-34.6kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注