已完成 K8S安装过程的前7个过程。
节点名称 | 机器IP | OS版本 | kubernetes版本 |
---|---|---|---|
master1 | 192.168.0.145 | Centos 7.9 | 1.24.2 |
master2 | 192.168.0.200 | Centos 7.9 | 1.24.2 |
node1 | 192.168.0.233 | Centos 7.9 | 1.24.2 |
cd /opt
wget https://dl.k8s.io/v1.24.2/kubernetes-server-linux-amd64.tar.gz
tar -xvf ./kubernetes-server-linux-amd64.tar.gz
~/.bash_profile
,追加下边内容PATH=$PATH:/opt/kubernetes/server/bin
export PATH
source ~/.bash_profile
mkdir -p /opt/kubenetes/cfg
mkdir -p /opt/kubernetes/logs
cat > /opt/kubenetes/cfg/kube-apiserver.conf <
--service-cluster-ip-range
:设置 kubernetes service IP 地址范围;--etcd-servers
:设置 etcd 集群的 IP地址;touch /opt/kubenetes/cfg/token.sh
chmod +x /opt/kubenetes/cfg/token.sh
#!/bin/sh
TLS_BOOTSTRAPPING_TOKEN=`head -c 16 /dev/urandom | od -An -t x | tr -d ' '`
cat > /opt/kubenetes/cfg/token.csv <
sh /opt/kubenetes/cfg/token.sh
/usr/lib/systemd/system/kube-apiserver.service
文件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/server/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl enable kube-apiserver
systemctl start kube-apiserver
systemctl status kube-apiserver
#!/bin/sh
KUBE_APISERVER=192.168.0.110
kubectl config set-cluster kubernetes \
--certificate-authority=/etc/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=https://${KUBE_APISERVER}:8443 \
--kubeconfig=kube-controller-manager.kubeconfig
kubectl config set-credentials system:kube-controller-manager \
--client-certificate=/etc/kubernetes/ssl/kube-controller-manager.pem \
--client-key=/etc/kubernetes/ssl/kube-controller-manager-key.pem \
--embed-certs=true --kubeconfig=kube-controller-manager.kubeconfig
kubectl config set-context system:kube-controller-manager \
--cluster=kubernetes \
--user=system:kube-controller-manager \
--kubeconfig=kube-controller-manager.kubeconfig
kubectl config use-context system:kube-controller-manager --kubeconfig=kube-controller-manager.kubeconfig
上边配置中 KUBE_APISERVER
的 IP 地址为 K8S安装过程二:安装Keepalived服务 中的 VIP 地址。
chmod +x kube-controller-manager.sh
sh kube-controller-manager.sh
KUBE_CONTROLLER_MANAGER_OPTS="--secure-port=10257 \
--bind-address=127.0.0.1 \
--kubeconfig=/opt/kubernetes/cfg/kube-controller-manager.kubeconfig \
--service-cluster-ip-range=10.255.0.0/16 \
--cluster-name=kubernetes \
--cluster-signing-cert-file=/etc/kubernetes/ssl/ca.pem \
--cluster-signing-key-file=/etc/kubernetes/ssl/ca-key.pem \
--allocate-node-cidrs=true \
--cluster-cidr=172.16.0.0/16 \
--cluster-signing-duration=87600h \
--root-ca-file=/etc/kubernetes/ssl/ca.pem \
--service-account-private-key-file=/etc/kubernetes/ssl/ca-key.pem \
--leader-elect=true \
--feature-gates=RotateKubeletServerCertificate=true \
--controllers=*,bootstrapsigner,tokencleaner \
--horizontal-pod-autoscaler-sync-period=10s \
--tls-cert-file=/etc/kubernetes/ssl/kube-controller-manager.pem \
--tls-private-key-file=/etc/kubernetes/ssl/kube-controller-manager-key.pem \
--use-service-account-credentials=true \
--alsologtostderr=true \
--logtostderr=false \
--log-dir=/opt/kubernetes/logs \
--v=4"
--cluster-cidr
:设置 POD 的 IP 地址范围,这个值一定不能和 4.1 章节中 --service-cluster-ip-range
参数重叠,否则 CNI 网络插件将无法正常启动,很多诡异的错误将会产生。切记,一定不要将这两个参数的值设置成重叠的IP范围。--service-cluster-ip-range
:设置 kubernetes service 的IP范围,这个值与 4.1 中 --service-cluster-ip-range
变量值一定要设置成相同值。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/server/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl enable kube-controller-manager
systemctl start kube-controller-manager
systemctl status kube-controller-manager
#!/bin/sh
KUBE_APISERVER="https://192.168.0.110:8443"
kubectl config set-cluster kubernetes \
--certificate-authority=/etc/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kube-scheduler.kubeconfig
kubectl config set-credentials "kube-scheduler" \
--client-certificate=/etc/kubernetes/ssl/kube-scheduler.pem \
--client-key=/etc/kubernetes/ssl/kube-scheduler-key.pem \
--embed-certs=true \
--kubeconfig=kube-scheduler.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user="kube-scheduler" \
--kubeconfig=kube-scheduler.kubeconfig
kubectl config use-context default --kubeconfig=kube-scheduler.kubeconfig
上边配置中 KUBE_APISERVER
的 IP 地址为 K8S安装过程二:安装Keepalived服务 中的 VIP 地址。
chmod +x kube-scheduler-kubeconfig.sh
sh kube-scheduler-kubeconfig.sh
KUBE_SCHEDULER_OPTS="--bind-address=127.0.0.1 \
--kubeconfig=/opt/kubernetes/cfg/kube-scheduler.kubeconfig \
--leader-elect=true \
--alsologtostderr=true \
--logtostderr=false \
--log-dir=/opt/kubernetes/logs \
--v=4"
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/server/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl enable kube-scheduler
systemctl start kube-scheduler
systemctl status kube-scheduler
KUBE_APISERVER="https://192.168.0.110:8443"
kubectl config set-cluster kubernetes \
--certificate-authority=/etc/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER}
--kubeconfig=admin.kubeconfig
kubectl config set-credentials "admin" \
--client-certificate=/etc/kubernetes/ssl/admin.pem \
--client-key=/etc/kubernetes/ssl/admin-key.pem \
--embed-certs=true
--kubeconfig=admin.kubeconfig
kubectl config set-context kubernetes \
--cluster=kubernetes \
--user="admin"
--kubeconfig=admin.kubeconfig
kubectl config use-context kubernetes --kubeconfig=admin.kubeconfig
chmod +x admin-kubeconfig.sh
sh admin-kubeconfig.sh
mkdir ~/.kube
cp admin.kubeconfig ~/.kube/config
kubectl get cs
到这一步表示 kubernetes master 节点的服务已经全部正常启动,接下来就可以开始安装 kubernetes worker 节点的服务。在安装 kubernetes worker 节点服务之前,首先创建 kubernetes worker 节点连接 kubernetes master 节点的授权文件,如下边的第 8 章节内容。
TLS_BOOTSTRAPPING_TOKEN=80364b85129737d29b7b963c15f962b4
KUBE_APISERVER="https://192.168.0.110:8443"
kubectl config set-cluster kubernetes \
--certificate-authority=/etc/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kubelet-bootstrap.kubeconfig
kubectl config set-credentials "kubelet-bootstrap" \
--token=${TLS_BOOTSTRAPPING_TOKEN} \
--kubeconfig=kubelet-bootstrap.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user="kubelet-bootstrap" \
--kubeconfig=kubelet-bootstrap.kubeconfig
kubectl config use-context default --kubeconfig=kubelet-bootstrap.kubeconfig
chmod +x kubelet-bootstrap-kubeconfig.sh
sh kubelet-bootstrap-kubeconfig.sh
上述命令将会生成 kubelet-bootstrap.kubeconfig 配置文件,后续 kubernetes worker 节点连接 kubernetes master 节点将会使用到该文件。
kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap
创建集群角色绑定配置。只需要在安装第一个 kubernetes master 节点时执行一次即可,不需要在后续的节点上执行这条命令。
cd /opt
tar -czvf kubernetes.tar.gz /opt/kubernetes
后续安装 kubernetes worker 节点时,将会从当前 kubernetes master 节点获取 kubernetes.tar.gz 进行安装。
helm 时 kubernetes 中比较流行的一个应用程序管理工具,可在master 节点上安装这个工具,或者在其他的专门的管理节点上安装helm工具。helm工具访问 kubernetes 集群需要使用 ~/.kube/config 这个配置文件。
cd /opt
wget https://get.helm.sh/helm-v3.10.2-linux-amd64.tar.gz
tar -xzvf helm-v3.10.2-linux-amd64.tar.gz
cp /opt/linux-amd64/helm /usr/local/bin/
helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts