kubeadm部署kubenetes单机集群

52 阅读4分钟

kubeadm部署kubenetes单机集群

主、从节点都部署 kubelet kubeadm kubectl和docker-ce-19.03.13 ,但只有主节点部署初始化控制平面

环境准备:

#yum -y install chrony ;systemctl  enable  --now chronyd
#swapoff -a   #临时取消
#vim /etc/fstab  #注释swap分区,永久取消
​
getenforce  #查看selinux状态
systemctl disable --now firewall
setenforce 0

主节点:

#swapoff -a
#vim /etc/fstab #域名解析
[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.28 master01.magedu.com master01 kubeapi.magedu.com
10.0.0.48 node1.magedu.com node1 kubeapi.magedu.com
10.0.0.58 node2.magedu.com node2 kubeapi.magedu.com
​
 #安装kubelet,kubeadm,kubectl
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
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 kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet
​
 #安装docker

[root@centos8 ~]# cat C8_docker.sh 

. ./etc/init.d/functions
COLOR="echo -e \033[1;32m"
GREEN="echo  -e \033[1;31m"
VERSION="-19.03.13"
END="\E[0m"
​
install_docker () {
${GREEN}"开始安装docker..."${END}
sleep 1
cat > /etc/yum.repos.d/docker.repo  <<EOF
[docker]
name=docker
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck=0
EOF
yum clean all
yum -y install docker-ce$VERSION  docker-ce-cli$VERSION  || { ${COLOR}"yum源失败,请检查yum源配置"{END};exit; }#镜像加速
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://si7y70hh.mirror.aliyuncs.com"]
}
EOF
systemctl enable --now docker
docker version && ${GREEN}"Docker安装成功"${END}
​
}
​
rpm -q docker-ce &> /dev/null  && action "Docker已经安装" || install_docker


 #配置Kubenetes
[root@master ~]# echo "net.bridge.bridge-nf-call-iptables = 1" > /etc/sysctl.d/k8s.conf
[root@node01 ~]# sysctl -p /etc/sysctl.d/k8s.conf
[root@centos8  ~]#kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.19.4 --control-plane-endpoint kubeapi.magedu.com --apiserver-advertise-address 10.0.0.28 --pod-network-cidr 10.244.0.0/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/
​
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
​
  kubeadm join kubeapi.magedu.com:6443 --token fisn72.pr2rbdru34jyvi77 \
    --discovery-token-ca-cert-hash sha256:570010d6e6f361412fa2768c8967bf8eb3cc918cb89765cb56b7f8b563fda6ec \
    --control-plane 
​
Then you can join any number of worker nodes by running the following on each as root:
​
kubeadm join kubeapi.magedu.com:6443 --token fisn72.pr2rbdru34jyvi77 \
    --discovery-token-ca-cert-hash sha256:570010d6e6f361412fa2768c8967bf8eb3cc918cb89765cb56b7f8b563fda6ec 
[root@centos8  ~]#scp /etc/hosts 10.0.0.48:/etc/hosts
root@10.0.0.48's password: 
hosts                                   100%  321     7.7KB/s   00:00  
​
[root@centos8  ~]#mkdir -p $HOME/.kube
[root@centos8  ~]#cp /etc/kubernetes/admin.conf  .kube/config [root@centos8  ~]#kubectl get nodes
NAME         STATUS   ROLES    AGE    VERSION
centos8.3m   Ready    master   107s   v1.19.4
​
 #配置flannel
[root@centos8  ~]#wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml 
[root@centos8  ~]# kubectl apply -f  kube-flannel.yml [root@master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

podsecuritypolicy.policy/psp.flannel.unprivileged 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

 #将worker节点加入worker集群,分别执行
kubeadm join kubeapi.magedu.com:6443 --token fisn72.pr2rbdru34jyvi77 \
    --discovery-token-ca-cert-hash sha256:570010d6e6f361412fa2768c8967bf8eb3cc918cb89765cb56b7f8b563fda6ec 

 
  #列出所有节点
