生产环境---K8s单节点二进制部署

文章目录

      • 一、K8s单master群集环境介绍
      • 二、Master节点组件介绍
      • 三、node节点组件介绍
      • 四、实验步骤
      • 五、Etcd群集部署
      • 六、Flannel网络部署
      • 七、Master节点部署
      • 八、node节点部署
        • node1节点配置
        • node2节点配置
      • 九、验证群集信息

一、K8s单master群集环境介绍

群集拓扑图:
生产环境---K8s单节点二进制部署_第1张图片

服务器规划:
生产环境---K8s单节点二进制部署_第2张图片

自签SSL证书:
生产环境---K8s单节点二进制部署_第3张图片

二、Master节点组件介绍

1、kube-apiserver:是集群的统一入口,各组件协调者,所有对象资源的增删改查和监听操 作都交给 APIServer 处理后再提交给 Etcd 存储;

2、kube-controller-manager:处理群集中常规后台任务,一个资源对应一个控制器,而 controller-manager 就是负责管理这些控制器的;

3、kube-scheduler:根据调度算法为新创建的 Pod 选择一个 Node 节点,可以任意部署,可以部署在同一个节点上,也可以部署在不同节点上。

4、kube-bootstrap:用户绑定到系统群集的角色,用于master控制node节点,在master节点上创建,推送给node节点,node节点使用该用户像master发送颁发证书请求

三、node节点组件介绍

1、kubelet:是master在node节点上的agent,可以管理本机运行容器的生命周期,例如创建容器、Pod挂载数据卷、下载secret、获取容器和节点状态等工作,kubelet 将每个 Pod转换成一组容器。

2、kube-proxy:在 node节点上实现 Pod网络代理,维护网络规划和四层负载均衡工作。

3、docker:容器运行环境

四、实验步骤

部署etcd群集----》》部署flannel容器群集网络----》》部署master节点----》》部署node节点

五、Etcd群集部署

参考上篇博客ETCD群集,点此跳

六、Flannel网络部署

生产环境---K8s单节点二进制部署_第4张图片

  • Flannel是Overlay网络的一种,也是将源数据封装在另一种网络包里面进行路由转发和通信;
  • Flannel通过给每台宿主机分配一个子网的方式为容器提供虚拟网络,它基于Linux TUN/TAP,使用UDP/VXLAN封装IP包来创建overlay网络,并借助etcd(也支持kubernetes)维护网络的分配情况。
  • 在2个不同子网的容器通信时将数据流量封装在UDP协议中,外层又加了一层ip,(两层ip)内部ip是pod的ip,中间承载协议是UDP,外部是node真实网卡地址

flannel部署步骤如下:

  • 在msater节点中为flannel分配子网,并写入etcd数据库中
[root@k8s_master ~]# /opt/etcd/bin/etcdctl --ca-file=/opt/etcd/ssl/ca.pem --cert-file=/opt/etcd/ssl/server.pem --key-file=/opt/etcd/ssl/server-key.pem --endpoints="https://192.168.142.220:2379,https://192.168.5.20:2379,https://192.168.5.30:2379" set /coreos.com/network/config '{"Network":"172.17.0.0/16","Backend":{"Type":"vxlan"}}'
  • 2个node节点上安装docker引擎
    Docker-ce版部署,点此跳

  • 2个node节点解压安装flannel二进制源码包,源码包下载地址:https://github.com/coreos/flannel/releases

tar zxvf flannel-v0.10.0-linux-amd64.tar.gz 
  • 2个node节点创建K8S工作目录便于管理
##创建目录
mkdir -p /opt/kubernetes/{cfg,ssl,bin}
##移动flanneld到工作目录
mv flanneld mk-docker-opts.sh /opt/kubernetes/bin/
  • 2个node节点更改flannel配置文件
cat <<EOF >/opt/kubernetes/cfg/flanneld.cfg
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.5.10:2379,https://192.168.5.20:2379,https://192.168.5.30:2379 \
-etcd-cafile=/opt/etcd/ssl/ca.pem \
-etcd-certfile=/opt/etcd/ssl/server.pem \
-etcd-keyfile=/opt/etcd/ssl/server-key.pem 
EOF
  • 2个node节点编写flannel的启动脚本
cat <<EOF >/usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service

[Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld.cfg
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \$FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
  • 2个node节点启动flannel服务
chmod +x /usr/lib/systemd/system/flanneld.service
systemctl start flanneld.service
systemctl enable flanneld.service
systemctl status flanneld.service
  • 配置docker连接flannel
vim /usr/lib/systemd/system/docker.service
##13行添加
EnvironmentFile=/run/flannel/subnet.env 
##修改14行
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS(添加该变量,加入flannel网络中) -H fd:// --containerd=/run/containerd/containerd.sock

生产环境---K8s单节点二进制部署_第5张图片

  • 2个node节点检查自身分配的子网段
cat /run/flannel/subnet.env
  • 2个node节点重启容器使配置文件生效
systemctl daemon-reload
systemctl restart docker
  • 检查flannel网络互通性,2个节点都安装centos7容器,进入容器使用ping指令检查网络
##2个节点都安装容器
docker run -it centos:7 /bin/bash
yum install -y net-tools

##node1容器ip
[root@ae25456150cb /]# ifconfig | sed -n '2p'|awk '{print $2}'
172.17.53.2

##noede2容器ip
[root@5f9a085e6bbd /]# ifconfig | sed -n '2p'|awk '{print $2}'
172.17.20.2

###测试联通性
[root@ae25456150cb /]# ping 172.17.20.2
PING 172.17.20.2 (172.17.20.2) 56(84) bytes of data.
64 bytes from 172.17.20.2: icmp_seq=1 ttl=62 time=1.12 ms
64 bytes from 172.17.20.2: icmp_seq=2 ttl=62 time=1.04 ms
64 bytes from 172.17.20.2: icmp_seq=3 ttl=62 time=1.09 ms
^C
--- 172.17.20.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 1.041/1.088/1.128/0.035 ms


七、Master节点部署

  • 在master节点上,生成apiserver组件证书
##创建证书存放目录
[root@k8s_master ~]# mkdir /root/k8s-cert
[root@k8s_master ~]# cd  /root/k8s-cert

##创建证书生成脚本
[root@k8s_master k8s-cert]# vi k8s-cert.sh 
###添加以下内容
cat > ca-config.json <<EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
EOF

cat > ca-csr.json <<EOF
{
    "CN": "kubernetes",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Beijing",
            "ST": "Beijing",
            "O": "k8s",
            "OU": "System"
        }
    ]
}
EOF

cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

#-----------------------

cat > server-csr.json <<EOF
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",
      "127.0.0.1",
      "192.168.5.10",
      "192.168.5.11",
      "192.168.5.100",
      "192.168.5.12",
      "192.168.5.13",
      "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

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server

#-----------------------

cat > admin-csr.json <<EOF
{
  "CN": "admin",
  "hosts": [],
  "key": {
    "algo": "rsa",
   "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing",
      "O": "system:masters",
      "OU": "System"
    }
  ]
}
EOF

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin

#-----------------------

cat > kube-proxy-csr.json <<EOF
{
 "CN": "system:kube-proxy",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}
EOF

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy

###执行脚本
[root@k8s_master k8s-cert]# bash k8s-cert.sh 
##此时会生成8个证书
[root@k8s_master k8s-cert]# ls *.pem
admin-key.pem  admin.pem  ca-key.pem  ca.pem  kube-proxy-key.pem  kube-proxy.pem  server-key.pem  server.pem
  • 创建k8s工作目录 ,便于管理
[root@k8s_master k8s-cert]# mkdir -p /opt/kubernetes/{bin,ssl,cfg}

##复制证书到存放点
[root@k8s_master k8s-cert]# cp -p *.pem /opt/kubernetes/ssl/
[root@k8s_master k8s-cert]# ls /opt/kubernetes/ssl/
admin-key.pem  admin.pem  ca-key.pem  ca.pem  kube-proxy-key.pem  kube-proxy.pem  server-key.pem  server.pem
  • 下载并解压kubernetes-server二进制源码包,
    https://dl.k8s.io/v1.16.6/kubernetes-server-linux-amd64.tar.gz(自行解决问题)
[root@k8s_master bin]# tar zxvf kubernetes-server-linux-amd64.tar.gz 
[root@k8s_master ~]# cd kubernetes/server/bin/

##复制关键命令道存放点
[root@k8s_master bin]# cp kube-apiserver kubectl kube-controller-manager kube-scheduler kube-proxy kubelet /opt/kubernetes/bin/

  • 创建token文件
[root@k8s_master cfg]# cd /opt/kubernetes/cfg
##生成序列号
[root@k8s_master cfg]# export BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')
[root@k8s_master cfg]# echo $BOOTSTRAP_TOKEN 
c345888c7d4ffb7eacc5fdd56bef72c1

