1.什么是 Kubernetes #

2. 基础安装 #

2.1 安装些必备组件 #

yum install vim wget ntpdate -y

2.2 关闭防火墙 #

systemctl stop firewalld & systemctl disable firewalld

2.3 关闭 Swap 分区 #

#临时关闭
swapoff -a

2.4 关闭 Selinux #

# 暂时关闭 selinux
setenforce 0

# 永久关闭
vi /etc/sysconfig/selinux
# 修改以下参数,设置为disable
SELINUX=disabled

2.5 统一我们的系统时间和时区 #

# 统一时区,为上海时区
ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
bash -c "echo 'Asia/Shanghai' > /etc/timezone"

# 统一使用阿里服务器进行时间更新
ntpdate ntp1.aliyun.com

2.6 安装 Docker #

yum install -y yum-utils device-mapper-persistent-data lvm2


sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker
systemctl enable docker

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://fwvjnv59.mirror.aliyuncs.com"]
}
EOF

sudo systemctl daemon-reload
sudo systemctl restart docker.service

2.7 安装 Kubernetes 组件 #

2.7.1 切换阿里云源 #

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.7.2 安装 Kubernetes 组件 #

yum install -y kubelet kubeadm kubectl
# 启动kubelet
systemctl enable kubelet && systemctl start kubelet

2.8 设置bridge-nf-call-iptables #

echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables

3. Master #

3.1 修改主机名称为 master #

hostnamectl set-hostname  master

3.2 配置hosts #

ip addr
vim /etc/hosts

172.31.178.169  master  master

3.3 配置 Kubernetes 初始化文件 #

kubeadm config print init-defaults > init-kubeadm.conf
vim init-kubeadm.conf
- imageRepository: k8s.gcr.io 更换k8s镜像仓库
+ imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
- localAPIEndpointc,advertiseAddress为master ip ,port默认不修改
localAPIEndpoint:
+ advertiseAddress: 172.31.178.169  # 此处为master的IP
  bindPort: 6443
# 配置子网络
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
+ podSubnet: 10.244.0.0/16    # 添加这个

3.3 拉取其它组件 #

c844dfc8104d55a90f18d4dc0a57d5c4

// 查看缺少的组件
kubeadm config images list --config init-kubeadm.conf
// 拉取缺少的组件
kubeadm config images pull --config init-kubeadm.conf

3.4 初始化 Kubernetes #

kubeadm init --config init-kubeadm.conf
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 172.31.178.169:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:8aac19f4dbe68f1e15ba3d80e141acdc912e353f9757ad69187e8fb9780bc975 

3.5 安装 Flannel #

#wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
wget https://img.zhufengpeixun.com/kube-flannel.yml
docker pull quay.io/coreos/flannel:v0.13.0-rc2
kubectl apply -f kube-flannel.yml
net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }

3.6 查看启动情况 #

kubectl get nodes

NAME     STATUS   ROLES                  AGE     VERSION
master   Ready    control-plane,master   9m34s   v1.20.4

3.7 Node节点配置 #

hostnamectl set-hostname node1

3.8 拷贝 Master 节点配置文件 #

scp $HOME/.kube/config root@172.31.178.170:~/
mkdir -p $HOME/.kube
sudo mv $HOME/config $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

3.9 加入 Master 节点 #

kubeadm join 172.16.81.164:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:b4a059eeffa2e52f2eea7a5d592be10c994c7715c17bda57bbc3757d4f13903d

3.10. 安装 Flannel #

scp ~/kube-flannel.yml root@172.31.178.170:~/
kubectl apply -f kube-flannel.yml

4. 查看状态 #

kubectl get nodes
NAME     STATUS   ROLES                  AGE    VERSION
master   Ready    control-plane,master   24m    v1.20.4
node1    Ready    <none>                 101s   v1.20.4

5.直接布署nginx #

kubectl create deployment nginx --image=nginx
[root@master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
kubectl get pod,svc
NAME                            READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-bt5n6      1/1     Running   0          5m32s

curl 127.0.0.1:32636
//快速扩容为3个副本
[root@master ~]# kubectl scale deployment nginx --replicas=3
deployment.apps/nginx scaled

6.通过yaml布署mysql #

6.1 配置文件 #

apiVersion: v1
kind: ReplicationController                           
metadata:
  name: mysql                                          
spec:
  replicas: 1           #Pod副本的期待数量
  selector:
    app: mysql          #符合目标的Pod拥有此标签
  template:             #根据此模板创建Pod的副本(实例)
    metadata:
      labels:
        app: mysql     #Pod副本拥有的标签,对应RC的Selector
    spec:
      containers:      #Pod内容器的定义部分
      - name: mysql            #容器的名称
        image: mysql    #容器对应的Docker image
        ports: 
        - containerPort: 3306       #容器应用监听的端口号
        env:                        #注入容器内的环境变量
        - name: MYSQL_ROOT_PASSWORD 
          value: "123456"

6.2 创建POD #

kubectl create -f mysql-rc.yaml
replicationcontroller/mysql created

kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
mysql         1/1     Running      0      5m56s

6.3 查看状态 #

kubectl describe pod mysql