k8s 是现在最热门的技术之一. 常混在技术圈的你, 是否有种蠢蠢欲动的感觉, 却因 k8s 环境的安装复杂性, 却放弃. 本篇文章将对 k8s 的安装进行展开.
1. 云主机环境
k8s 的安装最大的障碍是网络问题, 为了减少不必要的折腾, 就直接选取境外境外节点的服务器. 以腾讯云 CVM 为例.
安装以上配置开两台云主机, 进行实验.
2. 基础设置
SSH 登录后, 对主机进行一些基础设置
1. 设置主机名
- hostnamectl set-hostname master
- hostnamectl set-hostname node01
2. 设置 prompt
- $ vi .bashrc
- host_name="k8s 安装"
- # Color list
- green=$'\e[1;32m'
- red=$'\e[1;31m'
- blue=$'\e[1;34m'
- cyan=$'\e[1;36m':
- reset_color=$'\e[m'
- PS1='\[${green}\][\u]@\[${blue}\][\h]\[${cyan}\]${host_name} ->\[${reset_color}\] \W\$'
3. host 修改
- VIM /etc/hosts
- 172.31.187.45 master
- 172.31.187.44 node01
4. SSH 免登
- SSH-keygen
- SSH-copy-id node01/master
5. 关闭防火墙
- systemctl stop firewalld
- systemctl disable firewalld
6. 禁用 SELINUX
- setenforce 0
- sed -i -re '/^\s*SELINUX=/s/^/#/' -e '$i\\SELINUX=disabled' /etc/selinux/config
7. 关闭 swap
- swapoff -a
- sed -i 's/.*swap.*/#&/' /etc/fstab
8. ipv6 设置
- cat <<EOF> /etc/sysctl.d/k8s.conf
- net.bridge.bridge-nf-call-ip6tables = 1
- net.bridge.bridge-nf-call-iptables = 1
- EOF
- sysctl --system
3. 安装 docker
- yum install -y yum-utils \
- device-mapper-persistent-data \
- lvm2
- yum-config-manager \
- --add-repo \
- https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
- yum makecache fast
- yum install docker-ce
- systemctl start docker.service
- systemctl enable docker.service
4. kubernetes 安装
1. 添加 kubernetes 源
- 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
2. 安装 kubeadm,kubelet,kubectl
- yum install -y kubelet kubeadm kubectl
- systemctl enable kubelet
- systemctl start kubelet
5. 集群安装
1. 初始化集群
kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=172.31.187.45
2. node 节点加入集群
kubeadm join 172.31.187.45:6443 --token 8xvtce.sjvzzaqkg9dp7375 --discovery-token-ca-cert-hash sha256:03c4632898741c6e0e8d3455dfe9781cf083848c8a16e73fb72690974aa34f59
3. 设置 KUBECONFIG
- cp /etc/kubernetes/admin.conf $HOME/
- chown $(id -u):$(id -g) $HOME/admin.conf
- export KUBECONFIG=$HOME/admin.conf
4. flannel 网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
6. 查看集群信息
- kubectl get nodes
- # 查看集群信息
- kubectl cluster-info
- # 查看所有的 Pod 命令
- kubectl get pods --all-namespaces
7. 测试
- # 部署 hello-world
- kubectl run hello-world --replicas=2 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0 --port=8080
- # 集群信息
- kubectl get deployments hello-world
- kubectl describe deployments hello-world
- kubectl get replicasets
- kubectl describe replicasets
- # 创建 service
- kubectl expose deployment hello-world --type=NodePort --name=example-service
- # 查看 service
- kubectl describe services example-service
- kubectl get pods --selector="run=load-balancer-example" --output=wide
8. 安装 dashboard
- # 安装 dashboard
- kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
- # 删除 dashboard
- kubectl -n kube-system delete $(kubectl -n kube-system get pod -o name | grep dashboard)
以上完成了 k8s 环境的安装
注意, 实验完成后, 为了后续的继续学习, 建议将主机打为镜像, 将云主机释放, 在下次开始实验时, 以自己构建的镜像作为模版来创建主机. 祝你, 操作实验顺利.
来源: https://www.qcloud.com/developer/article/1363364