在VMware Workstation虚拟机中创建三台CentOS-8.1.1911-x86_64-dvd1安装kubernetes环境
1、网络配置选择:桥接模式(其他的不容易网络联通)
2、cpu选择2核
3、内存选择3G
4、磁盘默认20G
创建完成后记录三台虚拟机的ip分别是:
192.168.3.87
192.168.3.88
192.168.3.89
执行脚本start.sh:
sh start.sh
脚本内容如下:
cat <<EOF >> /etc/hosts
192.168.3.87 master
192.168.3.88 node1
192.168.3.89 node2
EOF
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapoff -a
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
sudo yum remove podman
swapoff -a
cat /etc/fstab
cat > /etc/resolv.conf << EOF
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF
nmcli c reload
nmcli c up ens160
rm -f /etc/yum.repos.d/CentOS-AppStream.repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum clean all
yum makecache
yum install chrony -y
vim /etc/chrony.conf
修改时间同步的软件的配置文件:vim /etc/chrony.conf
添加内容为server ntp.aliyun.com iburst
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#pool 2.centos.pool.ntp.org iburst # 注释原来的
server ntp.aliyun.com iburst # 使用阿里的
执行脚本start2.sh:
sh start2.sh
脚本内容如下:
systemctl restart chronyd.service
chronyc sources -v
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum -y install docker-ce
docker --version
sudo mkdir -p /etc/docker
cat <<EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://9oggfmz6.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
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
systemctl daemon-reload && systemctl restart docker
yum install -y kubelet-1.23.1 kubeadm-1.23.1 kubectl-1.23.1 docker-ce
mkdir -p $HOME/.kube
kubeadm init --image-repository=registry.aliyuncs.com/google_containers
kubeadm init --pod-network-cidr=10.244.0.0/16 --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.189.128:6443 --token 3wk0qd.piun73d9s25vu7v7 \
--discovery-token-ca-cert-hash sha256:cc0d0e1a9d4ec6d356a789a6265ff88d62d3ef61fbb745337695eede1ce37031
master 节点添加认证
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
# 从上面找到并记录下这一句,在各个node节点虚拟机执行
kubeadm join 192.168.189.128:6443 --token 3wk0qd.piun73d9s25vu7v7 \
--discovery-token-ca-cert-hash sha256:cc0d0e1a9d4ec6d356a789a6265ff88d62d3ef61fbb745337695eede1ce37031
kubectl get nodes
wget -c https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
# 1、使用下面的命令操作使得master 可以作为node使用 承载pod
kubectl taint nodes --all node-role.kubernetes.io/master-
# 可能会出现下面的结果 因为taint(master标记的污点已经被去掉了)没有关系
# taint "node-role.kubernetes.io/master" not found
# taint "node-role.kubernetes.io/master" not found
# 2、可以用下面的命令确认一下taint已经没有了
kubectl describe nodes master |grep Taint
# Taints:
kubectl get pods -n kube-system
测试集群
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
# 查看
kubectl get svc nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.99.68.218 <none> 80:31033/TCP 3m42s
http://master:32696/
http://node1:32696/
http://node2:32696/
官网地址:dashboard
kubernetes 部署 dashboard(详细步骤)
# 版本很重要
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
# 启动控制台(一共有3种方式)
kubectl proxy --port=8888 --address="192.168.0.7" --accept-hosts="^.*"
# 控制台访问
http://192.168.0.7:8888/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/login
使用token登录
[root@master ~]# kubectl create serviceaccount dashboard-admin -n kube-system
serviceaccount/dashboard-admin created
[root@master ~]# kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
clusterrolebinding.rbac.authorization.k8s.io/dashboard-cluster-admin created
[root@master ~]# kubectl get secrets -n kube-system | grep dashboard
dashboard-admin-token-g448b kubernetes.io/service-account-token 3 25s
[root@master ~]# kubectl describe secrets -n kube-system dashboard-admin-token-g448b
Name: dashboard-admin-token-g448b
Namespace: kube-system
Labels: <none>
Annotations: kubernetes.io/service-account.name: dashboard-admin
kubernetes.io/service-account.uid: 2a5574cf-af67-481b-a5bc-6f13c87765ad
Type: kubernetes.io/service-account-token
Data
====
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IllpbE8yckpURExsQU1CMHlSemg3RTFvdTR0Z3kwbjNSZk1sdHVQWUh4bmcifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tZzQ0OGIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiMmE1NTc0Y2YtYWY2Ny00ODFiLWE1YmMtNmYxM2M4Nzc2NWFkIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.f082BlD037t39QBYPs-7v1yW55z7kK68LwkM6OCaQCeDZZEmpBPHxuxQR5cog8AGPQXIBGA-JcB5rqHZV-zk3aoOw8ZfAZDc7lyVCqkkZdup0qxwGj2ywetvAkR9JMPIK-tZjpXnQLrMwvMGthiOjVGqYJXI2Gp1lu9E1440WD6boAiH0p_vTWeKUqsUE6_4UQdVtwUnzTBwcz6ASMigXytUDvM7iTpQTU-F9ZasaixYoIzcR9FTW9vIoF-bAhCG8Biri7V7ubFAzLzBmFTtTA9vU92pTWSS-0iz1YlFFa_RwGKY6daPVOtepcgB3FNrBDFxgVtBC_KTD2sjZY2_Cw
ca.crt: 1099 bytes
namespace: 11 bytes
Kubernetes的两种登录方式和三种可视化UI界面
# 删除之前的service
kubectl delete service kubernetes-dashboard --namespace=kubernetes-dashboard
# 启动dashboard服务
kubectl apply -f dashboard-svc.yaml
dashboard-svc.yaml内容
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: 30000 # 增加
selector:
k8s-app: kubernetes-dashboard
# 查看端口号
kubectl get svc -n kube-system
【问题解决】您的连接不是私密连接;因为此网站发送了 Google Chrome 无法处理的杂乱凭据。网络错误和攻击通常是暂时的,因此,此网页稍后可能会恢复正常。
当前页面点击任意空白处,直接键盘输入“thisisunsafe”即可解决。
原理是chrome认为这个网站是不安全的,不让你访问。而你输入了这句话,就类似于签了生死状,了解了风险。chrome就放你进去,但那之后再出问题也就没有责任了。
https://192.168.0.7:30000/