K8s集群部署(二进制安装部署详细手册)

这篇具有很好参考价值的文章主要介绍了K8s集群部署(二进制安装部署详细手册)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  一、简介

K8s部署主要有两种方式:
  • 1、Kubeadm

  Kubeadm是一个K8s部署工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。

  • 2、二进制

   从github下载发行版的二进制包,手动部署每个组件,组成Kubernetes集群。

 本文通过二进制安装部署的方式在centos7上搭建kubernetes集群。

二、环境准备

1、初始化配置

#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
#关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config 
#关闭swap
sed -ri 's/.*swap.*/#&/' /etc/fstab

#设置主机名
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node
#添加hosts

cat > /etc/hosts << EOF
192.168.44.137 k8s-master
192.168.44.140 k8s-node
EOF

#将桥接的IPV4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system   #生效
#时间同步
yum install ntpdate -y
ntpdate time.windows.com

2、cfssl证书生成工具准备

(1)cfssl证书生成工具准备

#创建目录存放cfssl工具
mkdir /software-cfssl

#下载相关工具
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -P /software-cfssl/
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -P /software-cfssl/
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -P /software-cfssl/

cd /software-cfssl/
chmod +x *
cp cfssl_linux-amd64 /usr/local/bin/cfssl
cp cfssljson_linux-amd64 /usr/local/bin/cfssljson
cp cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
自签证书颁发机构(CA)
#创建工作目录
mkdir -p ~/TLS/{etcd,k8s}
cd ~/TLS/etcd/

#生成自签CA配置
cat > ca-config.json << EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "www": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
EOF

cat > ca-csr.json << EOF
{
    "CN": "etcd CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Aaron",
            "ST": "Aaron"
        }
    ]
}
EOF


生成自签CA证书
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

说明:
当前目录下会生成 ca.pem和ca-key.pem文件

k8s 二进制 集群安装,kubernetes,linux,运维,docker,容器

#使用自签CA签发etcd https证书
cat > server-csr.json << EOF
{
    "CN": "etcd",
    "hosts": [
    "192.168.44.137",
    "192.168.44.140"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Aaron",
            "ST": "Aaron"
        }
    ]
}
EOF

#生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server

说明:
当前目录下会生成 server.pem 和 server-key.pem

k8s 二进制 集群安装,kubernetes,linux,运维,docker,容器

3、安装docker

wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce
systemctl enable docker && systemctl start docker

#配置镜像加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://kd88kykb.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

 4、安装部署etcd

#下载etcd二进制文件
wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz

#创建工作目录并解压二进制包
mkdir /opt/etcd/{bin,cfg,ssl} -p
tar -xf etcd-v3.4.9-linux-amd64.tar.gz
mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/

