k8s是现在最热门的技术之一。常混在技术圈的你,是否有种蠢蠢欲动的感觉,却因k8s环境的安装复杂性,却放弃。本篇文章将对k8s的安装进行展开。
k8s的安装最大的障碍是网络问题,为了减少不必要的折腾,就直接选取境外境外节点的服务器。以腾讯云CVM为例。
安装以上配置开两台云主机,进行实验。
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
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
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
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
kubectl get nodes
# 查看集群信息
kubectl cluster-info
# 查看所有的 Pod 命令
kubectl get pods --all-namespaces
# 部署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
# 安装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环境的安装
注意,实验完成后,为了后续的继续学习,建议将主机打为镜像,将云主机释放,在下次开始实验时,以自己构建的镜像作为模版来创建主机。祝你,操作实验顺利。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。