k8s作为容器集群管理系统有着明显的优势,比如动态扩容/缩容。
1. 准备环境
最基本的集群需要三个节点,在三个节点上都安装k8s Node,在其中一个节点上安装Master。
操作系统 | IP | hostname | 安装组件 | 备注 |
---|
CentOS Linux 7 | 192.168.17.81 | k8s-master | kubeadm,kubelet,kubectl,docker | k8s主节点 | CentOS Linux 7 | 192.168.17.83 | k8s-node83 | kubeadm,kubelet,kubectl,docker | k8s节点 | CentOS Linux 7 | 192.168.17.85 | k8s-node85 | kubeadm,kubelet,kubectl,docker | k8s节点 |
1.1 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
三个节点分别执行上述命令。
1.2 关闭swap
swapoff -a # 临时关闭
vim /etc/fstab # 注释到swap那一行 永久关闭
?三个节点分别执行上述命令。
1.3?设置系统主机名以及Host文件
master主节点(192.168.17.81)执行
hostnamectl set-hostname k8s-master
node节点(192.168.17.83)执行
hostnamectl set-hostname k8s-node83
??node节点(192.168.17.85)执行
hostnamectl set-hostname k8s-node85
1.4?添加主机名和ip地址映射
编辑/etc/hosts,添加如下内容:
192.168.17.81 k8s-master
192.168.17.83 k8s-node83
192.168.17.85 k8s-node85
在三个节点上分别执行。
2. 执行安装
2.1 安装docker
安装依赖
yum install -y yum-utils
设置镜像仓库:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装docker
yum install docker-ce docker-ce-cli containerd.io
设置docker自动启动
systemctl enable docker && systemctl start docker
2.2?添加阿里云的yum源
cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
三个节点分别执行。?
2.3?安装kubeadm、kubelet和qubectl
yum install -y kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2
systemctl start kubelet
systemctl enable kubelet
三个节点分别执行。
2.4?部署k8s的master和node节点
部署master节点,在192.168.17.81执行
初始化master节点
kubeadm init --apiserver-advertise-address=192.168.17.81 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.0 --service-cidr=10.10.0.0/16 --pod-network-cidr=10.11.0.0/16
网段问题,两个网段不要重,后面是/16,不要与当前机器网段一样。
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.XX.XX:6443 --token qfaevl.x8w6cme8fxxx \
--discovery-token-ca-cert-hash sha256:e66afc25fee165dc2c12bff269bee86XXX
拷贝master节点的/etc/kubernetes/admin.conf配置文件到/etc/kubernetes/目录下,并执行如下命令:
scp root@192.168.17.81:/etc/kubernetes/admin.conf /etc/kubernetes/.
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
查看节点信息:
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady master 34m v1.16.2
k8s-node83 NotReady <none> 16m v1.16.2
k8s-node85 NotReady <none> 21m v1.16.2
管理状态重置
kubeadm reset
2.5??安装网络插件(CNI)
安装flannel插件(轻量级用于快速搭建使用,初学推荐)
下载yaml文件
wget http://download.stisd.cn/k8s/kube-flannel.yml
修改net-conf.json下面的网段为上面init pod-network-cidr的网段地址(必须正确否则会导致集群网络问题)
sed -i 's/10.244.0.0/10.11.0.0/' kube-flannel.yml
修改完安装插件,执行
kubectl apply -f kube-flannel.yml
kubectl get pods -n kube-system
3.?测试k8s集群
在Kubernetes集群中创建一个pod,验证是否正常运行
创建一个pod,开放对外端口访问,这里会随机映射一个端口
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
查看pod状态,必须是running状态而且ready是1,并查看nginx svc的80端口映射到了哪个端口
kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-86c57db685-g6776 1/1 Running 0 14m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 72m
service/nginx NodePort 10.10.134.200 <none> 80:32693/TCP 14m
通过运行节点访问
??至此,k8s的基础环境就搭建完成了。
将ngnix扩容为3个节点:
kubectl scale deployment nginx --replicas=3
通过命令查看:
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-86c57db685-g6776 1/1 Running 0 68m
nginx-86c57db685-jsgm9 1/1 Running 0 83s
nginx-86c57db685-pbs7v 1/1 Running 0 83s
可以看到已经是3个节点了。
4.??部署Dashboard
下载配置文件
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
修改dashboard端口
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 30001
selector:
k8s-app: kubernetes-dashboard
部署dashboard
kubectl apply -f kubernetes-dashboard.yaml
创建角色
kubectl create serviceaccount dashboard-admin -n kube-system
赋权限就是分组到kube-system:dashboard-admin
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
token进行登陆
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
登录界面:
输入token进行登录。
删除dashboard
kubectl delete service kubernetes-dashboard --namespace=kubernetes-dashboard
kubectl delete service dashboard-metrics-scraper --namespace=kubernetes-dashboard
5. 遇到的问题
查看信息时,出现连接本地8080,并且被拒绝。
kubectl get pods
The connection to the server localhost:8080 was refused - did you specify the right host or port?
原因是未执行节点配置。
拷贝master节点的/etc/kubernetes/admin.conf配置文件到/etc/kubernetes/目录下,并执行如下命令
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
|