# 关闭防火墙 systemctl stop firewalld systemctl disable firewalld # 关闭selinux sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/config # 永久 setenforce 0 # 临时 # 关闭swap swapoff -a # 临时 sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久 # 根据规划设置主机名 hostnamectl set-hostname# 在master添加hosts cat >> /etc/hosts << EOF 192.168.137.81 k8s-master1 192.168.137.82 k8s-node1 192.168.137.83 k8s-node2 EOF # 将桥接的IPv4流量传递到iptables的链 cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system # 生效 # 时间同步 yum install ntpdate -y ntpdate time.windows.com
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo yum -y install docker-ce systemctl enable docker && systemctl start docker
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://v27k018o.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo 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
yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0 systemctl enable kubelet
kubeadm init \ --apiserver-advertise-address=192.168.137.81 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.20.0 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16 \ --ignore-preflight-errors=all
# cat kubeadm.conf apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration kubernetesVersion: v1.20.0 imageRepository: registry.aliyuncs.com/google_containers networking: podSubnet: 10.244.0.0/16 serviceSubnet: 10.96.0.0/12 # kubeadm init --config kubeadm.conf --ignore-preflight-errors=all
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 Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf 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.137.81:6443 --token tix7ff.pnjcwvl6awyaeh8i \ --discovery-token-ca-cert-hash sha256:617842ea5040ce5e7f971d387b58693cbfa79261763b68c589fe8d124f1a5154
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master1 NotReady control-plane,master 95s v1.20.0
kubeadm join 192.168.137.81:6443 --token tix7ff.pnjcwvl6awyaeh8i \ --discovery-token-ca-cert-hash sha256:617842ea5040ce5e7f971d387b58693cbfa79261763b68c589fe8d124f1a5154
kubeadm token create --print-join-command
wget https://docs.projectcalico.org/manifests/calico.yaml
kubectl apply -f calico.yaml kubectl get pods -n kube-system
[root@k8s-master ]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE calico-kube-controllers-5f6cfd688c-4hfmv 1/1 Running 0 2m37s calico-node-8swrp 1/1 Running 0 2m37s calico-node-nv96p 1/1 Running 0 2m37s calico-node-s2vl6 1/1 Running 0 2m37s coredns-7f89b7bc75-7nzm8 0/1 Running 0 22m coredns-7f89b7bc75-9c46c 0/1 Running 0 22m etcd-k8s-master1 1/1 Running 0 22m kube-apiserver-k8s-master1 1/1 Running 0 22m kube-controller-manager-k8s-master1 1/1 Running 0 22m kube-proxy-clc6k 1/1 Running 0 18m kube-proxy-dvlvr 1/1 Running 0 22m kube-proxy-jr6hm 1/1 Running 0 18m kube-scheduler-k8s-master1 1/1 Running 0 22m [root@k8s-master ]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master1 Ready control-plane,master 23m v1.20.0 k8s-node1 Ready18m v1.20.0 k8s-node2 Ready 18m v1.20.0
kubectl create deployment nginx --image=nginx kubectl expose deployment nginx --port=80 --type=NodePort kubectl get pod,svc
[root@k8s-master ~]# kubectl get pod,svc NAME READY STATUS RESTARTS AGE pod/nginx-6799fc88d8-nkzk6 1/1 Running 0 6m51s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1443/TCP 32m service/nginx NodePort 10.108.121.252 80:30167/TCP 6m2s
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
# vi recommended.yaml ... kind: Service apiVersion: v1 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kubernetes-dashboard spec: ports: - port: 443 targetPort: 8443 nodePort: 30001 selector: k8s-app: kubernetes-dashboard type: NodePort ... # kubectl apply -f recommended.yaml # kubectl get pods -n kubernetes-dashboard
# 创建用户 $ kubectl create serviceaccount dashboard-admin -n kube-system # 用户授权 $ 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}')
[root@k8s-master ~]# kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}') Name: dashboard-admin-token-cgmld Namespace: kube-system Labels:Annotations: kubernetes.io/service-account.name: dashboard-admin kubernetes.io/service-account.uid: 3208e176-5115-4425-bb67-d45863bc05f7 Type: kubernetes.io/service-account-token Data ==== ca.crt: 1066 bytes namespace: 11 bytes token: eyJhbGciOiJSUzI1NiIsImtpZCI6IlBnN2lzMk8zYndPX3ZONnc0cnFnRjhsVnczOTVlNGxXSDl4c1Z0OGtmNDAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tY2dtbGQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiMzIwOGUxNzYtNTExNS00NDI1LWJiNjctZDQ1ODYzYmMwNWY3Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.DY9SLeakUzVUqUHLJjLuaBtv0EOj6l-zsCfmzKTtsiTkaX39bGpLInToKuSpbXHYAmkpCvoZH22ghmOds3LFgvQBCIt6M83rrL83aPzhDjAKtPtPkz9vJGR7K5LnfrB9AX5dVhiU_AkaVIBIFcqTxVlFpl6W1EzTc0uJDM7K8Gr2XnPvRfUMe8WaEWR7tVxMEEhPhP2waEYmcc5uFz5unI_g6lTMYRJnhZCjfqh7lS9NA_8WgmoQnQjxW4cYAsqrdCzbroTEMCslH_pCj-PZNxf7mKVXZwklYL78t8klU_AytuhdaV88iRR3HEuBMYLbfJjy6RLkyt_ORweaXb8npg