ubuntu 18.04下使用kubeadm安装部署k8s
测试环境:ubuntu18.04
部署目标:部署一个k8s master节点+一个k8s slave节点
名称 | 内网IP | 公网IP |
---|---|---|
k8s-master | 172.24.151.213 | 47.113.227.49 |
k8s-slave | 172.24.151.214 | 47.113.185.4 |
安装步骤
准备环境
x
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
关闭swap
x
swapoff -a
永久关闭swap,编辑
/etc/fstab
,删除对应行。安装docker
x
# (Install Docker CE)
## Set up the repository:
### Install packages to allow apt to use a repository over HTTPS
apt-get update && apt-get install -y \
apt-transport-https ca-certificates curl software-properties-common gnupg2
x
# Add Docker’s official GPG key:
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -
x
# Add the Docker apt repository:
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
x
apt-get update && apt-get install -y \
containerd.io=1.2.13-2 \
docker-ce=5:19.03.11~3-0~ubuntu-$(lsb_release -cs) \
docker-ce-cli=5:19.03.11~3-0~ubuntu-$(lsb_release -cs)
x
# Set up the Docker daemon
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"registry-mirrors": ["https://qpb5oc44.mirror.aliyuncs.com"]
}
EOF
x
mkdir -p /etc/systemd/system/docker.service.d
x
# Restart Docker
systemctl daemon-reload
systemctl restart docker
添加自启动
x
sudo systemctl enable docker
安装kubeadm,kubelet,kubectl
x
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
初始化master节点(在master机器执行)
x
kubeadm init \
--apiserver-advertise-address=172.24.151.213 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.18.3 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
如果init失败,可以先kubeadm reset,然后删除所有image,再执行init。
给当前用户kubectl权限:
x
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
此时使用
kubectl get node
命令查看节点状态,发现还处于NotReady
状态。这是因为没安装网络插件。安装pod网络插件(在master机器执行)
x
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.12.0/Documentation/kube-flannel.yml
如果安装慢,可以考虑将kube-flannel.yml下载到本地,替换里面的镜像地址quay.io为国内源quay.mirrors.ustc.edu.cn。然后再执行kubectl apply -f kube-flannel.yml。
通过命令
kubectl get pods -A
查看当前运行的系统pods。此时再通过
kubectl get node
命令查看节点状态,发现还处于ready状态。加入node节点(在slave机器执行)
x
kubeadm join 172.24.151.213:6443 --token zuxz7o.by58pol47lm5r802 \
--discovery-token-ca-cert-hash sha256:150578713a0d7116867cf62fb648c039d42d3b9ac9ba0e8e6201d89967fd98c0
测试集群
x
$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
查看服务状态,得到k8s给nginx分配的端口
xxxxxxxxxx
$ kubectl get pod,svc
通过任一节点ip,加上上面得到的端口,即可访问到nginx的主页。
部署dashboard
下载yaml文件
x
$ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml -O kubernetes-dashboard.yaml
修改镜像地址,使之能够国内网络访问
x
image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
改为
image: docker.io/mirrorgooglecontainers/kubernetes-dashboard-amd64:v1.10.1
修改service,指定类型
x
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
执行yaml文件
x
$ kubectl apply -f kubernetes-dashboard.yaml
通过https://nodeip:nodeport即可访问到界面。
生成dashboard token
x
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin
kubectl describe secrets -n kubernetes-dashboard $(kubectl -n kubernetes-dashboard get secret | awk '/dashboard-admin/{print $1}')
问题解决
如何删除pod
x
kubectl delete -f kubernetes-dashboard.yaml
init失败后,如何重新init
x
kubeadm reset
使用
docker images
查看是否有id相同的image,如果存在,则也需要把这些image删除。dashboard因为证书问题打不开的问题
创建自签名证书
x
sudo su -
touch /root/.rnd
mkdir -p /data/tls && cd /data/tls
openssl genrsa -out ca.key 2048
openssl req -new -x509 -key ca.key -out ca.crt -days 3650 -subj "/C=CN/ST=HB/L=WH/O=DM/OU=YPT/CN=CA"
//生成私钥
openssl genrsa -out dashboard.key 2048
申请签名请求
x
# ip为dashaboard访问地址ip
export ip=172.24.151.213
openssl req -new -sha256 -key dashboard.key -out dashboard.csr -subj "/C=CN/ST=HB/L=WH/O=DM/OU=YPT/CN=$ip"
cat > dashboard.cnf <<EOF
extensions = san
[san]
keyUsage = digitalSignature
extendedKeyUsage = clientAuth,serverAuth
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
subjectAltName = IP:$ip,IP:127.0.0.1,DNS:$ip,DNS:localhost
EOF
签发证书
x
openssl x509 -req -sha256 -days 3650 -in dashboard.csr -out dashboard.crt -CA ca.crt -CAkey ca.key -CAcreateserial -extfile dashboard.cnf
删除旧kubernetes-dashboard
xxxxxxxxxx
kubectl delete -f kubernetes-dashboard.yaml
修改kubernetes-dashboard.yaml 文件,注释以下行
x
# apiVersion: v1
# kind: Secret
# metadata:
# labels:
# k8s-app: kubernetes-dashboard
# name: kubernetes-dashboard-certs
# namespace: kubernetes-dashboard
# type: Opaque
创建 secret kubernetes-dashboard-certs
xxxxxxxxxx
kubectl create secret generic kubernetes-dashboard-certs --from-file="/data/tls/dashboard.crt,/data/tls/dashboard.key" -n kubernetes-dashboard
重新部署dashboard
x
kubectl create -f kubernetes-dashboard.yaml