k8s官方案例练习-公开外部 IP 地址以访问集群中应用程序
Posted jackluo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s官方案例练习-公开外部 IP 地址以访问集群中应用程序相关的知识,希望对你有一定的参考价值。
先准备镜像:
[root@localhost node]# ls Dockerfile server.js [root@localhost node]# cat Dockerfile FROM node:6.14.2 EXPOSE 8080 COPY server.js . CMD node server.js [root@localhost node]# cat server.js var http = require(‘http‘); var handleRequest = function(request, response) { console.log(‘Received request for URL: ‘ + request.url); response.writeHead(200); response.end(‘Hello World!‘); }; var www = http.createServer(handleRequest); www.listen(8080); [root@localhost node]#
build 镜像
[root@localhost node]# docker build -t hello-world . Sending build context to Docker daemon 3.072kB Step 1/4 : FROM node:6.14.2 6.14.2: Pulling from library/node 3d77ce4481b1: Pull complete 7d2f32934963: Pull complete 0c5cf711b890: Pull complete 9593dc852d6b: Pull complete 4e3b8a1eb914: Pull complete ddcf13cc1951: Pull complete 2e460d114172: Pull complete d94b1226fbf2: Pull complete Digest: sha256:62b9d88be259a344eb0b4e0dd1b12347acfe41c1bb0f84c3980262f8032acc5a Status: Downloaded newer image for node:6.14.2 ---> 00165cd5d0c0 Step 2/4 : EXPOSE 8080 ---> Running in 960b23083ba5 Removing intermediate container 960b23083ba5 ---> 0f92fce429ef Step 3/4 : COPY server.js . ---> b2d70b5cead9 Step 4/4 : CMD node server.js ---> Running in 598d107259b9 Removing intermediate container 598d107259b9 ---> 931d59ffd088 Successfully built 931d59ffd088 Successfully tagged hello-world:latest
更改镜像名再推上仓库
[root@localhost node]# docker tag hello-world 192.168.9.14/public/hello-world [root@localhost node]# docker push 192.168.9.14/public/hello-world The push refers to repository [192.168.9.14/public/hello-world]
#更改配置
[root@localhost node]# vi /etc/docker/
daemon.json key.json
[root@localhost node]# vi /etc/docker/daemon.json
[root@localhost node]# systemctl restart docker
[root@localhost node]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://8l7c94kc.mirror.aliyuncs.com"],
"insecure-registries": [
"192.168.9.14"
]
}
登陆并推上去
[root@localhost node]# docker login 192.168.9.14 Username: admin Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@localhost node]# docker push 192.168.9.14/public/hello-world The push refers to repository [192.168.9.14/public/hello-world] 8e98d6b5cc41: Pushed aeaa1edefd60: Pushed 6e650662f0e3: Pushed 8c825a971eaf: Pushed bf769027dbbd: Pushed f3693db46abb: Pushed bb6d734b467e: Pushed 5f349fdc9028: Pushed 2c833f307fd8: Pushed latest: digest: sha256:b05a2e511c8ef72e4f9783e16f6f4e76f224aed3b69be5efce0ddecffaac0d8c size: 2214
为一个在五个 pod 中运行的应用程序创建服务
在集群中运行 Hello World 应用程序:
[root@master01 node]# kubectl run hello-world --replicas=5 --labels="run=load-balancer-example" --image=192.168.9.14/public/hello-world --port=8080 kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. deployment.apps/hello-world created [root@master01 node]# kubectl get pods NAME READY STATUS RESTARTS AGE hello-world-c66bdd88-2kcxc 0/1 ContainerCreating 0 22s hello-world-c66bdd88-gv82g 0/1 ContainerCreating 0 22s hello-world-c66bdd88-l2b74 0/1 ContainerCreating 0 22s hello-world-c66bdd88-s7jpk 0/1 ContainerCreating 0 22s hello-world-c66bdd88-vpjnt 0/1 ContainerCreating 0 22s kuard-5cd647675b-65cwg 1/1 Running 0 2d kuard-5cd647675b-65r9c 1/1 Running 0 2d kuard-5cd647675b-f9r9f 1/1 Running 0 2d nginx 1/1 Running 0 46h nginx-78b75497b7-rpt8t 1/1 Running 0 23h
显示 显示有关 ReplicaSet 对象的信息
[root@master01 node]# kubectl describe replicasets Name: hello-world-c66bdd88 Namespace: default Selector: pod-template-hash=c66bdd88,run=load-balancer-example Labels: pod-template-hash=c66bdd88 run=load-balancer-example Annotations: deployment.kubernetes.io/desired-replicas: 5 deployment.kubernetes.io/max-replicas: 7 deployment.kubernetes.io/revision: 1 Controlled By: Deployment/hello-world Replicas: 5 current / 5 desired Pods Status: 5 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: pod-template-hash=c66bdd88 run=load-balancer-example Containers: hello-world: Image: 192.168.9.14/public/hello-world Port: 8080/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 9m39s replicaset-controller Created pod: hello-world-c66bdd88-l2b74 Normal SuccessfulCreate 9m39s replicaset-controller Created pod: hello-world-c66bdd88-vpjnt Normal SuccessfulCreate 9m39s replicaset-controller Created pod: hello-world-c66bdd88-gv82g Normal SuccessfulCreate 9m39s replicaset-controller Created pod: hello-world-c66bdd88-s7jpk Normal SuccessfulCreate 9m38s replicaset-controller Created pod: hello-world-c66bdd88-2kcxc Name: kuard-5cd647675b Namespace: default Selector: app=kuard,pod-template-hash=5cd647675b Labels: app=kuard pod-template-hash=5cd647675b Annotations: deployment.kubernetes.io/desired-replicas: 3 deployment.kubernetes.io/max-replicas: 4 deployment.kubernetes.io/revision: 1 Controlled By: Deployment/kuard Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=kuard pod-template-hash=5cd647675b Containers: kuard: Image: 192.168.9.14/library/kuard-amd64:1 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Events: <none> Name: nginx-78b75497b7 Namespace: default Selector: app=nginx,pod-template-hash=78b75497b7 Labels: app=nginx pod-template-hash=78b75497b7 Annotations: deployment.kubernetes.io/desired-replicas: 1 deployment.kubernetes.io/max-replicas: 2 deployment.kubernetes.io/revision: 1 Controlled By: Deployment/nginx Replicas: 1 current / 1 desired Pods Status: 1 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=nginx pod-template-hash=78b75497b7 Containers: nginx: Image: nginx:1.17.7 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Events: <none> [root@master01 node]# kubectl get replicasets NAME DESIRED CURRENT READY AGE hello-world-c66bdd88 5 5 5 9m47s kuard-5cd647675b 3 3 3 2d1h nginx-78b75497b7 1 1 1 23h
创建公开 deployment 的 Service 对象
[root@master01 node]# kubectl expose deployment hello-world --type=LoadBalancer --name=my-service service/my-service exposed
[root@master01 node]# kubectl get services my-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service LoadBalancer 10.108.42.200 <pending> 8080:30798/TCP 58s
获取 service 详情
[root@master01 node]# kubectl describe services my-service Name: my-service Namespace: default Labels: run=load-balancer-example Annotations: <none> Selector: run=load-balancer-example Type: LoadBalancer IP: 10.108.42.200 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 30798/TCP Endpoints: 10.244.3.13:8080,10.244.3.14:8080,10.244.4.15:8080 + 2 more... Session Affinity: None External Traffic Policy: Cluster Events: <none>
查看
[root@master01 node]# kubectl get pods --output=wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES hello-world-c66bdd88-2kcxc 1/1 Running 0 21m 10.244.4.17 worker01 <none> <none> hello-world-c66bdd88-gv82g 1/1 Running 0 21m 10.244.3.14 worker02 <none> <none> hello-world-c66bdd88-l2b74 1/1 Running 0 21m 10.244.3.13 worker02 <none> <none> hello-world-c66bdd88-s7jpk 1/1 Running 0 21m 10.244.4.15 worker01 <none> <none> hello-world-c66bdd88-vpjnt 1/1 Running 0 21m 10.244.4.16 worker01 <none> <none> kuard-5cd647675b-65cwg 1/1 Running 0 2d1h 10.244.4.2 worker01 <none> <none> kuard-5cd647675b-65r9c 1/1 Running 0 2d1h 10.244.4.5 worker01 <none> <none> kuard-5cd647675b-f9r9f 1/1 Running 0 2d1h 10.244.4.4 worker01 <none> <none> nginx 1/1 Running 0 47h 10.244.3.10 worker02 <none> <none> nginx-78b75497b7-rpt8t 1/1 Running 0 24h 10.244.4.14 worker01 <none> <none> [root@master01 node]# [root@master01 node]# [root@master01 node]# curl 192.168.9.15:30798 Hello World![root@master01 node]#
清理现场
[root@master01 node]# kubectl delete services my-service service "my-service" deleted [root@master01 node]# kubectl delete deployment hello-world deployment.apps "hello-world" deleted
以上是关于k8s官方案例练习-公开外部 IP 地址以访问集群中应用程序的主要内容,如果未能解决你的问题,请参考以下文章