Docker安装

1.添加阿里云yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2.查看可用版本
yum list docker-ce --showduplicates | sort 
3.安装docker18.09.9
yum -y install docker-ce-18.09.9-3.el7 docker-ce-cli-18.09.9

或者 yum -y install docker-ce-18.09.6 docker-ce-cli-18.09.6 containerd.io
4. 启动docker并设置开机自启
systemctl enable docker && systemctl start docker

5.配置阿里云docker镜像加速 修改Cgroup Driver
cat > /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://gqk8w9va.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
6.配置完后重启docker
systemctl restart docker
7. 验证
docker info
docker run hello-world

二、k8s安装准备工作

1.1 修改主机名
hostnamectl set-hostname master
more /etc/hostname   
1.2 修改hosts文件
cat >> /etc/hosts << EOF
172.27.9.131    master
172.27.9.135    node01
172.27.9.136    node02
EOF
3. 禁用swap
查看swap是否关闭
free -m

total        used        free      shared  buff/cache   available
Mem:           1994         682         612           9         699        1086
Swap:             0           0           0
3.1 临时禁用
 swapoff -a
3.2 永久禁用

//修改fstab文件,注释swap自动挂载
sed -i '/^\/dev\/mapper\/centos-swap/c#/dev/mapper/centos-swap swap  swap  defaults 0 0' /etc/fstab

4. 内核参数修改
4.1 临时修改
sysctl net.bridge.bridge-nf-call-iptables=1
sysctl net.bridge.bridge-nf-call-ip6tables=1
4.2 永久修改
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl -p /etc/sysctl.d/k8s.conf

安装Kubeadm

需要科学上网
cat >/etc/yum.repos.d/kubernetes.repo<<EOF
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
        https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
使用阿里云yum源
cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[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
更新缓存(不是必须)
yum clean all

yum -y makecache
版本查看
 yum list kubelet --showduplicates | sort -r 
安装kubelet、kubeadm和kubectl
yum -y install kubelet-1.16.9 kubeadm-1.16.9 kubectl-1.16.9
设置kubelet开机自启
systemctl enable kubelet
设置k8s命令自动补全
yum -y install bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
下载镜像
image.sh 

#!/bin/bash
url=registry.cn-hangzhou.aliyuncs.com/google_containers
version=v1.16.15
images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`)
for imagename in ${images[@]} ; do
  docker pull $url/$imagename
  docker tag $url/$imagename k8s.gcr.io/$imagename
  docker rmi -f $url/$imagename
done

初始化集群

master节点操作,配置 kubeadm 初始化文件
 kubeadm init --apiserver-advertise-address 172.21.0.8 --pod-network-cidr=10.244.0.0/16
初始化master
kubeadm init --config=kubeadm-config.yaml --upload-certs
拷贝 kubeconfig 文件
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
master添加节点

node1和node2相同操作

1.创建目录,这里的路径为/root
mkdir -p $HOME/.kube 

2.把master节点上的config文件拷贝到node1和node2的$HOME/.kube
scp master:~/.kube/config $HOME/.kube

3.修改权限
chown $(id -u):$(id -g) $HOME/.kube/config
将node1和node2加入到集群中
mater 创建成功 后复制 

kubeadm join 172.27.9.131:6443 --token px979r.mphk9ee5ya8fgy44     --discovery-token-ca-cert-hash sha256:5e7c7cd1cc1f86c0761e54b9380de22968b6b221cb98939c14ab2942223f6f51 
 
安装pod网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
查看
kubectl get pods -n kube-system
安装Dashboard(可选)
//下载文件  v2.0.0-rc3是中文版本,beta8是英文版本
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml

wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc3/aio/deploy/recommended.yaml  
  
//修改Service为NodePort类型
42行下增加一行
nodePort: 30001
  
44行下增加一行
type: NodePort
  

//原先内容
spec:
  ports:
    - port: 443
      targetPort: 8443
  selector:
    k8s-app: kubernetes-dashboard


//修改后内容
spec:
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001   #增加,指定nodeport端口
  selector:
    k8s-app: kubernetes-dashboard
  type: NodePort		#增加,修改类型为nodeport
Logo

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

更多推荐