实验环境

IP备注系统
192.168.1.10mastercentos7
192.168.1.20node1centos7
192.168.1.30node2centos7

版本信息

  • docker 18.09.0
  • k8s v1.20.2

更改主机名

master

[root@localhost ~]# hostname master
[root@localhost ~]# bash
[root@master ~]#

node1

[root@localhost ~]# hostname node1
[root@localhost ~]# bash
[root@node1 ~]#

node2

[root@localhost ~]# hostname node2
[root@localhost ~]# bash
[root@node2 ~]#

免密登录

master

[root@master ~]# vi /etc/hosts
#末尾添加
192.168.1.10 master
192.168.1.20 node1
192.168.1.30 node2

[root@master ~]# ssh-keygen  #一直回车
[root@master ~]# ssh-copy-id -i master
[root@master ~]# ssh-copy-id -i node1
[root@master ~]# ssh-copy-id -i node2

#将hosts文件传给node1和node2
[root@master ~]# scp /etc/hosts node1:/etc/hosts
hosts                                        100%  217    15.2KB/s   00:00    
[root@master ~]# scp /etc/hosts node2:/etc/hosts
hosts                                        100%  217    29.7KB/s   00:00  

更新并安装依赖

yum -y update
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp

安装docker

在每一台机器上都安装好Docker,版本为18.09.0

01 安装必要的依赖
	sudo yum install -y yum-utils device-mapper-persistent-data lvm2
 
02 设置docker仓库
	sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
	
【设置要设置一下阿里云镜像加速器】
登录阿里云 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors 容器镜像服务中查看,参见下图
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
 "registry-mirrors": ["https://******.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload


03 安装docker

yum install -y docker-ce-18.09.0 docker-ce-cli-18.09.0 containerd.io


04 启动docker
	sudo systemctl start docker && sudo systemctl enable docker

在这里插入图片描述

系统基础前提配置

# (1)关闭防火墙
systemctl stop firewalld && systemctl disable firewalld

# (2)关闭selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

# (3)关闭swap
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

# (4)配置iptables的ACCEPT规则
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT

# (5)设置系统参数
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system

Installing kubeadm, kubelet and kubectl

(1)配置yum源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
       http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

(2)安装kubeadm&kubelet&kubectl

yum install -y kubeadm kubelet kubectl -y

(3)docker和k8s设置同一个cgroup

# docker
vi /etc/docker/daemon.json
    "exec-opts": ["native.cgroupdriver=systemd"],
    
systemctl restart docker
    
# kubelet,这边如果发现输出directory not exist,也说明是没问题的,大家继续往下进行即可
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
	
systemctl enable kubelet && systemctl start kubelet

kube init初始化master

# 此操作只在master节点执行 查看日志,记录join信息
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.20.2 --apiserver-advertise-address 192.168.1.10 --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

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.1.10:6443 --token 1ha7wh.gs6i1z30tpmdm3or \
    --discovery-token-ca-cert-hash sha256:06324a09f5e51f4a012f24041d94e73385fe0ac1a2e98cc114005cdc66c9cae2 
·····
【若要重新初始化集群状态:kubeadm reset,然后再进行上述操作】

记得保存好最后kubeadm join的信息

(3)根据日志提示

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

部署calico网络插件

选择网络插件:https://kubernetes.io/docs/concepts/cluster-administration/addons/
calico网络插件:https://docs.projectcalico.org/v3.9/getting-started/kubernetes/

calico,同样在master节点上操作

# 在k8s中安装calico
kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml

# 确认一下calico是否安装成功
kubectl get pods --all-namespaces -w

在这里插入图片描述

kube join

记得保存初始化master节点的最后打印信息
(1)node1和node2执行

kubeadm join 192.168.1.10:6443 --token 1ha7wh.gs6i1z30tpmdm3or \
    --discovery-token-ca-cert-hash sha256:06324a09f5e51f4a012f24041d94e73385fe0ac1a2e98cc114005cdc66c9cae2

(2)master节点检查集群信息

[root@master ~]# kubectl get nodes
NAME     STATUS   ROLES                  AGE     VERSION
master   Ready    control-plane,master   5m35s   v1.20.2
node1    Ready    <none>                 64s     v1.20.2
node2    Ready    <none>                 61s     v1.20.2

创建pod测试

cat > pod_nginx_rs.yaml <<EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
  labels:
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      name: nginx
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
EOF

部署yaml文件

kubectl apply -f pod_nginx_rs.yaml

查看pod

kubectl get pods
kubectl get pods -o wide
kubectl describe pod nginx

通过rs将pod扩容

kubectl scale rs nginx --replicas=5
kubectl get pods -o wide

删除pod

kubectl delete -f pod_nginx_rs.yaml
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