这里记录怎么部署一个kubernetes集群的(非生产环境)
操作系统: Rocky Linux 8.5
虚拟化软件:vmware Fusion
网络环境:NET
内存:4G
CPU:2核
必须关闭Linux swap功能。删除/etc/fstab下关于swap分区的挂载命令
需要在两个节点上都安装Docker,过程如下
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
配置加速器和修改Docker的cgroup驱动为systemd,因为k8s 1.23开始只支持systemd 的cgroup驱动
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"exec-opts": ["native.cgroupdriver=systemd"],
"storage-driver": "overlay2",
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"registry-mirrors": ["https://u7g484go.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl enable docker
注意:使用Overlay2做存储驱动的时候内核版本必须是4以上对于CentOS等系统必须 3.10.0-514 或者以上
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
yum install -y --setopt=obsolutes=0 kubeadm-1.23.5 kubelet-1.23.5 kubectl-1.23.5
在初始化的时候会检测下面的软件,所以需要安装下
yum install -y iproute-tc
firewall-cmd --permanent --add-port=6443/tcp
firewall-cmd --permanent --add-port=2379-2380/tcp
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=10251/tcp
firewall-cmd --permanent --add-port=10252/tcp
firewall-cmd --permanent --add-port=10255/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=8472/udp
firewall-cmd --add-masquerade --permanent
# only if you want NodePorts exposed on control plane IP as well
firewall-cmd --permanent --add-port=30000-32767/tcp
firewall-cmd --reload
注意:建议关闭firewalld服务,其中的规则有可能造成pod间通讯的错误,如果不关闭如何解决需要进一步探索
systemctl stop firewalld
systemctl disable firewalld
systemctl enable kubelet
kubeadm init --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
注意:--pod-network-cidr=10.244.0.0/16 不能修改,因为之后需要安装flannel
--image-repository 由于网路的原因需要从 registry.cn-hangzhou.aliyuncs.com/google_containers pull镜像
安装完成后会有如下结果:
kubeadm join 192.168.31.11:6443 --token swkspd.r4p6j5i33pckfly8 \
--discovery-token-ca-cert-hash sha256:52bf8a742b4c49366c39c899e3efac61d80274bb50d1e84facdbe9e9a71ec1d0
用来使得WorkNode加入集群中
root用户:
export KUBECONFIG=/etc/kubernetes/admin.conf
非root用户:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
集群安装成功后,运行kubectl get nodes 会发现masternode处于NotReady的状态,这是由于CoreDNS没有初始化,这是正确的,因为我们没有安装任何网络插件。这里通过安装flannel来实现
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
命令执行后,一段时间后,可以发现CoreDNS初始化成功。flannel安装成功
执行:
kubeadm join 192.168.31.11:6443 --token swkspd.r4p6j5i33pckfly8 \
--discovery-token-ca-cert-hash sha256:52bf8a742b4c49366c39c899e3efac61d80274bb50d1e84facdbe9e9a71ec1d0
稍后在master node查看状态
kubectl get node
NAME STATUS ROLES AGE VERSION
masternode Ready control-plane,master 27h v1.23.6
worknode1 Ready <none> 22h v1.23.6
表示加入成功。
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml
完成过后,可以通过如下命令查询
kubectl get pods -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-799d786dbf-tw9bt 1/1 Running 0 26h
kubernetes-dashboard-fb8648fd9-thlvw 1/1 Running 0 26h
creating Service Account with the name admin-user in namespace kubernetes-dashboard
admin-user.yml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
执行
kubectl apply -f admin-user.yml
user-binding.yml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
执行
kubectl apply -f user-binding.yml
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
获取的Code在之后访问的界面中需要
通过kubectl proxy启动
kubectl proxy
启动dashborad后可以通过http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ 访问
但是这个地址仅仅在服务器本地才可以使用,在外网访问可以通过nginx来实现转发,需要注意的是必须支持https,也就是nginx必须安装证书
安装nginx,这里就不重复写过程了,通过源码编译或者RPM包都可以。
配置如下:
server {
listen 4443 ssl http2;
ssl_certificate /usr/local/nginx/conf/7712402_k8s.lokie.wang.pem;
ssl_certificate_key /usr/local/nginx/conf/7712402_k8s.lokie.wang.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
server_name k8s.lokie.wang;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://localhost:8001;
}
}
本文为Lokie.Wang原创文章,转载无需和我联系,但请注明来自lokie博客http://lokie.wang