要在Kubernetes中创建一个Ubuntu 20.04容器暴露22端口并在后台永久运行,可以按照以下步骤进行操作:
创建一个名为ubuntu-ssh的Deployment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| apiVersion: apps/v1 kind: Deployment metadata: name: ubuntu-ssh spec: replicas: 1 selector: matchLabels: app: ubuntu-ssh template: metadata: labels: app: ubuntu-ssh spec: containers: - name: ubuntu-ssh image: ubuntu:20.04 command: ["/bin/bash"] args: ["-c", "while true; do sleep 30; done"] ports: - containerPort: 22
|
这将创建一个名为ubuntu-ssh的Deployment,该Deployment将在单个Pod中运行一个名为ubuntu-ssh的容器。容器将使用ubuntu:20.04作为基本镜像,运行一个无限循环的sleep命令来保持容器运行,并暴露22端口。
创建一个名为ubuntu-ssh的Service:
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: Service metadata: name: ubuntu-ssh spec: selector: app: ubuntu-ssh ports: - name: ssh protocol: TCP port: 22 targetPort: 22 type: ClusterIP
|
这将创建一个名为ubuntu-ssh的Service,并将其连接到Deployment。Service将使用ClusterIP类型,并在22端口上公开SSH服务。
在容器中安装SSH服务器:
1 2 3
| kubectl exec -it <ubuntu-ssh-pod> -- /bin/bash apt-get update apt-get install -y openssh-server
|
这将在运行ubuntu-ssh容器的Pod中打开一个shell,并安装SSH服务器。
在容器中配置SSH服务器:
1 2 3
| echo 'root:password' | chpasswd sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config service ssh restart
|
这将设置root用户的密码为password,启用root用户的SSH登录,并重新启动SSH服务器。
现在,您已经在Kubernetes中创建了一个Ubuntu 20.04容器,该容器暴露22端口并在后台永久运行。您可以使用SSH客户端连接到该容器,例如:
其中,<service-ip>是在步骤2中创建的Service的IP地址。