[root@centos8  ~]# kubectl get nodes -o wide
NAME                    STATUS   ROLES    AGE     VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
centos8                 Ready    <none>   2m46s   v1.19.4   10.0.0.58     <none>        CentOS Linux 8 (Core)   4.18.0-147.el8.x86_64   docker://19.3.13
centos8.3m              Ready    master   6m24s   v1.19.4   10.0.0.28     <none>        CentOS Linux 8 (Core)   4.18.0-147.el8.x86_64   docker://19.3.13
localhost.localdomain   Ready    <none>   3m50s   v1.19.4   10.0.0.48     <none>        CentOS Linux 8 (Core)   4.18.0-147.el8.x86_64   docker://19.3.13
​
#打印服务器上支持的API资源
[root@master ~]# kubectl api-resources#查看名称空间
[root@master ~]# kubectl get namespaces#创建部署镜像
[root@centos8  ~]#kubectl create deployment demoapp --image="ikubernetes/demoapp:v1.0"
deployment.apps/demoapp created
[root@centos8  ~]#kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
demoapp-59bb775755-szxz4   1/1     Running   0          23s
​
 #创建多个副本
[root@centos8  ~]#kubectl scale deployment demoapp --replicas=3 
deployment.apps/demoapp scaled
​
[root@centos8  ~]#kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
demoapp-59bb775755-28ztg   1/1     Running   0          18s
demoapp-59bb775755-62wwn   1/1     Running   0          18s
demoapp-59bb775755-szxz4   1/1     Running   0          5m9s
​
 #删除一个容器,会自动创建另一个容器
[root@master ~]# kubectl delete pods demoapp-5f7d8f9847-mh52l
​
​
 #指定clusterIP创建demoapp
[root@centos8  ~]#kubectl create service clusterip demoapp --tcp=80:80
service/demoapp created
[root@centos8  ~]#kubectl get service 
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
demoapp      ClusterIP   10.98.246.101   <none>        80/TCP    26s
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   13m
​
#利用clusterip访问
[root@centos8  ~]#curl  10.98.246.101
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: demoapp-59bb775755-62wwn, ServerIP: 10.244.1.3!
[root@centos8  ~]#curl  10.98.246.101
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: demoapp-59bb775755-28ztg, ServerIP: 10.244.2.2!
[root@centos8  ~]#curl  10.98.246.101
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: demoapp-59bb775755-62wwn, ServerIP: 10.244.1.3!
​
 #删除服务
[root@centos8  ~]#kubectl delete service demoapp
service "demoapp" deleted
​
​
 #使用指定的名称创建NodePort服务
 #指定nodeport创建demoapp
[root@centos8  ~]#kubectl create service nodeport  demoapp --tcp=80:80 --node-port=30003
service/demoapp created
[root@centos8  ~]#kubectl get service 
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
demoapp      NodePort    10.99.128.62   <none>        80:30003/TCP   5s
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        16m
​
 #利用nodeport访问
[root@centos8  ~]#curl 10.0.0.28:30003
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: demoapp-59bb775755-62wwn, ServerIP: 10.244.1.3!
[root@centos8  ~]#curl 10.0.0.28:30003
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: demoapp-59bb775755-28ztg, ServerIP: 10.244.2.2!
[root@centos8  ~]#curl 10.0.0.28:30003
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: demoapp-59bb775755-62wwn, ServerIP: 10.244.1.3!
[root@centos8  ~]#curl 10.0.0.28:30003
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: demoapp-59bb775755-28ztg, ServerIP: 10.244.2.2!
​
​
 #kubectl describe:显示特定资源或资源组的详细信息
[root@master ~]# kubectl describe service demoapp

 

从节点:

#swapoff -a
#vim /etc/fstab 
[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.28 master01.magedu.com master01 kubeapi.magedu.com
10.0.0.48 node1.magedu.com node1 kubeapi.magedu.com
10.0.0.58 node2.magedu.com node2 kubeapi.magedu.com
​
#setenforce 0
#yum install -y kubelet kubeadm kubectl;systemctl enable kubelet && systemctl start kubelet#anzhuang安装docker
​
加入节点
# kubeadm join kubeapi.magedu.com:6443 --token fisn72.pr2rbdru34jyvi77     --discovery-token-ca-cert-hash sha256:570010d6e6f361412fa2768c8967bf8eb3cc918cb89765cb56b7f8b563fda6ec
​
[root@localhost ~]# docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED
registry.aliyuncs.com/google_containers/kube-proxy   v1.19.4             635b36f4d89f        2 weeks
registry.aliyuncs.com/google_containers/pause        3.2                 80d28bedfe5d        9 mon
​
#10.0.0.58同样操作