##创建token文件
[root@k8s_master cfg]# cat > token.csv << EOF
> ${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
> EOF
  • 创建apiserver启动脚本
[root@k8s_master cfg]# vi apiserver.sh 
##添加以下内容
#!/bin/bash

MASTER_ADDRESS=$1
ETCD_SERVERS=$2

cat <<EOF >/opt/kubernetes/cfg/kube-apiserver

KUBE_APISERVER_OPTS="--logtostderr=true \\
--v=4 \\
--etcd-servers=${ETCD_SERVERS} \\
--bind-address=${MASTER_ADDRESS} \\
--secure-port=6443 \\
--advertise-address=${MASTER_ADDRESS} \\
--allow-privileged=true \\
--service-cluster-ip-range=10.0.0.0/24 \\
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\
--authorization-mode=RBAC,Node \\
--kubelet-https=true \\
--enable-bootstrap-token-auth \\
--token-auth-file=/opt/kubernetes/cfg/token.csv \\
--service-node-port-range=30000-50000 \\
--tls-cert-file=/opt/kubernetes/ssl/server.pem  \\
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \\
--client-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--etcd-cafile=/opt/etcd/ssl/ca.pem \\
--etcd-certfile=/opt/etcd/ssl/server.pem \\
--etcd-keyfile=/opt/etcd/ssl/server-key.pem"

EOF

cat <<EOF >/usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-apiserver
ExecStart=/opt/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable kube-apiserver
systemctl restart kube-apiserver
  • 开启apiserver服务
[root@k8s_master cfg]# bash apiserver.sh 192.168.5.10  https://192.168.5.10:2379,https://192.168.5.20:2379,https://192.168.5.30:2379
##检查服务进程是否开启
[root@k8s_master cfg]# ps aux | grep kube-apiserver
root      52113 19.7  8.3 397232 313124 ?       Ssl  23:30   0:10 /opt/kubernetes/bin/kube-apiserver --logtostderr=true --v=4 --etcd-servers=https://192.168.5.10:2379,https://192.168.5.20:2379,https://192.168.5.30:2379
......省略
  • 创建Scheruler服务启动脚本
[root@k8s_master cfg]# vi scheduler.sh 
##添加以下内容
#!/bin/bash

MASTER_ADDRESS=$1

cat <<EOF >/opt/kubernetes/cfg/kube-scheduler

KUBE_SCHEDULER_OPTS="--logtostderr=true \\
--v=4 \\
--master=${MASTER_ADDRESS}:8080 \\
--leader-elect"

EOF

cat <<EOF >/usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-scheduler
ExecStart=/opt/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable kube-scheduler
systemctl restart kube-scheduler

  • 启动Scheruler服务
[root@k8s_master cfg]# bash scheduler.sh 127.0.0.1
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-scheduler.service to /usr/lib/systemd/system/kube-scheduler.service.
  • 创建kube-controller-manager启动脚本
[root@k8s_master cfg]# vi controller-manager.sh 
##添加以下内容
#!/bin/bash

MASTER_ADDRESS=$1

cat <<EOF >/opt/kubernetes/cfg/kube-controller-manager


KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \\
--v=4 \\
--master=${MASTER_ADDRESS}:8080 \\
--leader-elect=true \\
--address=127.0.0.1 \\
--service-cluster-ip-range=10.0.0.0/24 \\
--cluster-name=kubernetes \\
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \\
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem  \\
--root-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--experimental-cluster-signing-duration=87600h0m0s"

EOF

cat <<EOF >/usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-controller-manager
ExecStart=/opt/kubernetes/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable kube-controller-manager
systemctl restart kube-controller-manager
  • 启动kube-controller-manager服务
[root@k8s_master cfg]# bash controller-manager.sh 127.0.0.1
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-controller-manager.service to /usr/lib/systemd/system/kube-controller-manager.service.
  • 查看master节点状态
[root@k8s_master cfg]# /opt/kubernetes/bin/kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-1               Healthy   {"health":"true"}   
etcd-0               Healthy   {"health":"true"}   
etcd-2               Healthy   {"health":"true"}  

##将命令写入系统变量,方便使用
[root@k8s_master cfg]# echo 'PATH=/opt/kubernetes/bin/:$PATH' >> /etc/profile
[root@k8s_master cfg]# source /etc/profile

八、node节点部署

  • 在master节点上操作
  • 发送kubelet、kube-proxy命令到node节点上
[root@k8s_master bin]# cd /opt/kubernetes/bin/
[root@k8s_master bin]# scp kubelet kube-proxy root@192.168.5.20:/opt/kubernetes/bin/
[root@k8s_master bin]# scp kubelet kube-proxy root@192.168.5.30:/opt/kubernetes/bin/
  • 配置kubeconfig文件,用于master和node互联
[root@k8s_master ~]# mkdir /root/kubeconfig
[root@k8s_master ~]# vi /root/kubeconfig/kubeconfig.sh
###添加以下内容
APISERVER=$1
SSL_DIR=$2

# 创建kubelet bootstrapping kubeconfig
export KUBE_APISERVER="https://$APISERVER:6443"

# 设置集群参数
kubectl config set-cluster kubernetes \
  --certificate-authority=$SSL_DIR/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=bootstrap.kubeconfig

# 设置客户端认证参数
kubectl config set-credentials kubelet-bootstrap \
  --token=c345888c7d4ffb7eacc5fdd56bef72c1 \
  --kubeconfig=bootstrap.kubeconfig

# 设置上下文参数
kubectl config set-context default \
  --cluster=kubernetes \
  --user=kubelet-bootstrap \
  --kubeconfig=bootstrap.kubeconfig
# 设置默认上下文
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig

#----------------------

# 创建kube-proxy kubeconfig文件

kubectl config set-cluster kubernetes \
  --certificate-authority=$SSL_DIR/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=kube-proxy.kubeconfig

kubectl config set-credentials kube-proxy \
  --client-certificate=$SSL_DIR/kube-proxy.pem \
  --client-key=$SSL_DIR/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
  • 执行kubeconfig文件,生成配置文件
[root@k8s_master kubeconfig]# bash kubeconfig 192.168.5.10 /root/k8s-cert
Cluster "kubernetes" set.
User "kubelet-bootstrap" set.
Context "default" created.
Switched to context "default".
Cluster "kubernetes" set.
User "kube-proxy" set.
Context "default" created.
Switched to context "default".
[root@k8s_master kubeconfig]# ls
bootstrap.kubeconfig  kubeconfig  kube-proxy.kubeconfig
  • 拷贝配置文件到node1节点
[root@k8s_master kubeconfig]# scp kube-proxy.kubeconfig bootstrap.kubeconfig root@192.168.5.20:/opt/kubernetes/cfg/
  • 创建bootstrap角色赋予权限用于连接apiserver请求签名
[root@k8s_master kubeconfig]# kubectl create clusterrolebinding kubelet-bootstrap \
> --clusterrole=system:node-bootstrapper \
> --user=kubelet-bootstrap

node1节点配置

  • 编写kubelet启动脚本
[root@k8s_node1 cfg]# cd /opt/kubernetes/cfg/ 
[root@k8s_node1 cfg]# vi kubelet.sh 
##添加以下内容
#!/bin/bash

NODE_ADDRESS=$1
DNS_SERVER_IP=${2:-"10.0.0.2"}

cat <<EOF >/opt/kubernetes/cfg/kubelet

KUBELET_OPTS="--logtostderr=true \\
--v=4 \\
--hostname-override=${NODE_ADDRESS} \\
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \\
--config=/opt/kubernetes/cfg/kubelet.config \\
--cert-dir=/opt/kubernetes/ssl \\
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"

EOF

cat <<EOF >/opt/kubernetes/cfg/kubelet.config

kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: ${NODE_ADDRESS}
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- ${DNS_SERVER_IP}
clusterDomain: cluster.local.
failSwapOn: false
authentication:
  anonymous:
    enabled: true
EOF

cat <<EOF >/usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service

[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet
ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
KillMode=process

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable kubelet
systemctl restart kubelet
  • 启动kubelet服务
[root@k8s_node1 cfg]# bash kubelet.sh 192.168.5.20
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@k8s_node1 cfg]# netstat -natp | grep kubelet
tcp        0      0 192.168.5.20:60028      192.168.5.10:6443       ESTABLISHED 61552/kubelet
  • 返回master节点,给node的kubelet颁发证书
##查看请求
[root@k8s_master kubeconfig]# kubectl get csr
NAME                                                   AGE     REQUESTOR           CONDITION
node-csr-fW-g9h9PRMbM3HfS_1CpUsNktiK0zNxRBI-qcmB5sLk   4m12s   kubelet-bootstrap   Pending

###同意请求,颁发证书
[root@k8s_master kubeconfig]# kubectl certificate approve node-csr-fW-g9h9PRMbM3HfS_1CpUsNktiK0zNxRBI-qcmB5sLk
##再次查看请求,发现处于Approved状态
[root@k8s_master kubeconfig]# kubectl get csr
NAME                                                   AGE     REQUESTOR           CONDITION
node-csr-fW-g9h9PRMbM3HfS_1CpUsNktiK0zNxRBI-qcmB5sLk   8m39s   kubelet-bootstrap   Approved,Issued

##此时node1节点已经加入到群集中
[root@k8s_master kubeconfig]# kubectl get nodes
NAME           STATUS   ROLES    AGE    VERSION
192.168.5.20   Ready    <none>   119s   v1.12.3
  • 进入node1节点创建proxy代理脚本,并启动
[root@k8s_node1 cfg]# vi /opt/kubernetes/cfg/proxy.sh
##添加以下内容
#!/bin/bash

NODE_ADDRESS=$1

cat <<EOF >/opt/kubernetes/cfg/kube-proxy

KUBE_PROXY_OPTS="--logtostderr=true \\
--v=4 \\
--hostname-override=${NODE_ADDRESS} \\
--cluster-cidr=10.0.0.0/24 \\
--proxy-mode=ipvs \\
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"

EOF

cat <<EOF >/usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-proxy
ExecStart=/opt/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable kube-proxy
systemctl restart kube-proxy

####启动服务
[root@k8s_node1 cfg]# bash proxy.sh  192.168.5.20
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
##检查是否启动
[root@k8s_node1 cfg]# netstat -natp | grep proxy
tcp        0      0 127.0.0.1:10249         0.0.0.0:*               LISTEN      64440/kube-proxy    
tcp        0      0 192.168.5.20:60888      192.168.5.10:6443       ESTABLISHED 64440/kube-proxy    
tcp6       0      0 :::10256                :::*                    LISTEN      64440/kube-proxy

node2节点配置

  • 在node1几点上复制配置文件到node2
[root@k8s_node1 cfg]# scp -r /opt/kubernetes/ root@192.168.5.30:/opt

##复制启动文件到node2
[root@k8s_node1 cfg]# cd /usr/lib/systemd/system

[root@k8s_node1 system]# scp kube* root@192.168.5.30:/usr/lib/systemd/system/

  • 在node2节点上,修改配置文件
##删除复制过来的证书,node2会自行申请
[root@k8s_node1 system]# rm -rf /opt/kubernetes/ssl/*

##修改kubelet
[root@k8s_node1sy stem]# vi /opt/kubernetes/cfg/kubelet
--hostname-override=192.168.5.30 \     ##修改ip为node2地址,192.168.5.30

##修改kubelet.config 文件
[root@k8s_node1 kubernetes]# vi /opt/kubernetes/cfg/kubelet.config 
address: 192.168.5.30     ##修改ip为node2地址,192.168.5.30

##修改kube-proxy
[root@k8s_node2 system]#  vi /opt/kubernetes/cfg/kube-proxy
--hostname-override=192.168.5.30 \     ##修改ip为node2地址,192.168.5.30
  • 开启node2的kubelet和proxy服务
[root@k8s_node2 system]# systemctl start kubelet
[root@k8s_node2 system]# systemctl enable  kubelet
[root@k8s_node2 system]# systemctl start   kube-proxy.service 
[root@k8s_node2 system]# systemctl enable  kube-proxy.service

九、验证群集信息

  • 进入master节点,查看收到的请求,并给kubelet颁发证书
[root@k8s_master kubeconfig]# kubectl get csr
NAME                                                   AGE     REQUESTOR           CONDITION
node-csr-Tm1PmuZZFV0wyMrs3RV3SCpr080CRK9d9LnRHGg2jGE   4m12s   kubelet-bootstrap   Pending
node-csr-fW-g9h9PRMbM3HfS_1CpUsNktiK0zNxRBI-qcmB5sLk   44m     kubelet-bootstrap   Approved,Issued

##同意请求
[root@k8s_master kubeconfig]# kubectl certificate approve node-csr-Tm1PmuZZFV0wyMrs3RV3SCpr080CRK9d9LnRHGg2jGE
  • 查看群集节点信息
[root@k8s_master kubeconfig]# kubectl get nodes
NAME           STATUS   ROLES    AGE   VERSION
192.168.5.20   Ready    <none>   39m   v1.12.3
192.168.5.30   Ready    <none>   63s   v1.12.3

你可能感兴趣的:(Docker/K8S)