一、单节点结构图

在这里插入图片描述

前文环境中最后部署了flannel网络组件,并实现了容器间的通信,本次实验,再之前基础上继续部署,实现k8s单节点架构,首先需要部署Master组件,也是核心组件:

1、kube-apiserver

提供了资源操作的唯一入口,并提供认证授权,访问控制,API注册和发现等机制

2、kube-controller-manager

负责维护集群的状态,比如故障检测,自动扩展,滚动更新等

3、kub-scheduler

负责资源的调度,按照预定的调度策略将Pod调度到相应的机器上(Model)

master节点apiserver 启动流程
在这里插入图片描述
kubelet :基础的命令
1、我们在使用此命令的时,例如 kubelet get nodes 查看节点信息时首先会经过master 节点查看各个node点的业务信息,过程中需要bootstrap的授权(bootstrap.kubeconfig权限配置)
2、当有了以上的权限配置且通过以上权限许可之后才会去找apiserver进行操作
3、apiserver 首先会验证node节点中的令牌(token)
如果验证成功,则令牌会释放出其中的证书,将证书再次进行身份验证(CA验证),身份验证就需要通过csr的签名,签名成功之后,再给与对应的证书颁发,颁发许可之后才会启动apiserver、授权给与请求命令相对于的服务。(最终授权给bootstrap)
如果在以上的验证中有一部失败,apiserver则不会进行启动
4、Api会最终授权给bootstrap,而命令请求过来之后,必须要有bootstarp的授权才能进行,否则无法输出结果
我们在本次实例中,就需要生成apiserver的token、证书和签名,最后要将证书制作出来

二、部署master组件

	在master上操作,api-server生成证书

[root@localhost k8s]# unzip master.zip			#将master.zip拖入到k8s目录下
[root@localhost k8s]# mkdir /opt/kubernetes/{cfg,bin,ssl} -p
[root@localhost k8s]# mkdir k8s-cert
[root@localhost k8s]# cd k8s-cert/
[root@localhost k8s-cert]# ls
k8s-cert.sh

vim 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.111.10",  //master1
      "192.168.111.20",  //master2
      "192.168.111.100",  //vip
      "192.168.111.30",  //lb (master)
      "192.168.111.40",  //lb (backup)
      "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
	生成k8s证书
[root@localhost k8s-cert]# bash k8s-cert.sh 

[root@localhost k8s-cert]# ls *pem
admin-key.pem  ca-key.pem  kube-proxy-key.pem  server-key.pem
admin.pem      ca.pem      kube-proxy.pem      server.pem

[root@localhost k8s-cert]# cp ca*pem server*pem /opt/kubernetes/ssl/
[root@localhost k8s-cert]# cd ..


	解压kubernetes压缩包
[root@localhost k8s]# tar zxvf kubernetes-server-linux-amd64.tar.gz
[root@localhost k8s]# cd /root/k8s/kubernetes/server/bin
	复制关键命令文件
[root@localhost bin]# cp kube-apiserver kubectl kube-controller-manager kube-scheduler /opt/kubernetes/bin/
[root@localhost k8s]# cd /root/k8s
[root@localhost k8s]# head -c 16 /dev/urandom | od -An -t x | tr -d ' ' 		#可以随机生成序列号
9823008dffdcd6d22dc660d767aca94e
[root@localhost k8s]# vim /opt/kubernetes/cfg/token.csv
9823008dffdcd6d22dc660d767aca94e,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
	二进制文件,token,证书都准备好,开启apiserver
[root@localhost k8s]# vim 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



[root@localhost k8s]# bash apiserver.sh 192.168.111.10 https://192.168.111.10:2379,https://192.168.111.30:2379,https://192.168.111.40:2379
[root@localhost k8s]# ps aux | grep kube
	监听的https端口
[root@localhost k8s]# netstat -ntap | grep 6443
[root@localhost k8s]# netstat -ntap | grep 8080

	启动scheduler服务
[root@localhost k8s]#vim 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

[root@localhost k8s]# ./scheduler.sh 127.0.0.1
[root@localhost k8s]# ps aux | grep kube
[root@localhost k8s]# chmod +x controller-manager.sh 
[root@localhost k8s]# vim 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

[root@localhost k8s]# ./controller-manager.sh 127.0.0.1
[root@localhost k8s]# /opt/kubernetes/bin/kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-2               Healthy   {"health":"true"}   
etcd-1               Healthy   {"health":"true"}   
etcd-0               Healthy   {"health":"true"}  

node1节点

	master上操作
把 kubelet、kube-proxy拷贝到node节点上去
/root/k8s/kubernetes/server/bin
[root@localhost bin]# scp kubelet kube-proxy root@192.168.111.30:/opt/kubernetes/bin/
[root@localhost bin]# scp kubelet kube-proxy root@192.168.111.40:/opt/kubernetes/bin/



	node01节点操作(复制node.zip到/root目录下再解压)
解压node.zip,获得kubelet.sh  proxy.sh 
[root@localhost ~]# unzip node.zip 


	在master上操作
[root@localhost k8s]# mkdir kubeconfig
[root@localhost k8s]# cd kubeconfig/
	拷贝kubeconfig.sh文件进行重命名
