ubuntu18.04 下安装K8S集群(一)
接下来全部过程都使用root进行1、安装dockercurl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -apt install docker.iosystemctl enable dockersystemctl start dockersystemctl status dockercat >/etc/d
接下来全部过程都使用root进行
1、安装docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
apt install docker.io
systemctl enable docker
systemctl start docker
systemctl status docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"],
"live-restore": true
}
EOF
systemctl daemon-reload
systemctl restart docker
2、安装kubeadm、kubectl、kubelet
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg| apt-key add -
cat << EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main
EOF
apt-get update && apt-get install -y kubelet kubeadm kubectl
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
systemctl enable kubelet
systemctl daemon-reload
systemctl restart kubelet
3、修改配置
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 禁用Selinux
apt install selinux-utils
setenforce 0
# 禁用swap
swapoff -a
# !!! 同时把/etc/fstab包含swap那行记录删掉!!!
4、分别配置三台虚拟机
如果你的电脑配置不高,可以将两个node调成 1G 1CPU。
- 修改hostname
echo k8s-master > /etc/hostname
我将三台虚拟机hostname分别设置为:
k8s-master
k8s-node1
k8s-node2
- 设置静态IP
查看虚拟机分配的 IP、DNS主机、网关、掩码 并将其设置为静态
5、建立集群
在mster上运行
kubeadm init \
--pod-network-cidr=10.244.0.0/16 \ # 这里是k8s集群内部地址,不懂就这么填
--apiserver-advertise-address=<master地址> \ # !!!这里填写master IP地址!!!
--kubernetes-version=v1.18.5 \ # 这里是版本
--ignore-preflight-errors=Swap \
--image-repository registry.aliyuncs.com/google_containers # 这里制定国内源,是不翻墙的重点
输出信息
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.35.130:6443 --token 13zy98.ubjskmoo7rqthc4n \
--discovery-token-ca-cert-hash sha256:5a2fbd7d74327c3ea716835fc49cb2cf8d9db3ec29af2925812cbc4b811d8383
初始化成功以后,集群还无法使用,上图红框是需要执行的操作,执行完以后才能使用集群。
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
接下来配置flannel网络,可以到 https://github.com/coreos/flannel 找到此处
将下面命令复制下来在命令行中执行 kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
使用如下命令查看master是否配置成功
kubectl get nodes
如果出现ready则表示成功
如果此处提示host port 连接不成功
在/etc/hosts 添加
199.232.28.133 raw.githubusercontent.com
此时master已经搭建成功!!
6.将node节点加入到集群中
**
1.生成token(在master节点执行)
kubeadm token create
2.生成认证(在master节点执行)
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
3.加入集群(node节点执行)(注:命令就是创建master节点时,所提到的要在node3中执行的那条命令)
kubeadm join 192.168.25.70:6443 --token tdu953.b74wa05832h6x7yn \
--discovery-token-ca-cert-hash sha256:fab411b7d324dce4cf476f190d336e0dacceba5d8074b586b3e6a72002d8c383
node3节点加入成功!!!
4.master节点查看
可以查看node3节点是否已经添加成功
kubectl get nodes
执行完发现node3节点的状态是Ready,因此添加成功
7. 测试集群
简介
用户可以用 Kubernetes Dashboard 部署容器化的应用、监控应用的状态、执行故障排查任务以及管理 Kubernetes 各种资源。
部署
官方参考文档:
https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/
github项目地址:
https://github.com/kubernetes/dashboard
执行yaml文件直接部署:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml
1
[root@master ~]# kubectl -n kubernetes-dashboard get pods
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-694557449d-wfv4b 1/1 Running 0 4m50s
kubernetes-dashboard-9774cc786-v9ltx 1/1 Running 0 4m50s
[root@master ~]# kubectl -n kubernetes-dashboard get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.100.165.238 <none> 8000/TCP 4m52s
kubernetes-dashboard ClusterIP 10.109.218.125 <none> 443/TCP 4m52s
12345678910
访问
使用nodeport方式将dashboard服务暴露在集群外,指定使用30443端口,可自定义:
kubectl patch svc kubernetes-dashboard -n kubernetes-dashboard \
-p '{"spec":{"type":"NodePort","ports":[{"port":443,"targetPort":8443,"nodePort":30443}]}}'
12
查看暴露的service,已修改为nodeport类型:
# kubectl -n kubernetes-dashboard get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.102.18.37 <none> 8000/TCP 69s
kubernetes-dashboard NodePort 10.110.118.188 <none> 443:30443/TCP 69s
1234
也可以通过其他方式暴露服务,这里不再赘述。
登录
浏览器访问dashboard:
https://<node_ip>:30443
Dashboard 支持 Kubeconfig 和 Token 两种认证方式,我们这里选择Token认证方式登录。
官方参考文档:
https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/creating-sample-user.md
创建dashboard-adminuser.yaml:
cat > dashboard-adminuser.yaml << EOF
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
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
EOF
创建登录用户
kubectl apply -f dashboard-adminuser.yaml
- 说明:上面创建了一个叫admin-user的服务账号,并放在kubernetes-dashboard 命名空间下,并将cluster-admin角色绑定到admin-user账户,这样admin-user账户就有了管理员的权限。默认情况下,kubeadm创建集群时已经创建了cluster-admin角色,我们直接绑定即可。
查看admin-user账户的token
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
- 把获取到的Token复制到登录界面的Token输入框中,成功登陆dashboard。
上面参考:https://blog.csdn.net/lgfx21/article/details/109771498
更多推荐
所有评论(0)