1.制作tomcat镜像
此处直接拉取
查看已有可镜像
先设置docker阿里源,即添加 "registry-mirrors": ["https://mj9kvemk.mirror.aliyuncs.com"]
[root@k8s-master docker]# pwd /etc/docker [root@k8s-master docker]# cat daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2", "storage-opts": [ "overlay2.override_kernel_check=true" ], "registry-mirrors": ["https://mj9kvemk.mirror.aliyuncs.com"] } [root@k8s-master docker]#
service docker restart
[root@k8s-master docker]# docker pull tomcat:8 8: Pulling from library/tomcat e79bb959ec00: Pull complete d4b7902036fe: Pull complete 1b2a72d4e030: Pull complete de423484a946: Pull complete ceaac3b844f7: Pull complete 88f01b722a52: Pull complete c23be56a9ac1: Pull complete d852ffd6d31f: Pull complete 11775a3d792d: Pull complete acd9db02854a: Pull complete 3032f09d91b5: Pull complete Digest: sha256:84e91645e3176f55e1f2ad63edf657216ed4ef3dde82e381f1b8ceac1145a21c Status: Downloaded newer image for tomcat:8 [root@k8s-master docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 8 f1332ae3f570 3 days ago 463MB k8s.gcr.io/kube-proxy v1.14.0 5cd54e388aba 7 days ago 82.1MB k8s.gcr.io/kube-scheduler v1.14.0 00638a24688b 7 days ago 81.6MB k8s.gcr.io/kube-controller-manager v1.14.0 b95b1efa0436 7 days ago 158MB k8s.gcr.io/kube-apiserver v1.14.0 ecf910f40d6e 7 days ago 210MB quay.io/coreos/flannel v0.11.0-amd64 ff281650a721 2 months ago 52.6MB k8s.gcr.io/coredns 1.3.1 eb516548c180 2 months ago 40.3MB k8s.gcr.io/etcd 3.3.10 2c4adeb21b4f 4 months ago 258MB k8s.gcr.io/pause 3.1 da86e6ba6ca1 15 months ago 742kB [root@k8s-master docker]#
2.利用docker启动镜像,验证可用性
[root@k8s-master docker]# docker run -d -p 58080:8080 --name mytomcat_1 tomcat:8 c8a00a7aab85ee3b342fb7cd1515a464bde1195397432173b6126f4242244ef9 [root@k8s-master docker]# docker ps -a|grep tomcat c8a00a7aab85 tomcat:8 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:58080->8080/tcp mytomcat_1 [root@k8s-master docker]#
chrome验证 http://192.168.111.130:58080/
3.发布k8s集群
创建一个deployment
(可参考https://blog.csdn.net/wucong60/article/details/81699196)
kubernetes yaml语法,横杠表示数组,缩进表示层级,缩进不能有tab,参考Kubernetes之YAML文件
[root@k8s-master ~]# pwd /root [root@k8s-master ~]# cat deployment-hello.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: hello spec: replicas: 4 template: metadata: labels: run: hello spec: containers: - name: hello image: tomcat:8 #确保node节点上有该镜像且可正常运行,注意是node节点机器上,不是master机器 imagePullPolicy: IfNotPresent ##Always,IfNotPresent,Never ports: - name: http containerPort: 8080
kubectl 创建pod
[root@k8s-master ~]# kubectl create -f deployment-hello.yaml
deployment.extensions/hello created
deployments详情
root@k8s-master ~]# kubectl get deployments #查看deploy是否成功 NAME READY UP-TO-DATE AVAILABLE AGE hello 0/4 4 0 55s
[root@k8s-master ~]# kubectl get rs ##因为deployment是三层架构,看rs是否成功,我们看自动创建4个rs.名称后面的字符串是 模板的哈希值。是不会发生变化的,最后pod的是随机值
NAME DESIRED CURRENT READY AGE
hello-68df45bc79 4 4 0 8m29s
[root@k8s-master ~]# kubectl get pods #查看最后一层pod NAME READY STATUS RESTARTS AGE hello-7d46c7db4c-42bwl 0/1 ContainerCreating 0 67s hello-7d46c7db4c-rg9fq 0/1 ContainerCreating 0 67s hello-7d46c7db4c-tclps 0/1 ContainerCreating 0 67s hello-7d46c7db4c-whvsw 0/1 ContainerCreating 0 67s
[root@k8s-master ~]# kubectl describe deployment hello
Name: hello
Namespace: default
CreationTimestamp: Wed, 03 Apr 2019 10:18:53 +0800
Labels: run=hello
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=hello
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=hello
Containers:
hello:
Image: tomcat:8
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: hello-57b49c67cf (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m30s deployment-controller Scaled up replica set hello-57b49c67cf to 4
[root@k8s-master ~]#
##过两分钟状态就会变为running
[root@k8s-master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
hello 4/4 4 4 6m52s
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-57b49c67cf-6hh59 1/1 Running 0 4m56s
hello-57b49c67cf-6ml78 1/1 Running 0 4m56s
hello-57b49c67cf-8xqgw 1/1 Running 0 4m56s
hello-57b49c67cf-pd826 1/1 Running 0 4m56s
[root@k8s-master ~]# kubectl describe pod hello-57b49c67cf-6hh59
Name: hello-57b49c67cf-6hh59
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: k8s-node1/192.168.111.131
Start Time: Wed, 03 Apr 2019 10:18:53 +0800
Labels: pod-template-hash=57b49c67cf
run=hello
Annotations: <none>
Status: Running
IP: 10.244.1.22
Controlled By: ReplicaSet/hello-57b49c67cf
Containers:
hello:
Container ID: docker://8cd27dd2bb4dffd50f8faf8938870af9fa9489f853498f37277a360e3059c476
Image: mytomcat:v8
Image ID: docker-pullable://tomcat@sha256:3e3d18321127bb9114f4226f95802d3899aeec4c36df84d0359e5da300e9bc72
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 03 Apr 2019 10:18:57 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-92rjn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-92rjn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-92rjn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m37s default-scheduler Successfully assigned default/hello-57b49c67cf-6hh59 to k8s-node1
Normal Pulled 5m34s kubelet, k8s-node1 Container image "mytomcat:v8" already present on machine
Normal Created 5m34s kubelet, k8s-node1 Created container hello
Normal Started 5m33s kubelet, k8s-node1 Started container hello
如果kubectl get pod状态为ErrImagePull && ImagePullBackOff ErrImageNeverPull,请确保node上有相应的镜像,若无,则在node机器执行第1,2步
pod发布位置
root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES hello-7d46c7db4c-42bwl 0/1 ContainerCreating 0 5m53s <none> k8s-node1 <none> <none> hello-7d46c7db4c-rg9fq 0/1 ContainerCreating 0 5m53s <none> k8s-node1 <none> <none> hello-7d46c7db4c-tclps 0/1 ContainerCreating 0 5m53s <none> k8s-node1 <none> <none> hello-7d46c7db4c-whvsw 0/1 ContainerCreating 0 5m53s <none> k8s-node1 <none> <none>
正常情况下
[root@k8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE hello-665548d-5fcbf 1/1 Running 0 11m hello-665548d-h69xx 1/1 Running 0 11m hello-665548d-jl6m6 1/1 Running 0 11m hello-665548d-zd7kw 1/1 Running 0 11m
创建一个service
[root@k8s-master ~]# cat service-hello.yaml apiVersion: v1 kind: Service metadata: name: service-hello labels: name: service-hello spec: type: NodePort #这里代表是NodePort类型的,另外还有ingress,LoadBalancer ports: - port: 80 #这里的端口和clusterIP(kubectl describe service service-hello中的IP的port)对应,即在集群中所有机器上curl 10.98.166.242:80可访问发布的应用服务。 targetPort: 8080 #端口一定要和container暴露出来的端口对应,nodejs暴露出来的端口是8081,所以这里也应是8081 protocol: TCP nodePort: 31111 # 所有的节点都会开放此端口30000--32767,此端口供外部调用。 selector: run: hello #这里选择器一定要选择容器的标签,之前写name:kube-node是错的。 [root@k8s-master ~]# pwd /root [root@k8s-master ~]#
kubectl create -f 创建对像,删除用kubectl delete -f
[root@k8s-master ~]# kubectl create -f service-hello.yaml
service/service-hello created
[root@k8s-master ~]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h24m
service-hello NodePort 10.98.166.242 <none> 80:31111/TCP 42s
[root@k8s-master ~]#
root@k8s-master ~]# kubectl get services -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h25m <none>
service-hello NodePort 10.98.166.242 <none> 80:31111/TCP 104s run=hello
[root@k8s-master ~]# kubectl describe service service-hello
Name: service-hello
Namespace: default
Labels: <none>
Annotations: <none>
Selector: run=hello
Type: NodePort
IP: 10.98.166.242
Port: <unset> 80/TCP
TargetPort: 8080/TCP
NodePort: <unset> 31111/TCP
Endpoints: 10.244.1.22:8080,10.244.1.23:8080,10.244.1.24:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master ~]#
service创建后,自动生成endpoints,每个pod ip都在里面,简单认为:动态存储pod名字与pod ip对应关系的list,并提供将请求转发到实际pod上的能力 kubernets之endpoints
[root@k8s-master ~]# kubectl get endpoints NAME ENDPOINTS AGE kubernetes 192.168.111.130:6443 20h service-hello 10.244.1.22:8080,10.244.1.23:8080,10.244.1.24:8080 + 1 more... 15h [root@k8s-master ~]# kubectl describe endpoint service-hello error: the server doesn't have a resource type "endpoint" [root@k8s-master ~]# kubectl describe endpoints service-hello Name: service-hello Namespace: default Labels: <none> Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2019-04-03T02:18:57Z Subsets: Addresses: 10.244.1.22,10.244.1.23,10.244.1.24,10.244.1.25 NotReadyAddresses: <none> Ports: Name Port Protocol ---- ---- -------- <unset> 8080 TCP Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning FailedToUpdateEndpoint 48m (x2 over 69m) endpoint-controller Failed to update endpoint default/service-hello: Operation cannot be fulfilled on endpoints "service-hello": the object has been modified; please apply your changes to the latest version and try again [root@k8s-master ~]#
4.验证发布
http://192.168.111.130:31111/ 或者http://192.168.111.131:31111/