部署K8s,我切到了root用户进行部署
su root
启动Docker
systemctl enable docker
systemctl status docker
systemctl start docker
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg |sudo apt-key add -
添加合适的镜像源到 sources.list
nano /etc/apt/sources.list
末尾添加
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
更新apt
apt-get update
安装
apt install kubeadm=1.23.6-00 kubelet=1.23.6-00 kubectl=1.23.6-00
apt-mark hold kubeadm kubelet kubectl
验证一下
kubeadm version
kubelet --version
kubectl version --client
swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
nano /etc/modules-load.d/containerd.conf
末尾添加
overlay
br_netfilter
使用 modprobe 命令添加模块
modprobe overlay
modprobe br_netfilter
配置 Kubernetes 网络。打开 kubernetes.conf 文件
nano /etc/sysctl.d/kubernetes.conf
添加文本
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
重新加载配置
sysctl --system
设置为主节点的服务器服务名
hostnamectl set-hostname master-node
由于我是单机配置所以就不配置work了,单机的master节点是不允许部署pod的,所以要执行下面命令,不然无法部署pod
允许master节点部署pod
kubectl taint nodes --all node-role.kubernetes.io/master-
如果不允许调度
kubectl taint nodes master1 node-role.kubernetes.io/master=:NoSchedule
污点可选参数NoSchedule: 一定不能被调度PreferNoSchedule: 尽量不要调度NoExecute: 不仅不会调度, 还会驱逐Node上已有的Pod
nano /etc/default/kubelet
添加文本
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
执行以下命令以重新加载配置
systemctl daemon-reload
systemctl restart kubelet
修改 Docker 守护程序配置文件
nano /etc/docker/daemon.json
添加内容
{"exec-opts": ["native.cgroupdriver=systemd"],"log-driver": "json-file","log-opts": {"max-size": "200m"},"storage-driver": "overlay2"}
加载配置文件
systemctl daemon-reload
systemctl restart docker
修改K8s配置文件
nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
添加内容
Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"
重新加载配置
systemctl daemon-reload
systemctl restart kubelet
初始化命令
kubeadm init --apiserver-advertise-address=本机ip --apiserver-cert-extra-sans=127.0.0.1 --pod-network-cidr=10.244.0.0/16 --image-repository=registry.aliyuncs.com/google_containers
启动成功的打印
root@uu22:/home/uu# kubeadm init --apiserver-advertise-address=10.60.1.140 --apiserver-cert-extra-sans=127.0.0.1 --pod-network-cidr=10.244.0.0/16 --image-repository=registry.aliyuncs.com/google_containers
I0314 10:54:45.506192 11038 version.go:255] remote version is much newer: v1.26.2; falling back to: stable-1.23
[init] Using Kubernetes version: v1.23.17
[preflight] Running pre-flight checks[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 23.0.1. Latest validated version: 20.10
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master-node] and IPs [10.96.0.1 10.60.1.140 127.0.0.1]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost master-node] and IPs [10.60.1.140 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master-node] and IPs [10.60.1.140 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 17.537067 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.23" in namespace kube-system with the configuration for the kubelets in the cluster
NOTE: The "kubelet-config-1.23" naming of the kubelet ConfigMap is deprecated. Once the UnversionedKubeletConfigMap feature gate graduates to Beta the default name will become just "kubelet-config". Kubeadm upgrade will handle this transition transparently.
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master-node as control-plane by adding the labels: [node-role.kubernetes.io/master(deprecated) node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node master-node as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: yop4zj.045c6fjmjgtpqwt3
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxyYour Kubernetes control-plane has initialized successfully!To start using your cluster, you need to run the following as a regular user:mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configAlternatively, if you are the root user, you can run:export KUBECONFIG=/etc/kubernetes/admin.confYou 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 10.60.1.140:6443 --token yop4zj.045c6fjmjgtpqwt3 \--discovery-token-ca-cert-hash sha256:f5618abf70020cba080c44cb51f2b4ccace6212478fa41c381c22397f80e64c8
查看节点状态
kubectl get nodes
安装网络插件,使节点状态变为ready
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
安装
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yamlnano ./recommended.yaml
编辑recommended.yaml
kind: Service
apiVersion: v1
metadata:labels:k8s-app: kubernetes-dashboardname: kubernetes-dashboardnamespace: kubernetes-dashboard
spec:type: NodePort #添加ports:- port: 443targetPort: 8443nodePort: 自定义端口 #添加selector:k8s-app: kubernetes-dashboard
执行安装
kubectl apply -f recommended.yaml
访问https://hostname:自定义端口
会出现Token验证登录界面如下,我的已经部署好了,提示的是token登录过期了
获取token
创建一个ServiceAccount :dashboard-admin
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard
将dashboard-admin 绑定到集群管理角色
kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin
查看dashboard-admin的登陆Token
kubectl get secret -n kubernetes-dashboard
kubectl describe secret dashboard-admin-token-4dppv -n kubernetes-dashboard
过程命令如下
root@uu22:/home/uu# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master-node NotReady control-plane,master 48m v1.23.6
root@uu22:/home/uu# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
namespace/kube-flannel created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
root@uu22:/home/uu# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master-node NotReady control-plane,master 49m v1.23.6
root@uu22:/home/uu# kubectl apply -f recommended.yaml
namespace/kubernetes-dashboard unchanged
serviceaccount/kubernetes-dashboard unchanged
service/kubernetes-dashboard unchanged
secret/kubernetes-dashboard-certs unchanged
secret/kubernetes-dashboard-csrf unchanged
secret/kubernetes-dashboard-key-holder unchanged
configmap/kubernetes-dashboard-settings unchanged
role.rbac.authorization.k8s.io/kubernetes-dashboard unchanged
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard unchanged
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard unchanged
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard unchanged
deployment.apps/kubernetes-dashboard unchanged
service/dashboard-metrics-scraper unchanged
deployment.apps/dashboard-metrics-scraper unchanged
root@uu22:/home/uu# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master-node Ready control-plane,master 51m v1.23.6
root@uu22:/home/uu# kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard
serviceaccount/dashboard-admin created
root@uu22:/home/uu# kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin
clusterrolebinding.rbac.authorization.k8s.io/dashboard-cluster-admin created
root@uu22:/home/uu# kubectl get secret -n kubernetes-dashboard
NAME TYPE DATA AGE
dashboard-admin-token-4dppv kubernetes.io/service-account-token 3 34s
default-token-tcg2k kubernetes.io/service-account-token 3 28m
kubernetes-dashboard-certs Opaque 0 27m
kubernetes-dashboard-csrf Opaque 1 27m
kubernetes-dashboard-key-holder Opaque 2 27m
kubernetes-dashboard-token-tww82 kubernetes.io/service-account-token 3 28m
root@uu22:/home/uu# kubectl describe secret dashboard-admin-token-4dppv -n kubernetes-dashboard
Name: dashboard-admin-token-4dppv
Namespace: kubernetes-dashboard
Labels:
Annotations: kubernetes.io/service-account.name: dashboard-adminkubernetes.io/service-account.uid: ec24e0cc-0d12-4d0e-a846-6b476eb7c6ffType: kubernetes.io/service-account-tokenData
====
ca.crt: 1099 bytes
namespace: 20 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IlVDcjJBM25wbmZTVjJPN3N4elZ5eWZXODJpQVE4MUtaZ1Z3dGhjdVh2SDQifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tNGRwcHYiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiZWMyNGUwY2MtMGQxMi00ZDBlLWE4NDYtNmI0NzZlYjdjNmZmIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmVybmV0ZXMtZGFzaGJvYXJkOmRhc2hib2FyZC1hZG1pbiJ9.mPFBGqEtxZLUEJ14J8lVacXRk5bfmQ4AbFyJeOt3JHh_ehiYbZfjohPRQc5ZGB4Jmy4MByAQG9sG04_HvCYPGVYSF8e88J8JjHvOC_Ff7AM1dCYmHmCskgWyBnXwdQiW5HjBk-YW_zspvA7V5pEU7BgLAuy82NVZvcLsi-N4apOxM34tFXmXnQkQeeDk5Eq9Yx7seyS6ieWxthJcHJdkFHm9Vl69qL0A8SLxmjPnHyssabAnRor9AJqtHwTEzQONJQyOkGpVJfrfE8TvpI-BxDVFvs2Ox4FXKsukbfSZJGt_6qiT1zjr-ACMKFkzXM0kXt3LApeLcnHE3Lcu8w1c-g
得到token就可以登陆了,我的已经部署了nginx了
部署Nginx
#创建pod
kubectl create deploy nginx --image=nginx
查看docker镜像
docker images
查看pod
kubectl get pods
编写nginx-service.yaml文件
kind: Service
apiVersion: v1
metadata:name: nginx-service
spec:type: NodePortselector:app: nginxports:- nodePort: 30164 #对外暴露的端口port: 80targetPort: 80
启动service
kubectl create -f nginx-service.yaml
访问主机ip:30164
打开K8s控制台可以看到
可以建多个services,负载pod
参考链接:
https://blog.csdn.net/weixin_39589455/article/details/128556366
https://blog.csdn.net/weixin_47752736/article/details/125052165
https://blog.csdn.net/weixin_39547392/article/details/112398620
https://www.cnblogs.com/gaoyuechen/p/8685771.html
https://blog.csdn.net/m0_48638643/article/details/127344538