作业4:CentOS7下部署Kubernetes集群
题量: 1 满分: 100.0
作答时间:06-01 05:25至06-14 05:26
修改主机名
hostnamectl set-hostname k8s_401_master
hostnamectl set-hostname k8s_401_node_01
hostnamectl set-hostname k8s_401_node_02
验证是否修改成功
cat /etc/hostname
查看IP
修改3个节点的映射
vi /etc/hosts
直接在文件末尾追加(不可以加下划线)
84.84.1.4 k8s_401_master
84.84.1.5 k8s_401_node01
84.84.1.6 k8s_401_node02
关闭3个节点防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl restart network
永久禁用3个节点的swap
sed -i.bak '/swap/s/^/#/' /etc/fstab
mount -a
关闭三个节点的SELinux,执行如下命令
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
将桥接的IPV4流量传递到iptables 的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
在全部节点上安装Docker
1.更新yum
yum update
全输入y,回车
2.安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
3.设置Docker源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4.安装dockers CE
4.1.查看docker安装版本
yum list docker-ce --showduplicates | sort -r
4.2安装docker CE
yum install docker-ce-18.09.6 docker-ce-cli-18.09.6 containerd.io
输入y
yum install -y docker-ce docker-ce-cli containerd.io
5.启动docker并设置开机启动
systemctl start docker
systemctl enable docker
6.查看docker版本
docker version
7.命令补全
7.1安装bash-completion
yum -y install bash-completion
7.2加载bash-completion
source /etc/profile.d/bash_completion.sh
登录阿里云https://account.aliyun.com/login/m_login.htm?oauth_callback=https%3A%2F%2Fwww.aliyun.com%2F
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://68l891h8.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
修改为自己的
重启服务
systemctl daemon-reload
systemctl restart docker
systemctl status docker.service
设置全部节点的k8s的yum软件源为阿里云
cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[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
EOF
更新缓存
yum clean all
yum -y makecache
在全部节点上安装Kubeadm Kubelet Kubectl
yum -y remove kubelet
yum -y remove kubeadm
yum -y remove kubectl
yum install -y kubelet-1.14.2 --nogpgcheck
yum install -y kubectl-1.14.2 --nogpgcheck
yum install -y kubeadm-1.14.2 --nogpgcheck
systemctl enable kubelet
kubeadm reset
启动Kunelet并设置开机启动
systemctl enable kubelet && systemctl start kubelet
命令补全
echo "source <(kubectl completion bash)" >>~/.bash_profile
source .bash_profile
k8s-master节点的安装
vi image.sh
#!/bin/bash
url=registry.cn-hangzhou.aliyuncs.com/google_containers
version=v1.14.2
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
chmod u+x image.sh
./image.sh
初始化
kubeadm init --apiserver-advertise-address 84.84.1.4 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
把84.84.1.4改为自己的ip地址
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source .bash_profile
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
wget https://docs.projectcalico.org/v3.10/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml --no-check-certificate
kubectl apply -f calico.yaml
删除污点
两个从节点
vi image.sh
#!/bin/bash
url=registry.cn-hangzhou.aliyuncs.com/google_containers
version=v1.14.2
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
chmod u+x image.sh
./image.sh
主机查看令牌
kubeadm token list
生成新令牌
kubeadm token create
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'
密串
92b46817271e5e482694a917ed3828a99f5d74a4987f06decd476a6904b937e7
令牌
6omj3h.d1d7klb5y5uc91ly
两个从节点 修改IP,令牌 密串为自己的
kubeadm join 84.84.1.4:6443 --token 6omj3h.d1d7klb5y5uc91ly --discovery-token-ca-cert-hash sha256:92b46817271e5e482694a917ed3828a99f5d74a4987f06decd476a6904b937e7
主机
recommended.yaml的内容
正常运行下面这条指令,但是我的拒绝连接,所以我在根目录新建recommended.yaml
wget https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml
vim recommended.yaml
以下是recommended.yaml的内容
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: v1
kind: Namespace
metadata:
name: kubernetes-dashboard
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
ports:
- port: 443
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
---
apiVersion: v1
kind: Secret
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-certs
namespace: kubernetes-dashboard
type: Opaque
---
apiVersion: v1
kind: Secret
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-csrf
namespace: kubernetes-dashboard
type: Opaque
data:
csrf: ""
---
apiVersion: v1
kind: Secret
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-key-holder
namespace: kubernetes-dashboard
type: Opaque
---
kind: ConfigMap
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-settings
namespace: kubernetes-dashboard
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
rules:
# Allow Dashboard to get, update and delete Dashboard exclusive secrets.
- apiGroups: [""]
resources: ["secrets"]
resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
verbs: ["get", "update", "delete"]
# Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
- apiGroups: [""]
resources: ["configmaps"]
resourceNames: ["kubernetes-dashboard-settings"]
verbs: ["get", "update"]
# Allow Dashboard to get metrics.
- apiGroups: [""]
resources: ["services"]
resourceNames: ["heapster", "dashboard-metrics-scraper"]
verbs: ["proxy"]
- apiGroups: [""]
resources: ["services/proxy"]
resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
verbs: ["get"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
rules:
# Allow Metrics Scraper to get metrics from the Metrics server
- apiGroups: ["metrics.k8s.io"]
resources: ["pods", "nodes"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: kubernetes-dashboard
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubernetes-dashboard
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kubernetes-dashboard
---
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: kubernetes-dashboard
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
spec:
securityContext:
seccompProfile:
type: RuntimeDefault
containers:
- name: kubernetes-dashboard
image: kubernetesui/dashboard:v2.5.0
imagePullPolicy: Always
ports:
- containerPort: 8443
protocol: TCP
args:
- --auto-generate-certificates
- --namespace=kubernetes-dashboard
# Uncomment the following line to manually specify Kubernetes API server Host
# If not specified, Dashboard will attempt to auto discover the API server and connect
# to it. Uncomment only if the default does not work.
# - --apiserver-host=http://my-address:port
volumeMounts:
- name: kubernetes-dashboard-certs
mountPath: /certs
# Create on-disk volume to store exec logs
- mountPath: /tmp
name: tmp-volume
livenessProbe:
httpGet:
scheme: HTTPS
path: /
port: 8443
initialDelaySeconds: 30
timeoutSeconds: 30
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsUser: 1001
runAsGroup: 2001
volumes:
- name: kubernetes-dashboard-certs
secret:
secretName: kubernetes-dashboard-certs
- name: tmp-volume
emptyDir: {}
serviceAccountName: kubernetes-dashboard
nodeSelector:
"kubernetes.io/os": linux
# Comment the following tolerations if Dashboard must not be deployed on master
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: dashboard-metrics-scraper
name: dashboard-metrics-scraper
namespace: kubernetes-dashboard
spec:
ports:
- port: 8000
targetPort: 8000
selector:
k8s-app: dashboard-metrics-scraper
---
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: dashboard-metrics-scraper
name: dashboard-metrics-scraper
namespace: kubernetes-dashboard
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: dashboard-metrics-scraper
template:
metadata:
labels:
k8s-app: dashboard-metrics-scraper
spec:
securityContext:
seccompProfile:
type: RuntimeDefault
containers:
- name: dashboard-metrics-scraper
image: kubernetesui/metrics-scraper:v1.0.7
ports:
- containerPort: 8000
protocol: TCP
livenessProbe:
httpGet:
scheme: HTTP
path: /
port: 8000
initialDelaySeconds: 30
timeoutSeconds: 30
volumeMounts:
- mountPath: /tmp
name: tmp-volume
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsUser: 1001
runAsGroup: 2001
serviceAccountName: kubernetes-dashboard
nodeSelector:
"kubernetes.io/os": linux
# Comment the following tolerations if Dashboard must not be deployed on master
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
volumes:
- name: tmp-volume
emptyDir: {}
一. 简答题(共1题,100分)
1. (简答题)
基本要求:
1. 1个Master节点,主机名:K8S_个人学号后三位_master;2个Node节点,主机名分别为:K8S_个人学号后三位_node_01和K8S_个人学号后三位_node_02。
2. docker版本docker,kubelet/kubeadm/kubectl版本V1.14.2,设置全部节点的kubernetes的YUM软件源为阿里云。
3. 浏览器访问个人Master节点的30067端口,访问Node选项,截屏,如下图所示。
4. 命令行测试
4.1 执行命令行方式和配置文件方式部署应用命令
4.2 执行查看副本数命令并截屏
4.3 执行查看deployment详细信息命令并截屏
4.4 执行查看集群基本组件状态命令并截屏
5. K8s集群部署Spring Boot应用
5.1 制作Dockerfile文件,生成镜像。
构建JDK镜像,Dockerfile文件内容如下。
FROM centos:latest
MAINTAINER 个人姓名拼音
ADD jdk-8u251-linux-x64.tar.gz /usr/local/java
ENV JAVA_HOME /usr/local/java/jdk1.8.0_251
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
CMD java -version
执行:docker build -t jdk1.8.0_251 .
首先,在Eclispe中创建一个简单的Spring Boot项目,网页输出Hello World项目名称为:springboot-K8s-1.0.0,发布项目获得:springboot-K8s-1.0.0.jar,把springboot-K8s-1.0.0.jar复制到Dockerfile文件所在的目录。
然后,构建项目镜像 ,Dockerfile文件内容如下。
FROM jdk1.8.0_251
MAINTAINER LIHONGBO
ADD springboot-K8s-1.0.0.jar /opt
RUN chmod +x /opt/springboot-K8s-1.0.0.jar
CMD java -jar /opt/springboot-K8s-1.0.0.jar
执行命令:docker build -t springboot-K8s-1.0.0-jar .
进而,部署镜像,执行如下命令
kubectl create deployment springboot-K8s \
--image=springboot-K8s-1.0.0-jar --dry-run -o yaml > deploy.yaml
# deploy.yaml内容
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: springboot-K8s
name: springboot-K8s
spec:
replicas: 1
selector:
matchLabels:
app: springboot-K8s
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: springboot-K8s
spec:
containers:
-image:springboot-K8s-1.0.0-jar
name:springboot-K8s-1.0.0-jar
#配置从本地拉取镜像,不从镜像仓库拉取 imagePullPolicy:Never
resources: {}
status: {}
最后,执行
kubectl apply -f deploy.yaml
kubectl expose deploy springboot-K8s --port=80 --type=NodePort
kubectl get service
kubectl get deploy
kubectl get pods
给出:上面3个get命令的运行结果截屏。
窗体底端