#将证书复制过来
cp ~/TLS/etcd/*.pem /opt/etcd/ssl/

 创建etcd配置文件

cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.44.137:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.44.137:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.44.137:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.44.137:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.44.137:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF

systemd管理etcd

cat > /usr/lib/systemd/system/etcd.service << EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem \
--logger=zap
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

启动etcd并设置开机自启

systemctl daemon-reload
systemctl start etcd
systemctl enable etcd

检查etcd集群状态

[root@k8s-master etcd]# ETCDCTL_API=3 /opt/etcd/bin/etcdctl --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --endpoints="https://192.168.44.137:2379" endpoint health --write-out=table 
+-----------------------------+--------+------------+-------+
|          ENDPOINT           | HEALTH |    TOOK    | ERROR |
+-----------------------------+--------+------------+-------+
| https://192.168.44.137:2379 |   true | 5.721724ms |       |
+-----------------------------+--------+------------+-------+

5、部署Master节点

(1)下载解压

#下载 
wget https://dl.k8s.io/v1.20.0/kubernetes-server-linux-amd64.tar.gz
#解压二进制包
mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs} 
tar zxvf kubernetes-server-linux-amd64.tar.gz
cd kubernetes/server/bin
cp kube-apiserver kube-scheduler kube-controller-manager kubelet  kube-proxy  /opt/kubernetes/bin
cp kubectl /usr/bin/

(2)部署kube-apiserver

生成kube-apiserver证书

#自签证书颁发机构(CA)

cd ~/TLS/k8s

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 -

使用自签CA签发kube-apiserver https证书

cat > server-csr.json << EOF
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",
      "127.0.0.1",
      "192.168.44.137",
      "192.168.44.140",
      "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

cp *.pem /opt/kubernetes/ssl/

(3)创建kube-apiserver配置文件

cat > /opt/kubernetes/cfg/kube-apiserver.conf << EOF
KUBE_APISERVER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--etcd-servers=https://192.168.44.137:2379 \\
--bind-address=192.168.44.137 \\
--secure-port=6443 \\
--advertise-address=192.168.44.137 \\
--allow-privileged=true \\
--service-cluster-ip-range=10.0.0.0/24 \\
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\
--authorization-mode=RBAC,Node \\
--enable-bootstrap-token-auth=true \\
--token-auth-file=/opt/kubernetes/cfg/token.csv \\
--service-node-port-range=30000-32767 \\
--kubelet-client-certificate=/opt/kubernetes/ssl/server.pem \\
--kubelet-client-key=/opt/kubernetes/ssl/server-key.pem \\
--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 \\
--service-account-issuer=api \\
--service-account-signing-key-file=/opt/kubernetes/ssl/server-key.pem \\
--etcd-cafile=/opt/etcd/ssl/ca.pem \\
--etcd-certfile=/opt/etcd/ssl/server.pem \\
--etcd-keyfile=/opt/etcd/ssl/server-key.pem \\
--requestheader-client-ca-file=/opt/kubernetes/ssl/ca.pem \\
--proxy-client-cert-file=/opt/kubernetes/ssl/server.pem \\
--proxy-client-key-file=/opt/kubernetes/ssl/server-key.pem \\
--requestheader-allowed-names=kubernetes \\
--requestheader-extra-headers-prefix=X-Remote-Extra- \\
--requestheader-group-headers=X-Remote-Group \\
--requestheader-username-headers=X-Remote-User \\
--enable-aggregator-routing=true \\
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/opt/kubernetes/logs/k8s-audit.log"
EOF

创建上述配置文件中token文件:

cat > /opt/kubernetes/cfg/token.csv << EOF
12ec3ac86c8778b840bf53a13c63be82,kubelet-bootstrap,10001,"system:node-bootstrapper"
EOF  

systemd管理apiserver

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

[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-apiserver.conf
ExecStart=/opt/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

启动并设置开机启动

systemctl daemon-reload
systemctl start kube-apiserver 
systemctl enable kube-apiserver

(4)部署kube-controller-manager

创建配置文件

cat > /opt/kubernetes/cfg/kube-controller-manager.conf << EOF
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--leader-elect=true \\
--kubeconfig=/opt/kubernetes/cfg/kube-controller-manager.kubeconfig \\
--bind-address=127.0.0.1 \\
--allocate-node-cidrs=true \\
--cluster-cidr=10.244.0.0/16 \\
--service-cluster-ip-range=10.0.0.0/24 \\
--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 \\
--cluster-signing-duration=87600h0m0s"
EOF

生成kube-controller-manager证书

# 切换工作目录
cd ~/TLS/k8s

# 创建证书请求文件
cat > kube-controller-manager-csr.json << EOF
{
  "CN": "system:kube-controller-manager",
  "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 kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager

生成kubeconfig文件

KUBE_CONFIG="/opt/kubernetes/cfg/kube-controller-manager.kubeconfig"
KUBE_APISERVER="https://192.168.44.137:6443"

kubectl config set-cluster kubernetes \
--certificate-authority=/opt/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-credentials kube-controller-manager \
--client-certificate=./kube-controller-manager.pem \
--client-key=./kube-controller-manager-key.pem \
--embed-certs=true \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-context default \
--cluster=kubernetes \
--user=kube-controller-manager \
--kubeconfig=${KUBE_CONFIG}
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

systemd管理controller-manager

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

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

[Install]
WantedBy=multi-user.target
EOF

启动并设置开机自启

systemctl daemon-reload
systemctl start kube-controller-manager
systemctl enable kube-controller-manager

(5)部署 kube-scheduler

创建配置文件

cat > /opt/kubernetes/cfg/kube-scheduler.conf << EOF
KUBE_SCHEDULER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--leader-elect \\
--kubeconfig=/opt/kubernetes/cfg/kube-scheduler.kubeconfig \\
--bind-address=127.0.0.1"
EOF

生成kube-scheduler证书

# 切换工作目录
cd ~/TLS/k8s

# 创建证书请求文件
cat > kube-scheduler-csr.json << EOF
{
  "CN": "system:kube-scheduler",
  "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 kube-scheduler-csr.json | cfssljson -bare kube-scheduler

生成kubeconfig文件 

KUBE_CONFIG="/opt/kubernetes/cfg/kube-scheduler.kubeconfig"
KUBE_APISERVER="https://192.168.44.137:6443"

kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-credentials kube-scheduler \
  --client-certificate=./kube-scheduler.pem \
  --client-key=./kube-scheduler-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-scheduler \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

systemd管理scheduler

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

[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-scheduler.conf
ExecStart=/opt/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

启动并设置开机启动

systemctl daemon-reload
systemctl start kube-scheduler
systemctl enable kube-scheduler

(6)查看集群状态

生成kubectl连接集群的证书

# 切换工作目录
cd ~/TLS/k8s

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

生成kubeconfig文件 :

mkdir /root/.kube

KUBE_CONFIG="/root/.kube/config"
KUBE_APISERVER="https://192.168.44.137:6443"

kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-credentials cluster-admin \
  --client-certificate=./admin.pem \
  --client-key=./admin-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-context default \
  --cluster=kubernetes \
  --user=cluster-admin \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

通过kubectl工具查看当前集群组件状态 :

k8s 二进制 集群安装,kubernetes,linux,运维,docker,容器

 如上说明Master节点组件运行正常。

授权kubelet-bootstrap用户允许请求证书

kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap

6、部署Node节点

在k8s-master节点操作,即同时作为Worker Node。

(1)部署kubelet

创建配置文件

cat > /opt/kubernetes/cfg/kubelet.conf << EOF
KUBELET_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--hostname-override=k8s-master \\
--network-plugin=cni \\
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \\
--config=/opt/kubernetes/cfg/kubelet-config.yml \\
--cert-dir=/opt/kubernetes/ssl \\
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
EOF

配置文件

cat > /opt/kubernetes/cfg/kubelet-config.yml << EOF
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 0.0.0.0
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- 10.0.0.2
clusterDomain: cluster.local 
failSwapOn: false
authentication:
  anonymous:
    enabled: false
  webhook:
    cacheTTL: 2m0s
    enabled: true
  x509:
    clientCAFile: /opt/kubernetes/ssl/ca.pem 
authorization:
  mode: Webhook
  webhook:
    cacheAuthorizedTTL: 5m0s
    cacheUnauthorizedTTL: 30s
evictionHard:
  imagefs.available: 15%
  memory.available: 100Mi
  nodefs.available: 10%
  nodefs.inodesFree: 5%
maxOpenFiles: 1000000
maxPods: 110
EOF

 生成kubelet初次加入集群引导kubeconfig文件

KUBE_CONFIG="/opt/kubernetes/cfg/bootstrap.kubeconfig"
KUBE_APISERVER="https://192.168.44.137:6443"
TOKEN="12ec3ac86c8778b840bf53a13c63be82"


kubectl config set-cluster kubernetes \
--certificate-authority=/opt/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-credentials "kubelet-bootstrap" \
--token=${TOKEN} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-context default \
--cluster=kubernetes \
--user="kubelet-bootstrap" \
--kubeconfig=${KUBE_CONFIG}
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

systemd管理kubelet

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

[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet.conf
ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

启动并设置开机启动

systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet

允许kubelet证书申请并加入集群

#查看kubelet证书请求
[root@k8s-master .kube]# kubectl get csr
NAME                                                   AGE   SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-JCnTI0sEOBdcIBxtedaVs59BNvx-tYbfvzEiIwL9Z-c   54s   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Pending
#允许kubelet节点申请
[root@k8s-master .kube]# kubectl certificate approve node-csr-JCnTI0sEOBdcIBxtedaVs59BNvx-tYbfvzEiIwL9Z-c
certificatesigningrequest.certificates.k8s.io/node-csr-JCnTI0sEOBdcIBxtedaVs59BNvx-tYbfvzEiIwL9Z-c approved
#查看申请
[root@k8s-master .kube]# kubectl get csr
NAME                                                   AGE   SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-JCnTI0sEOBdcIBxtedaVs59BNvx-tYbfvzEiIwL9Z-c   92s   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Approved,Issued
#查看节点
[root@k8s-master .kube]# kubectl get nodes
NAME         STATUS     ROLES    AGE   VERSION
k8s-master   NotReady   <none>   20s   v1.20.0

(3)部署kube-proxy

 创建配置文件

cat > /opt/kubernetes/cfg/kube-proxy.conf << EOF
KUBE_PROXY_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--config=/opt/kubernetes/cfg/kube-proxy-config.yml"
EOF

配置参数文件

cat > /opt/kubernetes/cfg/kube-proxy-config.yml << EOF
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
metricsBindAddress: 0.0.0.0:10249
clientConnection:
  kubeconfig: /opt/kubernetes/cfg/kube-proxy.kubeconfig
hostnameOverride: k8s-master
clusterCIDR: 10.244.0.0/16
EOF

生成kube-proxy证书文件

# 切换工作目录
cd ~/TLS/k8s

# 创建证书请求文件
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

生成kube-proxy.kubeconfig文件

KUBE_CONFIG="/opt/kubernetes/cfg/kube-proxy.kubeconfig"
KUBE_APISERVER="https://192.168.44.137:6443"

kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/kubernetes/ssl/ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-credentials kube-proxy \
  --client-certificate=./kube-proxy.pem \
  --client-key=./kube-proxy-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-proxy \
  --kubeconfig=${KUBE_CONFIG}
  
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}

systemd管理kube-proxy

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

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

[Install]
WantedBy=multi-user.target
EOF

启动并设置开机自启

systemctl daemon-reload
systemctl start kube-proxy
systemctl enable kube-proxy

7、部署网络组件(Calico)

#下载k8s版本对应的calico  网址:https://projectcalico.docs.tigera.io/archive/v3.20/getting-started/kubernetes/requirements

 wget https://docs.projectcalico.org/archive/v3.20/manifests/calico.yaml --no-check-certificate

#通过CALICO_IPV4POOL_CIDR手动配置Pod子网范围
#   - name: CALICO_IPV4POOL_CIDR
#              value: "10.244.0.0/16"

kubectl apply -f calico.yaml
kubectl get pods -n kube-system
[root@k8s-master /]# kubectl get pods -n kube-system
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-577f77cb5c-x5kf6   1/1     Running   0          4m43s
calico-node-qkvbm                          1/1     Running   0          4m43s
[root@k8s-master /]# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    <none>   47m   v1.20.0

 授权apiserver访问kubelet

cat > apiserver-to-kubelet-rbac.yaml << EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
  name: system:kube-apiserver-to-kubelet
rules:
  - apiGroups:
      - ""
    resources:
      - nodes/proxy
      - nodes/stats
      - nodes/log
      - nodes/spec
      - nodes/metrics
      - pods/log
    verbs:
      - "*"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: system:kube-apiserver
  namespace: ""
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:kube-apiserver-to-kubelet
subjects:
  - apiGroup: rbac.authorization.k8s.io
    kind: User
    name: kubernetes
EOF

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

 8、新增Worker Node

#拷贝以部署好的相关文件到新节点

scp -r /opt/kubernetes root@192.168.44.140:/opt/

#删除kubelet证书和kubeconfig文件
rm -f /opt/kubernetes/cfg/kubelet.kubeconfig 
rm -f /opt/kubernetes/ssl/kubelet*

#修改主机名
vi /opt/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-node

vi /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-node

#启动并设置开机自启
systemctl daemon-reload
systemctl start kubelet kube-proxy
systemctl enable kubelet kube-proxy

在Master上同意新的Node kubelet证书申请

[root@k8s-master ~]# kubectl get csr
NAME                                                   AGE   SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-2MQh811Z5H8dtSNyjAIFWctYck3mGmaXEq6neY5PwTc   11s   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Pending
node-csr-rRvUZnnO9NK6PdS9Q_dn1-MBWTBgsOZar2dAuGxW3y0   19m   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Approved,Issued
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl certificate approve  node-csr-2MQh811Z5H8dtSNyjAIFWctYck3mGmaXEq6neY5PwTc
certificatesigningrequest.certificates.k8s.io/node-csr-2MQh811Z5H8dtSNyjAIFWctYck3mGmaXEq6neY5PwTc approved
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get csr
NAME                                                   AGE   SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-2MQh811Z5H8dtSNyjAIFWctYck3mGmaXEq6neY5PwTc   65s   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Approved,Issued
node-csr-rRvUZnnO9NK6PdS9Q_dn1-MBWTBgsOZar2dAuGxW3y0   20m   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Approved,Issued

查看Node状态

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    <none>   20m   v1.20.0
k8s-node     Ready    <none>   29s   v1.20.0

9、测试kubernetes集群

在集群中创建一个pod,验证是否正常运行:

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc

k8s 二进制 集群安装,kubernetes,linux,运维,docker,容器

10、部署Dashboard

wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
#增加 type: NodePort

kubectl apply -f recommended.yaml
kubectl get pod,svc -n kubernetes-dashboard

k8s 二进制 集群安装,kubernetes,linux,运维,docker,容器

 在键盘上敲击“thisisunsafe”,谷歌浏览器会自动刷新显示网页,进入页面。

 访问地址: https://NodeIP:30030
 创建service account并绑定默认cluster-admin管理员集群角色

kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

使用输出的token登陆Dashboard

k8s 二进制 集群安装,kubernetes,linux,运维,docker,容器

k8s 二进制 集群安装,kubernetes,linux,运维,docker,容器

问题1:修改calico.yaml中的docker.io源

官网calico.yaml 默认指定的源为docker.io地址,docker.io源地址为国外源,下载比较慢,安装calico插件时,有概率会出现安装错误。


wget https://docs.projectcalico.org/archive/v3.20/manifests/calico.yaml --no-check-certificate

# 查看需要的镜像文件
[root@k8s-master ~]# grep image calico.yaml 
          image: docker.io/calico/cni:v3.20.6
          image: docker.io/calico/cni:v3.20.6
          image: docker.io/calico/pod2daemon-flexvol:v3.20.6
          image: docker.io/calico/node:v3.20.6
          image: docker.io/calico/kube-controllers:v3.20.6


#将calico.yaml中的镜像地址指定为国内镜像,只需要将docker 配置国内镜像加速器,然后把"docker.io"删除掉就可以了

[root@k8s-master ~]# sed -i "s#docker.io/##g" calico.yaml
[root@k8s-master ~]#  grep image calico.yaml
          image: calico/cni:v3.20.6
          image: calico/cni:v3.20.6
          image: calico/pod2daemon-flexvol:v3.20.6
          image: calico/node:v3.20.6
          image: calico/kube-controllers:v3.20.6


#单独下载calico的相关镜像即可。
docker pull calico/cni:v3.20.6
docker pull calico/pod2daemon-flexvol:v3.20.6
docker pull calico/node:v3.20.6
docker pull calico/kube-controllers:v3.20.6

#下载完成以后,执行calico.yaml即可。
kubectl apply -f calico.yaml

kubectl get pods -n kube-system
 

参考链接:

https://blog.csdn.net/weixin_54059979/article/details/123974805

https://blog.csdn.net/qq_44078641/article/details/120049473文章来源地址https://www.toymoban.com/news/detail-614860.html

到了这里,关于K8s集群部署(二进制安装部署详细手册)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • k8s1.27.2版本二进制高可用集群部署

    说明:本次实验共有5台主机,3台master节点同时又是worker,os128、os129、os130 节点主机容器运行时用的containerd,worker131、worker132主机的用的docker 主机名 IP 组件 系统 os128 192.168.177.128 etcd、kube-apiserver、kube-controller-manager、kube-scheduler、kubelet、kube-proxy、containerd CentOS7.9 os129 192.16

    2024年01月22日
    浏览(84)
  • 【云原生】二进制部署k8s集群(中)搭建node节点

    在上文已经成功部署了etcd分布式数据库、master01节点, 本文将承接上文的内容,继续部署Kubernetes集群中的 worker node 节点和 CNI 网络插件 kubelet 采用 TLS Bootstrapping 机制,自动完成到 kube-apiserver 的注册,在 node 节点量较大或者后期自动扩容时非常有用。   Master apiserver 启用 T

    2024年02月09日
    浏览(57)
  • 二进制安装K8S(单Master集群架构)

    k8s集群master01:192.168.154.10 kube-apiserver kube-controller-manager kube-scheduler etcd k8s集群node01:192.168.154.11 kubelet kube-proxy docker k8s集群node02:192.168.154.12 etcd集群节点1:192.168.154.10 etcd etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库。

    2024年02月10日
    浏览(49)
  • K8s集群1.27最新版二进制高可用部署

    二进制方式安装Kubernetes高可用集群,虽然安装过程较为复杂,但这也是每个技术人员必须要掌握的内容。同时,在安装过程中,也可以更加深刻地理解每个组件的工作原理。 一、系统环境配置 (1)主机名配置 (2)所有节点修改yum源 (3)所有节点安装常用软件   (4)将master01节点配

    2024年02月07日
    浏览(49)
  • 二进制部署高可用k8s集群V1.20.11版本

    单master架构图 master节点 node1节点 node2节点   Etcd是一个分布式键值存储系统, K8s使用Etcd进行数据存储 ,所以先准备一个Etcd数据库,为解决Etcd单点故障,应采用集群方式进行部署,这里使用3台组件集群,可容忍1台机器故障,当然 也可以使用5台组件集群,可容忍2台机器故

    2024年01月22日
    浏览(45)
  • 【云原生】二进制k8s集群(下)部署高可用master节点

     在上一篇文章中,就已经完成了二进制k8s集群部署的搭建,但是单机master并不适用于企业的实际运用(因为单机master中,仅仅只有一台master作为节点服务器的调度指挥,一旦宕机。就意味着整个集群的瘫痪,所以成熟的k8s的集群一定要考虑到master的高可用。)企业的运用一

    2024年02月12日
    浏览(58)
  • 第3关 二进制形式安装K8s高可用生产级集群

    ------ 课程视频同步分享在今日头条和B站 大家好,我是博哥爱运维,下面是这次安装k8s集群相关系统及组件的详细版本号 Ubuntu 22.04.3 LTS k8s: v1.27.5 containerd: 1.6.23 etcd: v3.5.9 coredns: 1.11.1 calico: v3.24.6 下面是此次虚拟机集群安装前的IP等信息规划(完全模拟一个中小型企业K8S集群)

    2024年02月04日
    浏览(50)
  • 二进制搭建k8s集群 master和etcd

    etcd作为服务发现系统,有以下的特点: 简单:安装配置简单,而且提供了HTTP API进行交互,使用也很简单。 安全:支持SSL证书验证。 快速:单实例支持每秒2k+读操作。 可靠:采用raft算法,实现分布式系统数据的可用性和一致性。 etcd目前默认使用2379端口提供HTTP API服务,

    2024年02月12日
    浏览(47)
  • 二进制搭建 Kubernetes与k8s集群搭建(一)

    目录 二进制搭建 Kubernetes v1.20     操作系统初始化配置 部署 docker引擎 部署 etcd 集群 准备签发证书环境 在 master01 节点上操作      生成Etcd证书 在 node01 节点上操作 在 node02 节点上操作 部署 Master 组件 在 master01 节点上操作 部署 Worker Node 组件 在所有 node 节点上操作 在 mas

    2024年02月06日
    浏览(64)
  • k8s二进制部署--部署高可用

    notready是因为没有网络,因此无法创建pod 在同一个pod中的容器共享资源和网络,使用同一个网络命名空间。 每个pod都有一个全局的真实IP地址,同一个node之间的不同pod可以直接使用对方pod的ip地址进行通信。 pod1和pod2是通过docker0的网桥来进行通信。 Pod 地址与 docker0 在同一网

    2024年02月03日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包