[root@localhost kubeconfig]# mv kubeconfig.sh kubeconfig
[root@localhost kubeconfig]# vim kubeconfig 
删除以下部分
----------------删除以下部分----------------------------------------------------------------------
# 创建 TLS Bootstrapping Token
#BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')
BOOTSTRAP_TOKEN=0fb61c46f8991b718eb38d27b605b008

cat > token.csv <<EOF
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF

	
	获取token信息(红色部分)
[root@localhost ~]# cat /opt/kubernetes/cfg/token.csv 
9823008dffdcd6d22dc660d767aca94e,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
	配置文件修改为tokenID
# 设置客户端认证参数
kubectl config set-credentials kubelet-bootstrap \
  --token=9823008dffdcd6d22dc660d767aca94e \  #修改
  --kubeconfig=bootstrap.kubeconfig

设置环境变量(可以写入到/etc/profile中)
[root@localhost kubeconfig]# vim /etc/profile
export PATH=$PATH:/opt/kubernetes/bin/
[root@localhost kubeconfig]# source /etc/profile
[root@localhost kubeconfig]# kubectl get cs

生成配置文件
[root@localhost kubeconfig]# bash kubeconfig 192.168.111.10 /root/k8s/k8s-cert/
[root@localhost kubeconfig]# ls
bootstrap.kubeconfig  kubeconfig  kube-proxy.kubeconfig

拷贝配置文件到node节点
[root@localhost kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.111.30:/opt/kubernetes/cfg/
[root@localhost kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.111.40:/opt/kubernetes/cfg/

创建bootstrap角色赋予权限用于连接apiserver请求签名(关键)
[root@localhost kubeconfig]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap

	

	在node01节点上操作
[root@localhost ~]# bash kubelet.sh 192.168.111.30
[root@localhost ~]# ps aux | grep kube

	

	master上操作
	检查到node01节点的请求
[root@localhost kubeconfig]# kubectl get csr
NAME                                                   AGE     REQUESTOR           CONDITION
node-csr-8uXeegTylDg2JkswkjgoQUjhrjmHgt-d6uofuidOu6k   35s   kubelet-bootstrap     Pending  (等待集群给该节点颁发证书)

[root@localhost kubeconfig]# kubectl certificate approve node-csr-8uXeegTylDg2JkswkjgoQUjhrjmHgt-d6uofuidOu6k

继续查看证书状态
[root@localhost kubeconfig]# kubectl get csr
NAME                                                   AGE     REQUESTOR           CONDITION
node-csr-8uXeegTylDg2JkswkjgoQUjhrjmHgt-d6uofuidOu6k   3m18s   kubelet-bootstrap   Approved,Issued(已经被允许加入群集)

查看群集节点,成功加入node01节点
[root@localhost kubeconfig]# kubectl get node
NAME              STATUS   ROLES    AGE    VERSION
192.168.111.30   Ready    <none>   44s   v1.12.3



	在node01节点操作,启动proxy服务
[root@localhost ~]# bash proxy.sh 192.168.111.30

node02节点

	在node01节点操作
把现成的/opt/kubernetes目录复制到其他节点进行修改即可
[root@localhost ~]# scp -r /opt/kubernetes/ root@192.168.111.40:/opt/

把kubelet,kube-proxy的service文件拷贝到node2中
[root@localhost ~]# scp /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.111.40:/usr/lib/systemd/system/

	在node02上操作,进行修改
首先删除复制过来的证书,等会node02会自行申请证书
[root@localhost ~]# cd /opt/kubernetes/ssl/
[root@localhost ssl]# rm -rf *
修改配置文件kubelet  kubelet.config kube-proxy(三个配置文件)
[root@localhost ssl]# cd ../cfg/
[root@localhost cfg]# vim kubelet

KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.111.40 \  #修改IP
--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"

[root@localhost cfg]# vim kubelet.config 
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.111.40  #修改IP
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- 10.0.0.2
clusterDomain: cluster.local.
failSwapOn: false
authentication:
  anonymous:
    enabled: true
~                                  
[root@localhost cfg]# vim kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.111.40 \ #修改IP
--cluster-cidr=10.0.0.0/24 \
--proxy-mode=ipvs \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"


启动服务
[root@localhost cfg]# systemctl start kubelet.service 
[root@localhost cfg]# systemctl enable kubelet.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@localhost cfg]# systemctl start kube-proxy.service 
[root@localhost cfg]# systemctl enable kube-proxy.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.


	在master上操作查看请求
[root@localhost k8s]# kubectl get csr
NAME                                                   AGE   REQUESTOR           CONDITION
node-csr-8uXeegTylDg2JkswkjgoQUjhrjmHgt-d6uofuidOu6k   11m   kubelet-bootstrap   Approved,Issued
node-csr-u9JntWm0RetQiCIBcJgHXOlRc2sBTOcTcgqREeElh3g   25s   kubelet-bootstrap   Pending


	授权许可加入群集
[root@localhost k8s]# kubectl certificate approve node-csr-u9JntWm0RetQiCIBcJgHXOlRc2sBTOcTcgqREeElh3g
certificatesigningrequest.certificates.k8s.io/node-csr-u9JntWm0RetQiCIBcJgHXOlRc2sBTOcTcgqREeElh3g approved

	查看群集中的节点
[root@localhost kubeconfig]#  kubectl get node
NAME             STATUS   ROLES    AGE   VERSION
192.168.111.30   Ready    <none>   10m   v1.12.3
192.168.111.40   Ready    <none>   17s   v1.12.3

完事了

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