1、k8s的kubectl命令
启用harbor仓库
在harbor文件的路径下 server8上
在server1上的master上
[root@server1 ~]# export KUBECONFIG=/etc/kubernetes/admin.conf
启动成功
[root@server1 ~]# kubectl run nginx --image=nginx:latest
pod/nginx created
[root@server1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 10s
[root@server1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 27s 10.244.1.2 server2 <none> <none>
[root@server1 ~]# kubectl describe pod nginx
[root@server1 ~]# curl 10.244.1.2 #尝试访问它
[root@server1 ~]# kubectl delete pod nginx #删除容器
pod "nginx" deleted
2、K8s的容器负载均衡及版本回
检验所需的文件,下载并进行上传 server1上
lftp 172.25.254.250:/pub/images> get myapp.tar
15931392 bytes transferred
[root@server1 ~]# docker load -i myapp.tar #docker本地上传
[root@server1 ~]# docker images | grep myapp
ikubernetes/myapp v1 d4a5e0eaa84f 3 years ago 15.5MB
ikubernetes/myapp v2 54202d3f0f35 3 years ago 15.5MB
改标签
[root@server1 ~]# docker tag ikubernetes/myapp:v1 reg.westos.org/library/myapp:v1
[root@server1 ~]# docker tag ikubernetes/myapp:v2 reg.westos.org/library/myapp:v2
[root@server1 ~]# docker login reg.westos.org #登陆harbor的仓库
[root@server1 ~]# docker push reg.westos.org/library/myapp:v1 #上传
[root@server1 ~]# docker push reg.westos.org/library/myapp:v2
[root@server1 ~]# kubectl create deployment webserver --image=myapp:v1 #创建容器
deployment.apps/webserver created
[root@server1 ~]# kubectl get pod #查看
NAME READY STATUS RESTARTS AGE
webserver-555478bbb6-2wpzh 1/1 Running 0 26s
[root@server1 ~]# kubectl get pod -o wide #较详细的查看
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
webserver-555478bbb6-2wpzh 1/1 Running 0 52s 10.244.2.2 server3 <none> <none>
[root@server1 ~]# curl 10.244.2.2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@server1 ~]# curl 10.244.2.2/hostname.html
webserver-555478bbb6-2wpzh
特点:快速(可以自己实验试一试,删除一个副本)、方便(自动拉取或删除根据你的副本个数)
[root@server1 ~]# kubectl scale deployment webserver --replicas=3 #设置副本个数,做负载均衡使用
deployment.apps/webserver scaled
[root@server1 ~]# kubectl get pod #设置副本个数后,少了帮你拉取,多了帮你删
NAME READY STATUS RESTARTS AGE
webserver-555478bbb6-2wpzh 1/1 Running 0 4m32s
webserver-555478bbb6-7xcj6 1/1 Running 0 64s #新
webserver-555478bbb6-vbtzc 1/1 Running 0 64s #新
[root@server1 ~]# kubectl get pod -o wide #更详细的查看容器
curlNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
webserver-555478bbb6-2wpzh 1/1 Running 0 12m 10.244.2.2 server3 <none> <none>
webserver-555478bbb6-7xcj6 1/1 Running 0 9m15s 10.244.1.3 server2 <none> <none>
webserver-555478bbb6-vbtzc 1/1 Running 0 9m15s 10.244.2.3 server3 <none> <none>
[root@server1 ~]# curl 10.244.2.2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@server1 ~]# curl 10.244.2.2/hostname.html
webserver-555478bbb6-2wpzh
[root@server1 ~]# curl 10.244.2.3/hostname.html
webserver-555478bbb6-vbtzc
[root@server1 ~]# curl 10.244.1.3/hostname.html
webserver-555478bbb6-7xcj6
[root@server1 ~]# kubectl expose deployment webserver --port=80 --target-port=80 #负载均衡
service/webserver exposed
[root@server1 ~]# kubectl get svc #可以查到调度器的ip 10.111.70.237
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d16h
webserver ClusterIP 10.111.70.237 <none> 80/TCP 13s
[root@server1 ~]# kubectl describe svc webserver #以下是详细信息
其实是以iptables
[root@server1 ~]# kubectl set image deployment webserver myapp=myapp:v2
deployment.apps/webserver image updated
NAME DESIRED CURRENT READY AGE
replicaset.apps/webserver-555478bbb6 0 0 0 34m #之前的版本
replicaset.apps/webserver-779b7fd5d7 3 3 3 39s
[root@server1 ~]# kubectl rollout undo deployment webserver --to-revision=1 #版本回退
deployment.apps/webserver rolled back
[root@server1 ~]# mkdir pod
[root@server1 ~]# cd pod/
[root@server1 pod]# ls
[root@server1 pod]# vim pod.yaml
[root@server1 pod]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: ubuntu
image: ubuntu:trusty
command: ["echo"]
args: ["Hello World"]
网址:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/#pod-v1-core
[root@server1 pod]# kubectl api-versions #查看api的版本
[root@server1 pod]# kubectl delete deployments.apps webserver
deployment.apps "webserver" deleted
[root@server1 pod]#
[root@server1 pod]# kubectl run nginx --image=nginx
pod/nginx created
[root@server1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 13s
[root@server1 pod]# kubectl get pod nginx -o yaml #详细的查看它的信息
[root@server1 pod]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
[root@server1 pod]# kubectl delete pod nginx
pod "nginx" deleted
[root@server1 pod]# kubectl get pod
No resources found in default namespace.
[root@server1 pod]# kubectl apply -f pod.yaml
pod/pod-example created
[root@server1 pod]# kubectl get pod #启用成功
NAME READY STATUS RESTARTS AGE
pod-example 1/1 Running 0 2m37s
[root@server1 pod]# kubectl delete -f pod.yaml
pod "pod-example" deleted
[root@server1 pod]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
- name: busybox
image: busyboxplus
stdin: true
tty: true
[root@server1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod-example 2/2 Running 0 43s #2/2两个容器都已经准备好了
[root@server1 pod]# kubectl exec -it pod-example -- sh
Defaulted container "nginx" out of: nginx, busybox
command terminated with exit code 130
[root@server1 pod]# kubectl exec -it pod-example -c busybox -- sh
/ # curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
[root@server1 pod]# kubectl delete -f pod.yaml
pod "pod-example" deleted
[root@server1 pod]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
- name: busybox
image: nginx
[root@server1 pod]# kubectl delete -f pod.yaml
pod "pod-example" deleted
[root@server1 pod]# vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http #记得缩进
hostPort: 80
containerPort: 80
[root@server1 pod]# kubectl apply -f pod.yaml
pod/pod-example created
[root@server1 pod]# kubectl get pod -o wide #在server2上开启
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-example 1/1 Running 0 91s 10.244.1.11 server2 <none> <none>
在其他server上 进行iptables -t nat -nL |grep :80 #发现
Pod-控制器
控制器类型:
ReplicaSet
Deployment
DaemonSet
StatefulSet
Job
CronJob
建立文件/root/deployment
1、replicaset
[root@server1 deployment]# ls
deployment.yaml replicaset.yaml
[root@server1 deployment]# vim replicaset.yaml
apiVersion: apps/v1 #缩进很重要
kind: ReplicaSet
metadata:
name: replicaset
spec:
replicas: 3 #你的副本数
selector:
matchLabels:
app: nginx #标签
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
[root@server1 deployment]# kubectl apply -f replicaset.yaml
replicaset.apps/replicaset created
[root@server1 deployment]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicaset 3 3 3 113s
[root@server1 deployment]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-f27q9 1/1 Running 0 3m9s app=nginx
replicaset-hbfcb 1/1 Running 0 3m9s app=nginx
replicaset-mrtln 1/1 Running 0 3m9s app=nginx
[root@server1 deployment]# kubectl label pod replicaset-f27q9 app=myapp --overwrite
pod/replicaset-f27q9 labeled
[root@server1 deployment]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-f27q9 1/1 Running 0 4m23s app=myapp
replicaset-hbfcb 1/1 Running 0 4m23s app=nginx #rs只管标签为app=nginx的 也就是文件内写啥它官什么别的不管,所以他又拉取了一个
replicaset-mrtln 1/1 Running 0 4m23s app=nginx
replicaset-z2qlq 1/1 Running 0 15s app=nginx
[root@server1 deployment]# kubectl label pod replicaset-f27q9 app=nginx --overwrite #再次覆盖回去
pod/replicaset-f27q9 labeled
[root@server1 deployment]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-f27q9 1/1 Running 0 6m39s app=nginx #你会发现:它把之前的回收了。
replicaset-hbfcb 1/1 Running 0 6m39s app=nginx #AGE时间一致,时间存在短的被回收!
replicaset-mrtln 1/1 Running 0 6m39s app=nginx
[root@server1 deployment]# cp replicaset.yaml deployment.yaml
cp: overwrite ‘deployment.yaml’? y
[root@server1 deployment]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment #改这个
metadata:
name: deployment #改这个
spec:
replicas: 3 #改这个
[root@server1 deployment]# kubectl apply -f deployment.yaml
deployment.apps/deployment created
你的rs
2、deployment
[root@server1 deployment]# vim deployment.yaml
image: myapp:v1
[root@server1 deployment]# kubectl apply -f deployment.yaml
[root@server1 deployment]# kubectl get pod #过个1min 后就ok 这个第2步
NAME READY STATUS RESTARTS AGE
deployment-c5fbbb494-7m9t2 1/1 Running 0 21s
deployment-c5fbbb494-7ssjs 1/1 Running 0 16s
deployment-c5fbbb494-h6rqc 1/1 Running 0 11s
[root@server1 deployment]# kubectl get all #这个一直执行 第1步执行
NAME READY STATUS RESTARTS AGE
pod/deployment-c5fbbb494-7m9t2 1/1 Running 0 26s
pod/deployment-c5fbbb494-7ssjs 1/1 Running 0 21s
pod/deployment-c5fbbb494-h6rqc 1/1 Running 0 16s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d20h
service/webserver ClusterIP 10.111.70.237 <none> 80/TCP 4h36m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment 3/3 3 3 13m
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-6799fc88d8 0 0 0 13m
replicaset.apps/deployment-c5fbbb494 3 3 3 26s
3、 DaemonSet
[root@server1 deployment]# vim DaemonSet.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
labels:
k8s-app: zabbix-agent
spec:
selector:
matchLabels:
name: zabbix-agent
template:
metadata:
labels:
name: zabbix-agent
spec:
hostNetwork: true
containers:
- name: zabbix-agent
image: nginx
[root@server1 deployment]# kubectl delete -f deployment.yaml --all
[root@server1 deployment]# kubectl apply -f DaemonSet.yaml
[root@server1 deployment]# kubectl get pod
NAME READY STATUS RESTARTS AGE
daemonset-example-6znql 1/1 Running 0 3m35s
daemonset-example-9cnm4 1/1 Running 0 3m35s
[root@server1 deployment]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
daemonset-example-6znql 1/1 Running 0 2m54s 172.25.111.2 server2 <none> <none>
daemonset-example-9cnm4 1/1 Running 0 2m54s 172.25.111.3 server3 <none> <none>
[root@server1 deployment]# kubectl delete -f DaemonSet.yaml #没事回收好习惯
daemonset.apps "daemonset-example" deleted
4、Job
lftp 172.25.254.250 去下载perl.tar文件
上传到私有仓库中
vim job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
[root@server1 deployment]# kubectl describe pod pi--1-xjhjm
[root@server1 deployment]# kubectl logs pi--1-xjhjm
[root@server1 deployment]# vim cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-example
spec:
schedule: "* * * * *" #每分钟执行下面的命令
jobTemplate:
spec:
template:
spec:
containers:
- name: cronjob
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from k8s cluster
restartPolicy: OnFailure
[root@server1 deployment]# kubectl apply -f cronjob.yaml
Warning: batch/v1beta1 CronJob is deprecated in v1.21+, unavailable in v1.25+; use batch/v1 CronJob
cronjob.batch/cronjob-example created
[root@server1 deployment]# kubectl logs cronjob-example-27239577--1-wlcmq
Sat Oct 16 08:57:22 UTC 2021
Hello from k8s cluster
[root@server1 deployment]# kubectl logs cronjob-example-27239576--1-kjx8q
Sat Oct 16 08:56:16 UTC 2021 #他们之间差1min
Hello from k8s cluster
service
[
root@server1 ~]# mkdir service
[root@server1 ~]# cd
[root@server1 ~]# cd /etc/yum.repos.d/
[root@server1 yum.repos.d]# ls
docker.repo dvd.repo k8s.repo redhat.repo
[root@server1 yum.repos.d]# vim k8s.repo
[root@server1 yum.repos.d]# cat k8s.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=0 #每个server都要改
gpgcheck=0
[root@server1 yum.repos.d]# yum install -y ipvsadm #每个server都要下载
[root@server1 yum.repos.d]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@server1 yum.repos.d]# lsmod |grep ip_vs
ip_vs_sh 12688 0
ip_vs_wrr 12697 0
ip_vs_rr 12600 0
ip_vs 145497 6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_conntrack 133095 10 ip_vs,nf_nat,nf_nat_ipv4,nf_nat_ipv6,xt_conntrack,nf_nat_masquerade_ipv4,nf_nat_masquerade_ipv6,nf_conntrack_netlink,nf_conntrack_ipv4,nf_conntrack_ipv6
libcrc32c 12644 4 xfs,ip_vs,nf_nat,nf_conntrack
[root@server1 ~]# kubectl -n kube-system get pod
[root@server1 ~]# kubectl -n kube-system get cm
[root@server1 ~]# kubectl -n kube-system edit cm kube-proxy
configmap/kube-proxy edited #修改IPVS模式
[root@server1 ~]# kubectl get pod -n kube-system |grep kube-proxy | awk '{system("kubectl delete pod "$1" -n kube-system")}' #更新kube-proxy pod
对比可知 ipvsadm -ln 更简洁,容易观看
[root@server1 ~]# iptables -nL #太繁琐了
[root@server1 ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d22h
service/webserver ClusterIP 10.111.70.237 <none> 80/TCP 6h20m
[root@server1 ~]# kubectl delete svc webserver
service "webserver" deleted
[root@server1 ~]# ip addr
[root@server1 ~]# cd service/
[root@server1 service]# vim service.yaml
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: nginx
type: ClusterIP
[root@server1 service]# kubectl apply -f service.yaml
service/web-service created
[root@server1 service]# cp /root/deployment/deployment.yaml .
[root@server1 service]# ls
deployment.yaml service.yaml
[root@server1 service]# vim deployment.yaml
image: myapp:v1 #改这个
[root@server1 service]# kubectl apply -f deployment.yaml
deployment.apps/deployment created
[root@server1 service]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
deployment-6456d7c676-54fm5 1/1 Running 0 14s app=nginx,pod-template-hash=6456d7c676
deployment-6456d7c676-7c2kv 1/1 Running 0 14s app=nginx,pod-template-hash=6456d7c676
deployment-6456d7c676-txtjm 1/1 Running 0 14s app=nginx,pod-template-hash=6456d7c676
[root@server1 service]# kubectl describe svc web-service
Name: web-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.110.134.40
IPs: 10.110.134.40
Port: http 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.23:80,10.244.2.16:80,10.244.2.17:80
Session Affinity: None
Events: <none>
[root@server1 service]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d22h
web-service ClusterIP 10.110.134.40 <none> 80/TCP 18m
[root@server1 service]# curl 10.110.134.40
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@server1 service]# curl 10.110.134.40/hostname.html
deployment-6456d7c676-7c2kv
[root@server1 service]# curl 10.110.134.40/hostname.html
deployment-6456d7c676-txtjm
[root@server1 service]# curl 10.110.134.40/hostname.html
deployment-6456d7c676-54fm5 #负载均衡
可以看到 负载均衡的分配情况 (InActConn列)
server3上也能看到负载均衡
交互式的容器内,也能访问到
集群外部访问
[root@server1 service]# vim service.yaml
type: NodePort
[root@server1 service]# kubectl delete pod demo
pod "demo" deleted
[root@server1 service]# kubectl apply -f service.yaml
service/web-service configured
[root@server1 service]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d23h
web-service NodePort 10.110.134.40 <none> 80:32715/TCP 37m #端口随机生成
[root@server1 service]# netstat -antlp |grep :32715 #在其他server同样能访问
tcp 0 0 0.0.0.0:32715 0.0.0.0:* LISTEN 6592/kube-proxy
在server8上也能访问,私有仓库
额,没写的后续补充(能补充应该就怪了!)
下半了,白白!