标签
1创建目录
mkdir -p ~/huawei_k8s/labfile/labelfie
cd labfile/labelfie/
2.创建labelpod.yaml并使用多个标签
kind: Pod
apiVersion: v1
metadata:
name: labelpod
labels:
app: busybox
version: new
spec:
containers:
- name: labelpod
image: busybox
args:
- /bin/sh
-- c
- sleep 30000
3创建pod
kubectl apply -f labelpod.yaml
4查看pod的label
kubectl get pod --show-labels
5为labelpod添加一个新的label
kubectl label pod labelpod time=2019
kubectl get pod --show-labels
标签选择器
1创建labelpod2.yaml
kind: Pod
apiVersion: v1
metadata:
name: labelpod2
labels:
app: httpd
version: new
spec:
containers:
- name: httpd
image: httpd
2 创建查看
kubectl apply -f labelpod2.yaml
kubectl get pod --show-labels
3使用基于等值的标签选择器
kubectl get pod -l app=httpd
#或者
kubectl get pod -l app==httpd
4 基于不等值的标签选择器
kubectl get pod -l app!=httpd
5查看pod针对某个标签的值
kubectl get pod -L app
使用标签选额器实现调度
1 将节点2打上标签
kubectl label node node2 env=test
2 查看节点上的标签
kubectl get node -L env
3 使用nodeselector实现调度,创建yaml文件nsdeploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
nodeSelector:
env: test
4 创建deployment
kubectl apply -f nsdeploy.yaml
5 查看deployment中pod的位置
kubectl get pod -o wide
6 使用node affinity调度,创建一个新的yaml文件,nadeploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-deployment
labels:
app: httpd
spec:
replicas: 3
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: env
operator: In
values:
- test
7创建deployment
kubectl apply -f nadeploye2.yaml
8查看新建的deployment中的pod的位置,可以看到,3个pod全在node2上
kubectl get pod -o wide
使用sevice
1 创建service实验目录
mkdir ~/huawei_k8s/labfile/servvicefile
cd ~/huawei_k8s/labfile/servvicefile
2 创建后端httpd Deployement
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
spec:
replicas: 3
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd
ports:
- containerPort: 80
3 部署deployment
kubectl apply -f httpd-deploy.yaml
kubectl get depoyment
4 创建httpd-service.yaml
apiVersion: v1
kind: Service
metadata:
name: httpd-svc
spec:
selector:
app: httpd
ports:
- protocol: TCP
port: 8080
targetPort: 80
5 创建service
kubectl apply -f httpd-service.yaml
6 查看service信息,注意,千万不要删除kubernetes的服务!该服务为系统服务
kubectl get service
7 查看service中的endpoints
kubectl get endpoints
8 测试服务可用性,使用curl<服务ip:端口号>查看回显,显示"it works!",代表成功,服务IP和端口为步骤6中获取的值
curl 10.233.242.186:8080
9 删除刚才创建的服务
kubeclt delete service httpd-svc
10 创建httpd-expos.yaml
apiVersion: v1
kind: Service
metadata:
name: httpd-svc
spec:
type: NodePort
selector:
app: httpd
ports:
- protocol: TCP
port: 8080
targetPort: 80
nodePort: 30144
11 创建httpd服务
kubctl apply -f httpd-expose.yaml
12 使用跳板机浏览器登陆<节点ip地址:端口>,显示"it works!“表示成功
使用DNS
1 创建client.yaml,用户创建一个客户端pod,测试dns功能
apiVersion: v1
kind: Pod
metadata:
name: clientpod
spec:
containers:
- name: clientpod
image: busybox:1.28.3
args:
- /bin/sh
- -c
- sleep 30000
2 创建并进入pod命令行界面
kubectl apply -f client.yaml
kubectl exec -it clientpod /bin/sh
3 使用nslookup命令查看服务域名
4 使用wget命令通过域名访问服务
5 验证成功后删除index文件并退出容器
rm index.html
rm exit
使用Headless Service
1 删除httpd servcie
kubectl delete svc httpd-svc
2 创建headless服务的yaml文件
apiVersion: v1
kind: Service
metadata:
name: headless-sve
spec:
selector:
app: httpd
ports:
- protocol: TCP
port: 80
targetPort: 80
clusterIP: None
3 创建headless服务,可以看到headless服务没有ip
4 进入client pod,查看dns解析
kubectl exec -it clientpod /bin/sh
nslookup headless-svc
5 访问服务,可以看到被本次访问的服务由上一步的addresss4的pod提供
wget headless-svc